‪TYPO3CMS  9.5
ResourceFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25 
26 class ‪ResourceFactoryTest extends FunctionalTestCase
27 {
31  private ‪$subject;
32 
37 
38  protected function ‪setUp(): void
39  {
40  parent::setUp();
41  $this->backendUser = $this->setUpBackendUserFromFixture(1);
42  $this->subject = GeneralUtility::makeInstance(ResourceFactory::class);
43  $this->storageRepository = GeneralUtility::makeInstance(StorageRepository::class);
44  }
45 
46  protected function ‪tearDown(): void
47  {
48  unset($this->storageRepository, $this->subject);
49  parent::tearDown();
50  }
51 
52  public function ‪bestStorageIsResolvedDataProvider(): array
53  {
54  // `{public}` will be replaced by public project path (not having trailing slash)
55  // double slashes `//` are used on purpose for given file identifiers
56  return $this->mapToDataSet([
57  // legacy storage
58  '/favicon.ico' => '0:/favicon.ico',
59  'favicon.ico' => '0:/favicon.ico',
60 
61  '{public}//favicon.ico' => '0:/favicon.ico',
62  '{public}/favicon.ico' => '0:/favicon.ico',
63 
64  // using storages with relative path
65  '/fileadmin/img.png' => '1:/img.png',
66  'fileadmin/img.png' => '1:/img.png',
67  '/fileadmin/images/img.png' => '1:/images/img.png',
68  'fileadmin/images/img.png' => '1:/images/img.png',
69  '/documents/doc.pdf' => '2:/doc.pdf',
70  'documents/doc.pdf' => '2:/doc.pdf',
71  '/fileadmin/nested/images/img.png' => '3:/images/img.png',
72  'fileadmin/nested/images/img.png' => '3:/images/img.png',
73 
74  '{public}//fileadmin/img.png' => '1:/img.png',
75  '{public}/fileadmin/img.png' => '1:/img.png',
76  '{public}//fileadmin/images/img.png' => '1:/images/img.png',
77  '{public}/fileadmin/images/img.png' => '1:/images/img.png',
78  '{public}//documents/doc.pdf' => '2:/doc.pdf',
79  '{public}/documents/doc.pdf' => '2:/doc.pdf',
80  '{public}//fileadmin/nested/images/img.png' => '3:/images/img.png',
81  '{public}/fileadmin/nested/images/img.png' => '3:/images/img.png',
82 
83  // using storages with absolute path
84  '/files/img.png' => '4:/img.png',
85  'files/img.png' => '4:/img.png',
86  '/files/images/img.png' => '4:/images/img.png',
87  'files/images/img.png' => '4:/images/img.png',
88  '/docs/doc.pdf' => '5:/doc.pdf',
89  'docs/doc.pdf' => '5:/doc.pdf',
90  '/files/nested/images/img.png' => '6:/images/img.png',
91  'files/nested/images/img.png' => '6:/images/img.png',
92 
93  '{public}//files/img.png' => '4:/img.png',
94  '{public}/files/img.png' => '4:/img.png',
95  '{public}//files/images/img.png' => '4:/images/img.png',
96  '{public}/files/images/img.png' => '4:/images/img.png',
97  '{public}//docs/doc.pdf' => '5:/doc.pdf',
98  '{public}/docs/doc.pdf' => '5:/doc.pdf',
99  '{public}//files/nested/images/img.png' => '6:/images/img.png',
100  '{public}/files/nested/images/img.png' => '6:/images/img.png',
101  ]);
102  }
103 
110  public function ‪bestStorageIsResolved(string $sourceIdentifier, string $expectedCombinedIdentifier): void
111  {
112  $this->‪createLocalStorages();
113  $sourceIdentifier = str_replace('{public}', ‪Environment::getPublicPath(), $sourceIdentifier);
114  $storage = $this->subject->getStorageObject(0, [], $sourceIdentifier);
115  $combinedIdentifier = sprintf('%d:%s', $storage->getUid(), $sourceIdentifier);
116  self::assertSame(
117  $expectedCombinedIdentifier,
118  $combinedIdentifier,
119  sprintf('Given identifier "%s"', $sourceIdentifier)
120  );
121  }
122 
123  private function ‪createLocalStorages(): void
124  {
125  $publicPath = ‪Environment::getPublicPath();
126  $prefixDelegate = function (string $value) use ($publicPath): string {
127  return $publicPath . '/' . $value;
128  };
129  // array indexes are not relevant here, but are those expected to be used as storage UID (`1:/file.png`)
130  // @todo it is possible to create ambiguous storages, e.g. `fileadmin/` AND `/fileadmin/`
131  $relativeNames = [1 => 'fileadmin/', 2 => 'documents/', 3 => 'fileadmin/nested/'];
132  $absoluteNames = array_map($prefixDelegate, [4 => 'files/', 5 => 'docs/', 6 => 'files/nested']);
133  foreach ($relativeNames as $relativeName) {
134  $this->storageRepository->createLocalStorage('rel:' . $relativeName, $relativeName, 'relative');
135  }
136  foreach ($absoluteNames as $absoluteName) {
137  $this->storageRepository->createLocalStorage('abs:' . $absoluteName, $absoluteName, 'absolute');
138  }
139  // path is outside public project path - which is expected to cause problems (that's why it's tested)
140  $outsideName = dirname($publicPath) . '/outside/';
141  $this->storageRepository->createLocalStorage('abs:' . $outsideName, $outsideName, 'absolute');
142  }
143 
148  private function mapToDataSet(array $map): array
149  {
150  array_walk($map, function (&$item, string $key) {
151  $item = [$key, $item];
152  });
153  return $map;
154  }
155 }
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceFactoryTest
Definition: ResourceFactoryTest.php:27
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceFactoryTest\$storageRepository
‪StorageRepository $storageRepository
Definition: ResourceFactoryTest.php:34
‪TYPO3\CMS\Core\Tests\Functional\Resource
Definition: ResourceFactoryTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceFactoryTest\setUp
‪setUp()
Definition: ResourceFactoryTest.php:36
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceFactoryTest\createLocalStorages
‪createLocalStorages()
Definition: ResourceFactoryTest.php:121
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:33
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:29
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceFactoryTest\bestStorageIsResolved
‪bestStorageIsResolved(string $sourceIdentifier, string $expectedCombinedIdentifier)
Definition: ResourceFactoryTest.php:108
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceFactoryTest\$subject
‪ResourceFactory $subject
Definition: ResourceFactoryTest.php:30
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceFactoryTest\bestStorageIsResolvedDataProvider
‪bestStorageIsResolvedDataProvider()
Definition: ResourceFactoryTest.php:50
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Tests\Functional\Resource\ResourceFactoryTest\tearDown
‪tearDown()
Definition: ResourceFactoryTest.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45