TYPO3 CMS  TYPO3_7-6
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 
23 {
27  protected $singletonInstances = [];
28 
32  protected $storageMock;
33 
34  protected function setUp()
35  {
37  $this->storageMock = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, [], [], '', false);
38  $this->storageMock->expects($this->any())->method('getUid')->will($this->returnValue(5));
39 
40  $mockedMetaDataRepository = $this->getMock(\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->getMock(
160  \TYPO3\CMS\Core\Resource\File::class,
161  ['loadStorage'],
162  [$fileProperties, $this->storageMock]
163  );
164  $mockedNewStorage = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, [], [], '', false);
165  $mockedResourceFactory = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceFactory::class);
166  $mockedResourceFactory
167  ->expects($this->once())
168  ->method('getStorageObject')
169  ->will($this->returnValue($mockedNewStorage));
170  \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Resource\ResourceFactory::class, $mockedResourceFactory);
171 
172  $subject->updateProperties(['storage' => 'different']);
173  $this->assertSame($mockedNewStorage, $subject->getStorage());
174  }
175 
180  {
181  $targetStorage = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, [], [], '', false);
182  $targetFolder = $this->getMock(\TYPO3\CMS\Core\Resource\Folder::class, [], [], '', false);
183  $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
184  $fixture = new \TYPO3\CMS\Core\Resource\File([], $this->storageMock);
185  $targetStorage->expects($this->once())->method('copyFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
186  $fixture->copyTo($targetFolder);
187  }
188 
193  {
194  $targetStorage = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, [], [], '', false);
195  $targetFolder = $this->getMock(\TYPO3\CMS\Core\Resource\Folder::class, [], [], '', false);
196  $targetFolder->expects($this->any())->method('getStorage')->will($this->returnValue($targetStorage));
197  $fixture = new \TYPO3\CMS\Core\Resource\File([], $this->storageMock);
198  $targetStorage->expects($this->once())->method('moveFile')->with($this->equalTo($fixture), $this->equalTo($targetFolder));
199  $fixture->moveTo($targetFolder);
200  }
201 
203  {
204  return [
205  ['somefile.jpg', 'somefile', 'jpg'],
206  ['SomeFile.PNG', 'SomeFile', 'png'],
207  ['somefile', 'somefile', ''],
208  ['somefile.tar.gz', 'somefile.tar', 'gz'],
209  ['somefile.tar.bz2', 'somefile.tar', 'bz2'],
210  ];
211  }
212 
217  public function getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename)
218  {
219  $fixture = new \TYPO3\CMS\Core\Resource\File([
220  'name' => $originalFilename,
221  'identifier' => '/' . $originalFilename
222  ],
223  $this->storageMock);
224  $this->assertSame($expectedBasename, $fixture->getNameWithoutExtension());
225  }
226 
231  public function getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension)
232  {
233  $fixture = new \TYPO3\CMS\Core\Resource\File([
234  'name' => $originalFilename,
235  'identifier' => '/' . $originalFilename
236  ], $this->storageMock);
237  $this->assertSame($expectedExtension, $fixture->getExtension());
238  }
239 
244  {
245  $fixture = new \TYPO3\CMS\Core\Resource\File(['testproperty' => 'testvalue'], $this->storageMock);
246  $this->assertTrue($fixture->hasProperty('testproperty'));
247  }
248 
253  {
254  $fixture = $this->getAccessibleMock(\TYPO3\CMS\Core\Resource\File::class, ['dummy'], [[], $this->storageMock]);
255  $fixture->_set('metaDataLoaded', true);
256  $fixture->_set('metaDataProperties', ['testproperty' => 'testvalue']);
257  $this->assertTrue($fixture->hasProperty('testproperty'));
258  }
259 }
getNameWithoutExtensionReturnsCorrectName($originalFilename, $expectedBasename)
Definition: FileTest.php:217
static setSingletonInstance($className, SingletonInterface $instance)
getExtensionReturnsCorrectExtension($originalFilename, $expectedBasename, $expectedExtension)
Definition: FileTest.php:231
static resetSingletonInstances(array $newSingletonInstances)
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)