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
|
|
|
use Joomla\Form\Field_Checkbox; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Test class for JFormFieldCheckbox. |
14
|
|
|
* |
15
|
|
|
* @since 1.0 |
16
|
|
|
*/ |
17
|
|
|
class JFormFieldCheckboxTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Sets up dependencies for the test. |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
*/ |
26
|
|
|
protected function setUp() |
27
|
|
|
{ |
28
|
|
|
// The real class cannot be autoloaded |
29
|
|
|
\Joomla\Form\FormHelper::loadFieldClass('checkbox'); |
30
|
|
|
|
31
|
|
|
parent::setUp(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Test the getInput method where there is no value from the element |
36
|
|
|
* and no checked attribute. |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
* |
40
|
|
|
* @since 1.0 |
41
|
|
|
*/ |
42
|
|
View Code Duplication |
public function testGetInputNoValueNoChecked() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$formField = \Joomla\Form\FormHelper::loadFieldType('checkbox'); |
|
|
|
|
45
|
|
|
|
46
|
|
|
// Test with no checked element |
47
|
|
|
$element = simplexml_load_string( |
48
|
|
|
'<field name="color" type="checkbox" value="red" />'); |
49
|
|
|
TestHelper::setValue($formField, 'element', $element); |
50
|
|
|
TestHelper::setValue($formField, 'id', 'myTestId'); |
51
|
|
|
TestHelper::setValue($formField, 'name', 'myTestName'); |
52
|
|
|
|
53
|
|
|
$this->assertEquals( |
54
|
|
|
'<input type="checkbox" name="myTestName" id="myTestId" value="red" />', |
55
|
|
|
TestHelper::invoke($formField, 'getInput'), |
56
|
|
|
'The field with no value and no checked attribute did not produce the right html' |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Test the getInput method where there is a value from the element |
62
|
|
|
* and no checked attribute. |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
* |
66
|
|
|
* @since 1.0 |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function testGetInputValueNoChecked() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$formField = \Joomla\Form\FormHelper::loadFieldType('checkbox'); |
|
|
|
|
71
|
|
|
|
72
|
|
|
// Test with no checked element |
73
|
|
|
$element = simplexml_load_string( |
74
|
|
|
'<field name="color" type="checkbox" value="red" />'); |
75
|
|
|
TestHelper::setValue($formField, 'element', $element); |
76
|
|
|
TestHelper::setValue($formField, 'id', 'myTestId'); |
77
|
|
|
TestHelper::setValue($formField, 'name', 'myTestName'); |
78
|
|
|
TestHelper::setValue($formField, 'value', 'red'); |
79
|
|
|
|
80
|
|
|
$this->assertEquals( |
81
|
|
|
'<input type="checkbox" name="myTestName" id="myTestId" value="red" checked="checked" />', |
82
|
|
|
TestHelper::invoke($formField, 'getInput'), |
83
|
|
|
'The field with a value and no checked attribute did not produce the right html' |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Test the getInput method where there is a checked attribute |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
* |
92
|
|
|
* @since 1.0 |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function testGetInputNoValueChecked() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
$formField = \Joomla\Form\FormHelper::loadFieldType('checkbox'); |
|
|
|
|
97
|
|
|
|
98
|
|
|
// Test with checked element |
99
|
|
|
$element = simplexml_load_string( |
100
|
|
|
'<field name="color" type="checkbox" value="red" checked="checked" />'); |
101
|
|
|
TestHelper::setValue($formField, 'element', $element); |
102
|
|
|
TestHelper::setValue($formField, 'id', 'myTestId'); |
103
|
|
|
TestHelper::setValue($formField, 'name', 'myTestName'); |
104
|
|
|
|
105
|
|
|
$this->assertEquals( |
106
|
|
|
'<input type="checkbox" name="myTestName" id="myTestId" value="red" checked="checked" />', |
107
|
|
|
TestHelper::invoke($formField, 'getInput'), |
108
|
|
|
'The field with no value and the checked attribute did not produce the right html' |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Test the getInput method where the field is disabled |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
* |
117
|
|
|
* @since 1.0 |
118
|
|
|
*/ |
119
|
|
View Code Duplication |
public function testGetInputDisabled() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
$formField = \Joomla\Form\FormHelper::loadFieldType('checkbox'); |
|
|
|
|
122
|
|
|
|
123
|
|
|
// Test with checked element |
124
|
|
|
$element = simplexml_load_string( |
125
|
|
|
'<field name="color" type="checkbox" value="red" disabled="true" />'); |
126
|
|
|
TestHelper::setValue($formField, 'element', $element); |
127
|
|
|
TestHelper::setValue($formField, 'id', 'myTestId'); |
128
|
|
|
TestHelper::setValue($formField, 'name', 'myTestName'); |
129
|
|
|
|
130
|
|
|
$this->assertEquals( |
131
|
|
|
'<input type="checkbox" name="myTestName" id="myTestId" value="red" disabled="disabled" />', |
132
|
|
|
TestHelper::invoke($formField, 'getInput'), |
133
|
|
|
'The field set to disabled did not produce the right html' |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
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.