df4a421cac5a1995fe4412323a71b75a564e79d5
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Link / TypolinkViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Link;
3
4 /* *
5 * This script is part of the TYPO3 project - inspiring people to share! *
6 * *
7 * TYPO3 is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License version 2 as published by *
9 * the Free Software Foundation. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14 * Public License for more details. *
15 * */
16
17 use TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\ViewHelperBaseTestcase;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Class TypolinkViewHelperTest
22 */
23 class TypolinkViewHelperTest extends ViewHelperBaseTestcase {
24
25 /**
26 * @test
27 */
28 public function renderCallsRenderChildrenOnce() {
29 /** @var \TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject $subject */
30 $subject = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper::class, array('renderChildren'));
31 $subject->expects($this->once())->method('renderChildren');
32 $subject->render('');
33 }
34
35 /**
36 * @test
37 */
38 public function renderReturnsContentOfRenderChildrenIfNoLinkParameterIsGiven() {
39 /** @var \TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject $subject */
40 $subject = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper::class, array('renderChildren'));
41 $subject->expects($this->any())->method('renderChildren')->will($this->returnValue('innerContent'));
42 $this->assertEquals('innerContent', $subject->render(''));
43 }
44
45 /**
46 * @test
47 */
48 public function renderGivesMergedParametersToContentObjectRenderer() {
49 /** @var \TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject $subject */
50 $subject = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper::class, array('renderChildren'));
51 $subject->expects($this->any())->method('renderChildren')->will($this->returnValue('innerContent'));
52 $contentObjectRendererMock = $this->getMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, array(), array(), '', FALSE);
53
54 $params = '19 _blank css-class "testtitle with whitespace" &X=y';
55 $target = '-';
56 $class = 'fluid_class';
57 $title = 'a new title';
58 $additionalParams = '&a=b';
59 $additionalAttributes = array(
60 'value1' => 'param1',
61 'value2' => 'par&am2', // Check htmlspecialchars is applied
62 );
63
64 $expectedParametersToStdWrap = array(
65 'typolink.' => array(
66 'parameter' => '"19" - "css-class fluid_class" "a new title" "&X=y&a=b"',
67 'ATagParams' => 'value1="param1" value2="par&amp;am2"',
68 ),
69 );
70
71 $contentObjectRendererMock->expects($this->once())->method('stdWrap')->with('innerContent', $expectedParametersToStdWrap);
72
73 GeneralUtility::addInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, $contentObjectRendererMock);
74
75 $subject->render($params, $target, $class, $title, $additionalParams, $additionalAttributes);
76 }
77
78 /**
79 * @test
80 */
81 public function renderReturnsResultOfContentObjectRenderer() {
82 /** @var \TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject $subject */
83 $subject = $this->getMock(\TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper::class, array('renderChildren'));
84 $subject->expects($this->any())->method('renderChildren')->will($this->returnValue('innerContent'));
85 $contentObjectRendererMock = $this->getMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, array(), array(), '', FALSE);
86 $contentObjectRendererMock->expects($this->once())->method('stdWrap')->will($this->returnValue('foo'));
87 GeneralUtility::addInstance(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, $contentObjectRendererMock);
88 $this->assertEquals('foo', $subject->render('42'));
89 }
90
91 /**
92 * @return array
93 */
94 public function typoScriptConfigurationData() {
95 return array(
96 'empty input' => array(
97 '', // input from link field
98 '', // target from fluid
99 '', // class from fluid
100 '', // title from fluid
101 '', // additional parameters from fluid
102 array(),
103 ),
104 'simple id input' => array(
105 19,
106 '',
107 '',
108 '',
109 '',
110 array(
111 0 => '"19"',
112 ),
113 ),
114 'external url with target' => array(
115 'www.web.de _blank',
116 '',
117 '',
118 '',
119 '',
120 array(
121 0 => '"www.web.de"',
122 1 => '"_blank"',
123 ),
124 ),
125 'page with class' => array(
126 '42 - css-class',
127 '',
128 '',
129 '',
130 '',
131 array(
132 0 => '"42"',
133 1 => '-',
134 2 => '"css-class"',
135 ),
136 ),
137 'page with extended class' => array(
138 '42 - css-class',
139 '',
140 'fluid_class',
141 '',
142 '',
143 array(
144 0 => '"42"',
145 1 => '-',
146 2 => '"css-class fluid_class"',
147 ),
148 ),
149 'page with title' => array(
150 '42 - - "a link title"',
151 '',
152 '',
153 '',
154 '',
155 array(
156 0 => '"42"',
157 1 => '-',
158 2 => '-',
159 3 => '"a link title"'
160 )
161 ),
162 'page with overridden title' => array(
163 '42 - - "a link title"',
164 '',
165 '',
166 'another link title',
167 '',
168 array(
169 0 => '"42"',
170 1 => '-',
171 2 => '-',
172 3 => '"another link title"',
173 ),
174 ),
175 'page with title and parameters' => array(
176 '42 - - "a link title" &x=y',
177 '',
178 '',
179 '',
180 '',
181 array(
182 0 => '"42"',
183 1 => '-',
184 2 => '-',
185 3 => '"a link title"',
186 4 => '"&x=y"',
187 ),
188 ),
189 'page with title and extended parameters' => array(
190 '42 - - "a link title" &x=y',
191 '',
192 '',
193 '',
194 '&a=b',
195 array(
196 0 => '"42"',
197 1 => '-',
198 2 => '-',
199 3 => '"a link title"',
200 4 => '"&x=y&a=b"',
201 ),
202 ),
203 'full parameter usage' => array(
204 '19 _blank css-class "testtitle with whitespace" &X=y',
205 '-',
206 'fluid_class',
207 'a new title',
208 '&a=b',
209 array(
210 0 => '"19"',
211 1 => '-',
212 2 => '"css-class fluid_class"',
213 3 => '"a new title"',
214 4 => '"&X=y&a=b"',
215 ),
216 ),
217 'only page id and overwrite' => array(
218 '42',
219 '',
220 '',
221 '',
222 '&a=b',
223 array(
224 0 => '"42"',
225 1 => '-',
226 2 => '-',
227 3 => '-',
228 4 => '"&a=b"',
229 ),
230 ),
231 'email' => array(
232 'a@b.tld',
233 '',
234 '',
235 '',
236 '',
237 array(
238 '"a@b.tld"',
239 ),
240 ),
241 );
242 }
243
244 /**
245 * @test
246 * @dataProvider typoScriptConfigurationData
247 */
248 public function createTypolinkParameterArrayFromArgumentsReturnsExpectedArray($input, $targetFromFluid, $classFromFluid, $titleFromFluid, $additionalParametersFromFluid, $expected) {
249 /** @var \TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface $subject */
250 $subject = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper::class, array('dummy'));
251 $result = $subject->_call('createTypolinkParameterArrayFromArguments', $input, $targetFromFluid, $classFromFluid, $titleFromFluid, $additionalParametersFromFluid);
252 $this->assertSame($expected, $result);
253 }
254
255 }