r90965 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90964‎ | r90965 | r90966 >
Date:14:55, 28 June 2011
Author:salvatoreingala
Status:deferred
Tags:
Comment:
- Fixed tests (broken in r90884)
- Added test for GadgetPrefs::getMessages()
- Added a missing edge case for GadgetPrefs::isPrefsDescriptionValid()
Modified paths:
  • /branches/salvatoreingala/Gadgets/Gadgets_tests.php (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/Gadgets_tests.php
@@ -82,12 +82,23 @@
8383
8484 //Test preferences descriptions validator (generic)
8585 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() ) ) );
8989
 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+
90101 //Test with wrong type
91 - $this->assertFalse( Gadget::isPrefsDescriptionValid( array(
 102+ $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array(
92103 'fields' => array(
93104 'testUnexisting' => array(
94105 'type' => 'unexistingtype',
@@ -98,7 +109,7 @@
99110 ) ) );
100111
101112 //Test with wrong preference name
102 - $this->assertFalse( Gadget::isPrefsDescriptionValid( array(
 113+ $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array(
103114 'fields' => array(
104115 'testWrongN@me' => array(
105116 'type' => 'boolean',
@@ -109,7 +120,7 @@
110121 ) ) );
111122
112123 //Test with too long preference name (41 characters)
113 - $this->assertFalse( Gadget::isPrefsDescriptionValid( array(
 124+ $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array(
114125 'fields' => array(
115126 'aPreferenceNameExceedingTheLimitOf40Chars' => array(
116127 'type' => 'boolean',
@@ -120,7 +131,7 @@
121132 ) ) );
122133
123134 //This must pass, instead (40 characters is fine)
124 - $this->assertTrue( Gadget::isPrefsDescriptionValid( array(
 135+ $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( array(
125136 'fields' => array(
126137 'otherPreferenceNameThatS40CharactersLong' => array(
127138 'type' => 'boolean',
@@ -132,7 +143,7 @@
133144
134145
135146 //Test with an unexisting field parameter
136 - $this->assertFalse( Gadget::isPrefsDescriptionValid( array(
 147+ $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array(
137148 'fields' => array(
138149 'testBoolean' => array(
139150 'type' => 'boolean',
@@ -156,7 +167,7 @@
157168 )
158169 );
159170
160 - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) );
 171+ $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) );
161172
162173 $correct2 = array(
163174 'fields' => array(
@@ -168,13 +179,13 @@
169180 )
170181 );
171182
172 - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) );
 183+ $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) );
173184
174185 //Tests with wrong default values
175186 $wrong = $correct;
176187 foreach ( array( 0, 1, '', 'false', 'true', null, array() ) as $def ) {
177188 $wrong['fields']['testBoolean']['default'] = $def;
178 - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) );
 189+ $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) );
179190 }
180191 }
181192
@@ -193,27 +204,27 @@
194205 )
195206 );
196207
197 - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) );
 208+ $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) );
198209
199210 //Tests with wrong default values
200211 $wrong = $correct;
201212 foreach ( array( null, true, false, 0, 1, array(), 'short', 'veryverylongstring' ) as $def ) {
202213 $wrong['fields']['testString']['default'] = $def;
203 - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) );
 214+ $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) );
204215 }
205216
206217 //Tests with correct default values (when required is false)
207218 $correct2 = $correct;
208219 foreach ( array( '', '6chars', '1234567890' ) as $def ) {
209220 $correct2['fields']['testString']['default'] = $def;
210 - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) );
 221+ $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) );
211222 }
212223
213224 //Test with empty default when "required" is true
214225 $wrong = $correct;
215226 $wrong['fields']['testString']['required'] = true;
216227 $wrong['fields']['testString']['default'] = '';
217 - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) );
 228+ $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) );
218229 }
219230
220231 //Tests for 'number' type preferences
@@ -245,27 +256,27 @@
246257 )
247258 );
248259
249 - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correctFloat ) );
250 - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correctInt ) );
 260+ $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correctFloat ) );
 261+ $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correctInt ) );
251262
252263 //Tests with wrong default values (with 'required' = true)
253264 $wrongFloat = $correctFloat;
254265 foreach ( array( '', false, true, null, array(), -100, +100 ) as $def ) {
255266 $wrongFloat['fields']['testNumber']['default'] = $def;
256 - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrongFloat ) );
 267+ $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrongFloat ) );
257268 }
258269
259270 $wrongInt = $correctInt;
260271 foreach ( array( '', false, true, null, array(), -100, +100, 2.7182818 ) as $def ) {
261272 $wrongInt['fields']['testNumber']['default'] = $def;
262 - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrongInt ) );
 273+ $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrongInt ) );
263274 }
264275
265276 //If required=false, default=null must be accepted, too
266277 foreach ( array( $correctFloat, $correctInt ) as $correct ) {
267278 $correct['fields']['testNumber']['required'] = false;
268279 $correct['fields']['testNumber']['default'] = null;
269 - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) );
 280+ $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) );
270281 }
271282 }
272283
@@ -292,73 +303,82 @@
293304 $correct2 = $correct;
294305 foreach ( array( null, true, 3, 'test' ) as $def ) {
295306 $correct2['fields']['testSelect']['default'] = $def;
296 - $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) );
 307+ $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) );
297308 }
298309
299310 //Tests with wrong default values
300311 $wrong = $correct;
301312 foreach ( array( '', 'true', 'null', false, array(), 0, 1, 3.0001 ) as $def ) {
302313 $wrong['fields']['testSelect']['default'] = $def;
303 - $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) );
 314+ $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) );
304315 }
305316 }
306317
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+ )
345368 )
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 - )
357369 )
358370 )
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 ) );
360382
361 - $this->assertTrue( Gadget::isPrefsDescriptionValid( $prefsDescription ) );
362 -
363383 $prefs = array(
364384 'testBoolean' => false,
365385 'testBoolean2' => null, //wrong
@@ -368,12 +388,12 @@
369389 'testSelect2' => false //wrong
370390 );
371391
372 - $this->assertFalse( Gadget::checkPrefsAgainstDescription( $prefsDescription, $prefs ) );
 392+ $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( $prefsDescription, $prefs ) );
373393
374394 $prefs2 = $prefs;
375 - Gadget::matchPrefsWithDescription( $prefsDescription, $prefs2 );
 395+ GadgetPrefs::matchPrefsWithDescription( $prefsDescription, $prefs2 );
376396 //Now $prefs2 should pass validation
377 - $this->assertTrue( Gadget::checkPrefsAgainstDescription( $prefsDescription, $prefs2 ) );
 397+ $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( $prefsDescription, $prefs2 ) );
378398
379399 //$prefs2 should have testBoolean, testNumber and testSelect unchanged, the other reset to defaults
380400 $this->assertEquals( $prefs2['testBoolean'], $prefs['testBoolean'] );
@@ -413,4 +433,17 @@
414434 //Call with invalid param
415435 $g->setPrefs( 'wrongparam' );
416436 }
 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+ }
417450 }

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r90884Moved gadget preferences static methods to the GadgetPrefs classsalvatoreingala16:17, 27 June 2011

Status & tagging log