‪TYPO3CMS  11.5
StorageRepositoryTest.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 
25 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
26 
27 class ‪StorageRepositoryTest extends FunctionalTestCase
28 {
32  private ‪$subject;
33 
34  protected function ‪setUp(): void
35  {
36  parent::setUp();
37  $this->setUpBackendUserFromFixture(1);
38  $this->subject = GeneralUtility::makeInstance(StorageRepository::class);
39  }
40 
41  protected function ‪tearDown(): void
42  {
43  unset($this->storageRepository, $this->subject);
44  parent::tearDown();
45  }
46 
47  public function ‪bestStorageIsResolvedDataProvider(): array
48  {
49  // `{public}` will be replaced by public project path (not having trailing slash)
50  // double slashes `//` are used on purpose for given file identifiers
51  return $this->mapToDataSet([
52  // legacy storage
53  '/favicon.ico' => '0:/favicon.ico',
54  'favicon.ico' => '0:/favicon.ico',
55 
56  '{public}//favicon.ico' => '0:/favicon.ico',
57  '{public}/favicon.ico' => '0:/favicon.ico',
58 
59  // using storages with relative path
60  '/fileadmin/img.png' => '1:/img.png',
61  'fileadmin/img.png' => '1:/img.png',
62  '/fileadmin/images/img.png' => '1:/images/img.png',
63  'fileadmin/images/img.png' => '1:/images/img.png',
64  '/documents/doc.pdf' => '2:/doc.pdf',
65  'documents/doc.pdf' => '2:/doc.pdf',
66  '/fileadmin/nested/images/img.png' => '3:/images/img.png',
67  'fileadmin/nested/images/img.png' => '3:/images/img.png',
68 
69  '{public}//fileadmin/img.png' => '1:/img.png',
70  '{public}/fileadmin/img.png' => '1:/img.png',
71  '{public}//fileadmin/images/img.png' => '1:/images/img.png',
72  '{public}/fileadmin/images/img.png' => '1:/images/img.png',
73  '{public}//documents/doc.pdf' => '2:/doc.pdf',
74  '{public}/documents/doc.pdf' => '2:/doc.pdf',
75  '{public}//fileadmin/nested/images/img.png' => '3:/images/img.png',
76  '{public}/fileadmin/nested/images/img.png' => '3:/images/img.png',
77 
78  // using storages with absolute path
79  '/files/img.png' => '4:/img.png',
80  'files/img.png' => '4:/img.png',
81  '/files/images/img.png' => '4:/images/img.png',
82  'files/images/img.png' => '4:/images/img.png',
83  '/docs/doc.pdf' => '5:/doc.pdf',
84  'docs/doc.pdf' => '5:/doc.pdf',
85  '/files/nested/images/img.png' => '6:/images/img.png',
86  'files/nested/images/img.png' => '6:/images/img.png',
87 
88  '{public}//files/img.png' => '4:/img.png',
89  '{public}/files/img.png' => '4:/img.png',
90  '{public}//files/images/img.png' => '4:/images/img.png',
91  '{public}/files/images/img.png' => '4:/images/img.png',
92  '{public}//docs/doc.pdf' => '5:/doc.pdf',
93  '{public}/docs/doc.pdf' => '5:/doc.pdf',
94  '{public}//files/nested/images/img.png' => '6:/images/img.png',
95  '{public}/files/nested/images/img.png' => '6:/images/img.png',
96  ]);
97  }
98 
105  public function ‪bestStorageIsResolved(string $sourceIdentifier, string $expectedCombinedIdentifier): void
106  {
107  $this->‪createLocalStorages();
108  $sourceIdentifier = str_replace('{public}', ‪Environment::getPublicPath(), $sourceIdentifier);
109  $storage = $this->subject->getStorageObject(0, [], $sourceIdentifier);
110  $combinedIdentifier = sprintf('%d:%s', $storage->getUid(), $sourceIdentifier);
111  self::assertSame(
112  $expectedCombinedIdentifier,
113  $combinedIdentifier,
114  sprintf('Given identifier "%s"', $sourceIdentifier)
115  );
116  }
117 
118  private function ‪createLocalStorages(): void
119  {
120  $publicPath = ‪Environment::getPublicPath();
121  $prefixDelegate = static function (string $value) use ($publicPath): string {
122  return $publicPath . '/' . $value;
123  };
124  // array indexes are not relevant here, but are those expected to be used as storage UID (`1:/file.png`)
125  // @todo it is possible to create ambiguous storages, e.g. `fileadmin/` AND `/fileadmin/`
126  $relativeNames = [1 => 'fileadmin/', 2 => 'documents/', 3 => 'fileadmin/nested/'];
127  // @todo: All these directories must exist. This is because createLocalStorage() calls testCaseSensitivity()
128  // which creates a file in each directory without checking if the directory does exist. Arguably, this
129  // should be handled in testCaseSensitivity(). For now, we create the directories in question and
130  // suppress errors so only the first test creates them and subsequent tests don't emit a warning here.
131  @mkdir($this->instancePath . '/documents');
132  @mkdir($this->instancePath . '/fileadmin/nested');
133  $absoluteNames = array_map($prefixDelegate, [4 => 'files/', 5 => 'docs/', 6 => 'files/nested']);
134  @mkdir($this->instancePath . '/files');
135  @mkdir($this->instancePath . '/docs');
136  @mkdir($this->instancePath . '/files/nested');
137  foreach ($relativeNames as $relativeName) {
138  $this->subject->createLocalStorage('rel:' . $relativeName, $relativeName, 'relative');
139  }
140  foreach ($absoluteNames as $absoluteName) {
141  $this->subject->createLocalStorage('abs:' . $absoluteName, $absoluteName, 'absolute');
142  }
143  }
144 
149  private function mapToDataSet(array $map): array
150  {
151  array_walk($map, static function (&$item, string $key) {
152  $item = [$key, $item];
153  });
154  return $map;
155  }
156 
160  public function copyFileCopiesMetadata(): void
161  {
162  $this->‪importCSVDataSet(__DIR__ . '/../Fixtures/sys_file_storage.csv');
163  $this->setUpBackendUser(1);
164  mkdir(‪Environment::getPublicPath() . '/fileadmin/foo');
165  file_put_contents(‪Environment::getPublicPath() . '/fileadmin/foo/bar.txt', 'Temp file');
166  ‪$subject = GeneralUtility::makeInstance(StorageRepository::class)->‪findByUid(1);
168  'title' => 'Temp file title',
169  'description' => 'Temp file description',
170  ];
172  ‪$fileToCopy = GeneralUtility::makeInstance(ResourceFactory::class)->getFileObjectFromCombinedIdentifier('1:/foo/bar.txt');
174  ‪$targetParentFolder = GeneralUtility::makeInstance(ResourceFactory::class)->getFolderObjectFromCombinedIdentifier('1:/');
177  self::assertNotEquals(‪$fileToCopy->getMetaData()->get()['file'], ‪$newFile->getMetaData()->get()['file']);
178  self::assertNotEquals(‪$fileToCopy->getMetaData()->get()['uid'], ‪$newFile->getMetaData()->get()['uid']);
179  self::assertEquals(‪$fileToCopyMetaData['title'], ‪$newFile->getMetaData()->get()['title']);
180  self::assertEquals(‪$fileToCopyMetaData['description'], ‪$newFile->getMetaData()->get()['description']);
181  }
182 }
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest\createLocalStorages
‪createLocalStorages()
Definition: StorageRepositoryTest.php:117
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest\$targetParentFolder
‪$targetParentFolder
Definition: StorageRepositoryTest.php:173
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:206
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest
Definition: StorageRepositoryTest.php:28
‪TYPO3\CMS\Core\Tests\Functional\Resource
Definition: ProcessedFileTest.php:18
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest\bestStorageIsResolved
‪bestStorageIsResolved(string $sourceIdentifier, string $expectedCombinedIdentifier)
Definition: StorageRepositoryTest.php:104
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest\$subject
‪StorageRepository $subject
Definition: StorageRepositoryTest.php:31
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest\tearDown
‪tearDown()
Definition: StorageRepositoryTest.php:40
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest\$fileToCopyMetaData
‪$fileToCopyMetaData
Definition: StorageRepositoryTest.php:166
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest\importCSVDataSet
‪array< string, mapToDataSet(array $map):array { array_walk( $map, static function(& $item, string $key) { $item=[ $key, $item];});return $map;} public function copyFileCopiesMetadata():void { $this-> importCSVDataSet(__DIR__ . '/../Fixtures/sys_file_storage.csv')
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:38
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest\setUp
‪setUp()
Definition: StorageRepositoryTest.php:33
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:43
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest\bestStorageIsResolvedDataProvider
‪bestStorageIsResolvedDataProvider()
Definition: StorageRepositoryTest.php:46
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest\$newFile
‪$newFile
Definition: StorageRepositoryTest.php:175
‪TYPO3\CMS\Core\Tests\Functional\Resource\StorageRepositoryTest\$fileToCopy
‪$fileToCopy
Definition: StorageRepositoryTest.php:171
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Resource\StorageRepository\findByUid
‪findByUid(int $uid)
Definition: StorageRepository.php:92