‪TYPO3CMS  10.4
FileTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
33 class ‪FileTest extends UnitTestCase
34 {
38  protected ‪$resetSingletonInstances = true;
39 
43  protected ‪$storageMock;
44 
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48  $this->storageMock = $this->createMock(ResourceStorage::class);
49  $this->storageMock->expects(self::any())->method('getUid')->willReturn(5);
50 
51  $mockedMetaDataRepository = $this->createMock(MetaDataRepository::class);
52  $mockedMetaDataRepository->expects(self::any())->method('findByFile')->willReturn(['file' => 1]);
53  GeneralUtility::setSingletonInstance(MetaDataRepository::class, $mockedMetaDataRepository);
54  }
55 
59  protected function ‪prepareFixture(): ‪File
60  {
61  $fixture = new ‪File(['testfile'], $this->storageMock);
62  return $fixture;
63  }
64 
68  public function ‪commonPropertiesAreAvailableWithOwnGetters(): void
69  {
70  $properties = [
71  'name' => ‪StringUtility::getUniqueId('name_'),
72  'storage' => ‪$this->storageMock,
73  'size' => 1024
74  ];
75  $fixture = new File($properties, $this->storageMock);
76  foreach ($properties as $key => $value) {
77  self::assertEquals($value, call_user_func([$fixture, 'get' . $key]));
78  }
79  }
80 
86  public function ‪fileIndexStatusIsTrueIfUidIsSet(): void
87  {
88  $fixture = new File(['uid' => 1], $this->storageMock);
89  self::assertTrue($fixture->isIndexed());
90  }
91 
95  public function ‪updatePropertiesUpdatesFileProperties(): void
96  {
97  $identifier = '/' . ‪StringUtility::getUniqueId('identifier_');
98  $fixture = new File(['uid' => 1, 'identifier' => '/test'], $this->storageMock);
99  $fixture->updateProperties(['identifier' => $identifier]);
100  self::assertEquals($identifier, $fixture->getIdentifier());
101  }
102 
107  {
108  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'], $this->storageMock);
109  $fixture->updateProperties(['foo' => 'foobar']);
110  self::assertEquals('/test', $fixture->getIdentifier());
111  self::assertEquals('/test', $fixture->getProperty('identifier'));
112  }
113 
117  public function ‪updatePropertiesDiscardsUidIfAlreadySet(): void
118  {
119  $fixture = new File(['uid' => 1, 'identifier' => '/test'], $this->storageMock);
120  $fixture->updateProperties(['uid' => 3]);
121  self::assertEquals(1, $fixture->getUid());
122  }
123 
128  {
129  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'], $this->storageMock);
130  $fixture->updateProperties(['foo' => 'foobar', 'baz' => 'foobaz']);
131  self::assertEquals(['foo', 'baz'], $fixture->getUpdatedProperties());
132  }
133 
138  {
139  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'], $this->storageMock);
140  $fixture->updateProperties(['foo' => 'asdf']);
141  self::assertEmpty($fixture->getUpdatedProperties());
142  }
143 
148  {
149  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'], $this->storageMock);
150  $fixture->updateProperties(['foo' => 'foobar', 'baz' => 'foobaz']);
151  $fixture->updateProperties(['foo' => 'fdsw', 'baz' => 'asdf']);
152  self::assertEquals(['foo', 'baz'], $fixture->getUpdatedProperties());
153  }
154 
159  {
160  $fileProperties = [
161  'uid' => 1,
162  'storage' => 'first',
163  ];
164  $subject = $this->getMockBuilder(File::class)
165  ->setMethods(['loadStorage'])
166  ->setConstructorArgs([$fileProperties, $this->storageMock])
167  ->getMock();
168  $mockedNewStorage = $this->createMock(ResourceStorage::class);
169  $mockedResourceFactory = $this->createMock(ResourceFactory::class);
170  $mockedResourceFactory
171  ->expects(self::once())
172  ->method('getStorageObject')
173  ->willReturn($mockedNewStorage);
174  GeneralUtility::setSingletonInstance(ResourceFactory::class, $mockedResourceFactory);
175 
176  $subject->updateProperties(['storage' => 'different']);
177  self::assertSame($mockedNewStorage, $subject->getStorage());
178  }
179 
184  {
185  $targetStorage = $this->createMock(ResourceStorage::class);
186  $targetFolder = $this->createMock(Folder::class);
187  $targetFolder->expects(self::any())->method('getStorage')->willReturn($targetStorage);
188  $fixture = new File([], $this->storageMock);
189  $targetStorage->expects(self::once())->method('copyFile')->with(self::equalTo($fixture), self::equalTo($targetFolder));
190  $fixture->copyTo($targetFolder);
191  }
192 
197  {
198  $targetStorage = $this->createMock(ResourceStorage::class);
199  $targetFolder = $this->createMock(Folder::class);
200  $targetFolder->expects(self::any())->method('getStorage')->willReturn($targetStorage);
201  $fixture = new File([], $this->storageMock);
202  $targetStorage->expects(self::once())->method('moveFile')->with(self::equalTo($fixture), self::equalTo($targetFolder));
203  $fixture->moveTo($targetFolder);
204  }
205 
206  public function ‪filenameExtensionDataProvider(): array
207  {
208  return [
209  ['somefile.jpg', 'somefile', 'jpg'],
210  ['SomeFile.PNG', 'SomeFile', 'png'],
211  ['somefile', 'somefile', ''],
212  ['somefile.tar.gz', 'somefile.tar', 'gz'],
213  ['somefile.tar.bz2', 'somefile.tar', 'bz2'],
214  ];
215  }
216 
221  public function ‪getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename): void
222  {
223  $fixture = new File(
224  [
225  'name' => $originalFilename,
226  'identifier' => '/' . $originalFilename
227  ],
228  $this->storageMock
229  );
230  self::assertSame($expectedBasename, $fixture->getNameWithoutExtension());
231  }
232 
237  public function ‪getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension): void
238  {
239  $fixture = new File([
240  'name' => $originalFilename,
241  'identifier' => '/' . $originalFilename
242  ], $this->storageMock);
243  self::assertSame($expectedExtension, $fixture->getExtension());
244  }
245 
249  public function ‪hasPropertyReturnsTrueFilePropertyExists(): void
250  {
251  $fixture = new File(['testproperty' => 'testvalue'], $this->storageMock);
252  self::assertTrue($fixture->hasProperty('testproperty'));
253  }
254 
259  {
260  $fixture = $this->getMockBuilder(File::class)
261  ->setConstructorArgs([[], $this->storageMock])
262  ->setMethods(['getMetaData'])
263  ->getMock();
264 
265  $metaDataAspectMock = $this->getMockBuilder(MetaDataAspect::class)
266  ->setConstructorArgs([$fixture])
267  ->setMethods(['get'])
268  ->getMock();
269 
270  $metaDataAspectMock->expects(self::any())->method('get')->willReturn(['testproperty' => 'testvalue']);
271  $fixture->expects(self::any())->method('getMetaData')->willReturn($metaDataAspectMock);
272 
273  self::assertTrue($fixture->hasProperty('testproperty'));
274  self::assertSame('testvalue', $fixture->getProperty('testproperty'));
275  }
276 
280  public function ‪getPropertiesContainsUidOfSysFileMetadata(): void
281  {
282  $fileData = [
283  'uid' => 1357,
284  'name' => 'dummy.svg'
285  ];
286  $metaData = [
287  'uid' => 2468,
288  'file' => 1357,
289  'title' => 'Dummy SVG'
290  ];
291  $file = new File($fileData, $this->storageMock, $metaData);
292 
293  self::assertSame(
294  1357,
295  $file->getProperties()['uid']
296  );
297  self::assertSame(
298  2468,
299  $file->getProperties()['metadata_uid']
300  );
301  }
302 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:40
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\getNameWithoutExtensionReturnsCorrectName
‪getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename)
Definition: FileTest.php:219
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\fileIndexStatusIsTrueIfUidIsSet
‪fileIndexStatusIsTrueIfUidIsSet()
Definition: FileTest.php:84
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesReloadsStorageObjectIfStorageChanges
‪updatePropertiesReloadsStorageObjectIfStorageChanges()
Definition: FileTest.php:156
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesUpdatesFileProperties
‪updatePropertiesUpdatesFileProperties()
Definition: FileTest.php:93
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\moveToCallsMoveOperationOnTargetFolderStorage
‪moveToCallsMoveOperationOnTargetFolderStorage()
Definition: FileTest.php:194
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\prepareFixture
‪File prepareFixture()
Definition: FileTest.php:57
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\$storageMock
‪ResourceStorage $storageMock
Definition: FileTest.php:41
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesLeavesPropertiesUntouchedIfNotSetInNewProperties
‪updatePropertiesLeavesPropertiesUntouchedIfNotSetInNewProperties()
Definition: FileTest.php:104
‪TYPO3\CMS\Core\Resource\MetaDataAspect
Definition: MetaDataAspect.php:27
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\hasPropertyReturnsTrueIfMetadataPropertyExists
‪hasPropertyReturnsTrueIfMetadataPropertyExists()
Definition: FileTest.php:256
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\getExtensionReturnsCorrectExtension
‪getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension)
Definition: FileTest.php:235
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\filenameExtensionDataProvider
‪filenameExtensionDataProvider()
Definition: FileTest.php:204
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\getPropertiesContainsUidOfSysFileMetadata
‪getPropertiesContainsUidOfSysFileMetadata()
Definition: FileTest.php:278
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest
Definition: FileTest.php:34
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesRecordsNamesOfChangedProperties
‪updatePropertiesRecordsNamesOfChangedProperties()
Definition: FileTest.php:125
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\hasPropertyReturnsTrueFilePropertyExists
‪hasPropertyReturnsTrueFilePropertyExists()
Definition: FileTest.php:247
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\commonPropertiesAreAvailableWithOwnGetters
‪commonPropertiesAreAvailableWithOwnGetters()
Definition: FileTest.php:66
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesMarksPropertyAsChangedOnlyOnce
‪updatePropertiesMarksPropertyAsChangedOnlyOnce()
Definition: FileTest.php:145
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\copyToCallsCopyOperationOnTargetFolderStorage
‪copyToCallsCopyOperationOnTargetFolderStorage()
Definition: FileTest.php:181
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: FileTest.php:37
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesDiscardsUidIfAlreadySet
‪updatePropertiesDiscardsUidIfAlreadySet()
Definition: FileTest.php:115
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\setUp
‪setUp()
Definition: FileTest.php:43
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesDoesNotRecordPropertyNameIfSameValueIsProvided
‪updatePropertiesDoesNotRecordPropertyNameIfSameValueIsProvided()
Definition: FileTest.php:135