‪TYPO3CMS  10.4
LegacyLinkNotationConverterTest.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 
20 use PHPUnit\Framework\MockObject\MockObject;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 class ‪LegacyLinkNotationConverterTest extends UnitTestCase
32 {
33  protected function ‪setUp(): void
34  {
35  parent::setUp();
36  $this->resetSingletonInstances = true;
37  }
38 
45  {
46  return [
47  'simple page - old style' => [
48  // original input value
49  '13',
50  // split values
51  [
52  'type' => ‪LinkService::TYPE_PAGE,
53  'pageuid' => 13
54  ],
55  // final unified URN
56  't3://page?uid=13'
57  ],
58  'page with type - old style' => [
59  '13,31',
60  [
61  'type' => ‪LinkService::TYPE_PAGE,
62  'pageuid' => 13,
63  'pagetype' => 31
64  ],
65  't3://page?uid=13&type=31'
66  ],
67  'page with type and fragment - old style' => [
68  '13,31#uncool',
69  [
70  'type' => ‪LinkService::TYPE_PAGE,
71  'pageuid' => '13',
72  'pagetype' => '31',
73  'fragment' => 'uncool'
74  ],
75  't3://page?uid=13&type=31#uncool'
76  ],
77  'page with type and parameters and fragment - old style' => [
78  '13,31?unbel=ievable#uncool',
79  [
80  'type' => ‪LinkService::TYPE_PAGE,
81  'pageuid' => '13',
82  'pagetype' => '31',
83  'parameters' => 'unbel=ievable',
84  'fragment' => 'uncool'
85  ],
86  't3://page?uid=13&type=31&unbel=ievable#uncool'
87  ],
88  'page with type and parameters as another parameter and fragment - old style' => [
89  '13,31,&unbel=ievable&but=possibly#uncool',
90  [
91  'type' => ‪LinkService::TYPE_PAGE,
92  'pageuid' => '13',
93  'pagetype' => '31',
94  'parameters' => 'unbel=ievable&but=possibly',
95  'fragment' => 'uncool'
96  ],
97  't3://page?uid=13&type=31&unbel=ievable&but=possibly#uncool'
98  ],
99  'page with type and parameters as third parameter and explicitly allow type=0' => [
100  '1,0,&param=2',
101  [
102  'type' => ‪LinkService::TYPE_PAGE,
103  'pageuid' => '1',
104  'pagetype' => '0',
105  'parameters' => 'param=2',
106  ],
107  't3://page?uid=1&type=0&param=2'
108  ],
109  'record of table - old 2-part identifier' => [
110  'record:tx_myext_entity:456',
111  [
112  'type' => ‪LinkService::TYPE_RECORD,
113  'identifier' => 'tx_myext_entity',
114  'table' => 'tx_myext_entity',
115  'uid' => 456,
116  'url' => 'record:tx_myext_entity:456',
117  'value' => 'tx_myext_entity:456'
118  ],
119  't3://record?identifier=tx_myext_entity&uid=456'
120  ],
121  'record of table - old 3-part identifier' => [
122  'record:usage1:tx_myext_entity:456',
123  [
124  'type' => ‪LinkService::TYPE_RECORD,
125  'identifier' => 'usage1',
126  'table' => 'tx_myext_entity',
127  'uid' => 456,
128  'url' => 'record:usage1:tx_myext_entity:456',
129  'value' => 'usage1:tx_myext_entity:456'
130  ],
131  't3://record?identifier=usage1&uid=456'
132  ],
133  ];
134  }
135 
144  public function ‪resolveReturnsSplitParameters($input, $expected): void
145  {
146  $subject = new ‪LegacyLinkNotationConverter();
147  self::assertEquals($expected, $subject->resolve($input));
148  }
149 
160  public function ‪splitParametersToUnifiedIdentifier(string $input, array $parameters, string $expected): void
161  {
162  $subject = new ‪LinkService();
163  self::assertEquals($expected, $subject->asString($parameters));
164  }
165 
180  {
181  return [
182  'file without FAL - VERY old style' => [
183  'fileadmin/on/steroids.png',
184  [
185  'type' => ‪LinkService::TYPE_FILE,
186  'file' => 'fileadmin/on/steroids.png'
187  ],
188  't3://file?identifier=fileadmin%2Fon%2Fsteroids.png'
189  ],
190  'file without FAL and anchor - VERY old style' => [
191  'fileadmin/on/steroids.png#page-23',
192  [
193  'type' => ‪LinkService::TYPE_FILE,
194  'file' => 'fileadmin/on/steroids.png',
195  'fragment' => 'page-23'
196  ],
197  't3://file?identifier=fileadmin%2Fon%2Fsteroids.png#page-23'
198  ],
199  'file without FAL with file prefix - VERY old style' => [
200  'file:fileadmin/on/steroids.png',
201  [
202  'type' => ‪LinkService::TYPE_FILE,
203  'file' => 'fileadmin/on/steroids.png'
204  ],
205  't3://file?identifier=fileadmin%2Fon%2Fsteroids.png'
206  ],
207  'file without FAL with file prefix and anchor - VERY old style' => [
208  'file:fileadmin/on/steroids.png#page-13',
209  [
210  'type' => ‪LinkService::TYPE_FILE,
211  'file' => 'fileadmin/on/steroids.png',
212  'fragment' => 'page-13'
213  ],
214  't3://file?identifier=fileadmin%2Fon%2Fsteroids.png#page-13'
215  ],
216  'file with FAL uid - old style' => [
217  'file:23',
218  [
219  'type' => ‪LinkService::TYPE_FILE,
220  'file' => 23
221  ],
222  't3://file?uid=23'
223  ],
224  'file with FAL uid and anchor - old style' => [
225  'file:23#page-13',
226  [
227  'type' => ‪LinkService::TYPE_FILE,
228  'file' => 23,
229  'fragment' => 'page-13',
230  ],
231  't3://file?uid=23#page-13'
232  ],
233  'folder without FAL - VERY old style' => [
234  'fileadmin/myimages/',
235  [
236  'type' => ‪LinkService::TYPE_FOLDER,
237  'folder' => 'fileadmin/myimages/'
238  ],
239  't3://folder?storage=0&identifier=%2Ffileadmin%2Fmyimages%2F'
240  ],
241  'folder with combined identifier and file prefix (FAL) - old style' => [
242  'file:2:/myimages/',
243  [
244  'type' => ‪LinkService::TYPE_FOLDER,
245  'folder' => '2:/myimages/'
246  ],
247  't3://folder?storage=2&identifier=%2Fmyimages%2F'
248  ],
249  ];
250  }
251 
262  public function ‪resolveFileReferencesToSplitParameters($input, $expected): void
263  {
265  $storage = $this->getMockBuilder(ResourceStorage::class)
266  ->disableOriginalConstructor()
267  ->getMock();
268 
269  $factory = $this->getMockBuilder(ResourceFactory::class)
270  ->disableOriginalConstructor()
271  ->getMock();
272 
273  // fake methods to return proper objects
274  if ($expected['type'] === ‪LinkService::TYPE_FILE) {
275  $fileObject = new ‪File(['identifier' => $expected['file']], $storage);
276  $factory->method('getFileObjectFromCombinedIdentifier')->with($expected['file'])
277  ->willReturn($fileObject);
278  $factory->method('retrieveFileOrFolderObject')->with($expected['file'])
279  ->willReturn($fileObject);
280  $factory->method('getFileObject')->with($expected['file'])->willReturn($fileObject);
281  $expected['file'] = $fileObject;
282  }
283  // fake methods to return proper objects
284  if ($expected['type'] === ‪LinkService::TYPE_FOLDER) {
285  if (strpos($expected['folder'], 'file:') === 0) {
286  $expected['folder'] = substr($expected['folder'], 5);
287  }
288  $folderObject = new ‪Folder($storage, $expected['folder'], $expected['folder']);
289  $factory->method('retrieveFileOrFolderObject')->with($expected['folder'])
290  ->willReturn($folderObject);
291  $factory->method('getFolderObjectFromCombinedIdentifier')->with($expected['folder'])
292  ->willReturn($folderObject);
293  $expected['folder'] = $folderObject;
294  }
295  GeneralUtility::setSingletonInstance(ResourceFactory::class, $factory);
296 
297  $subject = new ‪LegacyLinkNotationConverter();
298 
299  self::assertEquals($expected, $subject->resolve($input));
300  }
301 
314  public function ‪splitParametersToUnifiedIdentifierForFiles(string $input, array $parameters, string $expected): void
315  {
316  // fake methods to return proper objects
317  if ($parameters['type'] === ‪LinkService::TYPE_FILE) {
318  $fileObject = $this->getMockBuilder(File::class)
319  ->onlyMethods(['getUid', 'getIdentifier'])
320  ->disableOriginalConstructor()
321  ->getMock();
322  $uid = 0;
323  if (‪MathUtility::canBeInterpretedAsInteger($parameters['file'])) {
324  $uid = $parameters['file'];
325  }
326  $fileObject->expects(self::once())->method('getUid')->willReturn($uid);
327  $fileObject->expects(self::any())->method('getIdentifier')->willReturn($parameters['file']);
328  $parameters['file'] = $fileObject;
329  }
330  // fake methods to return proper objects
331  if ($parameters['type'] === ‪LinkService::TYPE_FOLDER) {
332  if (strpos($parameters['folder'], 'file:') === 0) {
333  $parameters['folder'] = substr($parameters['folder'], 5);
334  }
335  // fake "0" storage
336  if (!‪MathUtility::canBeInterpretedAsInteger($parameters['folder'][0])) {
337  $parameters['folder'] = '0:' . $parameters['folder'];
338  }
339  $folderObject = $this->getMockBuilder(Folder::class)
340  ->onlyMethods(['getCombinedIdentifier', 'getStorage', 'getIdentifier'])
341  ->disableOriginalConstructor()
342  ->getMock();
343  $folderObject->expects(self::any())->method('getCombinedIdentifier')->willReturn($parameters['folder']);
344  $folderData = explode(':', $parameters['folder']);
346  $storage = $this->getMockBuilder(ResourceStorage::class)
347  ->onlyMethods(['getUid'])
348  ->disableOriginalConstructor()
349  ->getMock();
350  $storage->method('getUid')->willReturn($folderData[0]);
351  $folderObject->method('getStorage')->willReturn($storage);
352  $folderObject->method('getIdentifier')->willReturn($folderData[1]);
353  $parameters['folder'] = $folderObject;
354  }
355 
356  $subject = new ‪LinkService();
357  self::assertEquals($expected, $subject->asString($parameters));
358  }
359 
364  {
365  return [
366  'URL encoded local' => [
367  'phar%3a//some-file.jpg',
368  ],
369  'URL encoded absolute' => [
370  'phar%3a///path/some-file.jpg',
371  ],
372  'not URL encoded local' => [
373  'phar://some-file.jpg',
374  ],
375  'not URL encoded absolute' => [
376  'phar:///path/some-file.jpg',
377  ],
378  ];
379  }
380 
388  public function ‪resolveThrowExceptionWithPharReferences(string $pharUrl): void
389  {
390  $this->expectException(\RuntimeException::class);
391  $this->expectExceptionCode(1530030673);
392  (new ‪LegacyLinkNotationConverter())->resolve($pharUrl);
393  }
394 }
‪TYPO3\CMS\Core\Utility\MathUtility\canBeInterpretedAsInteger
‪static bool canBeInterpretedAsInteger($var)
Definition: MathUtility.php:74
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Core\Resource\ResourceFactory
Definition: ResourceFactory.php:41
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Core\Utility\MathUtility
Definition: MathUtility.php:22
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46