‪TYPO3CMS  9.5
FileTest.php
Go to the documentation of this file.
1 <?php
2 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 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪FileTest extends UnitTestCase
28 {
32  protected ‪$resetSingletonInstances = true;
33 
37  protected ‪$storageMock;
38 
39  protected function ‪setUp(): void
40  {
41  $this->storageMock = $this->createMock(ResourceStorage::class);
42  $this->storageMock->expects($this->any())->method('getUid')->will($this->returnValue(5));
43 
44  $mockedMetaDataRepository = $this->createMock(MetaDataRepository::class);
45  $mockedMetaDataRepository->expects($this->any())->method('findByFile')->will($this->returnValue(['file' => 1]));
46  GeneralUtility::setSingletonInstance(MetaDataRepository::class, $mockedMetaDataRepository);
47  }
48 
52  protected function ‪prepareFixture(): ‪File
53  {
54  $fixture = new ‪File(['testfile'], $this->storageMock);
55  return $fixture;
56  }
57 
61  public function ‪commonPropertiesAreAvailableWithOwnGetters(): void
62  {
63  $properties = [
64  'name' => $this->getUniqueId(),
65  'storage' => ‪$this->storageMock,
66  'size' => 1024
67  ];
68  $fixture = new File($properties, $this->storageMock);
69  foreach ($properties as $key => $value) {
70  $this->assertEquals($value, call_user_func([$fixture, 'get' . $key]));
71  }
72  }
73 
79  public function ‪fileIndexStatusIsTrueIfUidIsSet(): void
80  {
81  $fixture = new File(['uid' => 1], $this->storageMock);
82  $this->assertTrue($fixture->isIndexed());
83  }
84 
88  public function ‪updatePropertiesUpdatesFileProperties(): void
89  {
90  $identifier = '/' . $this->getUniqueId();
91  $fixture = new File(['uid' => 1, 'identifier' => '/test'], $this->storageMock);
92  $fixture->updateProperties(['identifier' => $identifier]);
93  $this->assertEquals($identifier, $fixture->getIdentifier());
94  }
95 
100  {
101  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'], $this->storageMock);
102  $fixture->updateProperties(['foo' => 'foobar']);
103  $this->assertEquals('/test', $fixture->getIdentifier());
104  $this->assertEquals('/test', $fixture->getProperty('identifier'));
105  }
106 
110  public function ‪updatePropertiesDiscardsUidIfAlreadySet(): void
111  {
112  $fixture = new File(['uid' => 1, 'identifier' => '/test'], $this->storageMock);
113  $fixture->updateProperties(['uid' => 3]);
114  $this->assertEquals(1, $fixture->getUid());
115  }
116 
121  {
122  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'], $this->storageMock);
123  $fixture->updateProperties(['foo' => 'foobar', 'baz' => 'foobaz']);
124  $this->assertEquals(['foo', 'baz'], $fixture->getUpdatedProperties());
125  }
126 
131  {
132  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'], $this->storageMock);
133  $fixture->updateProperties(['foo' => 'asdf']);
134  $this->assertEmpty($fixture->getUpdatedProperties());
135  }
136 
141  {
142  $fixture = new File(['uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'], $this->storageMock);
143  $fixture->updateProperties(['foo' => 'foobar', 'baz' => 'foobaz']);
144  $fixture->updateProperties(['foo' => 'fdsw', 'baz' => 'asdf']);
145  $this->assertEquals(['foo', 'baz'], $fixture->getUpdatedProperties());
146  }
147 
152  {
153  $fileProperties = [
154  'uid' => 1,
155  'storage' => 'first',
156  ];
157  $subject = $this->getMockBuilder(File::class)
158  ->setMethods(['loadStorage'])
159  ->setConstructorArgs([$fileProperties, $this->storageMock])
160  ->getMock();
161  $mockedNewStorage = $this->createMock(\‪TYPO3\CMS\Core\Resource\ResourceStorage::class);
162  $mockedResourceFactory = $this->createMock(\‪TYPO3\CMS\Core\Resource\ResourceFactory::class);
163  $mockedResourceFactory
164  ->expects($this->once())
165  ->method('getStorageObject')
166  ->will($this->returnValue($mockedNewStorage));
167  GeneralUtility::setSingletonInstance(\‪TYPO3\CMS\Core\Resource\ResourceFactory::class, $mockedResourceFactory);
168 
169  $subject->updateProperties(['storage' => 'different']);
170  $this->assertSame($mockedNewStorage, $subject->getStorage());
171  }
172 
177  {
178  $targetStorage = $this->createMock(\‪TYPO3\CMS\Core\Resource\ResourceStorage::class);
179  $targetFolder = $this->createMock(\‪TYPO3\CMS\Core\Resource\Folder::class);
180  $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
181  $fixture = new File([], $this->storageMock);
182  $targetStorage->expects($this->once())->method('copyFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
183  $fixture->copyTo($targetFolder);
184  }
185 
190  {
191  $targetStorage = $this->createMock(\‪TYPO3\CMS\Core\Resource\ResourceStorage::class);
192  $targetFolder = $this->createMock(\‪TYPO3\CMS\Core\Resource\Folder::class);
193  $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
194  $fixture = new File([], $this->storageMock);
195  $targetStorage->expects($this->once())->method('moveFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
196  $fixture->moveTo($targetFolder);
197  }
198 
199  public function ‪filenameExtensionDataProvider(): array
200  {
201  return [
202  ['somefile.jpg', 'somefile', 'jpg'],
203  ['SomeFile.PNG', 'SomeFile', 'png'],
204  ['somefile', 'somefile', ''],
205  ['somefile.tar.gz', 'somefile.tar', 'gz'],
206  ['somefile.tar.bz2', 'somefile.tar', 'bz2'],
207  ];
208  }
209 
214  public function ‪getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename): void
215  {
216  $fixture = new File(
217  [
218  'name' => $originalFilename,
219  'identifier' => '/' . $originalFilename
220  ],
221  $this->storageMock
222  );
223  $this->assertSame($expectedBasename, $fixture->getNameWithoutExtension());
224  }
225 
230  public function ‪getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension): void
231  {
232  $fixture = new File([
233  'name' => $originalFilename,
234  'identifier' => '/' . $originalFilename
235  ], $this->storageMock);
236  $this->assertSame($expectedExtension, $fixture->getExtension());
237  }
238 
242  public function ‪hasPropertyReturnsTrueFilePropertyExists(): void
243  {
244  $fixture = new File(['testproperty' => 'testvalue'], $this->storageMock);
245  $this->assertTrue($fixture->hasProperty('testproperty'));
246  }
247 
252  {
253  $fixture = $this->getAccessibleMock(File::class, ['dummy'], [[], $this->storageMock]);
254  $fixture->_set('metaDataLoaded', true);
255  $fixture->_set('metaDataProperties', ['testproperty' => 'testvalue']);
256  $this->assertTrue($fixture->hasProperty('testproperty'));
257  }
258 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:37
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\getNameWithoutExtensionReturnsCorrectName
‪getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename)
Definition: FileTest.php:212
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\fileIndexStatusIsTrueIfUidIsSet
‪fileIndexStatusIsTrueIfUidIsSet()
Definition: FileTest.php:77
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesReloadsStorageObjectIfStorageChanges
‪updatePropertiesReloadsStorageObjectIfStorageChanges()
Definition: FileTest.php:149
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesUpdatesFileProperties
‪updatePropertiesUpdatesFileProperties()
Definition: FileTest.php:86
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\moveToCallsMoveOperationOnTargetFolderStorage
‪moveToCallsMoveOperationOnTargetFolderStorage()
Definition: FileTest.php:187
‪TYPO3
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\prepareFixture
‪File prepareFixture()
Definition: FileTest.php:50
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:3
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\$storageMock
‪ResourceStorage $storageMock
Definition: FileTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesLeavesPropertiesUntouchedIfNotSetInNewProperties
‪updatePropertiesLeavesPropertiesUntouchedIfNotSetInNewProperties()
Definition: FileTest.php:97
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\hasPropertyReturnsTrueIfMetadataPropertyExists
‪hasPropertyReturnsTrueIfMetadataPropertyExists()
Definition: FileTest.php:249
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\getExtensionReturnsCorrectExtension
‪getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension)
Definition: FileTest.php:228
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\filenameExtensionDataProvider
‪filenameExtensionDataProvider()
Definition: FileTest.php:197
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest
Definition: FileTest.php:28
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:74
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesRecordsNamesOfChangedProperties
‪updatePropertiesRecordsNamesOfChangedProperties()
Definition: FileTest.php:118
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\hasPropertyReturnsTrueFilePropertyExists
‪hasPropertyReturnsTrueFilePropertyExists()
Definition: FileTest.php:240
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\commonPropertiesAreAvailableWithOwnGetters
‪commonPropertiesAreAvailableWithOwnGetters()
Definition: FileTest.php:59
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesMarksPropertyAsChangedOnlyOnce
‪updatePropertiesMarksPropertyAsChangedOnlyOnce()
Definition: FileTest.php:138
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\copyToCallsCopyOperationOnTargetFolderStorage
‪copyToCallsCopyOperationOnTargetFolderStorage()
Definition: FileTest.php:174
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: FileTest.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesDiscardsUidIfAlreadySet
‪updatePropertiesDiscardsUidIfAlreadySet()
Definition: FileTest.php:108
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\setUp
‪setUp()
Definition: FileTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Resource\FileTest\updatePropertiesDoesNotRecordPropertyNameIfSameValueIsProvided
‪updatePropertiesDoesNotRecordPropertyNameIfSameValueIsProvided()
Definition: FileTest.php:128