‪TYPO3CMS  ‪main
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 final class ‪FileTest extends UnitTestCase
34 {
35  protected bool ‪$resetSingletonInstances = true;
36 
40  protected ‪$storageMock;
41 
42  protected function ‪setUp(): void
43  {
44  parent::setUp();
45  $this->storageMock = $this->createMock(ResourceStorage::class);
46  $this->storageMock->method('getUid')->willReturn(5);
47 
48  $mockedMetaDataRepository = $this->createMock(MetaDataRepository::class);
49  $mockedMetaDataRepository->method('findByFile')->willReturn(['file' => 1]);
50  GeneralUtility::setSingletonInstance(MetaDataRepository::class, $mockedMetaDataRepository);
51  }
52 
53  protected function ‪prepareFixture(): ‪File
54  {
55  $fixture = new ‪File(['testfile'], $this->storageMock);
56  return $fixture;
57  }
58 
62  public function ‪commonPropertiesAreAvailableWithOwnGetters(): void
63  {
64  $properties = [
65  'name' => ‪StringUtility::getUniqueId('name_'),
66  'storage' => ‪$this->storageMock,
67  'size' => 1024,
68  ];
69  $fixture = new ‪File($properties, $this->storageMock);
70  foreach ($properties as $key => $value) {
71  self::assertEquals($value, $fixture->{'get' . $key}());
72  }
73  }
74 
80  public function fileIndexStatusIsTrueIfUidIsSet(): void
81  {
82  $fixture = new File(['uid' => 1], $this->storageMock);
83  self::assertTrue($fixture->isIndexed());
84  }
85 
89  public function updatePropertiesUpdatesFileProperties(): void
90  {
91  ‪$identifier = '/' . ‪StringUtility::getUniqueId('identifier_');
92  $fixture = new File(['uid' => 1, 'identifier' => '/test'], $this->storageMock);
93  $fixture->updateProperties(['identifier' => ‪$identifier]);
94  self::assertEquals(‪$identifier, $fixture->getIdentifier());
95  }
96 
100  public function updatePropertiesLeavesPropertiesUntouchedIfNotSetInNewProperties(): void
101  {
102  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'], $this->storageMock);
103  $fixture->updateProperties(['foo' => 'foobar']);
104  self::assertEquals('/test', $fixture->getIdentifier());
105  self::assertEquals('/test', $fixture->getProperty('identifier'));
106  }
107 
111  public function updatePropertiesDiscardsUidIfAlreadySet(): void
112  {
113  $fixture = new File(['uid' => 1, 'identifier' => '/test'], $this->storageMock);
114  $fixture->updateProperties(['uid' => 3]);
115  self::assertEquals(1, $fixture->getUid());
116  }
117 
121  public function updatePropertiesRecordsNamesOfChangedProperties(): void
122  {
123  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'], $this->storageMock);
124  $fixture->updateProperties(['foo' => 'foobar', 'baz' => 'foobaz']);
125  self::assertEquals(['foo', 'baz'], $fixture->getUpdatedProperties());
126  }
127 
131  public function updatePropertiesDoesNotRecordPropertyNameIfSameValueIsProvided(): void
132  {
133  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'], $this->storageMock);
134  $fixture->updateProperties(['foo' => 'asdf']);
135  self::assertEmpty($fixture->getUpdatedProperties());
136  }
137 
141  public function updatePropertiesMarksPropertyAsChangedOnlyOnce(): void
142  {
143  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'], $this->storageMock);
144  $fixture->updateProperties(['foo' => 'foobar', 'baz' => 'foobaz']);
145  $fixture->updateProperties(['foo' => 'fdsw', 'baz' => 'asdf']);
146  self::assertEquals(['foo', 'baz'], $fixture->getUpdatedProperties());
147  }
148 
152  public function updatePropertiesReloadsStorageObjectIfStorageChanges(): void
153  {
154  $fileProperties = [
155  'uid' => 1,
156  'storage' => 'first',
157  ];
158  $subject = $this->getMockBuilder(File::class)
159  ->addMethods(['loadStorage'])
160  ->setConstructorArgs([$fileProperties, $this->storageMock])
161  ->getMock();
162  $mockedNewStorage = $this->createMock(ResourceStorage::class);
163  $mockedStorageRepository = $this->createMock(StorageRepository::class);
164  $mockedStorageRepository
165  ->expects(self::once())
166  ->method('findByUid')
167  ->willReturn($mockedNewStorage);
168  GeneralUtility::addInstance(StorageRepository::class, $mockedStorageRepository);
169 
170  $subject->updateProperties(['storage' => 'different']);
171  self::assertSame($mockedNewStorage, $subject->getStorage());
172  }
173 
177  public function copyToCallsCopyOperationOnTargetFolderStorage(): void
178  {
179  $targetStorage = $this->createMock(ResourceStorage::class);
180  $targetFolder = $this->createMock(Folder::class);
181  $targetFolder->method('getStorage')->willReturn($targetStorage);
182  $fixture = new File([], $this->storageMock);
183  $targetStorage->expects(self::once())->method('copyFile')->with(self::equalTo($fixture), self::equalTo($targetFolder));
184  $fixture->copyTo($targetFolder);
185  }
186 
190  public function moveToCallsMoveOperationOnTargetFolderStorage(): void
191  {
192  $targetStorage = $this->createMock(ResourceStorage::class);
193  $targetFolder = $this->createMock(Folder::class);
194  $targetFolder->method('getStorage')->willReturn($targetStorage);
195  $fixture = new File([], $this->storageMock);
196  $targetStorage->expects(self::once())->method('moveFile')->with(self::equalTo($fixture), self::equalTo($targetFolder));
197  $fixture->moveTo($targetFolder);
198  }
199 
200  public static function filenameExtensionDataProvider(): array
201  {
202  return [
203  ['somefile.jpg', 'somefile', 'jpg'],
204  ['SomeFile.PNG', 'SomeFile', 'png'],
205  ['somefile', 'somefile', ''],
206  ['somefile.tar.gz', 'somefile.tar', 'gz'],
207  ['somefile.tar.bz2', 'somefile.tar', 'bz2'],
208  ];
209  }
210 
215  public function getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename): void
216  {
217  $fixture = new ‪File(
218  [
219  'name' => $originalFilename,
220  'identifier' => '/' . $originalFilename,
221  ],
222  $this->storageMock
223  );
224  self::assertSame($expectedBasename, $fixture->getNameWithoutExtension());
225  }
226 
231  public function getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension): void
232  {
233  $fixture = new File([
234  'name' => $originalFilename,
235  'identifier' => '/' . $originalFilename,
236  ], $this->storageMock);
237  self::assertSame($expectedExtension, $fixture->getExtension());
238  }
239 
243  public function hasPropertyReturnsTrueFilePropertyExists(): void
244  {
245  $fixture = new File(['testproperty' => 'testvalue'], $this->storageMock);
246  self::assertTrue($fixture->hasProperty('testproperty'));
247  }
248 
252  public function hasPropertyReturnsTrueIfMetadataPropertyExists(): void
253  {
254  $fixture = $this->getMockBuilder(File::class)
255  ->setConstructorArgs([[], $this->storageMock])
256  ->onlyMethods(['getMetaData'])
257  ->getMock();
258 
259  $metaDataAspectMock = $this->getMockBuilder(MetaDataAspect::class)
260  ->setConstructorArgs([$fixture])
261  ->onlyMethods(['get'])
262  ->getMock();
263 
264  $metaDataAspectMock->method('get')->willReturn(['testproperty' => 'testvalue']);
265  $fixture->method('getMetaData')->willReturn($metaDataAspectMock);
266 
267  self::assertTrue($fixture->hasProperty('testproperty'));
268  self::assertSame('testvalue', $fixture->getProperty('testproperty'));
269  }
270 
274  public function getPropertiesContainsUidOfSysFileMetadata(): void
275  {
276  $fileData = [
277  'uid' => 1357,
278  'name' => 'dummy.svg',
279  ];
280  $metaData = [
281  'uid' => 2468,
282  'file' => 1357,
283  'title' => 'Dummy SVG',
284  ];
285  $file = new File($fileData, $this->storageMock, $metaData);
286 
287  self::assertSame(
288  1357,
289  $file->getProperties()['uid']
290  );
291  self::assertSame(
292  2468,
293  $file->getProperties()['metadata_uid']
294  );
295  }
296 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:40
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\getNameWithoutExtensionReturnsCorrectName
‪getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename)
Definition: FileTest.php:214
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\fileIndexStatusIsTrueIfUidIsSet
‪fileIndexStatusIsTrueIfUidIsSet()
Definition: FileTest.php:79
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesReloadsStorageObjectIfStorageChanges
‪updatePropertiesReloadsStorageObjectIfStorageChanges()
Definition: FileTest.php:151
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesUpdatesFileProperties
‪updatePropertiesUpdatesFileProperties()
Definition: FileTest.php:88
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\moveToCallsMoveOperationOnTargetFolderStorage
‪moveToCallsMoveOperationOnTargetFolderStorage()
Definition: FileTest.php:189
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\prepareFixture
‪prepareFixture()
Definition: FileTest.php:52
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\$storageMock
‪ResourceStorage $storageMock
Definition: FileTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesLeavesPropertiesUntouchedIfNotSetInNewProperties
‪updatePropertiesLeavesPropertiesUntouchedIfNotSetInNewProperties()
Definition: FileTest.php:99
‪TYPO3\CMS\Core\Resource\MetaDataAspect
Definition: MetaDataAspect.php:27
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\hasPropertyReturnsTrueIfMetadataPropertyExists
‪hasPropertyReturnsTrueIfMetadataPropertyExists()
Definition: FileTest.php:251
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\getExtensionReturnsCorrectExtension
‪getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension)
Definition: FileTest.php:230
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:38
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\filenameExtensionDataProvider
‪static filenameExtensionDataProvider()
Definition: FileTest.php:199
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:38
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\getPropertiesContainsUidOfSysFileMetadata
‪getPropertiesContainsUidOfSysFileMetadata()
Definition: FileTest.php:273
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest
Definition: FileTest.php:34
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:128
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesRecordsNamesOfChangedProperties
‪updatePropertiesRecordsNamesOfChangedProperties()
Definition: FileTest.php:120
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\hasPropertyReturnsTrueFilePropertyExists
‪hasPropertyReturnsTrueFilePropertyExists()
Definition: FileTest.php:242
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\commonPropertiesAreAvailableWithOwnGetters
‪commonPropertiesAreAvailableWithOwnGetters()
Definition: FileTest.php:61
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesMarksPropertyAsChangedOnlyOnce
‪updatePropertiesMarksPropertyAsChangedOnlyOnce()
Definition: FileTest.php:140
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\copyToCallsCopyOperationOnTargetFolderStorage
‪copyToCallsCopyOperationOnTargetFolderStorage()
Definition: FileTest.php:176
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: FileTest.php:35
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesDiscardsUidIfAlreadySet
‪updatePropertiesDiscardsUidIfAlreadySet()
Definition: FileTest.php:110
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\setUp
‪setUp()
Definition: FileTest.php:41
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesDoesNotRecordPropertyNameIfSameValueIsProvided
‪updatePropertiesDoesNotRecordPropertyNameIfSameValueIsProvided()
Definition: FileTest.php:130