TYPO3 CMS  TYPO3_8-7
LegacyLinkNotationConverterTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
24 
25 class LegacyLinkNotationConverterTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
26 {
33  {
34  return [
35  'simple page - old style' => [
36  // original input value
37  '13',
38  // split values
39  [
40  'type' => LinkService::TYPE_PAGE,
41  'pageuid' => 13
42  ],
43  // final unified URN
44  't3://page?uid=13'
45  ],
46  'page with type - old style' => [
47  '13,31',
48  [
49  'type' => LinkService::TYPE_PAGE,
50  'pageuid' => 13,
51  'pagetype' => 31
52  ],
53  't3://page?uid=13&type=31'
54  ],
55  'page with type and fragment - old style' => [
56  '13,31#uncool',
57  [
58  'type' => LinkService::TYPE_PAGE,
59  'pageuid' => '13',
60  'pagetype' => '31',
61  'fragment' => 'uncool'
62  ],
63  't3://page?uid=13&type=31#uncool'
64  ],
65  'page with type and parameters and fragment - old style' => [
66  '13,31?unbel=ievable#uncool',
67  [
68  'type' => LinkService::TYPE_PAGE,
69  'pageuid' => '13',
70  'pagetype' => '31',
71  'parameters' => 'unbel=ievable',
72  'fragment' => 'uncool'
73  ],
74  't3://page?uid=13&type=31&unbel=ievable#uncool'
75  ],
76  'page with type and parameters as another parameter and fragment - old style' => [
77  '13,31,&unbel=ievable&but=possibly#uncool',
78  [
79  'type' => LinkService::TYPE_PAGE,
80  'pageuid' => '13',
81  'pagetype' => '31',
82  'parameters' => 'unbel=ievable&but=possibly',
83  'fragment' => 'uncool'
84  ],
85  't3://page?uid=13&type=31&unbel=ievable&but=possibly#uncool'
86  ],
87  'page with type and parameters as third parameter and explicitly allow type=0' => [
88  '1,0,&param=2',
89  [
90  'type' => LinkService::TYPE_PAGE,
91  'pageuid' => '1',
92  'pagetype' => '0',
93  'parameters' => 'param=2',
94  ],
95  't3://page?uid=1&type=0&param=2'
96  ],
97  'page with alias - old style' => [
98  'alias13',
99  [
100  'type' => LinkService::TYPE_PAGE,
101  'pagealias' => 'alias13'
102  ],
103  't3://page?alias=alias13'
104  ],
105  'record of table - old 2-part identifier' => [
106  'record:tx_myext_entity:456',
107  [
108  'type' => LinkService::TYPE_RECORD,
109  'identifier' => 'tx_myext_entity',
110  'table' => 'tx_myext_entity',
111  'uid' => 456,
112  'url' => 'record:tx_myext_entity:456',
113  'value' => 'tx_myext_entity:456'
114  ],
115  't3://record?identifier=tx_myext_entity&uid=456'
116  ],
117  'record of table - old 3-part identifier' => [
118  'record:usage1:tx_myext_entity:456',
119  [
120  'type' => LinkService::TYPE_RECORD,
121  'identifier' => 'usage1',
122  'table' => 'tx_myext_entity',
123  'uid' => 456,
124  'url' => 'record:usage1:tx_myext_entity:456',
125  'value' => 'usage1:tx_myext_entity:456'
126  ],
127  't3://record?identifier=usage1&uid=456'
128  ],
129  ];
130  }
131 
140  public function resolveReturnsSplitParameters($input, $expected)
141  {
142  $subject = new LegacyLinkNotationConverter();
143  $this->assertEquals($expected, $subject->resolve($input));
144  }
145 
156  public function splitParametersToUnifiedIdentifier($input, $parameters, $expected)
157  {
158  $subject = new LinkService();
159  $this->assertEquals($expected, $subject->asString($parameters));
160  }
161 
176  {
177  return [
178  'file without FAL - VERY old style' => [
179  'fileadmin/on/steroids.png',
180  [
181  'type' => LinkService::TYPE_FILE,
182  'file' => 'fileadmin/on/steroids.png'
183  ],
184  't3://file?identifier=fileadmin%2Fon%2Fsteroids.png'
185  ],
186  'file without FAL with file prefix - VERY old style' => [
187  'file:fileadmin/on/steroids.png',
188  [
189  'type' => LinkService::TYPE_FILE,
190  'file' => 'fileadmin/on/steroids.png'
191  ],
192  't3://file?identifier=fileadmin%2Fon%2Fsteroids.png'
193  ],
194  'file with FAL uid - old style' => [
195  'file:23',
196  [
197  'type' => LinkService::TYPE_FILE,
198  'file' => 23
199  ],
200  't3://file?uid=23'
201  ],
202  'folder without FAL - VERY old style' => [
203  'fileadmin/myimages/',
204  [
205  'type' => LinkService::TYPE_FOLDER,
206  'folder' => 'fileadmin/myimages/'
207  ],
208  't3://folder?storage=0&identifier=%2Ffileadmin%2Fmyimages%2F'
209  ],
210  'folder with combined identifier and file prefix (FAL) - old style' => [
211  'file:2:/myimages/',
212  [
213  'type' => LinkService::TYPE_FOLDER,
214  'folder' => '2:/myimages/'
215  ],
216  't3://folder?storage=2&identifier=%2Fmyimages%2F'
217  ],
218  ];
219  }
220 
231  public function resolveFileReferencesToSplitParameters($input, $expected)
232  {
234  $storage = $this->getMockBuilder(ResourceStorage::class)
235  ->disableOriginalConstructor()
236  ->getMock();
237 
238  $factory = $this->getMockBuilder(ResourceFactory::class)
239  ->disableOriginalConstructor()
240  ->getMock();
241 
242  // fake methods to return proper objects
243  if ($expected['type'] === LinkService::TYPE_FILE) {
244  $fileObject = new File(['identifier' => $expected['file']], $storage);
245  $factory->expects($this->any())->method('getFileObjectFromCombinedIdentifier')->with($expected['file'])
246  ->willReturn($fileObject);
247  $factory->expects($this->any())->method('retrieveFileOrFolderObject')->with($expected['file'])
248  ->willReturn($fileObject);
249  $factory->expects($this->any())->method('getFileObject')->with($expected['file'])->willReturn($fileObject);
250  $expected['file'] = $fileObject;
251  }
252  // fake methods to return proper objects
253  if ($expected['type'] === LinkService::TYPE_FOLDER) {
254  if (substr($expected['folder'], 0, 5) === 'file:') {
255  $expected['folder'] = substr($expected['folder'], 5);
256  }
257  $folderObject = new Folder($storage, $expected['folder'], $expected['folder']);
258  $factory->expects($this->any())->method('retrieveFileOrFolderObject')->with($expected['folder'])
259  ->willReturn($folderObject);
260  $factory->expects($this->any())->method('getFolderObjectFromCombinedIdentifier')->with($expected['folder'])
261  ->willReturn($folderObject);
262  $expected['folder'] = $folderObject;
263  }
264 
266  $subject = $this->getAccessibleMock(LegacyLinkNotationConverter::class, ['dummy']);
267  $subject->_set('resourceFactory', $factory);
268  $this->assertEquals($expected, $subject->resolve($input));
269  }
270 
283  public function splitParametersToUnifiedIdentifierForFiles($input, $parameters, $expected)
284  {
285  // fake methods to return proper objects
286  if ($parameters['type'] === LinkService::TYPE_FILE) {
287  $fileObject = $this->getMockBuilder(File::class)
288  ->setMethods(['getUid', 'getIdentifier'])
289  ->disableOriginalConstructor()
290  ->getMock();
291  $uid = 0;
292  if (MathUtility::canBeInterpretedAsInteger($parameters['file'])) {
293  $uid = $parameters['file'];
294  }
295  $fileObject->expects($this->once())->method('getUid')->willReturn($uid);
296  $fileObject->expects($this->any())->method('getIdentifier')->willReturn($parameters['file']);
297  $parameters['file'] = $fileObject;
298  }
299  // fake methods to return proper objects
300  if ($parameters['type'] === LinkService::TYPE_FOLDER) {
301  if (substr($parameters['folder'], 0, 5) === 'file:') {
302  $parameters['folder'] = substr($parameters['folder'], 5);
303  }
304  // fake "0" storage
305  if (!MathUtility::canBeInterpretedAsInteger($parameters['folder'][0])) {
306  $parameters['folder'] = '0:' . $parameters['folder'];
307  }
308  $folderObject = $this->getMockBuilder(Folder::class)
309  ->setMethods(['getCombinedIdentifier', 'getStorage', 'getIdentifier'])
310  ->disableOriginalConstructor()
311  ->getMock();
312  $folderObject->expects($this->any())->method('getCombinedIdentifier')->willReturn($parameters['folder']);
313  $folderData = explode(':', $parameters['folder']);
315  $storage = $this->getMockBuilder(ResourceStorage::class)
316  ->setMethods(['getUid'])
317  ->disableOriginalConstructor()
318  ->getMock();
319  $storage->method('getUid')->willReturn($folderData[0]);
320  $folderObject->expects($this->any())->method('getStorage')->willReturn($storage);
321  $folderObject->expects($this->any())->method('getIdentifier')->willReturn($folderData[1]);
322  $parameters['folder'] = $folderObject;
323  }
324 
325  $subject = new LinkService();
326  $this->assertEquals($expected, $subject->asString($parameters));
327  }
328 
333  {
334  return [
335  'URL encoded local' => [
336  'phar%3a//some-file.jpg',
337  ],
338  'URL encoded absolute' => [
339  'phar%3a///path/some-file.jpg',
340  ],
341  'not URL encoded local' => [
342  'phar://some-file.jpg',
343  ],
344  'not URL encoded absolute' => [
345  'phar:///path/some-file.jpg',
346  ],
347  ];
348  }
349 
357  public function resolveThrowExceptionWithPharReferences(string $pharUrl)
358  {
359  $this->expectException(\RuntimeException::class);
360  $this->expectExceptionCode(1530030673);
361  (new LegacyLinkNotationConverter())->resolve($pharUrl);
362  }
363 }