‪TYPO3CMS  ‪main
TypoLinkCodecServiceTest.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 PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use Symfony\Component\DependencyInjection\Container;
28 use TYPO3\CMS\Core\LinkHandling\TypoLinkCodecService;
30 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
31 
32 final class ‪TypoLinkCodecServiceTest extends UnitTestCase
33 {
34  protected TypoLinkCodecService ‪$subject;
35 
36  protected function ‪setUp(): void
37  {
38  parent::setUp();
39  $this->subject = new TypoLinkCodecService(new ‪NoopEventDispatcher());
40  }
41 
42  #[DataProvider('encodeReturnsExpectedResultDataProvider')]
43  #[Test]
44  public function ‪encodeReturnsExpectedResult(array $parts, string $expected): void
45  {
46  self::assertSame($expected, $this->subject->encode($parts));
47  }
48 
49  public static function ‪encodeReturnsExpectedResultDataProvider(): array
50  {
51  return [
52  'empty input' => [
53  [
54  'url' => '',
55  'target' => '',
56  'class' => '',
57  'title' => '',
58  'additionalParams' => '',
59  ],
60  '',
61  ],
62  'full parameter usage' => [
63  [
64  'url' => '19',
65  'target' => '_blank',
66  'class' => 'css-class',
67  'title' => 'testtitle with whitespace',
68  'additionalParams' => '&x=y',
69  ],
70  '19 _blank css-class "testtitle with whitespace" &x=y',
71  ],
72  'crazy title and partial items only' => [
73  [
74  'url' => 'foo',
75  'title' => 'a "link\\ ti\\"tle',
76  ],
77  'foo - - "a \\"link\\\\ ti\\\\\\"tle"',
78  ],
79  ];
80  }
81 
82  #[DataProvider('decodeReturnsExpectedResultDataProvider')]
83  #[Test]
84  public function ‪decodeReturnsExpectedResult(string $typoLink, array $expected): void
85  {
86  self::assertSame($expected, $this->subject->decode($typoLink));
87  }
88 
89  public static function ‪decodeReturnsExpectedResultDataProvider(): array
90  {
91  return [
92  'empty input' => [
93  '',
94  [
95  'url' => '',
96  'target' => '',
97  'class' => '',
98  'title' => '',
99  'additionalParams' => '',
100  ],
101  ],
102  'simple id input' => [
103  '19',
104  [
105  'url' => '19',
106  'target' => '',
107  'class' => '',
108  'title' => '',
109  'additionalParams' => '',
110  ],
111  ],
112  'external url with target' => [
113  'www.web.de _blank',
114  [
115  'url' => 'www.web.de',
116  'target' => '_blank',
117  'class' => '',
118  'title' => '',
119  'additionalParams' => '',
120  ],
121  ],
122  'page with class' => [
123  '42 - css-class',
124  [
125  'url' => '42',
126  'target' => '',
127  'class' => 'css-class',
128  'title' => '',
129  'additionalParams' => '',
130  ],
131  ],
132  'page with title' => [
133  '42 - - "a link title"',
134  [
135  'url' => '42',
136  'target' => '',
137  'class' => '',
138  'title' => 'a link title',
139  'additionalParams' => '',
140  ],
141  ],
142  'page with crazy title' => [
143  '42 - - "a \\"link\\\\ ti\\\\\\"tle"',
144  [
145  'url' => '42',
146  'target' => '',
147  'class' => '',
148  'title' => 'a "link\\ ti\\"tle',
149  'additionalParams' => '',
150  ],
151  ],
152  'page with title and parameters' => [
153  '42 - - "a link title" &x=y',
154  [
155  'url' => '42',
156  'target' => '',
157  'class' => '',
158  'title' => 'a link title',
159  'additionalParams' => '&x=y',
160  ],
161  ],
162  'page with complex title' => [
163  '42 - - "a \\"link\\" title with \\\\" &x=y',
164  [
165  'url' => '42',
166  'target' => '',
167  'class' => '',
168  'title' => 'a "link" title with \\',
169  'additionalParams' => '&x=y',
170  ],
171  ],
172  'full parameter usage' => [
173  '19 _blank css-class "testtitle with whitespace" &X=y',
174  [
175  'url' => '19',
176  'target' => '_blank',
177  'class' => 'css-class',
178  'title' => 'testtitle with whitespace',
179  'additionalParams' => '&X=y',
180  ],
181  ],
182  ];
183  }
184 
185  #[Test]
187  {
188  $beforeTypoLinkEncodedEvent = null;
189 
190  $container = new Container();
191  $container->set(
192  'before-typo-link-encoded-listener',
193  static function (‪BeforeTypoLinkEncodedEvent $event) use (&$beforeTypoLinkEncodedEvent) {
194  $beforeTypoLinkEncodedEvent = $event;
195  $beforeTypoLinkEncodedEvent->‪setParameters(['foo', 'bar']);
196  }
197  );
198 
199  $listenerProdiver = GeneralUtility::makeInstance(ListenerProvider::class, $container);
200  $listenerProdiver->addListener(BeforeTypoLinkEncodedEvent::class, 'before-typo-link-encoded-listener');
201 
202  $result = (new TypoLinkCodecService(new ‪EventDispatcher($listenerProdiver)))->encode([
203  'url' => 'https://example.com',
204  ]);
205 
206  self::assertEquals('bar foo', $result);
207  self::assertInstanceOf(BeforeTypoLinkEncodedEvent::class, $beforeTypoLinkEncodedEvent);
208  self::assertEquals(['url' => 'https://example.com'], $beforeTypoLinkEncodedEvent->getTypoLinkParts());
209  self::assertEquals(['foo', 'bar'], $beforeTypoLinkEncodedEvent->getParameters());
210  }
211 
212  #[Test]
213  public function ‪afterTypoLinkDecodedEventIsCalled(): void
214  {
215  $afterTypoLinkDecodedEvent = null;
216 
217  $container = new Container();
218  $container->set(
219  'after-typo-link-decoded-listener',
220  static function (‪AfterTypoLinkDecodedEvent $event) use (&$afterTypoLinkDecodedEvent) {
221  $afterTypoLinkDecodedEvent = $event;
222  $afterTypoLinkDecodedEvent->‪setTypoLinkParts(
223  array_merge($afterTypoLinkDecodedEvent->getTypoLinkParts(), ['foo' => 'bar'])
224  );
225  }
226  );
227 
228  $listenerProdiver = GeneralUtility::makeInstance(ListenerProvider::class, $container);
229  $listenerProdiver->addListener(AfterTypoLinkDecodedEvent::class, 'after-typo-link-decoded-listener');
230 
231  $result = (new TypoLinkCodecService(new ‪EventDispatcher($listenerProdiver)))->decode('https://example.com');
232 
233  $expected = [
234  'url' => 'https://example.com',
235  'target' => '',
236  'class' => '',
237  'title' => '',
238  'additionalParams' => '',
239  'foo' => 'bar',
240  ];
241 
242  self::assertEquals($expected, $result);
243  self::assertInstanceOf(AfterTypoLinkDecodedEvent::class, $afterTypoLinkDecodedEvent);
244  self::assertEquals('https://example.com', $afterTypoLinkDecodedEvent->getTypoLink());
245  self::assertEquals($expected, $afterTypoLinkDecodedEvent->getTypoLinkParts());
246  }
247 }
‪TYPO3\CMS\Core\EventDispatcher\EventDispatcher
Definition: EventDispatcher.php:30
‪TYPO3\CMS\Core\EventDispatcher\NoopEventDispatcher
Definition: NoopEventDispatcher.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\EventDispatcher\ListenerProvider
Definition: ListenerProvider.php:30