TYPO3 CMS  TYPO3_8-7
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 
20 class ResourceFactoryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
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->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
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  $mockedRegistry = $this->createMock(\TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class);
81  $mockedRegistry->expects($this->once())->method('getDriverClass')->with($this->equalTo($driverFixtureClass))->will($this->returnValue($driverFixtureClass));
82  \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class, $mockedRegistry);
83  $obj = $this->subject->getDriverObject($driverFixtureClass, []);
84  $this->assertInstanceOf(\TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class, $obj);
85  }
86 
87  /***********************************
88  * File Handling
89  ***********************************/
90 
94  public function retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithRelativePath()
95  {
97  $subject = $this->getAccessibleMock(
98  \TYPO3\CMS\Core\Resource\ResourceFactory::class,
99  ['getFolderObjectFromCombinedIdentifier'],
100  [],
101  '',
102  false
103  );
104  $subject
105  ->expects($this->once())
106  ->method('getFolderObjectFromCombinedIdentifier')
107  ->with('typo3');
108  $subject->retrieveFileOrFolderObject('typo3');
109  }
110 
114  public function retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithAbsolutePath()
115  {
117  $subject = $this->getAccessibleMock(
118  \TYPO3\CMS\Core\Resource\ResourceFactory::class,
119  ['getFolderObjectFromCombinedIdentifier'],
120  [],
121  '',
122  false
123  );
124  $subject
125  ->expects($this->once())
126  ->method('getFolderObjectFromCombinedIdentifier')
127  ->with('typo3');
128  $subject->retrieveFileOrFolderObject(PATH_site . 'typo3');
129  }
130 
135  {
136  $this->subject = $this->getAccessibleMock(\TYPO3\CMS\Core\Resource\ResourceFactory::class, ['getFileObjectFromCombinedIdentifier'], [], '', false);
137  $filename = 'typo3temp/var/tests/4711.txt';
138  $this->subject->expects($this->once())
139  ->method('getFileObjectFromCombinedIdentifier')
140  ->with($filename);
141  // Create and prepare test file
143  $this->filesCreated[] = PATH_site . $filename;
144  $this->subject->retrieveFileOrFolderObject($filename);
145  }
146 
147  /***********************************
148  * Storage AutoDetection
149  ***********************************/
150 
158  public function findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)
159  {
160  $this->subject->_set('localDriverStorageCache', $storageConfiguration);
161  $this->assertSame($expectedStorageId, $this->subject->_callRef('findBestMatchingStorageByLocalPath', $path));
162  }
163 
168  {
169  return [
170  'NoLocalStoragesReturnDefaultStorage' => [
171  [],
172  'my/dummy/Image.png',
173  0
174  ],
175  'NoMatchReturnsDefaultStorage' => [
176  [1 => 'fileadmin/', 2 => 'fileadmin2/public/'],
177  'my/dummy/Image.png',
178  0
179  ],
180  'MatchReturnsTheMatch' => [
181  [1 => 'fileadmin/', 2 => 'other/public/'],
182  'fileadmin/dummy/Image.png',
183  1
184  ],
185  'TwoFoldersWithSameStartReturnsCorrect' => [
186  [1 => 'fileadmin/', 2 => 'fileadmin/public/'],
187  'fileadmin/dummy/Image.png',
188  1
189  ],
190  'NestedStorageReallyReturnsTheBestMatching' => [
191  [1 => 'fileadmin/', 2 => 'fileadmin/public/'],
192  'fileadmin/public/Image.png',
193  2
194  ],
195  'CommonPrefixButWrongPath' => [
196  [1 => 'fileadmin/', 2 => 'uploads/test/'],
197  'uploads/bogus/dummy.png',
198  0
199  ],
200  'CommonPrefixRightPath' => [
201  [1 => 'fileadmin/', 2 => 'uploads/test/'],
202  'uploads/test/dummy.png',
203  2
204  ],
205  'FindStorageFromWindowsPath' => [
206  [1 => 'fileadmin/', 2 => 'uploads/test/'],
207  'uploads\\test\\dummy.png',
208  2
209  ],
210  ];
211  }
212 }
static addInstance($className, $instance)
static setSingletonInstance($className, SingletonInterface $instance)
static writeFileToTypo3tempDir($filepath, $content)
static resetSingletonInstances(array $newSingletonInstances)
findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)