‪TYPO3CMS  10.4
CheckboxViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 
25 {
26 
30  protected ‪$viewHelper;
31 
32  protected function ‪setUp(): void
33  {
34  parent::setUp();
35  $this->viewHelper = new ‪CheckboxViewHelper();
36  $this->‪injectDependenciesIntoViewHelper($this->viewHelper);
37  }
38 
43  {
44  $this->setArgumentsUnderTest(
45  $this->viewHelper,
46  [
47  'name' => 'foo',
48  ]
49  );
50  $expectedResult = '<input type="hidden" name="foo" value="" /><input type="checkbox" name="foo" value="" />';
51  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
52  self::assertEquals($expectedResult, $actualResult);
53  }
54 
59  {
60  $this->setArgumentsUnderTest(
61  $this->viewHelper,
62  [
63  'name' => 'foo',
64  'checked' => true,
65  ]
66  );
67  $expectedResult = '<input type="hidden" name="foo" value="" /><input type="checkbox" name="foo" value="" checked="checked" />';
68  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
69  self::assertEquals($expectedResult, $actualResult);
70  }
71 
78  {
79  $this->setArgumentsUnderTest(
80  $this->viewHelper,
81  [
82  'value' => 'foo',
83  'property' => 'someProperty',
84  'checked' => true
85  ]
86  );
87 
88  $formObject = new \stdClass();
89  $formObject->someProperty = false;
90 
91  $this->‪stubVariableContainer($formObject);
93 
94  $result = $this->viewHelper->initializeArgumentsAndRender();
95 
96  self::assertStringContainsString('<input type="hidden" name="fieldPrefix[objectName][someProperty]" value="" />', $result);
97  self::assertStringContainsString('<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" checked="checked" />', $result);
98  }
99 
106  {
107  $this->setArgumentsUnderTest(
108  $this->viewHelper,
109  [
110  'value' => 'foo',
111  'property' => 'someProperty',
112  ]
113  );
114 
115  $formObject = new \stdClass();
116  $formObject->someProperty = true;
117 
118  $this->‪stubVariableContainer($formObject);
120 
121  $result = $this->viewHelper->initializeArgumentsAndRender();
122 
123  self::assertStringContainsString('<input type="hidden" name="fieldPrefix[objectName][someProperty]" value="" />', $result);
124  self::assertStringContainsString(
125  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" checked="checked" />',
126  $result
127  );
128  }
129 
134  {
135  $this->setArgumentsUnderTest(
136  $this->viewHelper,
137  [
138  'value' => 'foo',
139  'property' => 'someProperty',
140  ]
141  );
142 
143  $formObject = new \stdClass();
144  $formObject->someProperty = [];
145 
146  $this->‪stubVariableContainer($formObject);
148 
149  $result = $this->viewHelper->initializeArgumentsAndRender();
150 
151  self::assertStringContainsString(
152  '<input type="checkbox" name="fieldPrefix[objectName][someProperty][]" value="foo" />',
153  $result
154  );
155  }
156 
161  {
162  $this->setArgumentsUnderTest(
163  $this->viewHelper,
164  [
165  'value' => 'foo',
166  'property' => 'someProperty',
167  'checked' => true
168  ]
169  );
170 
171  $formObject = new \stdClass();
172  $formObject->someProperty = ['foo'];
173 
174  $this->‪stubVariableContainer($formObject);
176 
177  $result = $this->viewHelper->initializeArgumentsAndRender();
178 
179  self::assertStringContainsString(
180  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" checked="checked" />',
181  $result
182  );
183  }
184 
189  {
190  $this->setArgumentsUnderTest(
191  $this->viewHelper,
192  [
193  'value' => 'bar',
194  'property' => 'someProperty',
195  'checked' => true
196  ]
197  );
198 
199  $formObject = new \ArrayObject(['foo', 'bar', 'baz']);
200 
201  $this->‪stubVariableContainer($formObject);
203 
204  $result = $this->viewHelper->initializeArgumentsAndRender();
205 
206  self::assertStringContainsString(
207  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="bar" checked="checked" />',
208  $result
209  );
210  }
211 
216  {
217  $this->setArgumentsUnderTest(
218  $this->viewHelper,
219  [
220  'value' => 'foo',
221  'property' => 'someProperty',
222  ]
223  );
224 
225  $formObject = new \stdClass();
226  $formObject->someProperty = 'bar';
227 
228  $this->‪stubVariableContainer($formObject);
230 
231  $result = $this->viewHelper->initializeArgumentsAndRender();
232 
233  self::assertStringContainsString(
234  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" checked="checked" />',
235  $result
236  );
237  }
238 
243  {
244  $this->setArgumentsUnderTest(
245  $this->viewHelper,
246  [
247  'value' => 'foo',
248  'property' => 'someProperty',
249  ]
250  );
251 
252  $formObject = new \stdClass();
253  $formObject->someProperty = null;
254 
255  $this->‪stubVariableContainer($formObject);
257 
258  $result = $this->viewHelper->initializeArgumentsAndRender();
259 
260  self::assertStringContainsString(
261  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" />',
262  $result
263  );
264  }
265 
270  {
271  $this->setArgumentsUnderTest(
272  $this->viewHelper,
273  [
274  'value' => 2,
275  'property' => 'someProperty',
276  ]
277  );
278 
279  $property1 = new \stdClass();
280  $property2 = new \stdClass();
281  $property3 = new \stdClass();
282 
283  $persistenceManager = $this->prophesize(PersistenceManager::class);
284  $persistenceManager->getIdentifierByObject($property1)->willReturn(1);
285  $persistenceManager->getIdentifierByObject($property2)->willReturn(2);
286  $persistenceManager->getIdentifierByObject($property3)->willReturn(3);
287  $this->viewHelper->injectPersistenceManager($persistenceManager->reveal());
288 
289  $formObject = new \stdClass();
290  $formObject->someProperty = [$property1, $property2, $property3];
291 
292  $this->‪stubVariableContainer($formObject);
294 
295  $result = $this->viewHelper->initializeArgumentsAndRender();
296 
297  self::assertStringContainsString(
298  '<input type="checkbox" name="fieldPrefix[objectName][someProperty][]" value="2" />',
299  $result
300  );
301  }
302 
306  public function ‪renderCallSetsErrorClassAttribute()
307  {
308  $this->setArgumentsUnderTest(
309  $this->viewHelper,
310  [
311  'value' => 'foo',
312  'property' => 'someProperty',
313  'errorClass' => 'error',
314  ]
315  );
316 
317  $formObject = new \stdClass();
318  $formObject->someProperty = null;
319 
320  $this->‪stubVariableContainer($formObject);
322 
323  $result = $this->viewHelper->initializeArgumentsAndRender();
324 
325  self::assertStringContainsString(
326  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" class="error" />',
327  $result
328  );
329  }
330 
335  {
336  $this->setArgumentsUnderTest(
337  $this->viewHelper,
338  [
339  'value' => 'foo',
340  'property' => 'someProperty',
341  ]
342  );
343 
344  $formObject = new \stdClass();
345  $formObject->someProperty = null;
346 
347  $this->‪stubVariableContainer($formObject);
349 
350  $result = $this->viewHelper->initializeArgumentsAndRender();
351 
352  self::assertStringContainsString(
353  '<input type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" class="f3-form-error" />',
354  $result
355  );
356  }
357 
362  {
363  $this->setArgumentsUnderTest(
364  $this->viewHelper,
365  [
366  'value' => 'foo',
367  'property' => 'someProperty',
368  'class' => 'css_class'
369  ]
370  );
371 
372  $formObject = new \stdClass();
373  $formObject->someProperty = null;
374 
375  $this->‪stubVariableContainer($formObject);
377 
378  $result = $this->viewHelper->initializeArgumentsAndRender();
379 
380  self::assertStringContainsString(
381  '<input class="css_class f3-form-error" type="checkbox" name="fieldPrefix[objectName][someProperty]" value="foo" />',
382  $result
383  );
384  }
385 }
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderSetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeArray
‪renderSetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeArray()
Definition: CheckboxViewHelperTest.php:159
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\$viewHelper
‪CheckboxViewHelper $viewHelper
Definition: CheckboxViewHelperTest.php:29
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\setUp
‪setUp()
Definition: CheckboxViewHelperTest.php:31
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase\injectDependenciesIntoViewHelper
‪injectDependenciesIntoViewHelper(ViewHelperInterface $viewHelper)
Definition: FormFieldViewHelperBaseTestcase.php:42
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderSetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeBoolean
‪renderSetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeBoolean()
Definition: CheckboxViewHelperTest.php:104
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase
Definition: FormFieldViewHelperBaseTestcase.php:28
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderCallSetsErrorClassAttribute
‪renderCallSetsErrorClassAttribute()
Definition: CheckboxViewHelperTest.php:305
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest
Definition: CheckboxViewHelperTest.php:25
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderDoesNotSetsCheckedAttributeIfBoundPropertyIsNull
‪renderDoesNotSetsCheckedAttributeIfBoundPropertyIsNull()
Definition: CheckboxViewHelperTest.php:241
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderSetsCheckedAttributeForListOfObjects
‪renderSetsCheckedAttributeForListOfObjects()
Definition: CheckboxViewHelperTest.php:268
‪TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
Definition: PersistenceManager.php:29
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderCorrectlySetsTagNameAndDefaultAttributes
‪renderCorrectlySetsTagNameAndDefaultAttributes()
Definition: CheckboxViewHelperTest.php:41
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderIgnoresValueOfBoundPropertyIfCheckedIsSet
‪renderIgnoresValueOfBoundPropertyIfCheckedIsSet()
Definition: CheckboxViewHelperTest.php:76
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderSetsCheckedAttributeIfSpecified
‪renderSetsCheckedAttributeIfSpecified()
Definition: CheckboxViewHelperTest.php:57
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form
Definition: AbstractFormFieldViewHelperTest.php:16
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderSetsCheckedAttributeIfBoundPropertyIsNotNull
‪renderSetsCheckedAttributeIfBoundPropertyIsNotNull()
Definition: CheckboxViewHelperTest.php:214
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderCallExtendsClassAttributeWithErrorClass
‪renderCallExtendsClassAttributeWithErrorClass()
Definition: CheckboxViewHelperTest.php:360
‪TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper
Definition: CheckboxViewHelper.php:62
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase\stubRequestWithMappingErrors
‪stubRequestWithMappingErrors()
Definition: FormFieldViewHelperBaseTestcase.php:65
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase\stubVariableContainer
‪stubVariableContainer($formObject)
Definition: FormFieldViewHelperBaseTestcase.php:81
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderAppendsSquareBracketsToNameAttributeIfBoundToAPropertyOfTypeArray
‪renderAppendsSquareBracketsToNameAttributeIfBoundToAPropertyOfTypeArray()
Definition: CheckboxViewHelperTest.php:132
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase\stubRequestWithoutMappingErrors
‪stubRequestWithoutMappingErrors()
Definition: FormFieldViewHelperBaseTestcase.php:51
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderCallSetsStandardErrorClassAttributeIfNonIsSpecified
‪renderCallSetsStandardErrorClassAttributeIfNonIsSpecified()
Definition: CheckboxViewHelperTest.php:333
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\CheckboxViewHelperTest\renderSetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeArrayObject
‪renderSetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeArrayObject()
Definition: CheckboxViewHelperTest.php:187