TYPO3 CMS  TYPO3_7-6
ResourceFactoryTest.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 
21 {
25  protected $singletonInstances = [];
26 
30  protected $subject;
31 
35  protected $filesCreated = [];
36 
37  protected function setUp()
38  {
40  $this->subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Resource\ResourceFactory::class, ['dummy'], [], '', false);
41  }
42 
43  protected function tearDown()
44  {
46  foreach ($this->filesCreated as $file) {
47  unlink($file);
48  }
49  parent::tearDown();
50  }
51 
52  /**********************************
53  * Storage Collections
54  **********************************/
59  {
60  $mockedMount = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, [], [], '', false);
61  $path = $this->getUniqueId();
62  $name = $this->getUniqueId();
63  $storageCollection = $this->subject->createFolderObject($mockedMount, $path, $name, 0);
64  $this->assertSame($mockedMount, $storageCollection->getStorage());
65  $this->assertEquals($path . '/', $storageCollection->getIdentifier());
66  $this->assertEquals($name, $storageCollection->getName());
67  }
68 
69  /**********************************
70  * Drivers
71  **********************************/
76  {
77  $mockedDriver = $this->getMockForAbstractClass(\TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class);
78  $driverFixtureClass = get_class($mockedDriver);
79  \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance($driverFixtureClass, $mockedDriver);
80  $mockedMount = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, [], [], '', false);
81  $mockedRegistry = $this->getMock(\TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class);
82  $mockedRegistry->expects($this->once())->method('getDriverClass')->with($this->equalTo($driverFixtureClass))->will($this->returnValue($driverFixtureClass));
83  \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class, $mockedRegistry);
84  $obj = $this->subject->getDriverObject($driverFixtureClass, []);
85  $this->assertInstanceOf(\TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class, $obj);
86  }
87 
88  /***********************************
89  * File Handling
90  ***********************************/
91 
95  public function retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithRelativePath()
96  {
98  $subject = $this->getAccessibleMock(
99  \TYPO3\CMS\Core\Resource\ResourceFactory::class,
100  ['getFolderObjectFromCombinedIdentifier'],
101  [],
102  '',
103  false
104  );
105  $subject
106  ->expects($this->once())
107  ->method('getFolderObjectFromCombinedIdentifier')
108  ->with('typo3');
109  $subject->retrieveFileOrFolderObject('typo3');
110  }
111 
115  public function retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithAbsolutePath()
116  {
118  $subject = $this->getAccessibleMock(
119  \TYPO3\CMS\Core\Resource\ResourceFactory::class,
120  ['getFolderObjectFromCombinedIdentifier'],
121  [],
122  '',
123  false
124  );
125  $subject
126  ->expects($this->once())
127  ->method('getFolderObjectFromCombinedIdentifier')
128  ->with('typo3');
129  $subject->retrieveFileOrFolderObject(PATH_site . 'typo3');
130  }
131 
136  {
137  $this->subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Resource\ResourceFactory::class, ['getFileObjectFromCombinedIdentifier'], [], '', false);
138  $filename = 'typo3temp/4711.txt';
139  $this->subject->expects($this->once())
140  ->method('getFileObjectFromCombinedIdentifier')
141  ->with($filename);
142  // Create and prepare test file
144  $this->filesCreated[] = PATH_site . $filename;
145  $this->subject->retrieveFileOrFolderObject($filename);
146  }
147 
148  /***********************************
149  * Storage AutoDetection
150  ***********************************/
151 
159  public function findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)
160  {
161  $this->subject->_set('localDriverStorageCache', $storageConfiguration);
162  $this->assertSame($expectedStorageId, $this->subject->_callRef('findBestMatchingStorageByLocalPath', $path));
163  }
164 
169  {
170  return [
171  'NoLocalStoragesReturnDefaultStorage' => [
172  [],
173  'my/dummy/Image.png',
174  0
175  ],
176  'NoMatchReturnsDefaultStorage' => [
177  [1 => 'fileadmin/', 2 => 'fileadmin2/public/'],
178  'my/dummy/Image.png',
179  0
180  ],
181  'MatchReturnsTheMatch' => [
182  [1 => 'fileadmin/', 2 => 'other/public/'],
183  'fileadmin/dummy/Image.png',
184  1
185  ],
186  'TwoFoldersWithSameStartReturnsCorrect' => [
187  [1 => 'fileadmin/', 2 => 'fileadmin/public/'],
188  'fileadmin/dummy/Image.png',
189  1
190  ],
191  'NestedStorageReallyReturnsTheBestMatching' => [
192  [1 => 'fileadmin/', 2 => 'fileadmin/public/'],
193  'fileadmin/public/Image.png',
194  2
195  ],
196  'CommonPrefixButWrongPath' => [
197  [1 => 'fileadmin/', 2 => 'uploads/test/'],
198  'uploads/bogus/dummy.png',
199  0
200  ],
201  'CommonPrefixRightPath' => [
202  [1 => 'fileadmin/', 2 => 'uploads/test/'],
203  'uploads/test/dummy.png',
204  2
205  ],
206  'FindStorageFromWindowsPath' => [
207  [1 => 'fileadmin/', 2 => 'uploads/test/'],
208  'uploads\\test\\dummy.png',
209  2
210  ],
211  ];
212  }
213 }
static addInstance($className, $instance)
static setSingletonInstance($className, SingletonInterface $instance)
static writeFileToTypo3tempDir($filepath, $content)
static resetSingletonInstances(array $newSingletonInstances)
findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)