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\Form\Rule; |
10
|
|
|
use Joomla\Form\FormHelper; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Test class for JForm. |
14
|
|
|
* |
15
|
|
|
* @since 1.0 |
16
|
|
|
*/ |
17
|
|
|
class JFormHelperTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Tests the Form::addFieldPath method. |
21
|
|
|
* |
22
|
|
|
* This method is used to add additional lookup paths for field helpers. |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
View Code Duplication |
public function testAddFieldPath() |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
// Check the default behaviour. |
29
|
|
|
$paths = FormHelper::addFieldPath(); |
|
|
|
|
30
|
|
|
|
31
|
|
|
// The default path is the class file folder/forms |
32
|
|
|
// use of realpath to ensure test works for on all platforms |
33
|
|
|
$valid = dirname(__DIR__) . '/field'; |
34
|
|
|
|
35
|
|
|
$this->assertThat( |
36
|
|
|
in_array($valid, $paths), |
37
|
|
|
$this->isTrue(), |
38
|
|
|
'Line:' . __LINE__ . ' The libraries fields path should be included by default.' |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
// Test adding a custom folder. |
42
|
|
|
FormHelper::addFieldPath(__DIR__); |
|
|
|
|
43
|
|
|
$paths = FormHelper::addFieldPath(); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$this->assertThat( |
46
|
|
|
in_array(__DIR__, $paths), |
47
|
|
|
$this->isTrue(), |
48
|
|
|
'Line:' . __LINE__ . ' An added path should be in the returned array.' |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Tests the Form::addFormPath method. |
54
|
|
|
* |
55
|
|
|
* This method is used to add additional lookup paths for form XML files. |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function testAddFormPath() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
// Check the default behaviour. |
62
|
|
|
$paths = FormHelper::addFormPath(); |
63
|
|
|
|
64
|
|
|
// The default path is the class file folder/forms |
65
|
|
|
// use of realpath to ensure test works for on all platforms |
66
|
|
|
$valid = dirname(__DIR__) . '/form'; |
67
|
|
|
|
68
|
|
|
$this->assertThat( |
69
|
|
|
in_array($valid, $paths), |
70
|
|
|
$this->isTrue(), |
71
|
|
|
'Line:' . __LINE__ . ' The libraries forms path should be included by default.' |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
// Test adding a custom folder. |
75
|
|
|
FormHelper::addFormPath(__DIR__); |
76
|
|
|
$paths = FormHelper::addFormPath(); |
77
|
|
|
|
78
|
|
|
$this->assertThat( |
79
|
|
|
in_array(__DIR__, $paths), |
80
|
|
|
$this->isTrue(), |
81
|
|
|
'Line:' . __LINE__ . ' An added path should be in the returned array.' |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Tests the Form::addRulePath method. |
87
|
|
|
* |
88
|
|
|
* This method is used to add additional lookup paths for form XML files. |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public function testAddRulePath() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
// Check the default behaviour. |
95
|
|
|
$paths = FormHelper::addRulePath(); |
|
|
|
|
96
|
|
|
|
97
|
|
|
// The default path is the class file folder/rules |
98
|
|
|
// use of realpath to ensure test works for on all platforms |
99
|
|
|
$valid = dirname(__DIR__) . '/rule'; |
100
|
|
|
|
101
|
|
|
$this->assertThat( |
102
|
|
|
in_array($valid, $paths), |
103
|
|
|
$this->isTrue(), |
104
|
|
|
'Line:' . __LINE__ . ' The libraries rule path should be included by default.' |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
// Test adding a custom folder. |
108
|
|
|
FormHelper::addRulePath(__DIR__); |
|
|
|
|
109
|
|
|
$paths = FormHelper::addRulePath(); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$this->assertThat( |
112
|
|
|
in_array(__DIR__, $paths), |
113
|
|
|
$this->isTrue(), |
114
|
|
|
'Line:' . __LINE__ . ' An added path should be in the returned array.' |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Test the Form::loadFieldType method. |
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function testLoadFieldType() |
124
|
|
|
{ |
125
|
|
|
$this->assertThat( |
126
|
|
|
FormHelper::loadFieldType('bogus'), |
|
|
|
|
127
|
|
|
$this->isFalse(), |
128
|
|
|
'Line:' . __LINE__ . ' loadFieldType should return false if class not found.' |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$this->assertThat( |
132
|
|
|
(FormHelper::loadFieldType('list') instanceof \Joomla\Form\Field_List), |
|
|
|
|
133
|
|
|
$this->isTrue(), |
134
|
|
|
'Line:' . __LINE__ . ' loadFieldType should return the correct class.' |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
// Add custom path. |
138
|
|
|
FormHelper::addFieldPath(__DIR__ . '/_testfields'); |
|
|
|
|
139
|
|
|
|
140
|
|
|
include_once '_testfields/test.php'; |
141
|
|
|
$this->assertThat( |
142
|
|
|
(FormHelper::loadFieldType('test') instanceof \Joomla\Form\Field_Test), |
|
|
|
|
143
|
|
|
$this->isTrue(), |
144
|
|
|
'Line:' . __LINE__ . ' loadFieldType should return the correct custom class.' |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
include_once '_testfields/bar.php'; |
148
|
|
|
$this->assertThat( |
149
|
|
|
(FormHelper::loadFieldType('foo.bar') instanceof \Foo\Form\Field_Bar), |
|
|
|
|
150
|
|
|
$this->isTrue(), |
151
|
|
|
'Line:' . __LINE__ . ' loadFieldType should return the correct custom class.' |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
include_once '_testfields/modal/foo.php'; |
155
|
|
|
$this->assertThat( |
156
|
|
|
(FormHelper::loadFieldType('modal_foo') instanceof \Joomla\Form\Field_Modal_Foo), |
|
|
|
|
157
|
|
|
$this->isTrue(), |
158
|
|
|
'Line:' . __LINE__ . ' loadFieldType should return the correct custom class.' |
159
|
|
|
); |
160
|
|
|
|
161
|
|
|
include_once '_testfields/modal/bar.php'; |
162
|
|
|
$this->assertThat( |
163
|
|
|
(FormHelper::loadFieldType('foo.modal_bar') instanceof \Foo\Form\Field_Modal_Bar), |
|
|
|
|
164
|
|
|
$this->isTrue(), |
165
|
|
|
'Line:' . __LINE__ . ' loadFieldType should return the correct custom class.' |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Test for Form::loadRuleType method. |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public function testLoadRuleType() |
175
|
|
|
{ |
176
|
|
|
// Test error handling. |
177
|
|
|
|
178
|
|
|
$this->assertThat( |
179
|
|
|
FormHelper::loadRuleType('bogus'), |
|
|
|
|
180
|
|
|
$this->isFalse(), |
181
|
|
|
'Line:' . __LINE__ . ' Loading an unknown rule should return false.' |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
// Test loading a custom rule. |
185
|
|
|
|
186
|
|
|
FormHelper::addRulePath(__DIR__ . '/_testrules'); |
|
|
|
|
187
|
|
|
|
188
|
|
|
$this->assertThat( |
189
|
|
|
(FormHelper::loadRuleType('custom') instanceof Rule), |
|
|
|
|
190
|
|
|
$this->isTrue(), |
191
|
|
|
'Line:' . __LINE__ . ' Loading a known rule should return a rule object.' |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
// Test all the stock rules load. |
195
|
|
|
|
196
|
|
|
$this->assertThat( |
197
|
|
|
(FormHelper::loadRuleType('boolean') instanceof Rule), |
|
|
|
|
198
|
|
|
$this->isTrue(), |
199
|
|
|
'Line:' . __LINE__ . ' Loading the boolean rule should return a rule object.' |
200
|
|
|
); |
201
|
|
|
|
202
|
|
|
$this->assertThat( |
203
|
|
|
(FormHelper::loadRuleType('email') instanceof Rule), |
|
|
|
|
204
|
|
|
$this->isTrue(), |
205
|
|
|
'Line:' . __LINE__ . ' Loading the email rule should return a rule object.' |
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
$this->assertThat( |
209
|
|
|
(FormHelper::loadRuleType('equals') instanceof Rule), |
|
|
|
|
210
|
|
|
$this->isTrue(), |
211
|
|
|
'Line:' . __LINE__ . ' Loading the equals rule should return a rule object.' |
212
|
|
|
); |
213
|
|
|
|
214
|
|
|
$this->assertThat( |
215
|
|
|
(FormHelper::loadRuleType('options') instanceof Rule), |
|
|
|
|
216
|
|
|
$this->isTrue(), |
217
|
|
|
'Line:' . __LINE__ . ' Loading the options rule should return a rule object.' |
218
|
|
|
); |
219
|
|
|
|
220
|
|
|
$this->assertThat( |
221
|
|
|
(FormHelper::loadRuleType('color') instanceof Rule), |
|
|
|
|
222
|
|
|
$this->isTrue(), |
223
|
|
|
'Line:' . __LINE__ . ' Loading the color rule should return a rule object.' |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
$this->assertThat( |
227
|
|
|
(FormHelper::loadRuleType('tel') instanceof Rule), |
|
|
|
|
228
|
|
|
$this->isTrue(), |
229
|
|
|
'Line:' . __LINE__ . ' Loading the tel rule should return a rule object.' |
230
|
|
|
); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
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.