‪TYPO3CMS  11.5
TypoLinkTagSoftReferenceParserTest.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' => '<p><a href="t3://page?uid=42">Click here</a></p>',
33  'elementKey' => 1,
34  'matchString' => '<a href="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 properties' => [
45  [
46  'content' => '<p><a class="link-page" href="t3://page?uid=42" target="_top" title="Foo">Click here</a></p>',
47  'elementKey' => 1,
48  'matchString' => '<a class="link-page" href="t3://page?uid=42" target="_top" title="Foo">',
49  ],
50  [
51  'subst' => [
52  'type' => 'db',
53  'recordRef' => 'pages:42',
54  'tokenValue' => '42',
55  ],
56  ],
57  ],
58  'link to external URL without scheme' => [
59  [
60  'content' => '<p><a class="link-page" href="www.example.com" target="_top" title="Foo">Click here</a></p>',
61  'elementKey' => 1,
62  'matchString' => '<a class="link-page" href="www.example.com" target="_top" title="Foo">',
63  ],
64  [
65  'subst' => [
66  'type' => 'external',
67  'tokenValue' => 'http://www.example.com',
68  ],
69  ],
70  ],
71  'link to external URL with scheme' => [
72  'typolink_tag' => [
73  'content' => '<p><a class="link-page" href="https://www.example.com" target="_top" title="Foo">Click here</a></p>',
74  'elementKey' => 1,
75  'matchString' => '<a class="link-page" href="https://www.example.com" target="_top" title="Foo">',
76  ],
77  [
78  'subst' => [
79  'type' => 'external',
80  'tokenValue' => 'https://www.example.com',
81  ],
82  ],
83  ],
84  'link to email' => [
85  [
86  'content' => '<p><a href="mailto:test@example.com">Click here</a></p>',
87  'elementKey' => 1,
88  'matchString' => '<a href="mailto:test@example.com">',
89  ],
90  [
91  'subst' => [
92  'type' => 'string',
93  'tokenValue' => 'test@example.com',
94  ],
95  ],
96  ],
97  'link to email without schema' => [
98  [
99  'content' => '<p><a href="test@example.com">Click here</a></p>',
100  'elementKey' => 1,
101  'matchString' => '<a href="test@example.com">',
102  ],
103  [
104  'subst' => [
105  'type' => 'string',
106  'tokenValue' => 'test@example.com',
107  ],
108  ],
109  ],
110  'link to phone number' => [
111  [
112  'content' => '<p><a href="tel:0123456789">Click here</a></p>',
113  'elementKey' => 1,
114  'matchString' => '<a href="tel:0123456789">',
115  ],
116  [
117  'subst' => [
118  'type' => 'string',
119  'tokenValue' => '0123456789',
120  ],
121  ],
122  ],
123  ];
124  }
125 
132  public function ‪findRefReturnsParsedElements(array $softrefConfiguration, array $expectedElement): void
133  {
134  $subject = $this->‪getParserByKey('typolink_tag');
135  $subject->setParserKey('typolink_tag', $softrefConfiguration);
136  $result = $subject->parse(
137  'tt_content',
138  'bodytext',
139  1,
140  $softrefConfiguration['content']
141  )->toNullableArray();
142 
143  self::assertArrayHasKey('elements', $result);
144  self::assertArrayHasKey($softrefConfiguration['elementKey'], $result['elements']);
145 
146  // Remove tokenID as this one depends on the softrefKey and doesn't need to be verified
147  unset($result['elements'][$softrefConfiguration['elementKey']]['subst']['tokenID']);
148 
149  $expectedElement['matchString'] = $softrefConfiguration['matchString'];
150  self::assertEquals($expectedElement, $result['elements'][$softrefConfiguration['elementKey']]);
151  }
152 
154  {
155  return [
156  'link to file' => [
157  [
158  'content' => '<p><a href="t3://file?uid=42">Click here</a></p>',
159  'elementKey' => 1,
160  'matchString' => '<a href="t3://file?uid=42">',
161  ],
162  [
163  'subst' => [
164  'type' => 'db',
165  'recordRef' => 'sys_file:42',
166  'tokenValue' => 'file:42',
167  ],
168  ],
169  ],
170  ];
171  }
172 
179  public function ‪findRefReturnsParsedElementsWithFile(array $softrefConfiguration, array $expectedElement): void
180  {
181  $fileObject = $this->prophesize(File::class);
182  $fileObject->getUid()->willReturn(42)->shouldBeCalledTimes(1);
183  $fileObject->getName()->willReturn('download.jpg');
184  $fileObject->getIdentifier()->willReturn('fileadmin/download.jpg');
185 
186  $resourceFactory = $this->prophesize(ResourceFactory::class);
187  $resourceFactory->getFileObject('42')->willReturn($fileObject->reveal());
188  // For `t3://file?identifier=42` handling
189  $resourceFactory->getFileObjectFromCombinedIdentifier('42')->willReturn($fileObject->reveal());
190  // For `file:23` handling
191  $resourceFactory->retrieveFileOrFolderObject('42')->willReturn($fileObject->reveal());
192  // For `fileadmin/download.jpg` handling
193  $resourceFactory->retrieveFileOrFolderObject('fileadmin/download.jpg')->willReturn($fileObject->reveal());
194 
195  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory->reveal());
196 
197  $subject = $this->‪getParserByKey('typolink_tag');
198  $subject->setParserKey('typolink_tag', $softrefConfiguration);
199  $result = $subject->parse(
200  'tt_content',
201  'bodytext',
202  1,
203  $softrefConfiguration['content'],
204  )->toNullableArray();
205 
206  self::assertArrayHasKey('elements', $result);
207  self::assertArrayHasKey($softrefConfiguration['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'][$softrefConfiguration['elementKey']]['subst']['tokenID']);
211 
212  $expectedElement['matchString'] = $softrefConfiguration['matchString'];
213  self::assertEquals($expectedElement, $result['elements'][$softrefConfiguration['elementKey']]);
214  }
215 
217  {
218  return [
219  'link to folder' => [
220  [
221  'content' => '<p><a href="t3://folder?storage=1&amp;identifier=%2Ffoo%2Fbar%2Fbaz">Click here</a></p>',
222  ],
223  ],
224  'link to folder with properties' => [
225  [
226  'content' => '<p><a class="link-page" href="t3://folder?storage=1&amp;identifier=%2Ffoo%2Fbar%2Fbaz" target="_top" title="Foo">Click here</a></p>',
227  ],
228  ],
229  ];
230  }
231 
237  public function ‪findRefReturnsNullWithFolder(array $softrefConfiguration): void
238  {
239  $folderObject = $this->prophesize(Folder::class);
240 
241  $resourceFactory = $this->prophesize(ResourceFactory::class);
242  $resourceFactory->getFolderObjectFromCombinedIdentifier('1:/foo/bar/baz')->willReturn($folderObject->reveal())->shouldBeCalledTimes(1);
243  GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactory->reveal());
244 
245  $result = $this->‪getParserByKey('typolink_tag')->parse(
246  'tt_content',
247  'bodytext',
248  1,
249  $softrefConfiguration['content']
250  )->toNullableArray();
251 
252  self::assertNull($result);
253  }
254 }
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkTagSoftReferenceParserTest\findRefReturnsParsedElementsDataProvider
‪findRefReturnsParsedElementsDataProvider()
Definition: TypoLinkTagSoftReferenceParserTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkTagSoftReferenceParserTest\findRefReturnsParsedElementsWithFileDataProvider
‪findRefReturnsParsedElementsWithFileDataProvider()
Definition: TypoLinkTagSoftReferenceParserTest.php:153
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkTagSoftReferenceParserTest
Definition: TypoLinkTagSoftReferenceParserTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkTagSoftReferenceParserTest\findRefReturnsNullWithFolder
‪findRefReturnsNullWithFolder(array $softrefConfiguration)
Definition: TypoLinkTagSoftReferenceParserTest.php:237
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkTagSoftReferenceParserTest\findRefReturnsParsedElementsWithFile
‪findRefReturnsParsedElementsWithFile(array $softrefConfiguration, array $expectedElement)
Definition: TypoLinkTagSoftReferenceParserTest.php:179
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkTagSoftReferenceParserTest\findRefReturnsParsedElements
‪findRefReturnsParsedElements(array $softrefConfiguration, array $expectedElement)
Definition: TypoLinkTagSoftReferenceParserTest.php:132
‪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
Definition: AbstractSoftReferenceParserTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\DataHandling\SoftReference\TypoLinkTagSoftReferenceParserTest\findRefReturnsNullWithFolderDataProvider
‪findRefReturnsNullWithFolderDataProvider()
Definition: TypoLinkTagSoftReferenceParserTest.php:216
‪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