‪TYPO3CMS  10.4
ResourceFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Psr\EventDispatcher\EventDispatcherInterface;
27 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
28 
32 class ‪ResourceFactoryTest extends UnitTestCase
33 {
37  protected ‪$resetSingletonInstances = true;
38 
42  protected ‪$subject;
43 
47  protected ‪$filesCreated = [];
48 
52  protected function ‪setUp(): void
53  {
54  parent::setUp();
55  $this->subject = $this->getAccessibleMock(ResourceFactory::class, ['dummy'], [], '', false);
56  }
57 
61  protected function ‪tearDown(): void
62  {
63  foreach ($this->filesCreated as $file) {
64  unlink($file);
65  }
66  parent::tearDown();
67  }
68 
69  /**********************************
70  * Storage Collections
71  **********************************/
76  {
77  $mockedMount = $this->createMock(ResourceStorage::class);
78  $path = ‪StringUtility::getUniqueId('path_');
79  $name = ‪StringUtility::getUniqueId('name_');
80  $storageCollection = $this->subject->createFolderObject($mockedMount, $path, $name, 0);
81  self::assertSame($mockedMount, $storageCollection->getStorage());
82  self::assertEquals($path, $storageCollection->getIdentifier());
83  self::assertEquals($name, $storageCollection->getName());
84  }
85 
86  /**********************************
87  * Drivers
88  **********************************/
93  {
94  $mockedDriver = $this->getMockForAbstractClass(AbstractDriver::class);
95  $driverFixtureClass = get_class($mockedDriver);
96  GeneralUtility::addInstance($driverFixtureClass, $mockedDriver);
97  $mockedRegistry = $this->createMock(DriverRegistry::class);
98  $mockedRegistry->expects(self::once())->method('getDriverClass')->with(self::equalTo($driverFixtureClass))->willReturn($driverFixtureClass);
99  GeneralUtility::setSingletonInstance(DriverRegistry::class, $mockedRegistry);
100  $obj = $this->subject->getDriverObject($driverFixtureClass, []);
101  self::assertInstanceOf(AbstractDriver::class, $obj);
102  }
103 
104  /***********************************
105  * File Handling
106  ***********************************/
107 
112  {
114  ‪$subject = $this->getAccessibleMock(
115  ResourceFactory::class,
116  ['getFolderObjectFromCombinedIdentifier'],
117  [],
118  '',
119  false
120  );
122  ->expects(self::once())
123  ->method('getFolderObjectFromCombinedIdentifier')
124  ->with('typo3');
126  }
127 
132  {
134  ‪$subject = $this->getAccessibleMock(
135  ResourceFactory::class,
136  ['getFolderObjectFromCombinedIdentifier'],
137  [],
138  '',
139  false
140  );
142  ->expects(self::once())
143  ->method('getFolderObjectFromCombinedIdentifier')
144  ->with('typo3');
146  }
147 
152  {
153  $this->subject = $this->getAccessibleMock(ResourceFactory::class, ['getFileObjectFromCombinedIdentifier'], [], '', false);
154  $filename = 'typo3temp/var/tests/4711.txt';
155  $this->subject->expects(self::once())
156  ->method('getFileObjectFromCombinedIdentifier')
157  ->with($filename);
158  // Create and prepare test file
160  $this->filesCreated[] = ‪Environment::getPublicPath() . '/' . $filename;
161  $this->subject->retrieveFileOrFolderObject($filename);
162  }
163 
164  /***********************************
165  * Storage AutoDetection
166  ***********************************/
167 
175  public function ‪findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)
176  {
177  $resourceFactory = new ‪ResourceFactory($this->prophesize(EventDispatcherInterface::class)->reveal());
178  $mock = \Closure::bind(static function (‪ResourceFactory $resourceFactory) use (&$path, $storageConfiguration) {
179  $resourceFactory->localDriverStorageCache = $storageConfiguration;
180  return $resourceFactory->‪findBestMatchingStorageByLocalPath($path);
181  }, null, ResourceFactory::class);
182  self::assertSame($expectedStorageId, $mock($resourceFactory));
183  }
184 
188  public function ‪storageDetectionDataProvider()
189  {
190  return [
191  'NoLocalStoragesReturnDefaultStorage' => [
192  [],
193  'my/dummy/Image.png',
194  0
195  ],
196  'NoMatchReturnsDefaultStorage' => [
197  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'fileadmin2/public/']),
198  'my/dummy/Image.png',
199  0
200  ],
201  'MatchReturnsTheMatch' => [
202  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'other/public/']),
203  'fileadmin/dummy/Image.png',
204  1
205  ],
206  'TwoFoldersWithSameStartReturnsCorrect' => [
207  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'fileadmin/public/']),
208  'fileadmin/dummy/Image.png',
209  1
210  ],
211  'NestedStorageReallyReturnsTheBestMatching' => [
212  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'fileadmin/public/']),
213  'fileadmin/public/Image.png',
214  2
215  ],
216  'CommonPrefixButWrongPath' => [
217  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'uploads/test/']),
218  'uploads/bogus/dummy.png',
219  0
220  ],
221  'CommonPrefixRightPath' => [
222  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'uploads/test/']),
223  'uploads/test/dummy.png',
224  2
225  ],
226  'FindStorageFromWindowsPath' => [
227  array_map([$this, 'asRelativePath'], [1 => 'fileadmin/', 2 => 'uploads/test/']),
228  'uploads\\test\\dummy.png',
229  2
230  ],
231  ];
232  }
233 
234  private function ‪asRelativePath(string $value): ‪LocalPath
235  {
236  return new ‪LocalPath($value, ‪LocalPath::TYPE_RELATIVE);
237  }
238 }
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\getDriverObjectAcceptsDriverClassName
‪getDriverObjectAcceptsDriverClassName()
Definition: ResourceFactoryTest.php:89
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\$subject
‪ResourceFactory $subject
Definition: ResourceFactoryTest.php:40
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound
‪findBestMatchingStorageByLocalPathReturnsDefaultStorageIfNoMatchIsFound(array $storageConfiguration, $path, $expectedStorageId)
Definition: ResourceFactoryTest.php:172
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\retrieveFileOrFolderObjectReturnsFileIfPathIsGiven
‪retrieveFileOrFolderObjectReturnsFileIfPathIsGiven()
Definition: ResourceFactoryTest.php:148
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Core\Resource\LocalPath\TYPE_RELATIVE
‪const TYPE_RELATIVE
Definition: LocalPath.php:31
‪TYPO3\CMS\Core\Tests\Unit\Resource
Definition: AbstractFileTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: ResourceFactoryTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithRelativePath
‪retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithRelativePath()
Definition: ResourceFactoryTest.php:108
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithAbsolutePath
‪retrieveFileOrFolderObjectCallsGetFolderObjectFromCombinedIdentifierWithAbsolutePath()
Definition: ResourceFactoryTest.php:128
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest
Definition: ResourceFactoryTest.php:33
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\createStorageCollectionObjectCreatesCollectionWithCorrectArguments
‪createStorageCollectionObjectCreatesCollectionWithCorrectArguments()
Definition: ResourceFactoryTest.php:72
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\storageDetectionDataProvider
‪array storageDetectionDataProvider()
Definition: ResourceFactoryTest.php:185
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFileToTypo3tempDir
‪static string writeFileToTypo3tempDir($filepath, $content)
Definition: GeneralUtility.php:1928
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\setUp
‪setUp()
Definition: ResourceFactoryTest.php:49
‪TYPO3\CMS\Core\Resource\Driver\DriverRegistry
Definition: DriverRegistry.php:24
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\asRelativePath
‪asRelativePath(string $value)
Definition: ResourceFactoryTest.php:231
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\tearDown
‪tearDown()
Definition: ResourceFactoryTest.php:58
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪TYPO3\CMS\Core\Resource\LocalPath
Definition: LocalPath.php:29
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Tests\Unit\Resource\ResourceFactoryTest\$filesCreated
‪array $filesCreated
Definition: ResourceFactoryTest.php:44
‪TYPO3\CMS\Core\Resource\ResourceFactory\findBestMatchingStorageByLocalPath
‪int findBestMatchingStorageByLocalPath(&$localPath)
Definition: ResourceFactory.php:200
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Resource\ResourceFactory\retrieveFileOrFolderObject
‪File Folder null retrieveFileOrFolderObject($input)
Definition: ResourceFactory.php:482
‪TYPO3\CMS\Core\Resource\Driver\AbstractDriver
Definition: AbstractDriver.php:25