‪TYPO3CMS  9.5
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 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21 
25 class ‪ResourceFactoryTest extends UnitTestCase
26 {
30  protected ‪$resetSingletonInstances = true;
31 
35  protected ‪$subject;
36 
40  protected ‪$filesCreated = [];
41 
45  protected function ‪setUp()
46  {
47  $this->subject = $this->getAccessibleMock(ResourceFactory::class, ['dummy'], [], '', false);
48  }
49 
53  protected function ‪tearDown()
54  {
55  foreach ($this->filesCreated as $file) {
56  unlink($file);
57  }
58  parent::tearDown();
59  }
60 
61  /**********************************
62  * Storage Collections
63  **********************************/
68  {
69  $mockedMount = $this->createMock(\‪TYPO3\CMS\Core\Resource\ResourceStorage::class);
70  $path = $this->getUniqueId();
71  $name = $this->getUniqueId();
72  $storageCollection = $this->subject->createFolderObject($mockedMount, $path, $name, 0);
73  $this->assertSame($mockedMount, $storageCollection->getStorage());
74  $this->assertEquals($path, $storageCollection->getIdentifier());
75  $this->assertEquals($name, $storageCollection->getName());
76  }
77 
78  /**********************************
79  * Drivers
80  **********************************/
85  {
86  $mockedDriver = $this->getMockForAbstractClass(\‪TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class);
87  $driverFixtureClass = get_class($mockedDriver);
88  \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance($driverFixtureClass, $mockedDriver);
89  $mockedRegistry = $this->createMock(\‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class);
90  $mockedRegistry->expects($this->once())->method('getDriverClass')->with($this->equalTo($driverFixtureClass))->will($this->returnValue($driverFixtureClass));
91  \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance(\‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry::class, $mockedRegistry);
92  $obj = $this->subject->getDriverObject($driverFixtureClass, []);
93  $this->assertInstanceOf(\‪TYPO3\CMS\Core\Resource\Driver\AbstractDriver::class, $obj);
94  }
95 
96  /***********************************
97  * File Handling
98  ***********************************/
99 
104  {
106  ‪$subject = $this->getAccessibleMock(
107  ResourceFactory::class,
108  ['getFolderObjectFromCombinedIdentifier'],
109  [],
110  '',
111  false
112  );
114  ->expects($this->once())
115  ->method('getFolderObjectFromCombinedIdentifier')
116  ->with('typo3');
118  }
119 
124  {
126  ‪$subject = $this->getAccessibleMock(
127  ResourceFactory::class,
128  ['getFolderObjectFromCombinedIdentifier'],
129  [],
130  '',
131  false
132  );
134  ->expects($this->once())
135  ->method('getFolderObjectFromCombinedIdentifier')
136  ->with('typo3');
138  }
139 
144  {
145  $this->subject = $this->getAccessibleMock(ResourceFactory::class, ['getFileObjectFromCombinedIdentifier'], [], '', false);
146  $filename = 'typo3temp/var/tests/4711.txt';
147  $this->subject->expects($this->once())
148  ->method('getFileObjectFromCombinedIdentifier')
149  ->with($filename);
150  // Create and prepare test file
151  \TYPO3\CMS\Core\Utility\GeneralUtility::writeFileToTypo3tempDir(‪Environment::getPublicPath() . '/' . $filename, '42');
152  $this->filesCreated[] = ‪Environment::getPublicPath() . '/' . $filename;
153  $this->subject->retrieveFileOrFolderObject($filename);
154  }
155 
156  /***********************************
157  * Storage AutoDetection
158  ***********************************/
159 
167  public function ‪findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)
168  {
169  $this->subject->_set('localDriverStorageCache', $storageConfiguration);
170  $this->assertSame($expectedStorageId, $this->subject->_callRef('findBestMatchingStorageByLocalPath', $path));
171  }
172 
176  public function ‪storageDetectionDataProvider()
177  {
178  return [
179  'NoLocalStoragesReturnDefaultStorage' => [
180  [],
181  'my/dummy/Image.png',
182  0
183  ],
184  'NoMatchReturnsDefaultStorage' => [
185  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'fileadmin2/public/']),
186  'my/dummy/Image.png',
187  0
188  ],
189  'MatchReturnsTheMatch' => [
190  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'other/public/']),
191  'fileadmin/dummy/Image.png',
192  1
193  ],
194  'TwoFoldersWithSameStartReturnsCorrect' => [
195  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'fileadmin/public/']),
196  'fileadmin/dummy/Image.png',
197  1
198  ],
199  'NestedStorageReallyReturnsTheBestMatching' => [
200  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'fileadmin/public/']),
201  'fileadmin/public/Image.png',
202  2
203  ],
204  'CommonPrefixButWrongPath' => [
205  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'uploads/test/']),
206  'uploads/bogus/dummy.png',
207  0
208  ],
209  'CommonPrefixRightPath' => [
210  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'uploads/test/']),
211  'uploads/test/dummy.png',
212  2
213  ],
214  'FindStorageFromWindowsPath' => [
215  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'uploads/test/']),
216  'uploads\\test\\dummy.png',
217  2
218  ],
219  ];
220  }
221 
222  private function ‪asRelativePath(string $value): ‪LocalPath
223  {
224  return new ‪LocalPath($value, ‪LocalPath::TYPE_RELATIVE);
225  }
226 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\getDriverObjectAcceptsDriverClassName
‪getDriverObjectAcceptsDriverClassName()
Definition: ResourceFactoryTest.php:81
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\$subject
‪ResourceFactory $subject
Definition: ResourceFactoryTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound
‪findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)
Definition: ResourceFactoryTest.php:164
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\retrieveFileOrFolderObjectReturnsFileIfPathIsGiven
‪retrieveFileOrFolderObjectReturnsFileIfPathIsGiven()
Definition: ResourceFactoryTest.php:140
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3
‪TYPO3\CMS\Core\Resource\LocalPath\TYPE_RELATIVE
‪const TYPE_RELATIVE
Definition: LocalPath.php:31
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:3
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ResourceFactoryTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithRelativePath
‪retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithRelativePath()
Definition: ResourceFactoryTest.php:100
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithAbsolutePath
‪retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithAbsolutePath()
Definition: ResourceFactoryTest.php:120
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest
Definition: ResourceFactoryTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\createStorageCollectionObjectCreatesCollectionWithCorrectArguments
‪createStorageCollectionObjectCreatesCollectionWithCorrectArguments()
Definition: ResourceFactoryTest.php:64
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\storageDetectionDataProvider
‪array storageDetectionDataProvider()
Definition: ResourceFactoryTest.php:173
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\setUp
‪setUp()
Definition: ResourceFactoryTest.php:42
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\asRelativePath
‪asRelativePath(string $value)
Definition: ResourceFactoryTest.php:219
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\tearDown
‪tearDown()
Definition: ResourceFactoryTest.php:50
‪TYPO3\CMS\Core\Resource\LocalPath
Definition: LocalPath.php:29
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\$filesCreated
‪array $filesCreated
Definition: ResourceFactoryTest.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory\retrieveFileOrFolderObject
‪File Folder null retrieveFileOrFolderObject($input)
Definition: ResourceFactory.php:491