‪TYPO3CMS  11.5
TypoLinkSoftReferenceParserTest.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 
26 {
28  {
29  return [
30  'link to page' => [
31  [
32  'content' => 't3://page?uid=42',
33  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
34  'matchString' => 't3://page?uid=42',
35  ],
36  [
37  'subst' => [
38  'type' => 'db',
39  'recordRef' => 'pages:42',
40  'tokenValue' => 42,
41  ],
42  ],
43  ],
44  'link to page with just a number' => [
45  [
46  'content' => '42',
47  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
48  'matchString' => '42',
49  ],
50  [
51  'subst' => [
52  'type' => 'db',
53  'recordRef' => 'pages:42',
54  'tokenValue' => '42',
55  ],
56  ],
57  ],
58  'link to page with just a number and type comma-separated' => [
59  [
60  'content' => '42,100',
61  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
62  'matchString' => '42,100',
63  ],
64  [
65  'subst' => [
66  'type' => 'db',
67  'recordRef' => 'pages:42',
68  'tokenValue' => '42',
69  ],
70  ],
71  ],
72  'link to external URL without scheme' => [
73  [
74  'content' => 'www.example.com',
75  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
76  'matchString' => 'www.example.com',
77  ],
78  [
79  'subst' => [
80  'type' => 'external',
81  'tokenValue' => 'http://www.example.com',
82  ],
83  ],
84  ],
85  'link to external URL with scheme' => [
86  [
87  'content' => 'https://www.example.com',
88  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
89  'matchString' => 'https://www.example.com',
90  ],
91  [
92  'subst' => [
93  'type' => 'external',
94  'tokenValue' => 'https://www.example.com',
95  ],
96  ],
97  ],
98  'link to email' => [
99  [
100  'content' => 'mailto:test@example.com',
101  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
102  'matchString' => 'mailto:test@example.com',
103  ],
104  [
105  'subst' => [
106  'type' => 'string',
107  'tokenValue' => 'test@example.com',
108  ],
109  ],
110  ],
111  'link to email without schema' => [
112  [
113  'content' => 'test@example.com',
114  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
115  'matchString' => 'test@example.com',
116  ],
117  [
118  'subst' => [
119  'type' => 'string',
120  'tokenValue' => 'test@example.com',
121  ],
122  ],
123  ],
124  'link to phone number' => [
125  [
126  'content' => 'tel:0123456789',
127  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
128  'matchString' => 'tel:0123456789',
129  ],
130  [
131  'subst' => [
132  'type' => 'string',
133  'tokenValue' => '0123456789',
134  ],
135  ],
136  ],
137  ];
138  }
139 
146  public function ‪findRefReturnsParsedElements(array $softrefConfiguration, array $expectedElement): void
147  {
148  $subject = $this->‪getParserByKey('typolink');
149  $subject->setParserKey('typolink', $softrefConfiguration);
150  $result = $subject->parse(
151  'tt_content',
152  'bodytext',
153  1,
154  $softrefConfiguration['content']
155  )->toNullableArray();
156 
157  self::assertArrayHasKey('elements', $result);
158  self::assertArrayHasKey($softrefConfiguration['elementKey'], $result['elements']);
159 
160  // Remove tokenID as this one depends on the softrefKey and doesn't need to be verified
161  unset($result['elements'][$softrefConfiguration['elementKey']]['subst']['tokenID']);
162 
163  $expectedElement['matchString'] = $softrefConfiguration['matchString'];
164  self::assertEquals($expectedElement, $result['elements'][$softrefConfiguration['elementKey']]);
165  }
166 
168  {
169  return [
170  'link to file' => [
171  [
172  'content' => 't3://file?uid=42',
173  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
174  'matchString' => 't3://file?uid=42',
175  ],
176  [
177  'subst' => [
178  'type' => 'db',
179  'recordRef' => 'sys_file:42',
180  'tokenValue' => 'file:42',
181  ],
182  ],
183  ],
184  'file with t3 mixed syntax' => [
185  [
186  'content' => 't3://file?identifier=42',
187  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
188  'matchString' => 't3://file?identifier=42',
189  ],
190  [
191  'subst' => [
192  'type' => 'db',
193  'recordRef' => 'sys_file:42',
194  'tokenValue' => 'file:42',
195  ],
196  ],
197  ],
198  'link to file with old FAL object syntax' => [
199  [
200  'content' => 'file:42',
201  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
202  'matchString' => 'file:42',
203  ],
204  [
205  'subst' => [
206  'type' => 'db',
207  'recordRef' => 'sys_file:42',
208  'tokenValue' => 'file:42',
209  ],
210  ],
211  ],
212  'link to file with simple file path' => [
213  [
214  'content' => 'fileadmin/download.jpg',
215  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
216  'matchString' => 'fileadmin/download.jpg',
217  ],
218  [
219  'subst' => [
220  'type' => 'db',
221  'recordRef' => 'sys_file:42',
222  'tokenValue' => 'file:42',
223  ],
224  ],
225  ],
226  ];
227  }
228 
235  public function ‪findRefReturnsParsedElementsWithFile(array $softrefConfiguration, array $expectedElement): void
236  {
237  $fileObject = $this->prophesize(File::class);
238  $fileObject->getUid()->willReturn(42)->shouldBeCalledTimes(1);
239  $fileObject->getName()->willReturn('download.jpg');
240  $fileObject->getIdentifier()->willReturn('fileadmin/download.jpg');
241 
242  $resourceFactory = $this->prophesize(ResourceFactory::class);
243  $resourceFactory->getFileObject('42')->willReturn($fileObject->reveal());
244  // For `t3://file?identifier=42` handling
245  $resourceFactory->getFileObjectFromCombinedIdentifier('42')->willReturn($fileObject->reveal());
246  // For `file:23` handling
247  $resourceFactory->retrieveFileOrFolderObject('42')->willReturn($fileObject->reveal());
248  // For `fileadmin/download.jpg` handling
249  $resourceFactory->retrieveFileOrFolderObject('fileadmin/download.jpg')->willReturn($fileObject->reveal());
250 
251  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory->reveal());
252 
253  $subject = $this->‪getParserByKey('typolink');
254  $subject->setParserKey('typolink', $softrefConfiguration);
255  $result = $subject->parse(
256  'tt_content',
257  'bodytext',
258  1,
259  $softrefConfiguration['content'],
260  )->toNullableArray();
261 
262  self::assertArrayHasKey('elements', $result);
263  self::assertArrayHasKey($softrefConfiguration['elementKey'], $result['elements']);
264 
265  // Remove tokenID as this one depends on the softrefKey and doesn't need to be verified
266  unset($result['elements'][$softrefConfiguration['elementKey']]['subst']['tokenID']);
267 
268  $expectedElement['matchString'] = $softrefConfiguration['matchString'];
269  self::assertEquals($expectedElement, $result['elements'][$softrefConfiguration['elementKey']]);
270  }
271 
273  {
274  return [
275  'link to folder' => [
276  [
277  'content' => 't3://folder?storage=1&amp;identifier=%2Ffoo%2Fbar%2Fbaz',
278  ],
279  ],
280  ];
281  }
282 
288  public function ‪findRefReturnsNullWithFolder(array $softrefConfiguration): void
289  {
290  $folderObject = $this->prophesize(Folder::class);
291 
292  $resourceFactory = $this->prophesize(ResourceFactory::class);
293  $resourceFactory->getFolderObjectFromCombinedIdentifier('1:/foo/bar/baz')->willReturn($folderObject->reveal())->shouldBeCalledTimes(1);
294  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory->reveal());
295 
296  $result = $this->‪getParserByKey('typolink')->parse(
297  'tt_content',
298  'bodytext',
299  1,
300  $softrefConfiguration['content']
301  )->toNullableArray();
302 
303  self::assertNull($result);
304  }
305 
307  {
308  return [
309  'URL encoded local' => [
310  'phar%3a//some-file.jpg',
311  ],
312  'URL encoded absolute' => [
313  'phar%3a///path/some-file.jpg',
314  ],
315  'not URL encoded local' => [
316  'phar://some-file.jpg',
317  ],
318  'not URL encoded absolute' => [
319  'phar:///path/some-file.jpg',
320  ],
321  ];
322  }
323 
328  public function ‪getTypoLinkPartsThrowExceptionWithPharReferences(string $pharUrl): void
329  {
330  $this->expectException(\RuntimeException::class);
331  $this->expectExceptionCode(1530030672);
332  $this->‪getParserByKey('typolink')->parse(
333  'tt_content',
334  'bodytext',
335  1,
336  $pharUrl
337  );
338  }
339 }
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkSoftReferenceParserTest\findRefReturnsParsedElementsWithFileDataProvider
‪findRefReturnsParsedElementsWithFileDataProvider()
Definition: TypoLinkSoftReferenceParserTest.php:167
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkSoftReferenceParserTest\findRefReturnsParsedElements
‪findRefReturnsParsedElements(array $softrefConfiguration, array $expectedElement)
Definition: TypoLinkSoftReferenceParserTest.php:146
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkSoftReferenceParserTest
Definition: TypoLinkSoftReferenceParserTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkSoftReferenceParserTest\findRefReturnsNullWithFolder
‪findRefReturnsNullWithFolder(array $softrefConfiguration)
Definition: TypoLinkSoftReferenceParserTest.php:288
‪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\Tests\Unit\DataHandling\SoftReference\TypoLinkSoftReferenceParserTest\findRefReturnsParsedElementsWithFile
‪findRefReturnsParsedElementsWithFile(array $softrefConfiguration, array $expectedElement)
Definition: TypoLinkSoftReferenceParserTest.php:235
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkSoftReferenceParserTest\findRefReturnsNullWithFolderDataProvider
‪findRefReturnsNullWithFolderDataProvider()
Definition: TypoLinkSoftReferenceParserTest.php:272
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference
Definition: AbstractSoftReferenceParserTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkSoftReferenceParserTest\findRefReturnsParsedElementsDataProvider
‪findRefReturnsParsedElementsDataProvider()
Definition: TypoLinkSoftReferenceParserTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkSoftReferenceParserTest\getTypoLinkPartsThrowExceptionWithPharReferencesDataProvider
‪getTypoLinkPartsThrowExceptionWithPharReferencesDataProvider()
Definition: TypoLinkSoftReferenceParserTest.php:306
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkSoftReferenceParserTest\getTypoLinkPartsThrowExceptionWithPharReferences
‪getTypoLinkPartsThrowExceptionWithPharReferences(string $pharUrl)
Definition: TypoLinkSoftReferenceParserTest.php:328
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\AbstractSoftReferenceParserTest
Definition: AbstractSoftReferenceParserTest.php:39
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\AbstractSoftReferenceParserTest\getParserByKey
‪getParserByKey($softrefKey)
Definition: AbstractSoftReferenceParserTest.php:43