|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (C) 2005 - 2016 Open Source Matters, Inc. All rights reserved. |
|
4
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Joomla\Form\Tests; |
|
8
|
|
|
|
|
9
|
|
|
use Joomla\Test\TestHelper; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Test class for JForm. |
|
13
|
|
|
* |
|
14
|
|
|
* @since 1.0 |
|
15
|
|
|
*/ |
|
16
|
|
|
class JFormFieldCheckboxesTest extends \PHPUnit_Framework_TestCase |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Sets up dependencies for the test. |
|
20
|
|
|
* |
|
21
|
|
|
* @since 1.0 |
|
22
|
|
|
* |
|
23
|
|
|
* @return void |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function setUp() |
|
26
|
|
|
{ |
|
27
|
|
|
// The real class cannot be autoloaded |
|
28
|
|
|
\Joomla\Form\FormHelper::loadFieldClass('checkboxes'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Test the getInput method with no value and no checked attribute. |
|
33
|
|
|
* |
|
34
|
|
|
* @since 1.0 |
|
35
|
|
|
* |
|
36
|
|
|
* @return void |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testGetInputNoValueNoChecked() |
|
39
|
|
|
{ |
|
40
|
|
|
$formFieldCheckboxes = $this->getMock('Joomla\\Form\\Field_Checkboxes', array('getOptions')); |
|
41
|
|
|
|
|
42
|
|
|
$option1 = new \stdClass; |
|
43
|
|
|
$option1->value = 'red'; |
|
44
|
|
|
$option1->text = 'red'; |
|
45
|
|
|
|
|
46
|
|
|
$option2 = new \stdClass; |
|
47
|
|
|
$option2->value = 'blue'; |
|
48
|
|
|
$option2->text = 'blue'; |
|
49
|
|
|
|
|
50
|
|
|
$optionsReturn = array($option1, $option2); |
|
51
|
|
|
$formFieldCheckboxes->expects($this->any()) |
|
52
|
|
|
->method('getOptions') |
|
53
|
|
|
->will($this->returnValue($optionsReturn)); |
|
54
|
|
|
|
|
55
|
|
|
// Test with no value, no checked element |
|
56
|
|
|
$element = simplexml_load_string( |
|
57
|
|
|
'<field name="color" type="checkboxes"> |
|
58
|
|
|
<option value="red">red</option> |
|
59
|
|
|
<option value="blue">blue</option> |
|
60
|
|
|
</field>'); |
|
61
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'element', $element); |
|
62
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'id', 'myTestId'); |
|
63
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'name', 'myTestName'); |
|
64
|
|
|
|
|
65
|
|
|
$expected = '<fieldset id="myTestId" class="checkboxes"><ul>' . |
|
66
|
|
|
'<li><input type="checkbox" id="myTestId0" name="myTestName" value="red"/><label for="myTestId0">red</label></li>' . |
|
67
|
|
|
'<li><input type="checkbox" id="myTestId1" name="myTestName" value="blue"/>' . |
|
68
|
|
|
'<label for="myTestId1">blue</label></li></ul></fieldset>'; |
|
69
|
|
|
|
|
70
|
|
|
$this->assertEquals( |
|
71
|
|
|
$expected, |
|
72
|
|
|
TestHelper::invoke($formFieldCheckboxes, 'getInput'), |
|
73
|
|
|
'The field with no value and no checked values did not produce the right html' |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Test the getInput method with one value selected and no checked attribute. |
|
79
|
|
|
* |
|
80
|
|
|
* @since 1.0 |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
public function testGetInputValueNoChecked() |
|
85
|
|
|
{ |
|
86
|
|
|
$formFieldCheckboxes = $this->getMock('Joomla\\Form\\Field_Checkboxes', array('getOptions')); |
|
87
|
|
|
|
|
88
|
|
|
$option1 = new \stdClass; |
|
89
|
|
|
$option1->value = 'red'; |
|
90
|
|
|
$option1->text = 'red'; |
|
91
|
|
|
|
|
92
|
|
|
$option2 = new \stdClass; |
|
93
|
|
|
$option2->value = 'blue'; |
|
94
|
|
|
$option2->text = 'blue'; |
|
95
|
|
|
|
|
96
|
|
|
$optionsReturn = array($option1, $option2); |
|
97
|
|
|
$formFieldCheckboxes->expects($this->any()) |
|
98
|
|
|
->method('getOptions') |
|
99
|
|
|
->will($this->returnValue($optionsReturn)); |
|
100
|
|
|
|
|
101
|
|
|
// Test with one value checked, no checked element |
|
102
|
|
|
$element = simplexml_load_string( |
|
103
|
|
|
'<field name="color" type="checkboxes"> |
|
104
|
|
|
<option value="red">red</option> |
|
105
|
|
|
<option value="blue">blue</option> |
|
106
|
|
|
</field>'); |
|
107
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'element', $element); |
|
108
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'id', 'myTestId'); |
|
109
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'value', 'red'); |
|
110
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'name', 'myTestName'); |
|
111
|
|
|
|
|
112
|
|
|
$expected = '<fieldset id="myTestId" class="checkboxes"><ul>' . |
|
113
|
|
|
'<li><input type="checkbox" id="myTestId0" name="myTestName" value="red" checked="checked"/>' . |
|
114
|
|
|
'<label for="myTestId0">red</label></li>' . |
|
115
|
|
|
'<li><input type="checkbox" id="myTestId1" name="myTestName" value="blue"/><label for="myTestId1">blue</label>' . |
|
116
|
|
|
'</li></ul></fieldset>'; |
|
117
|
|
|
|
|
118
|
|
|
$this->assertEquals( |
|
119
|
|
|
$expected, |
|
120
|
|
|
TestHelper::invoke($formFieldCheckboxes, 'getInput'), |
|
121
|
|
|
'The field with one value did not produce the right html' |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Test the getInput method with one value that is an array and no checked attribute. |
|
127
|
|
|
* |
|
128
|
|
|
* @since 1.0 |
|
129
|
|
|
* |
|
130
|
|
|
* @return void |
|
131
|
|
|
*/ |
|
132
|
|
View Code Duplication |
public function testGetInputValueArrayNoChecked() |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
$formFieldCheckboxes = $this->getMock('Joomla\\Form\\Field_Checkboxes', array('getOptions')); |
|
135
|
|
|
|
|
136
|
|
|
$option1 = new \stdClass; |
|
137
|
|
|
$option1->value = 'red'; |
|
138
|
|
|
$option1->text = 'red'; |
|
139
|
|
|
|
|
140
|
|
|
$option2 = new \stdClass; |
|
141
|
|
|
$option2->value = 'blue'; |
|
142
|
|
|
$option2->text = 'blue'; |
|
143
|
|
|
|
|
144
|
|
|
$optionsReturn = array($option1, $option2); |
|
145
|
|
|
$formFieldCheckboxes->expects($this->any()) |
|
146
|
|
|
->method('getOptions') |
|
147
|
|
|
->will($this->returnValue($optionsReturn)); |
|
148
|
|
|
|
|
149
|
|
|
// Test with one value checked, no checked element |
|
150
|
|
|
$element = simplexml_load_string( |
|
151
|
|
|
'<field name="color" type="checkboxes"> |
|
152
|
|
|
<option value="red">red</option> |
|
153
|
|
|
<option value="blue">blue</option> |
|
154
|
|
|
</field>'); |
|
155
|
|
|
$valuearray = array('red'); |
|
156
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'element', $element); |
|
157
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'id', 'myTestId'); |
|
158
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'value', $valuearray); |
|
159
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'name', 'myTestName'); |
|
160
|
|
|
|
|
161
|
|
|
$fieldsetString = '<fieldset id="myTestId" class="checkboxes"><ul>' . |
|
162
|
|
|
'<li><input type="checkbox" id="myTestId0" name="myTestName" value="red" checked="checked"/><label for="myTestId0">red</label></li>' . |
|
163
|
|
|
'<li><input type="checkbox" id="myTestId1" name="myTestName" value="blue"/><label for="myTestId1">blue</label></li></ul></fieldset>'; |
|
164
|
|
|
|
|
165
|
|
|
$this->assertEquals( |
|
166
|
|
|
$fieldsetString, |
|
167
|
|
|
TestHelper::invoke($formFieldCheckboxes, 'getInput'), |
|
168
|
|
|
'The field with one value did not produce the right html' |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Test the getInput method with no value and one value in checked. |
|
174
|
|
|
* |
|
175
|
|
|
* @since 1.0 |
|
176
|
|
|
* |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testGetInputNoValueOneChecked() |
|
180
|
|
|
{ |
|
181
|
|
|
$formFieldCheckboxes = $this->getMock('Joomla\\Form\\Field_Checkboxes', array('getOptions')); |
|
182
|
|
|
|
|
183
|
|
|
$option1 = new \stdClass; |
|
184
|
|
|
$option1->value = 'red'; |
|
185
|
|
|
$option1->text = 'red'; |
|
186
|
|
|
|
|
187
|
|
|
$option2 = new \stdClass; |
|
188
|
|
|
$option2->value = 'blue'; |
|
189
|
|
|
$option2->text = 'blue'; |
|
190
|
|
|
|
|
191
|
|
|
$optionsReturn = array($option1, $option2); |
|
192
|
|
|
$formFieldCheckboxes->expects($this->any()) |
|
193
|
|
|
->method('getOptions') |
|
194
|
|
|
->will($this->returnValue($optionsReturn)); |
|
195
|
|
|
|
|
196
|
|
|
// Test with nothing checked, one value in checked element |
|
197
|
|
|
$element = simplexml_load_string( |
|
198
|
|
|
'<field name="color" type="checkboxes" checked="blue"> |
|
199
|
|
|
<option value="red">red</option> |
|
200
|
|
|
<option value="blue">blue</option> |
|
201
|
|
|
</field>'); |
|
202
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'element', $element); |
|
203
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'id', 'myTestId'); |
|
204
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'name', 'myTestName'); |
|
205
|
|
|
|
|
206
|
|
|
$expected = '<fieldset id="myTestId" class="checkboxes"><ul>' . |
|
207
|
|
|
'<li><input type="checkbox" id="myTestId0" name="myTestName" value="red"/><label for="myTestId0">red</label></li>' . |
|
208
|
|
|
'<li><input type="checkbox" id="myTestId1" name="myTestName" value="blue" checked="checked"/>' . |
|
209
|
|
|
'<label for="myTestId1">blue</label></li></ul></fieldset>'; |
|
210
|
|
|
|
|
211
|
|
|
$this->assertEquals( |
|
212
|
|
|
$expected, |
|
213
|
|
|
TestHelper::invoke($formFieldCheckboxes, 'getInput'), |
|
214
|
|
|
'The field with no values and one value in the checked element did not produce the right html' |
|
215
|
|
|
); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Test the getInput method with no value and two values in the checked element. |
|
220
|
|
|
* |
|
221
|
|
|
* @since 1.0 |
|
222
|
|
|
* |
|
223
|
|
|
* @return void |
|
224
|
|
|
*/ |
|
225
|
|
View Code Duplication |
public function testGetInputNoValueTwoChecked() |
|
|
|
|
|
|
226
|
|
|
{ |
|
227
|
|
|
$formFieldCheckboxes = $this->getMock('Joomla\\Form\\Field_Checkboxes', array('getOptions')); |
|
228
|
|
|
|
|
229
|
|
|
$option1 = new \stdClass; |
|
230
|
|
|
$option1->value = 'red'; |
|
231
|
|
|
$option1->text = 'red'; |
|
232
|
|
|
|
|
233
|
|
|
$option2 = new \stdClass; |
|
234
|
|
|
$option2->value = 'blue'; |
|
235
|
|
|
$option2->text = 'blue'; |
|
236
|
|
|
|
|
237
|
|
|
$optionsReturn = array($option1, $option2); |
|
238
|
|
|
$formFieldCheckboxes->expects($this->any()) |
|
239
|
|
|
->method('getOptions') |
|
240
|
|
|
->will($this->returnValue($optionsReturn)); |
|
241
|
|
|
|
|
242
|
|
|
// Test with nothing checked, two values in checked element |
|
243
|
|
|
$element = simplexml_load_string( |
|
244
|
|
|
'<field name="color" type="checkboxes" checked="red,blue"> |
|
245
|
|
|
<option value="red">red</option> |
|
246
|
|
|
<option value="blue">blue</option> |
|
247
|
|
|
</field>'); |
|
248
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'element', $element); |
|
249
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'id', 'myTestId'); |
|
250
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'name', 'myTestName'); |
|
251
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'value', '""'); |
|
252
|
|
|
|
|
253
|
|
|
$expected = '<fieldset id="myTestId" class="checkboxes"><ul>' . |
|
254
|
|
|
'<li><input type="checkbox" id="myTestId0" name="myTestName" value="red"/><label for="myTestId0">red</label></li>' . |
|
255
|
|
|
'<li><input type="checkbox" id="myTestId1" name="myTestName" value="blue"/><label for="myTestId1">blue</label>' . |
|
256
|
|
|
'</li></ul></fieldset>'; |
|
257
|
|
|
|
|
258
|
|
|
$this->assertEquals( |
|
259
|
|
|
$expected, |
|
260
|
|
|
TestHelper::invoke($formFieldCheckboxes, 'getInput'), |
|
261
|
|
|
'The field with no values and two items in the checked element did not produce the right html' |
|
262
|
|
|
); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Test the getInput method with one value and a different checked value. |
|
267
|
|
|
* |
|
268
|
|
|
* @since 1.0 |
|
269
|
|
|
* |
|
270
|
|
|
* @return void |
|
271
|
|
|
*/ |
|
272
|
|
View Code Duplication |
public function testGetInputValueChecked() |
|
|
|
|
|
|
273
|
|
|
{ |
|
274
|
|
|
$formFieldCheckboxes = $this->getMock('Joomla\\Form\\Field_Checkboxes', array('getOptions')); |
|
275
|
|
|
|
|
276
|
|
|
$option1 = new \stdClass; |
|
277
|
|
|
$option1->value = 'red'; |
|
278
|
|
|
$option1->text = 'red'; |
|
279
|
|
|
|
|
280
|
|
|
$option2 = new \stdClass; |
|
281
|
|
|
$option2->value = 'blue'; |
|
282
|
|
|
$option2->text = 'blue'; |
|
283
|
|
|
|
|
284
|
|
|
$optionsReturn = array($option1, $option2); |
|
285
|
|
|
$formFieldCheckboxes->expects($this->any()) |
|
286
|
|
|
->method('getOptions') |
|
287
|
|
|
->will($this->returnValue($optionsReturn)); |
|
288
|
|
|
|
|
289
|
|
|
// Test with one item checked, a different value in checked element |
|
290
|
|
|
$element = simplexml_load_string( |
|
291
|
|
|
'<field name="color" type="checkboxes" checked="blue"> |
|
292
|
|
|
<option value="red">red</option> |
|
293
|
|
|
<option value="blue">blue</option> |
|
294
|
|
|
</field>'); |
|
295
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'element', $element); |
|
296
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'id', 'myTestId'); |
|
297
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'value', 'red'); |
|
298
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'name', 'myTestName'); |
|
299
|
|
|
|
|
300
|
|
|
$expected = '<fieldset id="myTestId" class="checkboxes"><ul><li>' . |
|
301
|
|
|
'<input type="checkbox" id="myTestId0" name="myTestName" value="red" checked="checked"/>' . |
|
302
|
|
|
'<label for="myTestId0">red</label></li><li><input type="checkbox" id="myTestId1" name="myTestName" value="blue"/>' . |
|
303
|
|
|
'<label for="myTestId1">blue</label></li></ul></fieldset>'; |
|
304
|
|
|
|
|
305
|
|
|
$this->assertEquals( |
|
306
|
|
|
$expected, |
|
307
|
|
|
TestHelper::invoke($formFieldCheckboxes, 'getInput'), |
|
308
|
|
|
'The field with one value and a different value in the checked element did not produce the right html' |
|
309
|
|
|
); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Test the getInput method with multiple values, no checked. |
|
314
|
|
|
* |
|
315
|
|
|
* @since 1.0 |
|
316
|
|
|
* |
|
317
|
|
|
* @return void |
|
318
|
|
|
*/ |
|
319
|
|
View Code Duplication |
public function testGetInputValuesNoChecked() |
|
|
|
|
|
|
320
|
|
|
{ |
|
321
|
|
|
$formFieldCheckboxes = $this->getMock('Joomla\\Form\\Field_Checkboxes', array('getOptions')); |
|
322
|
|
|
|
|
323
|
|
|
$option1 = new \stdClass; |
|
324
|
|
|
$option1->value = 'red'; |
|
325
|
|
|
$option1->text = 'red'; |
|
326
|
|
|
|
|
327
|
|
|
$option2 = new \stdClass; |
|
328
|
|
|
$option2->value = 'blue'; |
|
329
|
|
|
$option2->text = 'blue'; |
|
330
|
|
|
|
|
331
|
|
|
$optionsReturn = array($option1, $option2); |
|
332
|
|
|
$formFieldCheckboxes->expects($this->any()) |
|
333
|
|
|
->method('getOptions') |
|
334
|
|
|
->will($this->returnValue($optionsReturn)); |
|
335
|
|
|
|
|
336
|
|
|
// Test with two values checked, no checked element |
|
337
|
|
|
$element = simplexml_load_string( |
|
338
|
|
|
'<field name="color" type="checkboxes"> |
|
339
|
|
|
<option value="red">red</option> |
|
340
|
|
|
<option value="blue">blue</option> |
|
341
|
|
|
</field>'); |
|
342
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'element', $element); |
|
343
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'id', 'myTestId'); |
|
344
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'value', 'yellow,green'); |
|
345
|
|
|
TestHelper::setValue($formFieldCheckboxes, 'name', 'myTestName'); |
|
346
|
|
|
|
|
347
|
|
|
$expected = '<fieldset id="myTestId" class="checkboxes"><ul><li>' . |
|
348
|
|
|
'<input type="checkbox" id="myTestId0" name="myTestName" value="red"/><label for="myTestId0">red</label></li><li>' . |
|
349
|
|
|
'<input type="checkbox" id="myTestId1" name="myTestName" value="blue"/><label for="myTestId1">blue</label></li></ul></fieldset>'; |
|
350
|
|
|
|
|
351
|
|
|
$this->assertEquals( |
|
352
|
|
|
$expected, |
|
353
|
|
|
TestHelper::invoke($formFieldCheckboxes, 'getInput'), |
|
354
|
|
|
'The field with two values did not produce the right html' |
|
355
|
|
|
); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* Test the getOptions method. |
|
360
|
|
|
* |
|
361
|
|
|
* @since 1.0 |
|
362
|
|
|
* |
|
363
|
|
|
* @return void |
|
364
|
|
|
*/ |
|
365
|
|
|
public function testGetOptions() |
|
366
|
|
|
{ |
|
367
|
|
|
$formFieldCheckboxes = \Joomla\Form\FormHelper::loadFieldType('checkboxes'); |
|
|
|
|
|
|
368
|
|
|
|
|
369
|
|
|
$option1 = new \stdClass; |
|
370
|
|
|
$option1->value = 'yellow'; |
|
371
|
|
|
$option1->text = 'yellow'; |
|
372
|
|
|
$option1->disable = false; |
|
373
|
|
|
$option1->class = ''; |
|
374
|
|
|
$option1->onclick = ''; |
|
375
|
|
|
|
|
376
|
|
|
$option2 = new \stdClass; |
|
377
|
|
|
$option2->value = 'green'; |
|
378
|
|
|
$option2->text = 'green'; |
|
379
|
|
|
$option2->disable = false; |
|
380
|
|
|
$option2->class = ''; |
|
381
|
|
|
$option2->onclick = ''; |
|
382
|
|
|
|
|
383
|
|
|
$optionsExpected = array($option1, $option2); |
|
384
|
|
|
|
|
385
|
|
|
// Test with two values checked, no checked element |
|
386
|
|
|
TestHelper::setValue( |
|
387
|
|
|
$formFieldCheckboxes, 'element', simplexml_load_string( |
|
388
|
|
|
'<field name="color" type="checkboxes"> |
|
389
|
|
|
<option value="yellow">yellow</option> |
|
390
|
|
|
<option value="green">green</option> |
|
391
|
|
|
</field>') |
|
392
|
|
|
); |
|
393
|
|
|
|
|
394
|
|
|
$this->assertEquals( |
|
395
|
|
|
$optionsExpected, |
|
396
|
|
|
TestHelper::invoke($formFieldCheckboxes, 'getOptions'), |
|
397
|
|
|
'The field with two values did not produce the right options' |
|
398
|
|
|
); |
|
399
|
|
|
} |
|
400
|
|
|
} |
|
401
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.