Index: branches/salvatoreingala/Gadgets/Gadgets_tests.php |
— | — | @@ -82,12 +82,23 @@ |
83 | 83 | |
84 | 84 | //Test preferences descriptions validator (generic) |
85 | 85 | function testPrefsDescriptions() { |
86 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( null ) ); |
87 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( array() ) ); |
88 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( array( 'fields' => array() ) ) ); |
| 86 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( null ) ); |
| 87 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array() ) ); |
| 88 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( 'fields' => array() ) ) ); |
89 | 89 | |
| 90 | + //Test with stdClass instead if array |
| 91 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( (object)array( |
| 92 | + 'fields' => array( |
| 93 | + 'testBoolean' => array( |
| 94 | + 'type' => 'boolean', |
| 95 | + 'label' => 'foo', |
| 96 | + 'default' => 'bar' |
| 97 | + ) |
| 98 | + ) |
| 99 | + ) ) ); |
| 100 | + |
90 | 101 | //Test with wrong type |
91 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( array( |
| 102 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
92 | 103 | 'fields' => array( |
93 | 104 | 'testUnexisting' => array( |
94 | 105 | 'type' => 'unexistingtype', |
— | — | @@ -98,7 +109,7 @@ |
99 | 110 | ) ) ); |
100 | 111 | |
101 | 112 | //Test with wrong preference name |
102 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( array( |
| 113 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
103 | 114 | 'fields' => array( |
104 | 115 | 'testWrongN@me' => array( |
105 | 116 | 'type' => 'boolean', |
— | — | @@ -109,7 +120,7 @@ |
110 | 121 | ) ) ); |
111 | 122 | |
112 | 123 | //Test with too long preference name (41 characters) |
113 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( array( |
| 124 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
114 | 125 | 'fields' => array( |
115 | 126 | 'aPreferenceNameExceedingTheLimitOf40Chars' => array( |
116 | 127 | 'type' => 'boolean', |
— | — | @@ -120,7 +131,7 @@ |
121 | 132 | ) ) ); |
122 | 133 | |
123 | 134 | //This must pass, instead (40 characters is fine) |
124 | | - $this->assertTrue( Gadget::isPrefsDescriptionValid( array( |
| 135 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( array( |
125 | 136 | 'fields' => array( |
126 | 137 | 'otherPreferenceNameThatS40CharactersLong' => array( |
127 | 138 | 'type' => 'boolean', |
— | — | @@ -132,7 +143,7 @@ |
133 | 144 | |
134 | 145 | |
135 | 146 | //Test with an unexisting field parameter |
136 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( array( |
| 147 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
137 | 148 | 'fields' => array( |
138 | 149 | 'testBoolean' => array( |
139 | 150 | 'type' => 'boolean', |
— | — | @@ -156,7 +167,7 @@ |
157 | 168 | ) |
158 | 169 | ); |
159 | 170 | |
160 | | - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) ); |
| 171 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
161 | 172 | |
162 | 173 | $correct2 = array( |
163 | 174 | 'fields' => array( |
— | — | @@ -168,13 +179,13 @@ |
169 | 180 | ) |
170 | 181 | ); |
171 | 182 | |
172 | | - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) ); |
| 183 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
173 | 184 | |
174 | 185 | //Tests with wrong default values |
175 | 186 | $wrong = $correct; |
176 | 187 | foreach ( array( 0, 1, '', 'false', 'true', null, array() ) as $def ) { |
177 | 188 | $wrong['fields']['testBoolean']['default'] = $def; |
178 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); |
| 189 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
179 | 190 | } |
180 | 191 | } |
181 | 192 | |
— | — | @@ -193,27 +204,27 @@ |
194 | 205 | ) |
195 | 206 | ); |
196 | 207 | |
197 | | - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) ); |
| 208 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
198 | 209 | |
199 | 210 | //Tests with wrong default values |
200 | 211 | $wrong = $correct; |
201 | 212 | foreach ( array( null, true, false, 0, 1, array(), 'short', 'veryverylongstring' ) as $def ) { |
202 | 213 | $wrong['fields']['testString']['default'] = $def; |
203 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); |
| 214 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
204 | 215 | } |
205 | 216 | |
206 | 217 | //Tests with correct default values (when required is false) |
207 | 218 | $correct2 = $correct; |
208 | 219 | foreach ( array( '', '6chars', '1234567890' ) as $def ) { |
209 | 220 | $correct2['fields']['testString']['default'] = $def; |
210 | | - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) ); |
| 221 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
211 | 222 | } |
212 | 223 | |
213 | 224 | //Test with empty default when "required" is true |
214 | 225 | $wrong = $correct; |
215 | 226 | $wrong['fields']['testString']['required'] = true; |
216 | 227 | $wrong['fields']['testString']['default'] = ''; |
217 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); |
| 228 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
218 | 229 | } |
219 | 230 | |
220 | 231 | //Tests for 'number' type preferences |
— | — | @@ -245,27 +256,27 @@ |
246 | 257 | ) |
247 | 258 | ); |
248 | 259 | |
249 | | - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correctFloat ) ); |
250 | | - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correctInt ) ); |
| 260 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correctFloat ) ); |
| 261 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correctInt ) ); |
251 | 262 | |
252 | 263 | //Tests with wrong default values (with 'required' = true) |
253 | 264 | $wrongFloat = $correctFloat; |
254 | 265 | foreach ( array( '', false, true, null, array(), -100, +100 ) as $def ) { |
255 | 266 | $wrongFloat['fields']['testNumber']['default'] = $def; |
256 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrongFloat ) ); |
| 267 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrongFloat ) ); |
257 | 268 | } |
258 | 269 | |
259 | 270 | $wrongInt = $correctInt; |
260 | 271 | foreach ( array( '', false, true, null, array(), -100, +100, 2.7182818 ) as $def ) { |
261 | 272 | $wrongInt['fields']['testNumber']['default'] = $def; |
262 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrongInt ) ); |
| 273 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrongInt ) ); |
263 | 274 | } |
264 | 275 | |
265 | 276 | //If required=false, default=null must be accepted, too |
266 | 277 | foreach ( array( $correctFloat, $correctInt ) as $correct ) { |
267 | 278 | $correct['fields']['testNumber']['required'] = false; |
268 | 279 | $correct['fields']['testNumber']['default'] = null; |
269 | | - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) ); |
| 280 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
270 | 281 | } |
271 | 282 | } |
272 | 283 | |
— | — | @@ -292,73 +303,82 @@ |
293 | 304 | $correct2 = $correct; |
294 | 305 | foreach ( array( null, true, 3, 'test' ) as $def ) { |
295 | 306 | $correct2['fields']['testSelect']['default'] = $def; |
296 | | - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) ); |
| 307 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
297 | 308 | } |
298 | 309 | |
299 | 310 | //Tests with wrong default values |
300 | 311 | $wrong = $correct; |
301 | 312 | foreach ( array( '', 'true', 'null', false, array(), 0, 1, 3.0001 ) as $def ) { |
302 | 313 | $wrong['fields']['testSelect']['default'] = $def; |
303 | | - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); |
| 314 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
304 | 315 | } |
305 | 316 | } |
306 | 317 | |
307 | | - //Tests Gadget::setPrefsDescription, Gadget::checkPrefsAgainstDescription, |
308 | | - //Gadget::matchPrefsWithDescription and Gadget::setPrefs. |
309 | | - function testSetPrefs() { |
310 | | - $prefsDescription = array( |
311 | | - 'fields' => array( |
312 | | - 'testBoolean' => array( |
313 | | - 'type' => 'boolean', |
314 | | - 'label' => 'foo', |
315 | | - 'default' => true |
316 | | - ), |
317 | | - 'testBoolean2' => array( |
318 | | - 'type' => 'boolean', |
319 | | - 'label' => 'foo', |
320 | | - 'default' => true |
321 | | - ), |
322 | | - 'testNumber' => array( |
323 | | - 'type' => 'number', |
324 | | - 'label' => 'foo', |
325 | | - 'min' => 2.3, |
326 | | - 'max' => 13.94, |
327 | | - 'default' => 7 |
328 | | - ), |
329 | | - 'testNumber2' => array( |
330 | | - 'type' => 'number', |
331 | | - 'label' => 'foo', |
332 | | - 'min' => 2.3, |
333 | | - 'max' => 13.94, |
334 | | - 'default' => 7 |
335 | | - ), |
336 | | - 'testSelect' => array( |
337 | | - 'type' => 'select', |
338 | | - 'label' => 'foo', |
339 | | - 'default' => 3, |
340 | | - 'options' => array( |
341 | | - 'opt1' => null, |
342 | | - 'opt2' => true, |
343 | | - 'opt3' => 3, |
344 | | - 'opt4' => 'opt4value' |
| 318 | + //Data provider to be able to reuse this preference description for several tests. |
| 319 | + function prefsDescProvider() { |
| 320 | + return array( array( |
| 321 | + array( |
| 322 | + 'fields' => array( |
| 323 | + 'testBoolean' => array( |
| 324 | + 'type' => 'boolean', |
| 325 | + 'label' => '@foo', |
| 326 | + 'default' => true |
| 327 | + ), |
| 328 | + 'testBoolean2' => array( |
| 329 | + 'type' => 'boolean', |
| 330 | + 'label' => '@@foo2', |
| 331 | + 'default' => true |
| 332 | + ), |
| 333 | + 'testNumber' => array( |
| 334 | + 'type' => 'number', |
| 335 | + 'label' => '@foo3', |
| 336 | + 'min' => 2.3, |
| 337 | + 'max' => 13.94, |
| 338 | + 'default' => 7 |
| 339 | + ), |
| 340 | + 'testNumber2' => array( |
| 341 | + 'type' => 'number', |
| 342 | + 'label' => 'foo4', |
| 343 | + 'min' => 2.3, |
| 344 | + 'max' => 13.94, |
| 345 | + 'default' => 7 |
| 346 | + ), |
| 347 | + 'testSelect' => array( |
| 348 | + 'type' => 'select', |
| 349 | + 'label' => 'foo', |
| 350 | + 'default' => 3, |
| 351 | + 'options' => array( |
| 352 | + '@opt1' => null, |
| 353 | + '@opt2' => true, |
| 354 | + 'opt3' => 3, |
| 355 | + '@opt4' => 'opt4value' |
| 356 | + ) |
| 357 | + ), |
| 358 | + 'testSelect2' => array( |
| 359 | + 'type' => 'select', |
| 360 | + 'label' => 'foo', |
| 361 | + 'default' => 3, |
| 362 | + 'options' => array( |
| 363 | + '@opt1' => null, |
| 364 | + 'opt2' => true, |
| 365 | + 'opt3' => 3, |
| 366 | + 'opt4' => 'opt4value' |
| 367 | + ) |
345 | 368 | ) |
346 | | - ), |
347 | | - 'testSelect2' => array( |
348 | | - 'type' => 'select', |
349 | | - 'label' => 'foo', |
350 | | - 'default' => 3, |
351 | | - 'options' => array( |
352 | | - 'opt1' => null, |
353 | | - 'opt2' => true, |
354 | | - 'opt3' => 3, |
355 | | - 'opt4' => 'opt4value' |
356 | | - ) |
357 | 369 | ) |
358 | 370 | ) |
359 | | - ); |
| 371 | + ) ); |
| 372 | + } |
| 373 | + |
| 374 | + /** |
| 375 | + * Tests Gadget::setPrefsDescription, GadgetPrefs::checkPrefsAgainstDescription, |
| 376 | + * GadgetPrefs::matchPrefsWithDescription and Gadget::setPrefs. |
| 377 | + * |
| 378 | + * @dataProvider prefsDescProvider |
| 379 | + */ |
| 380 | + function testSetPrefs( $prefsDescription ) { |
| 381 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $prefsDescription ) ); |
360 | 382 | |
361 | | - $this->assertTrue( Gadget::isPrefsDescriptionValid( $prefsDescription ) ); |
362 | | - |
363 | 383 | $prefs = array( |
364 | 384 | 'testBoolean' => false, |
365 | 385 | 'testBoolean2' => null, //wrong |
— | — | @@ -368,12 +388,12 @@ |
369 | 389 | 'testSelect2' => false //wrong |
370 | 390 | ); |
371 | 391 | |
372 | | - $this->assertFalse( Gadget::checkPrefsAgainstDescription( $prefsDescription, $prefs ) ); |
| 392 | + $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( $prefsDescription, $prefs ) ); |
373 | 393 | |
374 | 394 | $prefs2 = $prefs; |
375 | | - Gadget::matchPrefsWithDescription( $prefsDescription, $prefs2 ); |
| 395 | + GadgetPrefs::matchPrefsWithDescription( $prefsDescription, $prefs2 ); |
376 | 396 | //Now $prefs2 should pass validation |
377 | | - $this->assertTrue( Gadget::checkPrefsAgainstDescription( $prefsDescription, $prefs2 ) ); |
| 397 | + $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( $prefsDescription, $prefs2 ) ); |
378 | 398 | |
379 | 399 | //$prefs2 should have testBoolean, testNumber and testSelect unchanged, the other reset to defaults |
380 | 400 | $this->assertEquals( $prefs2['testBoolean'], $prefs['testBoolean'] ); |
— | — | @@ -413,4 +433,17 @@ |
414 | 434 | //Call with invalid param |
415 | 435 | $g->setPrefs( 'wrongparam' ); |
416 | 436 | } |
| 437 | + |
| 438 | + |
| 439 | + /** |
| 440 | + * Tests GadgetPrefs::getMessages. |
| 441 | + * |
| 442 | + * @dataProvider prefsDescProvider |
| 443 | + */ |
| 444 | + function testGetMessages( $prefsDescription ) { |
| 445 | + $msgs = GadgetPrefs::getMessages( $prefsDescription ); |
| 446 | + $this->assertEquals( $msgs, array( |
| 447 | + 'foo', 'foo3', 'opt1', 'opt2', 'opt4' |
| 448 | + ) ); |
| 449 | + } |
417 | 450 | } |