[BUGFIX] EXT:form - parse finisher values only if string
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Domain / Finishers / AbstractFinisherTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Domain\Finishers;
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 Prophecy\Argument;
18 use TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher;
19 use TYPO3\CMS\Form\Domain\Finishers\FinisherContext;
20 use TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
21
22 /**
23 * Test case
24 */
25 class AbstractFinisherTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
26 {
27
28 /**
29 * @test
30 */
31 public function parseOptionReturnsNullIfOptionNameIsTranslation()
32 {
33 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
34 AbstractFinisher::class,
35 [],
36 '',
37 false
38 );
39
40 $this->assertNull($mockAbstractFinisher->_call('parseOption', 'translation'));
41 }
42
43 /**
44 * @test
45 */
46 public function parseOptionReturnsNullIfOptionNameNotExistsWithinOptions()
47 {
48 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
49 AbstractFinisher::class,
50 [],
51 '',
52 false
53 );
54
55 $mockAbstractFinisher->_set('options', []);
56
57 $this->assertNull($mockAbstractFinisher->_call('parseOption', 'foo'));
58 }
59
60 /**
61 * @test
62 */
63 public function parseOptionReturnsNullIfOptionNameNotExistsWithinDefaultOptions()
64 {
65 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
66 AbstractFinisher::class,
67 [],
68 '',
69 false
70 );
71
72 $mockAbstractFinisher->_set('options', []);
73
74 $this->assertNull($mockAbstractFinisher->_call('parseOption', 'foo'));
75 }
76
77 /**
78 * @test
79 */
80 public function parseOptionReturnsArrayOptionValuesAsArray()
81 {
82 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
83 AbstractFinisher::class,
84 [],
85 '',
86 false
87 );
88
89 $mockAbstractFinisher->_set('options', [
90 'foo' => ['bar', 'foobar']
91 ]);
92
93 $expected = ['bar', 'foobar'];
94
95 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'foo'));
96 }
97
98 /**
99 * @test
100 */
101 public function parseOptionReturnsValueFromFormRuntimeIfOptionNameReferenceAFormElementIdentifierWhoseValueIsAString()
102 {
103 $expected = 'element-value';
104 $elementIdentifier = 'element-identifier-1';
105
106 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
107 AbstractFinisher::class,
108 [],
109 '',
110 false
111 );
112
113 $mockAbstractFinisher->_set('options', [
114 'subject' => '{' . $elementIdentifier . '}'
115 ]);
116
117 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
118
119 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
120 $formRuntimeProphecy->offsetExists(Argument::exact($elementIdentifier))->willReturn(true);
121 $formRuntimeProphecy->offsetGet(Argument::exact($elementIdentifier))->willReturn($expected);
122
123 $finisherContextProphecy->getFormRuntime(Argument::cetera())
124 ->willReturn($formRuntimeProphecy->reveal());
125
126 $mockAbstractFinisher->_set('finisherContext', $finisherContextProphecy->reveal());
127
128 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'subject'));
129 }
130
131 /**
132 * @test
133 */
134 public function parseOptionReturnsNoReplacedValueFromFormRuntimeIfOptionNameReferenceAFormElementIdentifierWhoseValueIsNotAString()
135 {
136 $elementIdentifier = 'element-identifier-1';
137 $expected = '{' . $elementIdentifier . '}';
138
139 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
140 AbstractFinisher::class,
141 [],
142 '',
143 false
144 );
145
146 $mockAbstractFinisher->_set('options', [
147 'subject' => '{' . $elementIdentifier . '}'
148 ]);
149
150 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
151
152 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
153 $formRuntimeProphecy->offsetExists(Argument::exact($elementIdentifier))->willReturn(true);
154 $formElementValue = new \DateTime;
155 $formRuntimeProphecy->offsetGet(Argument::exact($elementIdentifier))->willReturn($formElementValue);
156
157 $finisherContextProphecy->getFormRuntime(Argument::cetera())
158 ->willReturn($formRuntimeProphecy->reveal());
159
160 $mockAbstractFinisher->_set('finisherContext', $finisherContextProphecy->reveal());
161
162 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'subject'));
163 }
164
165 /**
166 * @test
167 */
168 public function parseOptionReturnsNoReplacedValueFromFormRuntimeIfOptionNameReferenceANonExistingFormElement()
169 {
170 $elementIdentifier = 'element-identifier-1';
171
172 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
173 AbstractFinisher::class,
174 [],
175 '',
176 false
177 );
178
179 $mockAbstractFinisher->_set('options', [
180 'subject' => '{' . $elementIdentifier . '}'
181 ]);
182
183 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
184
185 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
186 $formRuntimeProphecy->offsetExists(Argument::cetera())->willReturn(true);
187 $formRuntimeProphecy->offsetGet(Argument::cetera())->willReturn(false);
188
189 $finisherContextProphecy->getFormRuntime(Argument::cetera())
190 ->willReturn($formRuntimeProphecy->reveal());
191
192 $mockAbstractFinisher->_set('finisherContext', $finisherContextProphecy->reveal());
193
194 $expected = '{' . $elementIdentifier . '}';
195 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'subject'));
196 }
197
198 /**
199 * @test
200 */
201 public function parseOptionReturnsDefaultOptionValueIfOptionNameNotExistsWithinOptionsButWithinDefaultOptions()
202 {
203 $expected = 'defaultValue';
204
205 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
206 AbstractFinisher::class,
207 [],
208 '',
209 false
210 );
211
212 $mockAbstractFinisher->_set('options', []);
213 $mockAbstractFinisher->_set('defaultOptions', [
214 'subject' => $expected
215 ]);
216
217 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
218
219 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
220 $formRuntimeProphecy->offsetExists(Argument::cetera())->willReturn(true);
221 $formRuntimeProphecy->offsetGet(Argument::cetera())->willReturn(false);
222
223 $finisherContextProphecy->getFormRuntime(Argument::cetera())
224 ->willReturn($formRuntimeProphecy->reveal());
225
226 $mockAbstractFinisher->_set('finisherContext', $finisherContextProphecy->reveal());
227
228 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'subject'));
229 }
230
231 /**
232 * @test
233 */
234 public function parseOptionReturnsDefaultOptionValueIfOptionValueIsAFormElementReferenceAndTheFormElementValueIsEmpty()
235 {
236 $elementIdentifier = 'element-identifier-1';
237 $expected = 'defaultValue';
238
239 $mockAbstractFinisher = $this->getAccessibleMockForAbstractClass(
240 AbstractFinisher::class,
241 [],
242 '',
243 false
244 );
245
246 $mockAbstractFinisher->_set('options', [
247 'subject' => '{' . $elementIdentifier . '}'
248 ]);
249 $mockAbstractFinisher->_set('defaultOptions', [
250 'subject' => $expected
251 ]);
252
253 $finisherContextProphecy = $this->prophesize(FinisherContext::class);
254
255 $formRuntimeProphecy = $this->prophesize(FormRuntime::class);
256 $formRuntimeProphecy->offsetExists(Argument::exact($elementIdentifier))->willReturn(true);
257 $formRuntimeProphecy->offsetGet(Argument::exact($elementIdentifier))->willReturn('');
258
259 $finisherContextProphecy->getFormRuntime(Argument::cetera())
260 ->willReturn($formRuntimeProphecy->reveal());
261
262 $mockAbstractFinisher->_set('finisherContext', $finisherContextProphecy->reveal());
263
264 $this->assertSame($expected, $mockAbstractFinisher->_call('parseOption', 'subject'));
265 }
266 }