‪TYPO3CMS  10.4
SoftReferenceIndexTest.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 Psr\EventDispatcher\EventDispatcherInterface;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪SoftReferenceIndexTest extends UnitTestCase
32 {
33  protected ‪$resetSingletonInstances = true;
34 
36  {
37  return [
38  'link to page' => [
39  [
40  'typolink' => [
41  'content' => 't3://page?uid=42',
42  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
43  'matchString' => 't3://page?uid=42',
44  ],
45  'typolink_tag' => [
46  'content' => '<p><a href="t3://page?uid=42">Click here</a></p>',
47  'elementKey' => 1,
48  'matchString' => '<a href="t3://page?uid=42">',
49  ],
50  ],
51  [
52  'subst' => [
53  'type' => 'db',
54  'recordRef' => 'pages:42',
55  'tokenValue' => 42,
56  ],
57  ],
58  ],
59  'link to page with properties' => [
60  [
61  'typolink_tag' => [
62  'content' => '<p><a class="link-page" href="t3://page?uid=42" target="_top" title="Foo">Click here</a></p>',
63  'elementKey' => 1,
64  'matchString' => '<a class="link-page" href="t3://page?uid=42" target="_top" title="Foo">',
65  ],
66  ],
67  [
68  'subst' => [
69  'type' => 'db',
70  'recordRef' => 'pages:42',
71  'tokenValue' => '42',
72  ],
73  ],
74  ],
75  'link to page with just a number' => [
76  [
77  'typolink' => [
78  'content' => '42',
79  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
80  'matchString' => '42',
81  ],
82  ],
83  [
84  'subst' => [
85  'type' => 'db',
86  'recordRef' => 'pages:42',
87  'tokenValue' => '42',
88  ],
89  ],
90  ],
91  'link to page with just a number and type comma-separated' => [
92  [
93  'typolink' => [
94  'content' => '42,100',
95  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
96  'matchString' => '42,100',
97  ],
98  ],
99  [
100  'subst' => [
101  'type' => 'db',
102  'recordRef' => 'pages:42',
103  'tokenValue' => '42',
104  ],
105  ],
106  ],
107  'link to external URL without scheme' => [
108  [
109  'typolink' => [
110  'content' => 'www.example.com',
111  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
112  'matchString' => 'www.example.com',
113  ],
114  'typolink_tag' => [
115  'content' => '<p><a class="link-page" href="www.example.com" target="_top" title="Foo">Click here</a></p>',
116  'elementKey' => 1,
117  'matchString' => '<a class="link-page" href="www.example.com" target="_top" title="Foo">',
118  ],
119  ],
120  [
121  'subst' => [
122  'type' => 'external',
123  'tokenValue' => 'http://www.example.com',
124  ],
125  ],
126  ],
127  'link to external URL with scheme' => [
128  [
129  'typolink' => [
130  'content' => 'https://www.example.com',
131  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
132  'matchString' => 'https://www.example.com',
133  ],
134  'typolink_tag' => [
135  'content' => '<p><a class="link-page" href="https://www.example.com" target="_top" title="Foo">Click here</a></p>',
136  'elementKey' => 1,
137  'matchString' => '<a class="link-page" href="https://www.example.com" target="_top" title="Foo">',
138  ],
139  ],
140  [
141  'subst' => [
142  'type' => 'external',
143  'tokenValue' => 'https://www.example.com',
144  ],
145  ],
146  ],
147  'link to email' => [
148  [
149  'typolink' => [
150  'content' => 'mailto:test@example.com',
151  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
152  'matchString' => 'mailto:test@example.com',
153  ],
154  'typolink_tag' => [
155  'content' => '<p><a href="mailto:test@example.com">Click here</a></p>',
156  'elementKey' => 1,
157  'matchString' => '<a href="mailto:test@example.com">',
158  ],
159  ],
160  [
161  'subst' => [
162  'type' => 'string',
163  'tokenValue' => 'test@example.com',
164  ],
165  ],
166  ],
167  'link to email without schema' => [
168  [
169  'typolink' => [
170  'content' => 'test@example.com',
171  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
172  'matchString' => 'test@example.com',
173  ],
174  'typolink_tag' => [
175  'content' => '<p><a href="test@example.com">Click here</a></p>',
176  'elementKey' => 1,
177  'matchString' => '<a href="test@example.com">',
178  ],
179  ],
180  [
181  'subst' => [
182  'type' => 'string',
183  'tokenValue' => 'test@example.com',
184  ],
185  ],
186  ],
187  'link to phone number' => [
188  [
189  'typolink' => [
190  'content' => 'tel:0123456789',
191  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
192  'matchString' => 'tel:0123456789',
193  ],
194  'typolink_tag' => [
195  'content' => '<p><a href="tel:0123456789">Click here</a></p>',
196  'elementKey' => 1,
197  'matchString' => '<a href="tel:0123456789">',
198  ],
199  ],
200  [
201  'subst' => [
202  'type' => 'string',
203  'tokenValue' => '0123456789',
204  ],
205  ],
206  ],
207  ];
208  }
209 
216  public function ‪findRefReturnsParsedElements(array $softrefConfiguration, array $expectedElement)
217  {
218  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
219  foreach ($softrefConfiguration as $softrefKey => $configuration) {
220  $subject = new ‪SoftReferenceIndex($eventDispatcher->reveal());
221  $result = $subject->findRef(
222  'tt_content',
223  'bodytext',
224  1,
225  $configuration['content'],
226  $softrefKey,
227  []
228  );
229 
230  self::assertArrayHasKey('elements', $result);
231  self::assertArrayHasKey($configuration['elementKey'], $result['elements']);
232 
233  // Remove tokenID as this one depends on the softrefKey and doesn't need to be verified
234  unset($result['elements'][$configuration['elementKey']]['subst']['tokenID']);
235 
236  $expectedElement['matchString'] = $configuration['matchString'];
237  self::assertEquals($expectedElement, $result['elements'][$configuration['elementKey']]);
238  }
239  }
240 
242  {
243  return [
244  'link to file' => [
245  [
246  'typolink' => [
247  'content' => 't3://file?uid=42',
248  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
249  'matchString' => 't3://file?uid=42',
250  ],
251  'typolink_tag' => [
252  'content' => '<p><a href="t3://file?uid=42">Click here</a></p>',
253  'elementKey' => 1,
254  'matchString' => '<a href="t3://file?uid=42">',
255  ],
256  ],
257  [
258  'subst' => [
259  'type' => 'db',
260  'recordRef' => 'sys_file:42',
261  'tokenValue' => 'file:42',
262  ],
263  ],
264  ],
265  'file with t3 mixed syntax' => [
266  [
267  'typolink' => [
268  'content' => 't3://file?identifier=42',
269  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
270  'matchString' => 't3://file?identifier=42',
271  ],
272  ],
273  [
274  'subst' => [
275  'type' => 'db',
276  'recordRef' => 'sys_file:42',
277  'tokenValue' => 'file:42',
278  ],
279  ],
280  ],
281  'link to file with old FAL object syntax' => [
282  [
283  'typolink' => [
284  'content' => 'file:42',
285  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
286  'matchString' => 'file:42',
287  ],
288  ],
289  [
290  'subst' => [
291  'type' => 'db',
292  'recordRef' => 'sys_file:42',
293  'tokenValue' => 'file:42',
294  ],
295  ],
296  ],
297  'link to file with simple file path' => [
298  [
299  'typolink' => [
300  'content' => 'fileadmin/download.jpg',
301  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
302  'matchString' => 'fileadmin/download.jpg',
303  ],
304  ],
305  [
306  'subst' => [
307  'type' => 'db',
308  'recordRef' => 'sys_file:42',
309  'tokenValue' => 'file:42',
310  ],
311  ],
312  ],
313  ];
314  }
315 
322  public function ‪findRefReturnsParsedElementsWithFile(array $softrefConfiguration, array $expectedElement)
323  {
324  $fileObject = $this->prophesize(File::class);
325  $fileObject->getUid()->willReturn(42)->shouldBeCalledTimes(count($softrefConfiguration));
326 
327  $resourceFactory = $this->prophesize(ResourceFactory::class);
328  $resourceFactory->getFileObject('42')->willReturn($fileObject->reveal());
329  // For `t3://file?identifier=42` handling
330  $resourceFactory->getFileObjectFromCombinedIdentifier('42')->willReturn($fileObject->reveal());
331  // For `file:23` handling
332  $resourceFactory->retrieveFileOrFolderObject('42')->willReturn($fileObject->reveal());
333  // For `fileadmin/download.jpg` handling
334  $resourceFactory->retrieveFileOrFolderObject('fileadmin/download.jpg')->willReturn($fileObject->reveal());
335 
336  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory->reveal());
337 
338  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
339  foreach ($softrefConfiguration as $softrefKey => $configuration) {
340  $subject = new ‪SoftReferenceIndex($eventDispatcher->reveal());
341  $result = $subject->findRef(
342  'tt_content',
343  'bodytext',
344  1,
345  $configuration['content'],
346  $softrefKey,
347  []
348  );
349 
350  self::assertArrayHasKey('elements', $result);
351  self::assertArrayHasKey($configuration['elementKey'], $result['elements']);
352 
353  // Remove tokenID as this one depends on the softrefKey and doesn't need to be verified
354  unset($result['elements'][$configuration['elementKey']]['subst']['tokenID']);
355 
356  $expectedElement['matchString'] = $configuration['matchString'];
357  self::assertEquals($expectedElement, $result['elements'][$configuration['elementKey']]);
358  }
359  }
360 
362  {
363  return [
364  'link to folder' => [
365  [
366  'typolink' => [
367  'content' => 't3://folder?storage=1&amp;identifier=%2Ffoo%2Fbar%2Fbaz',
368  ],
369  'typolink_tag' => [
370  'content' => '<p><a href="t3://folder?storage=1&amp;identifier=%2Ffoo%2Fbar%2Fbaz">Click here</a></p>',
371  ],
372  ],
373  ],
374  'link to folder with properties' => [
375  [
376  'typolink_tag' => [
377  'content' => '<p><a class="link-page" href="t3://folder?storage=1&amp;identifier=%2Ffoo%2Fbar%2Fbaz" target="_top" title="Foo">Click here</a></p>',
378  ],
379  ],
380  ],
381  ];
382  }
383 
389  public function ‪findRefReturnsNullWithFolder(array $softrefConfiguration)
390  {
391  $folderObject = $this->prophesize(Folder::class);
392 
393  $resourceFactory = $this->prophesize(ResourceFactory::class);
394  $resourceFactory->getFolderObjectFromCombinedIdentifier('1:/foo/bar/baz')->willReturn($folderObject->reveal())->shouldBeCalledTimes(count($softrefConfiguration));
395  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory->reveal());
396 
397  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
398  foreach ($softrefConfiguration as $softrefKey => $configuration) {
399  $subject = new ‪SoftReferenceIndex($eventDispatcher->reveal());
400  $result = $subject->findRef(
401  'tt_content',
402  'bodytext',
403  1,
404  $configuration['content'],
405  $softrefKey,
406  []
407  );
408 
409  self::assertNull($result);
410  }
411  }
412 
414  {
415  return [
416  'URL encoded local' => [
417  'phar%3a//some-file.jpg',
418  ],
419  'URL encoded absolute' => [
420  'phar%3a///path/some-file.jpg',
421  ],
422  'not URL encoded local' => [
423  'phar://some-file.jpg',
424  ],
425  'not URL encoded absolute' => [
426  'phar:///path/some-file.jpg',
427  ],
428  ];
429  }
430 
436  {
437  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
438  $this->expectException(\RuntimeException::class);
439  $this->expectExceptionCode(1530030672);
440  (new ‪SoftReferenceIndex($eventDispatcher->reveal()))->getTypoLinkParts($pharUrl);
441  }
442 }
‪TYPO3\CMS\Core\Tests\Unit\Database\SoftReferenceIndexTest\findRefReturnsParsedElements
‪findRefReturnsParsedElements(array $softrefConfiguration, array $expectedElement)
Definition: SoftReferenceIndexTest.php:216
‪TYPO3\CMS\Core\Tests\Unit\Database\SoftReferenceIndexTest\getTypoLinkPartsThrowExceptionWithPharReferencesDataProvider
‪getTypoLinkPartsThrowExceptionWithPharReferencesDataProvider()
Definition: SoftReferenceIndexTest.php:413
‪TYPO3\CMS\Core\Tests\Unit\Database\SoftReferenceIndexTest\findRefReturnsNullWithFolderDataProvider
‪findRefReturnsNullWithFolderDataProvider()
Definition: SoftReferenceIndexTest.php:361
‪TYPO3\CMS\Core\Tests\Unit\Database
Definition: ConnectionPoolTest.php:18
‪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\Database\SoftReferenceIndexTest\findRefReturnsParsedElementsWithFile
‪findRefReturnsParsedElementsWithFile(array $softrefConfiguration, array $expectedElement)
Definition: SoftReferenceIndexTest.php:322
‪TYPO3\CMS\Core\Tests\Unit\Database\SoftReferenceIndexTest\findRefReturnsParsedElementsDataProvider
‪findRefReturnsParsedElementsDataProvider()
Definition: SoftReferenceIndexTest.php:35
‪TYPO3\CMS\Core\Tests\Unit\Database\SoftReferenceIndexTest\findRefReturnsNullWithFolder
‪findRefReturnsNullWithFolder(array $softrefConfiguration)
Definition: SoftReferenceIndexTest.php:389
‪TYPO3\CMS\Core\Tests\Unit\Database\SoftReferenceIndexTest\findRefReturnsParsedElementsWithFileDataProvider
‪findRefReturnsParsedElementsWithFileDataProvider()
Definition: SoftReferenceIndexTest.php:241
‪TYPO3\CMS\Core\Tests\Unit\Database\SoftReferenceIndexTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: SoftReferenceIndexTest.php:33
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Tests\Unit\Database\SoftReferenceIndexTest
Definition: SoftReferenceIndexTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Database\SoftReferenceIndexTest\getTypoLinkPartsThrowExceptionWithPharReferences
‪getTypoLinkPartsThrowExceptionWithPharReferences(string $pharUrl)
Definition: SoftReferenceIndexTest.php:435
‪TYPO3\CMS\Core\Database\SoftReferenceIndex
Definition: SoftReferenceIndex.php:80