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