[BUGFIX] Submitted form data has precedence over value argument
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Form / RadioViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form;
3
4 /* *
5 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License, either version 3 *
9 * of the License, or (at your option) any later version. *
10 * *
11 * The TYPO3 project - inspiring people to share! *
12 * */
13
14 use TYPO3\CMS\Fluid\ViewHelpers\Form\RadioViewHelper;
15
16 /**
17 * Test for the "Radio" Form view helper
18 */
19 class RadioViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase {
20
21 /**
22 * @var \TYPO3\CMS\Fluid\ViewHelpers\Form\RadioViewHelper
23 */
24 protected $viewHelper;
25
26 protected function setUp() {
27 parent::setUp();
28 $this->viewHelper = $this->getAccessibleMock(RadioViewHelper::class, array('setErrorClassAttribute', 'getName', 'getValueAttribute', 'isObjectAccessorMode', 'getPropertyValue', 'registerFieldNameForFormTokenGeneration'));
29 $this->injectDependenciesIntoViewHelper($this->viewHelper);
30 $this->viewHelper->initializeArguments();
31 }
32
33 /**
34 * @test
35 */
36 public function renderCorrectlySetsTagNameAndDefaultAttributes() {
37 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
38 $mockTagBuilder->expects($this->once())->method('setTagName')->with('input');
39 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'radio');
40 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
41 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo');
42 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
43
44 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
45 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
46 $this->viewHelper->_set('tag', $mockTagBuilder);
47
48 $this->viewHelper->initialize();
49 $this->viewHelper->render();
50 }
51
52 /**
53 * @test
54 */
55 public function renderSetsCheckedAttributeIfSpecified() {
56 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
57 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'radio');
58 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
59 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo');
60 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
61 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
62
63 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
64 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
65 $this->viewHelper->_set('tag', $mockTagBuilder);
66
67 $this->viewHelper->initialize();
68 $this->viewHelper->render(TRUE);
69 }
70
71 /**
72 * @test
73 */
74 public function renderIgnoresBoundPropertyIfCheckedIsSet() {
75 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
76 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'radio');
77 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
78 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
79
80 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
81 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
82 $this->viewHelper->expects($this->never())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
83 $this->viewHelper->expects($this->never())->method('getPropertyValue')->will($this->returnValue(TRUE));
84 $this->viewHelper->_set('tag', $mockTagBuilder);
85
86 $this->viewHelper->initialize();
87 $this->viewHelper->render(TRUE);
88 $this->viewHelper->render(FALSE);
89 }
90
91 /**
92 * @test
93 */
94 public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeBoolean() {
95 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
96 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'radio');
97 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
98 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo');
99 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
100 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
101
102 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
103 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
104 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
105 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(TRUE));
106 $this->viewHelper->_set('tag', $mockTagBuilder);
107
108 $this->viewHelper->initialize();
109 $this->viewHelper->render();
110 }
111
112 /**
113 * @test
114 */
115 public function renderDoesNotAppendSquareBracketsToNameAttributeIfBoundToAPropertyOfTypeArray() {
116 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
117 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'radio');
118 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
119 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo');
120 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
121
122 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
123 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
124 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
125 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue(array()));
126 $this->viewHelper->_set('tag', $mockTagBuilder);
127
128 $this->viewHelper->initialize();
129 $this->viewHelper->render();
130 }
131
132 /**
133 * @test
134 */
135 public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeString() {
136 $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
137 $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'radio');
138 $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
139 $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo');
140 $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
141 $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
142
143 $this->viewHelper->expects($this->any())->method('getName')->will($this->returnValue('foo'));
144 $this->viewHelper->expects($this->any())->method('getValueAttribute')->will($this->returnValue('bar'));
145 $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->will($this->returnValue(TRUE));
146 $this->viewHelper->expects($this->any())->method('getPropertyValue')->will($this->returnValue('bar'));
147 $this->viewHelper->_set('tag', $mockTagBuilder);
148
149 $this->viewHelper->initialize();
150 $this->viewHelper->render();
151 }
152
153 /**
154 * @test
155 */
156 public function renderCallsSetErrorClassAttribute() {
157 $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
158 $this->viewHelper->render();
159 }
160
161 }