98f381858a27b6ac0f9fa9880f43f5730cf6ea1c
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Link / TypolinkViewHelperTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Link;
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
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Fluid\Core\Rendering\RenderingContext;
20 use TYPO3\CMS\Fluid\Tests\Unit\Core\Rendering\RenderingContextFixture;
21 use TYPO3\CMS\Fluid\ViewHelpers\Link\TypolinkViewHelper;
22 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
23 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
24
25 /**
26 * Class TypolinkViewHelperTest
27 */
28 class TypolinkViewHelperTest extends ViewHelperBaseTestcase
29 {
30 /**
31 * @var TypolinkViewHelper|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface
32 */
33 protected $subject;
34
35 /**
36 * @throws \InvalidArgumentException
37 */
38 protected function setUp()
39 {
40 $this->subject = $this->getAccessibleMock(TypolinkViewHelper::class, ['renderChildren']);
41 /** @var RenderingContext $renderingContext */
42 $renderingContext = $this->createMock(RenderingContextFixture::class);
43 $this->subject->setRenderingContext($renderingContext);
44 }
45
46 /**
47 * @test
48 */
49 public function renderReturnsResultOfContentObjectRenderer()
50 {
51 $this->subject->expects($this->any())->method('renderChildren')->will($this->returnValue('innerContent'));
52 $this->subject->setArguments([
53 'parameter' => '42',
54 'target' => '',
55 'class' => '',
56 'title' => '',
57 'additionalParams' => '',
58 'additionalAttributes' => [],
59 ]);
60 $contentObjectRendererMock = $this->createMock(ContentObjectRenderer::class);
61 $contentObjectRendererMock->expects($this->once())->method('stdWrap')->will($this->returnValue('foo'));
62 GeneralUtility::addInstance(ContentObjectRenderer::class, $contentObjectRendererMock);
63 $this->assertEquals('foo', $this->subject->render());
64 }
65
66 /**
67 * @test
68 */
69 public function renderCallsStdWrapWithrightParameters()
70 {
71 $addQueryString = true;
72 $addQueryStringMethod = 'GET,POST';
73 $addQueryStringExclude = 'cHash';
74
75 $this->subject->expects($this->any())->method('renderChildren')->will($this->returnValue('innerContent'));
76 $this->subject->setArguments([
77 'parameter' => '42',
78 'target' => '',
79 'class' => '',
80 'title' => '',
81 'additionalParams' => '',
82 'additionalAttributes' => [],
83 'addQueryString' => $addQueryString,
84 'addQueryStringMethod' => $addQueryStringMethod,
85 'addQueryStringExclude' => $addQueryStringExclude,
86 'absolute' => false
87 ]);
88 $contentObjectRendererMock = $this->createMock(ContentObjectRenderer::class);
89 $contentObjectRendererMock->expects($this->once())
90 ->method('stdWrap')
91 ->with(
92 'innerContent',
93 [
94 'typolink.' => [
95 'parameter' => '42',
96 'ATagParams' => '',
97 'useCacheHash' => false,
98 'addQueryString' => $addQueryString,
99 'addQueryString.' => [
100 'method' => $addQueryStringMethod,
101 'exclude' => $addQueryStringExclude,
102 ],
103 'forceAbsoluteUrl' => false,
104 ],
105 ]
106 )
107 ->will($this->returnValue('foo'));
108 GeneralUtility::addInstance(ContentObjectRenderer::class, $contentObjectRendererMock);
109 $this->assertEquals('foo', $this->subject->render());
110 }
111
112 /**
113 * @return array
114 */
115 public function typoScriptConfigurationData()
116 {
117 return [
118 'empty input' => [
119 '', // input from link field
120 '', // target from fluid
121 '', // class from fluid
122 '', // title from fluid
123 '', // additional parameters from fluid
124 '',
125 ],
126 'simple id input' => [
127 19,
128 '',
129 '',
130 '',
131 '',
132 '19',
133 ],
134 'external url with target' => [
135 'www.web.de _blank',
136 '',
137 '',
138 '',
139 '',
140 'www.web.de _blank',
141 ],
142 'page with extended class' => [
143 '42 - css-class',
144 '',
145 'fluid_class',
146 '',
147 '',
148 '42 - "css-class fluid_class"',
149 ],
150 'classes are unique' => [
151 '42 - css-class',
152 '',
153 'css-class',
154 '',
155 '',
156 '42 - css-class',
157 ],
158 'page with overridden title' => [
159 '42 - - "a link title"',
160 '',
161 '',
162 'another link title',
163 '',
164 '42 - - "another link title"',
165 ],
166 'page with title and extended parameters' => [
167 '42 - - "a link title" &x=y',
168 '',
169 '',
170 '',
171 '&a=b',
172 '42 - - "a link title" &x=y&a=b',
173 ],
174 'page with complex title and extended parameters' => [
175 '42 - - "a \\"link\\" title with \\\\" &x=y',
176 '',
177 '',
178 '',
179 '&a=b',
180 '42 - - "a \\"link\\" title with \\\\" &x=y&a=b',
181 ],
182 'full parameter usage' => [
183 '19 _blank css-class "testtitle with whitespace" &X=y',
184 '-',
185 'fluid_class',
186 'a new title',
187 '&a=b',
188 '19 - "css-class fluid_class" "a new title" &X=y&a=b',
189 ],
190 'only page id and overwrite' => [
191 '42',
192 '',
193 '',
194 '',
195 '&a=b',
196 '42 - - - &a=b',
197 ],
198 ];
199 }
200
201 /**
202 * @test
203 * @dataProvider typoScriptConfigurationData
204 * @param string $input
205 * @param string $targetFromFluid
206 * @param string $classFromFluid
207 * @param string $titleFromFluid
208 * @param string $additionalParametersFromFluid
209 * @param string $expected
210 */
211 public function createTypolinkParameterArrayFromArgumentsReturnsExpectedArray(
212 $input,
213 $targetFromFluid,
214 $classFromFluid,
215 $titleFromFluid,
216 $additionalParametersFromFluid,
217 $expected
218 ) {
219 $result = $this->subject->_call(
220 'createTypolinkParameterArrayFromArguments',
221 $input,
222 $targetFromFluid,
223 $classFromFluid,
224 $titleFromFluid,
225 $additionalParametersFromFluid
226 );
227 $this->assertSame($expected, $result);
228 }
229 }