‪TYPO3CMS  11.5
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 Prophecy\Argument;
21 use Prophecy\PhpUnit\ProphecyTrait;
22 use Psr\EventDispatcher\EventDispatcherInterface;
23 use Psr\Log\LoggerInterface;
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 
39 class ‪SoftReferenceIndexTest extends UnitTestCase
40 {
41  use ProphecyTrait;
42 
43  protected ‪$resetSingletonInstances = true;
44 
46  {
47  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
48  $runtimeCache = $this->prophesize(FrontendInterface::class);
49  $runtimeCache->get(Argument::any())->willReturn(false);
50  $runtimeCache->set(Argument::cetera())->willReturn(null);
51  $logger = $this->prophesize(LoggerInterface::class);
52 
53  $softReferenceParserFactory = new ‪SoftReferenceParserFactory($runtimeCache->reveal(), $logger->reveal());
54  $softReferenceParserFactory->addParser(new ‪TypolinkSoftReferenceParser($eventDispatcher->reveal()), 'typolink');
55  $softReferenceParserFactory->addParser(new ‪TypolinkTagSoftReferenceParser(), 'typolink_tag');
56 
57  return $softReferenceParserFactory;
58  }
59 
61  {
62  $eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
63 
64  return new ‪SoftReferenceIndex($eventDispatcher->reveal(), $this->createSoftReferenceParserFactory());
65  }
66 
67  public function ‪findRefReturnsParsedElementsDataProvider(): array
68  {
69  return [
70  'link to page' => [
71  [
72  'typolink' => [
73  'content' => 't3://page?uid=42',
74  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
75  'matchString' => 't3://page?uid=42',
76  ],
77  'typolink_tag' => [
78  'content' => '<p><a href="t3://page?uid=42">Click here</a></p>',
79  'elementKey' => 1,
80  'matchString' => '<a href="t3://page?uid=42">',
81  ],
82  ],
83  [
84  'subst' => [
85  'type' => 'db',
86  'recordRef' => 'pages:42',
87  'tokenValue' => 42,
88  ],
89  ],
90  ],
91  'link to page with properties' => [
92  [
93  'typolink_tag' => [
94  'content' => '<p><a class="link-page" href="t3://page?uid=42" target="_top" title="Foo">Click here</a></p>',
95  'elementKey' => 1,
96  'matchString' => '<a class="link-page" href="t3://page?uid=42" target="_top" title="Foo">',
97  ],
98  ],
99  [
100  'subst' => [
101  'type' => 'db',
102  'recordRef' => 'pages:42',
103  'tokenValue' => '42',
104  ],
105  ],
106  ],
107  'link to page with just a number' => [
108  [
109  'typolink' => [
110  'content' => '42',
111  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
112  'matchString' => '42',
113  ],
114  ],
115  [
116  'subst' => [
117  'type' => 'db',
118  'recordRef' => 'pages:42',
119  'tokenValue' => '42',
120  ],
121  ],
122  ],
123  'link to page with just a number and type comma-separated' => [
124  [
125  'typolink' => [
126  'content' => '42,100',
127  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
128  'matchString' => '42,100',
129  ],
130  ],
131  [
132  'subst' => [
133  'type' => 'db',
134  'recordRef' => 'pages:42',
135  'tokenValue' => '42',
136  ],
137  ],
138  ],
139  'link to external URL without scheme' => [
140  [
141  'typolink' => [
142  'content' => 'www.example.com',
143  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
144  'matchString' => 'www.example.com',
145  ],
146  'typolink_tag' => [
147  'content' => '<p><a class="link-page" href="www.example.com" target="_top" title="Foo">Click here</a></p>',
148  'elementKey' => 1,
149  'matchString' => '<a class="link-page" href="www.example.com" target="_top" title="Foo">',
150  ],
151  ],
152  [
153  'subst' => [
154  'type' => 'external',
155  'tokenValue' => 'http://www.example.com',
156  ],
157  ],
158  ],
159  'link to external URL with scheme' => [
160  [
161  'typolink' => [
162  'content' => 'https://www.example.com',
163  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
164  'matchString' => 'https://www.example.com',
165  ],
166  'typolink_tag' => [
167  'content' => '<p><a class="link-page" href="https://www.example.com" target="_top" title="Foo">Click here</a></p>',
168  'elementKey' => 1,
169  'matchString' => '<a class="link-page" href="https://www.example.com" target="_top" title="Foo">',
170  ],
171  ],
172  [
173  'subst' => [
174  'type' => 'external',
175  'tokenValue' => 'https://www.example.com',
176  ],
177  ],
178  ],
179  'link to email' => [
180  [
181  'typolink' => [
182  'content' => 'mailto:test@example.com',
183  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
184  'matchString' => 'mailto:test@example.com',
185  ],
186  'typolink_tag' => [
187  'content' => '<p><a href="mailto:test@example.com">Click here</a></p>',
188  'elementKey' => 1,
189  'matchString' => '<a href="mailto:test@example.com">',
190  ],
191  ],
192  [
193  'subst' => [
194  'type' => 'string',
195  'tokenValue' => 'test@example.com',
196  ],
197  ],
198  ],
199  'link to email without schema' => [
200  [
201  'typolink' => [
202  'content' => 'test@example.com',
203  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
204  'matchString' => 'test@example.com',
205  ],
206  'typolink_tag' => [
207  'content' => '<p><a href="test@example.com">Click here</a></p>',
208  'elementKey' => 1,
209  'matchString' => '<a href="test@example.com">',
210  ],
211  ],
212  [
213  'subst' => [
214  'type' => 'string',
215  'tokenValue' => 'test@example.com',
216  ],
217  ],
218  ],
219  'link to phone number' => [
220  [
221  'typolink' => [
222  'content' => 'tel:0123456789',
223  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
224  'matchString' => 'tel:0123456789',
225  ],
226  'typolink_tag' => [
227  'content' => '<p><a href="tel:0123456789">Click here</a></p>',
228  'elementKey' => 1,
229  'matchString' => '<a href="tel:0123456789">',
230  ],
231  ],
232  [
233  'subst' => [
234  'type' => 'string',
235  'tokenValue' => '0123456789',
236  ],
237  ],
238  ],
239  ];
240  }
241 
248  public function ‪findRefReturnsParsedElements(array $softrefConfiguration, array $expectedElement): void
249  {
250  foreach ($softrefConfiguration as $softrefKey => $configuration) {
251  $subject = $this->‪getSoftReferenceParserInstance();
252  $result = $subject->findRef(
253  'tt_content',
254  'bodytext',
255  1,
256  $configuration['content'],
257  $softrefKey,
258  []
259  );
260 
261  self::assertArrayHasKey('elements', $result);
262  self::assertArrayHasKey($configuration['elementKey'], $result['elements']);
263 
264  // Remove tokenID as this one depends on the softrefKey and doesn't need to be verified
265  unset($result['elements'][$configuration['elementKey']]['subst']['tokenID']);
266 
267  $expectedElement['matchString'] = $configuration['matchString'];
268  self::assertEquals($expectedElement, $result['elements'][$configuration['elementKey']]);
269  }
270  }
271 
273  {
274  return [
275  'link to file' => [
276  [
277  'typolink' => [
278  'content' => 't3://file?uid=42',
279  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
280  'matchString' => 't3://file?uid=42',
281  ],
282  'typolink_tag' => [
283  'content' => '<p><a href="t3://file?uid=42">Click here</a></p>',
284  'elementKey' => 1,
285  'matchString' => '<a href="t3://file?uid=42">',
286  ],
287  ],
288  [
289  'subst' => [
290  'type' => 'db',
291  'recordRef' => 'sys_file:42',
292  'tokenValue' => 'file:42',
293  ],
294  ],
295  ],
296  'file with t3 mixed syntax' => [
297  [
298  'typolink' => [
299  'content' => 't3://file?identifier=42',
300  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
301  'matchString' => 't3://file?identifier=42',
302  ],
303  ],
304  [
305  'subst' => [
306  'type' => 'db',
307  'recordRef' => 'sys_file:42',
308  'tokenValue' => 'file:42',
309  ],
310  ],
311  ],
312  'link to file with old FAL object syntax' => [
313  [
314  'typolink' => [
315  'content' => 'file:42',
316  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
317  'matchString' => 'file:42',
318  ],
319  ],
320  [
321  'subst' => [
322  'type' => 'db',
323  'recordRef' => 'sys_file:42',
324  'tokenValue' => 'file:42',
325  ],
326  ],
327  ],
328  'link to file with simple file path' => [
329  [
330  'typolink' => [
331  'content' => 'fileadmin/download.jpg',
332  'elementKey' => '8695f308356bcca1acac2749152a44a9:0',
333  'matchString' => 'fileadmin/download.jpg',
334  ],
335  ],
336  [
337  'subst' => [
338  'type' => 'db',
339  'recordRef' => 'sys_file:42',
340  'tokenValue' => 'file:42',
341  ],
342  ],
343  ],
344  ];
345  }
346 
353  public function ‪findRefReturnsParsedElementsWithFile(array $softrefConfiguration, array $expectedElement): void
354  {
355  $fileObject = $this->prophesize(File::class);
356  $fileObject->getName()->willReturn('download.jpg');
357  $fileObject->getIdentifier()->willReturn('fileadmin/download.jpg');
358  $fileObject->getUid()->willReturn(42)->shouldBeCalledTimes(count($softrefConfiguration));
359 
360  $resourceFactory = $this->prophesize(ResourceFactory::class);
361  $resourceFactory->getFileObject('42')->willReturn($fileObject->reveal());
362  // For `t3://file?identifier=42` handling
363  $resourceFactory->getFileObjectFromCombinedIdentifier('42')->willReturn($fileObject->reveal());
364  // For `file:23` handling
365  $resourceFactory->retrieveFileOrFolderObject('42')->willReturn($fileObject->reveal());
366  // For `fileadmin/download.jpg` handling
367  $resourceFactory->retrieveFileOrFolderObject('fileadmin/download.jpg')->willReturn($fileObject->reveal());
368 
369  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory->reveal());
370 
371  foreach ($softrefConfiguration as $softrefKey => $configuration) {
372  $subject = $this->‪getSoftReferenceParserInstance();
373  $result = $subject->findRef(
374  'tt_content',
375  'bodytext',
376  1,
377  $configuration['content'],
378  $softrefKey,
379  []
380  );
381 
382  self::assertArrayHasKey('elements', $result);
383  self::assertArrayHasKey($configuration['elementKey'], $result['elements']);
384 
385  // Remove tokenID as this one depends on the softrefKey and doesn't need to be verified
386  unset($result['elements'][$configuration['elementKey']]['subst']['tokenID']);
387 
388  $expectedElement['matchString'] = $configuration['matchString'];
389  self::assertEquals($expectedElement, $result['elements'][$configuration['elementKey']]);
390  }
391  }
392 
393  public function ‪findRefReturnsNullWithFolderDataProvider(): array
394  {
395  return [
396  'link to folder' => [
397  [
398  'typolink' => [
399  'content' => 't3://folder?storage=1&amp;identifier=%2Ffoo%2Fbar%2Fbaz',
400  ],
401  'typolink_tag' => [
402  'content' => '<p><a href="t3://folder?storage=1&amp;identifier=%2Ffoo%2Fbar%2Fbaz">Click here</a></p>',
403  ],
404  ],
405  ],
406  'link to folder with properties' => [
407  [
408  'typolink_tag' => [
409  'content' => '<p><a class="link-page" href="t3://folder?storage=1&amp;identifier=%2Ffoo%2Fbar%2Fbaz" target="_top" title="Foo">Click here</a></p>',
410  ],
411  ],
412  ],
413  ];
414  }
415 
421  public function ‪findRefReturnsNullWithFolder(array $softrefConfiguration): void
422  {
423  $folderObject = $this->prophesize(Folder::class);
424 
425  $resourceFactory = $this->prophesize(ResourceFactory::class);
426  $resourceFactory->getFolderObjectFromCombinedIdentifier('1:/foo/bar/baz')->willReturn($folderObject->reveal())->shouldBeCalledTimes(count($softrefConfiguration));
427  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory->reveal());
428 
429  foreach ($softrefConfiguration as $softrefKey => $configuration) {
430  $subject = $this->‪getSoftReferenceParserInstance();
431  $result = $subject->findRef(
432  'tt_content',
433  'bodytext',
434  1,
435  $configuration['content'],
436  $softrefKey,
437  []
438  );
439 
440  self::assertNull($result);
441  }
442  }
443 
445  {
446  return [
447  'URL encoded local' => [
448  'phar%3a//some-file.jpg',
449  ],
450  'URL encoded absolute' => [
451  'phar%3a///path/some-file.jpg',
452  ],
453  'not URL encoded local' => [
454  'phar://some-file.jpg',
455  ],
456  'not URL encoded absolute' => [
457  'phar:///path/some-file.jpg',
458  ],
459  ];
460  }
461 
466  public function ‪getTypoLinkPartsThrowExceptionWithPharReferences(string $pharUrl): void
467  {
468  $this->expectException(\RuntimeException::class);
469  $this->expectExceptionCode(1530030672);
470  ($this->‪getSoftReferenceParserInstance())->getTypoLinkParts($pharUrl, 'aTable', 0);
471  }
472 
478  public function ‪mixedRegistrationBehaviour(): void
479  {
480  $softReferenceParserFactory = $this->‪createSoftReferenceParserFactory();
481  $softReferenceParserFactory->addParser(new NotifySoftReferenceParser(), 'typolink');
482  $softReferenceParser = $softReferenceParserFactory->getSoftReferenceParser('typolink');
483 
484  self::assertTrue(get_class($softReferenceParser) === TypolinkSoftReferenceParser::class);
485  }
486 
491  {
492  ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'] = [
493  'GLOBAL' => [
494  'softRefParser' => [
495  'substitute' => \TYPO3\CMS\Core\DataHandling\SoftReference\SubstituteSoftReferenceParser::class,
496  ],
497  ],
498  ];
499  $runtimeCache = $this->prophesize(FrontendInterface::class);
500  $logger = $this->prophesize(LoggerInterface::class);
501 
502  $softReferenceParserFactory = new SoftReferenceParserFactory($runtimeCache->reveal(), $logger->reveal());
503 
504  self::assertIsObject($softReferenceParserFactory->getSoftReferenceParser('substitute'));
505  }
506 
512  {
513  $softReferenceParserFactory = $this->‪createSoftReferenceParserFactory();
514  foreach ($softReferenceParserFactory->getParsersBySoftRefParserList('idonotexist,typolink_tag') as $softReferenceParser) {
515  // Do nothing
516  }
517  }
518 }
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\findRefReturnsNullWithFolderDataProvider
‪findRefReturnsNullWithFolderDataProvider()
Definition: SoftReferenceIndexTest.php:392
‪TYPO3\CMS\Core\DataHandling\SoftReference\SoftReferenceParserFactory
Definition: SoftReferenceParserFactory.php:28
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\softReferenceParserFactoryAddsParserInGlobalsArray
‪softReferenceParserFactoryAddsParserInGlobalsArray()
Definition: SoftReferenceIndexTest.php:489
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\getTypoLinkPartsThrowExceptionWithPharReferencesDataProvider
‪getTypoLinkPartsThrowExceptionWithPharReferencesDataProvider()
Definition: SoftReferenceIndexTest.php:443
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\mixedRegistrationBehaviour
‪mixedRegistrationBehaviour()
Definition: SoftReferenceIndexTest.php:477
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\findRefReturnsParsedElementsWithFileDataProvider
‪findRefReturnsParsedElementsWithFileDataProvider()
Definition: SoftReferenceIndexTest.php:271
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\createSoftReferenceParserFactory
‪createSoftReferenceParserFactory()
Definition: SoftReferenceIndexTest.php:44
‪TYPO3\CMS\Core\DataHandling\SoftReference\NotifySoftReferenceParser
Definition: NotifySoftReferenceParser.php:24
‪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\UnitDeprecated\Database\SoftReferenceIndexTest\findRefReturnsNullWithFolder
‪findRefReturnsNullWithFolder(array $softrefConfiguration)
Definition: SoftReferenceIndexTest.php:420
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\getTypoLinkPartsThrowExceptionWithPharReferences
‪getTypoLinkPartsThrowExceptionWithPharReferences(string $pharUrl)
Definition: SoftReferenceIndexTest.php:465
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: SoftReferenceIndexTest.php:42
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\findRefReturnsParsedElementsWithFile
‪findRefReturnsParsedElementsWithFile(array $softrefConfiguration, array $expectedElement)
Definition: SoftReferenceIndexTest.php:352
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\getSoftReferenceParserInstance
‪getSoftReferenceParserInstance()
Definition: SoftReferenceIndexTest.php:59
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\findRefReturnsParsedElements
‪findRefReturnsParsedElements(array $softrefConfiguration, array $expectedElement)
Definition: SoftReferenceIndexTest.php:247
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database
Definition: SoftReferenceIndexTest.php:18
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\findRefReturnsParsedElementsDataProvider
‪findRefReturnsParsedElementsDataProvider()
Definition: SoftReferenceIndexTest.php:66
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Database\SoftReferenceIndex
Definition: SoftReferenceIndex.php:36
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest
Definition: SoftReferenceIndexTest.php:40
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Database\SoftReferenceIndexTest\declaringNonExistingParserKeysWillNotThrowAnException
‪declaringNonExistingParserKeysWillNotThrowAnException()
Definition: SoftReferenceIndexTest.php:510