[FEATURE] Add TypoLinkCodec
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Unit / Service / TypoLinkCodecServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Tests\Unit\Service;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Tests\UnitTestCase;
18 use TYPO3\CMS\Frontend\Service\TypoLinkCodecService;
19
20 /**
21 * Test case
22 */
23 class TypoLinkCodecServiceTest extends UnitTestCase {
24
25 /**
26 * @var TypoLinkCodecService
27 */
28 protected $subject;
29
30 /**
31 * Set up test subject
32 */
33 protected function setUp() {
34 $this->subject = new TypoLinkCodecService();
35 }
36
37 /**
38 * @test
39 * @dataProvider encodeReturnsExpectedResultDataProvider
40 * @param array $parts
41 * @param string$expected
42 */
43 public function encodeReturnsExpectedResult(array $parts, $expected) {
44 $this->assertSame($expected, $this->subject->encode($parts));
45 }
46
47 /**
48 * @return array
49 */
50 public function encodeReturnsExpectedResultDataProvider() {
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 /**
83 * @test
84 * @dataProvider decodeReturnsExpectedResultDataProvider
85 * @param string $typoLink
86 * @param array $expected
87 */
88 public function decodeReturnsExpectedResult($typoLink, array $expected) {
89 $this->assertSame($expected, $this->subject->decode($typoLink));
90 }
91
92 /**
93 * @return array
94 */
95 public function decodeReturnsExpectedResultDataProvider() {
96 return [
97 'empty input' => [
98 '',
99 [
100 'url' => '',
101 'target' => '',
102 'class' => '',
103 'title' => '',
104 'additionalParams' => ''
105 ],
106 ],
107 'simple id input' => [
108 '19',
109 [
110 'url' => '19',
111 'target' => '',
112 'class' => '',
113 'title' => '',
114 'additionalParams' => ''
115 ],
116 ],
117 'external url with target' => [
118 'www.web.de _blank',
119 [
120 'url' => 'www.web.de',
121 'target' => '_blank',
122 'class' => '',
123 'title' => '',
124 'additionalParams' => ''
125 ],
126 ],
127 'page with class' => [
128 '42 - css-class',
129 [
130 'url' => '42',
131 'target' => '',
132 'class' => 'css-class',
133 'title' => '',
134 'additionalParams' => ''
135 ],
136 ],
137 'page with title' => [
138 '42 - - "a link title"',
139 [
140 'url' => '42',
141 'target' => '',
142 'class' => '',
143 'title' => 'a link title',
144 'additionalParams' => ''
145 ],
146 ],
147 'page with crazy title' => [
148 '42 - - "a \\"link\\\\ ti\\\\\\"tle"',
149 [
150 'url' => '42',
151 'target' => '',
152 'class' => '',
153 'title' => 'a "link\\ ti\\"tle',
154 'additionalParams' => ''
155 ],
156 ],
157 'page with title and parameters' => [
158 '42 - - "a link title" &x=y',
159 [
160 'url' => '42',
161 'target' => '',
162 'class' => '',
163 'title' => 'a link title',
164 'additionalParams' => '&x=y'
165 ],
166 ],
167 'page with complex title' => [
168 '42 - - "a \\"link\\" title with \\\\" &x=y',
169 [
170 'url' => '42',
171 'target' => '',
172 'class' => '',
173 'title' => 'a "link" title with \\',
174 'additionalParams' => '&x=y'
175 ],
176 ],
177 'full parameter usage' => [
178 '19 _blank css-class "testtitle with whitespace" &X=y',
179 [
180 'url' => '19',
181 'target' => '_blank',
182 'class' => 'css-class',
183 'title' => 'testtitle with whitespace',
184 'additionalParams' => '&X=y'
185 ],
186 ],
187 ];
188 }
189
190 }