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