TYPO3 CMS  TYPO3_8-7
FileTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
22 class FileTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
27  protected $singletonInstances = [];
28 
32  protected $storageMock;
33 
34  protected function setUp()
35  {
37  $this->storageMock = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
38  $this->storageMock->expects($this->any())->method('getUid')->will($this->returnValue(5));
39 
40  $mockedMetaDataRepository = $this->createMock(\TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class);
41  $mockedMetaDataRepository->expects($this->any())->method('findByFile')->will($this->returnValue(['file' => 1]));
42  \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class, $mockedMetaDataRepository);
43  }
44 
45  protected function tearDown()
46  {
48  parent::tearDown();
49  }
50 
54  protected function prepareFixture()
55  {
56  $fixture = new \TYPO3\CMS\Core\Resource\File(['testfile'], $this->storageMock);
57  return $fixture;
58  }
59 
64  {
65  $properties = [
66  'name' => $this->getUniqueId(),
67  'storage' => $this->storageMock,
68  'size' => 1024
69  ];
70  $fixture = new \TYPO3\CMS\Core\Resource\File($properties, $this->storageMock);
71  foreach ($properties as $key => $value) {
72  $this->assertEquals($value, call_user_func([$fixture, 'get' . $key]));
73  }
74  }
75 
82  {
83  $fixture = new \TYPO3\CMS\Core\Resource\File(['uid' => 1], $this->storageMock);
84  $this->assertTrue($fixture->isIndexed());
85  }
86 
91  {
92  $identifier = '/' . $this->getUniqueId();
93  $fixture = new \TYPO3\CMS\Core\Resource\File(['uid' => 1, 'identifier' => '/test'], $this->storageMock);
94  $fixture->updateProperties(['identifier' => $identifier]);
95  $this->assertEquals($identifier, $fixture->getIdentifier());
96  }
97 
102  {
103  $fixture = new \TYPO3\CMS\Core\Resource\File(['uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'], $this->storageMock);
104  $fixture->updateProperties(['foo' => 'foobar']);
105  $this->assertEquals('/test', $fixture->getIdentifier());
106  $this->assertEquals('/test', $fixture->getProperty('identifier'));
107  }
108 
113  {
114  $fixture = new \TYPO3\CMS\Core\Resource\File(['uid' => 1, 'identifier' => '/test'], $this->storageMock);
115  $fixture->updateProperties(['uid' => 3]);
116  $this->assertEquals(1, $fixture->getUid());
117  }
118 
123  {
124  $fixture = new \TYPO3\CMS\Core\Resource\File(['uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'], $this->storageMock);
125  $fixture->updateProperties(['foo' => 'foobar', 'baz' => 'foobaz']);
126  $this->assertEquals(['foo', 'baz'], $fixture->getUpdatedProperties());
127  }
128 
133  {
134  $fixture = new \TYPO3\CMS\Core\Resource\File(['uid' => 1, 'foo' => 'asdf', 'identifier' => '/test'], $this->storageMock);
135  $fixture->updateProperties(['foo' => 'asdf']);
136  $this->assertEmpty($fixture->getUpdatedProperties());
137  }
138 
143  {
144  $fixture = new \TYPO3\CMS\Core\Resource\File(['uid' => 1, 'foo' => 'asdf', 'baz' => 'fdsw', 'identifier' => '/test'], $this->storageMock);
145  $fixture->updateProperties(['foo' => 'foobar', 'baz' => 'foobaz']);
146  $fixture->updateProperties(['foo' => 'fdsw', 'baz' => 'asdf']);
147  $this->assertEquals(['foo', 'baz'], $fixture->getUpdatedProperties());
148  }
149 
154  {
155  $fileProperties = [
156  'uid' => 1,
157  'storage' => 'first',
158  ];
159  $subject = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\File::class)
160  ->setMethods(['loadStorage'])
161  ->setConstructorArgs([$fileProperties, $this->storageMock])
162  ->getMock();
163  $mockedNewStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
164  $mockedResourceFactory = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
165  $mockedResourceFactory
166  ->expects($this->once())
167  ->method('getStorageObject')
168  ->will($this->returnValue($mockedNewStorage));
169  \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class, $mockedResourceFactory);
170 
171  $subject->updateProperties(['storage' => 'different']);
172  $this->assertSame($mockedNewStorage, $subject->getStorage());
173  }
174 
179  {
180  $targetStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
181  $targetFolder = $this->createMock(\TYPO3\CMS\Core\Resource\Folder::class);
182  $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
183  $fixture = new \TYPO3\CMS\Core\Resource\File([], $this->storageMock);
184  $targetStorage->expects($this->once())->method('copyFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
185  $fixture->copyTo($targetFolder);
186  }
187 
192  {
193  $targetStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
194  $targetFolder = $this->createMock(\TYPO3\CMS\Core\Resource\Folder::class);
195  $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
196  $fixture = new \TYPO3\CMS\Core\Resource\File([], $this->storageMock);
197  $targetStorage->expects($this->once())->method('moveFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
198  $fixture->moveTo($targetFolder);
199  }
200 
202  {
203  return [
204  ['somefile.jpg', 'somefile', 'jpg'],
205  ['SomeFile.PNG', 'SomeFile', 'png'],
206  ['somefile', 'somefile', ''],
207  ['somefile.tar.gz', 'somefile.tar', 'gz'],
208  ['somefile.tar.bz2', 'somefile.tar', 'bz2'],
209  ];
210  }
211 
216  public function getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename)
217  {
218  $fixture = new \TYPO3\CMS\Core\Resource\File(
219  [
220  'name' => $originalFilename,
221  'identifier' => '/' . $originalFilename
222  ],
223  $this->storageMock
224  );
225  $this->assertSame($expectedBasename, $fixture->getNameWithoutExtension());
226  }
227 
232  public function getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension)
233  {
234  $fixture = new \TYPO3\CMS\Core\Resource\File([
235  'name' => $originalFilename,
236  'identifier' => '/' . $originalFilename
237  ], $this->storageMock);
238  $this->assertSame($expectedExtension, $fixture->getExtension());
239  }
240 
245  {
246  $fixture = new \TYPO3\CMS\Core\Resource\File(['testproperty' => 'testvalue'], $this->storageMock);
247  $this->assertTrue($fixture->hasProperty('testproperty'));
248  }
249 
254  {
255  $fixture = $this->getAccessibleMock(\TYPO3\CMS\Core\Resource\File::class, ['dummy'], [[], $this->storageMock]);
256  $fixture->_set('metaDataLoaded', true);
257  $fixture->_set('metaDataProperties', ['testproperty' => 'testvalue']);
258  $this->assertTrue($fixture->hasProperty('testproperty'));
259  }
260 }
getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename)
Definition: FileTest.php:216
static setSingletonInstance($className, SingletonInterface $instance)
getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension)
Definition: FileTest.php:232
static resetSingletonInstances(array $newSingletonInstances)