Index: branches/salvatoreingala/Gadgets/Gadgets_tests.php |
— | — | @@ -89,7 +89,8 @@ |
90 | 90 | //Test with stdClass instead if array |
91 | 91 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( (object)array( |
92 | 92 | 'fields' => array( |
93 | | - 'testBoolean' => array( |
| 93 | + array( |
| 94 | + 'name' => 'testBoolean', |
94 | 95 | 'type' => 'boolean', |
95 | 96 | 'label' => 'foo', |
96 | 97 | 'default' => 'bar' |
— | — | @@ -100,7 +101,8 @@ |
101 | 102 | //Test with wrong type |
102 | 103 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
103 | 104 | 'fields' => array( |
104 | | - 'testUnexisting' => array( |
| 105 | + array( |
| 106 | + 'name' => 'testUnexisting', |
105 | 107 | 'type' => 'unexistingtype', |
106 | 108 | 'label' => 'foo', |
107 | 109 | 'default' => 'bar' |
— | — | @@ -108,10 +110,22 @@ |
109 | 111 | ) |
110 | 112 | ) ) ); |
111 | 113 | |
| 114 | + //Test with missing name |
| 115 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
| 116 | + 'fields' => array( |
| 117 | + array( |
| 118 | + 'type' => 'boolean', |
| 119 | + 'label' => 'foo', |
| 120 | + 'default' => true |
| 121 | + ) |
| 122 | + ) |
| 123 | + ) ) ); |
| 124 | + |
112 | 125 | //Test with wrong preference name |
113 | 126 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
114 | 127 | 'fields' => array( |
115 | | - 'testWrongN@me' => array( |
| 128 | + array( |
| 129 | + 'name' => 'testWrongN@me', |
116 | 130 | 'type' => 'boolean', |
117 | 131 | 'label' => 'foo', |
118 | 132 | 'default' => true |
— | — | @@ -119,10 +133,41 @@ |
120 | 134 | ) |
121 | 135 | ) ) ); |
122 | 136 | |
| 137 | + //Test with two fields with the same name |
| 138 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
| 139 | + 'fields' => array( |
| 140 | + array( |
| 141 | + 'name' => 'testBoolean', |
| 142 | + 'type' => 'boolean', |
| 143 | + 'label' => 'foo', |
| 144 | + 'default' => true |
| 145 | + ), |
| 146 | + array( |
| 147 | + 'name' => 'testBoolean', |
| 148 | + 'type' => 'string', |
| 149 | + 'label' => 'foo', |
| 150 | + 'default' => 'bar' |
| 151 | + ) |
| 152 | + ) |
| 153 | + ) ) ); |
| 154 | + |
| 155 | + //Test with fields encoded as associative array instead of regular array |
| 156 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
| 157 | + 'fields' => array( |
| 158 | + 'testBoolean' => array( |
| 159 | + 'name' => 'testBoolean', |
| 160 | + 'type' => 'string', |
| 161 | + 'label' => 'foo', |
| 162 | + 'default' => 'bar' |
| 163 | + ) |
| 164 | + ) |
| 165 | + ) ) ); |
| 166 | + |
123 | 167 | //Test with too long preference name (41 characters) |
124 | 168 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
125 | 169 | 'fields' => array( |
126 | | - 'aPreferenceNameExceedingTheLimitOf40Chars' => array( |
| 170 | + array( |
| 171 | + 'name' => 'aPreferenceNameExceedingTheLimitOf40Chars', |
127 | 172 | 'type' => 'boolean', |
128 | 173 | 'label' => 'foo', |
129 | 174 | 'default' => true |
— | — | @@ -133,7 +178,8 @@ |
134 | 179 | //This must pass, instead (40 characters is fine) |
135 | 180 | $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( array( |
136 | 181 | 'fields' => array( |
137 | | - 'otherPreferenceNameThatS40CharactersLong' => array( |
| 182 | + array( |
| 183 | + 'name' => 'otherPreferenceNameThatS40CharactersLong', |
138 | 184 | 'type' => 'boolean', |
139 | 185 | 'label' => 'foo', |
140 | 186 | 'default' => true |
— | — | @@ -145,7 +191,8 @@ |
146 | 192 | //Test with an unexisting field parameter |
147 | 193 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
148 | 194 | 'fields' => array( |
149 | | - 'testBoolean' => array( |
| 195 | + array( |
| 196 | + 'name' => 'testBoolean', |
150 | 197 | 'type' => 'boolean', |
151 | 198 | 'label' => 'foo', |
152 | 199 | 'default' => true, |
— | — | @@ -159,7 +206,8 @@ |
160 | 207 | function testPrefsDescriptionsBoolean() { |
161 | 208 | $correct = array( |
162 | 209 | 'fields' => array( |
163 | | - 'testBoolean' => array( |
| 210 | + array( |
| 211 | + 'name' => 'testBoolean', |
164 | 212 | 'type' => 'boolean', |
165 | 213 | 'label' => 'some label', |
166 | 214 | 'default' => true |
— | — | @@ -171,7 +219,8 @@ |
172 | 220 | |
173 | 221 | $correct2 = array( |
174 | 222 | 'fields' => array( |
175 | | - 'testBoolean' => array( |
| 223 | + array( |
| 224 | + 'name' => 'testBoolean', |
176 | 225 | 'type' => 'boolean', |
177 | 226 | 'label' => 'some label', |
178 | 227 | 'default' => false |
— | — | @@ -184,7 +233,7 @@ |
185 | 234 | //Tests with wrong default values |
186 | 235 | $wrong = $correct; |
187 | 236 | foreach ( array( 0, 1, '', 'false', 'true', null, array() ) as $def ) { |
188 | | - $wrong['fields']['testBoolean']['default'] = $def; |
| 237 | + $wrong['fields'][0]['default'] = $def; |
189 | 238 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
190 | 239 | } |
191 | 240 | } |
— | — | @@ -193,7 +242,8 @@ |
194 | 243 | function testPrefsDescriptionsString() { |
195 | 244 | $correct = array( |
196 | 245 | 'fields' => array( |
197 | | - 'testString' => array( |
| 246 | + array( |
| 247 | + 'name' => 'testString', |
198 | 248 | 'type' => 'string', |
199 | 249 | 'label' => 'some label', |
200 | 250 | 'minlength' => 6, |
— | — | @@ -209,14 +259,14 @@ |
210 | 260 | //Tests with wrong default values |
211 | 261 | $wrong = $correct; |
212 | 262 | foreach ( array( null, true, false, 0, 1, array(), 'short', 'veryverylongstring' ) as $def ) { |
213 | | - $wrong['fields']['testString']['default'] = $def; |
| 263 | + $wrong['fields'][0]['default'] = $def; |
214 | 264 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
215 | 265 | } |
216 | 266 | |
217 | 267 | //Tests with correct default values (when required is false) |
218 | 268 | $correct2 = $correct; |
219 | 269 | foreach ( array( '', '6chars', '1234567890' ) as $def ) { |
220 | | - $correct2['fields']['testString']['default'] = $def; |
| 270 | + $correct2['fields'][0]['default'] = $def; |
221 | 271 | $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
222 | 272 | } |
223 | 273 | |
— | — | @@ -231,7 +281,8 @@ |
232 | 282 | function testPrefsDescriptionsNumber() { |
233 | 283 | $correctFloat = array( |
234 | 284 | 'fields' => array( |
235 | | - 'testNumber' => array( |
| 285 | + array( |
| 286 | + 'name' => 'testNumber', |
236 | 287 | 'type' => 'number', |
237 | 288 | 'label' => 'some label', |
238 | 289 | 'min' => -15, |
— | — | @@ -244,7 +295,8 @@ |
245 | 296 | |
246 | 297 | $correctInt = array( |
247 | 298 | 'fields' => array( |
248 | | - 'testNumber' => array( |
| 299 | + array( |
| 300 | + 'name' => 'testNumber', |
249 | 301 | 'type' => 'number', |
250 | 302 | 'label' => 'some label', |
251 | 303 | 'min' => -15, |
— | — | @@ -262,20 +314,20 @@ |
263 | 315 | //Tests with wrong default values (with 'required' = true) |
264 | 316 | $wrongFloat = $correctFloat; |
265 | 317 | foreach ( array( '', false, true, null, array(), -100, +100 ) as $def ) { |
266 | | - $wrongFloat['fields']['testNumber']['default'] = $def; |
| 318 | + $wrongFloat['fields'][0]['default'] = $def; |
267 | 319 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrongFloat ) ); |
268 | 320 | } |
269 | 321 | |
270 | 322 | $wrongInt = $correctInt; |
271 | 323 | foreach ( array( '', false, true, null, array(), -100, +100, 2.7182818 ) as $def ) { |
272 | | - $wrongInt['fields']['testNumber']['default'] = $def; |
| 324 | + $wrongInt['fields'][0]['default'] = $def; |
273 | 325 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrongInt ) ); |
274 | 326 | } |
275 | 327 | |
276 | 328 | //If required=false, default=null must be accepted, too |
277 | 329 | foreach ( array( $correctFloat, $correctInt ) as $correct ) { |
278 | | - $correct['fields']['testNumber']['required'] = false; |
279 | | - $correct['fields']['testNumber']['default'] = null; |
| 330 | + $correct['fields'][0]['required'] = false; |
| 331 | + $correct['fields'][0]['default'] = null; |
280 | 332 | $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
281 | 333 | } |
282 | 334 | } |
— | — | @@ -284,7 +336,8 @@ |
285 | 337 | function testPrefsDescriptionsSelect() { |
286 | 338 | $correct = array( |
287 | 339 | 'fields' => array( |
288 | | - 'testSelect' => array( |
| 340 | + array( |
| 341 | + 'name' => 'testSelect', |
289 | 342 | 'type' => 'select', |
290 | 343 | 'label' => 'some label', |
291 | 344 | 'default' => 3, |
— | — | @@ -302,14 +355,14 @@ |
303 | 356 | //Tests with correct default values |
304 | 357 | $correct2 = $correct; |
305 | 358 | foreach ( array( null, true, 3, 'test' ) as $def ) { |
306 | | - $correct2['fields']['testSelect']['default'] = $def; |
| 359 | + $correct2['fields'][0]['default'] = $def; |
307 | 360 | $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
308 | 361 | } |
309 | 362 | |
310 | 363 | //Tests with wrong default values |
311 | 364 | $wrong = $correct; |
312 | 365 | foreach ( array( '', 'true', 'null', false, array(), 0, 1, 3.0001 ) as $def ) { |
313 | | - $wrong['fields']['testSelect']['default'] = $def; |
| 366 | + $wrong['fields'][0]['default'] = $def; |
314 | 367 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
315 | 368 | } |
316 | 369 | } |
— | — | @@ -318,7 +371,8 @@ |
319 | 372 | function testPrefsDescriptionsRange() { |
320 | 373 | $correct = array( |
321 | 374 | 'fields' => array( |
322 | | - 'testRange' => array( |
| 375 | + array( |
| 376 | + 'name' => 'testRange', |
323 | 377 | 'type' => 'range', |
324 | 378 | 'label' => 'some label', |
325 | 379 | 'default' => 35, |
— | — | @@ -331,27 +385,28 @@ |
332 | 386 | //Tests with correct default values |
333 | 387 | $correct2 = $correct; |
334 | 388 | foreach ( array( 15, 33, 45 ) as $def ) { |
335 | | - $correct2['fields']['testRange']['default'] = $def; |
| 389 | + $correct2['fields'][0]['default'] = $def; |
336 | 390 | $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
337 | 391 | } |
338 | 392 | |
339 | 393 | //Tests with wrong default values |
340 | 394 | $wrong = $correct; |
341 | 395 | foreach ( array( '', true, false, null, array(), '35', 14, 46, 30.2 ) as $def ) { |
342 | | - $wrong['fields']['testRange']['default'] = $def; |
| 396 | + $wrong['fields'][0]['default'] = $def; |
343 | 397 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
344 | 398 | } |
345 | 399 | |
346 | 400 | //Test with max not in the set min + k*step (step not given, so it's 1) |
347 | 401 | $wrong = $correct; |
348 | | - $wrong['fields']['testRange']['max'] = 45.5; |
| 402 | + $wrong['fields'][0]['max'] = 45.5; |
349 | 403 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
350 | 404 | |
351 | 405 | |
352 | 406 | //Tests with floating point min, max and step |
353 | 407 | $correct = array( |
354 | 408 | 'fields' => array( |
355 | | - 'testRange' => array( |
| 409 | + array( |
| 410 | + 'name' => 'testRange', |
356 | 411 | 'type' => 'range', |
357 | 412 | 'label' => 'some label', |
358 | 413 | 'default' => 0.20, |
— | — | @@ -367,14 +422,14 @@ |
368 | 423 | //Tests with correct default values |
369 | 424 | $correct2 = $correct; |
370 | 425 | foreach ( array( -2.8, -2.55, 0.20, 4.2 ) as $def ) { |
371 | | - $correct2['fields']['testRange']['default'] = $def; |
| 426 | + $correct2['fields'][0]['default'] = $def; |
372 | 427 | $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
373 | 428 | } |
374 | 429 | |
375 | 430 | //Tests with wrong default values |
376 | 431 | $wrong = $correct; |
377 | 432 | foreach ( array( '', true, false, null, array(), '0.20', -2.7, 0, 4.199999 ) as $def ) { |
378 | | - $wrong['fields']['testRange']['default'] = $def; |
| 433 | + $wrong['fields'][0]['default'] = $def; |
379 | 434 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
380 | 435 | } |
381 | 436 | } |
— | — | @@ -383,7 +438,8 @@ |
384 | 439 | function testPrefsDescriptionsDate() { |
385 | 440 | $correct = array( |
386 | 441 | 'fields' => array( |
387 | | - 'testDate' => array( |
| 442 | + array( |
| 443 | + 'name' => 'testDate', |
388 | 444 | 'type' => 'date', |
389 | 445 | 'label' => 'some label', |
390 | 446 | 'default' => null |
— | — | @@ -400,7 +456,7 @@ |
401 | 457 | '2011-12-31T23:59:59Z', |
402 | 458 | ) as $def ) |
403 | 459 | { |
404 | | - $correct2['fields']['testDate']['default'] = $def; |
| 460 | + $correct2['fields'][0]['default'] = $def; |
405 | 461 | $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
406 | 462 | } |
407 | 463 | |
— | — | @@ -423,7 +479,7 @@ |
424 | 480 | '2011:07-05T15:00:00Z' |
425 | 481 | ) as $def ) |
426 | 482 | { |
427 | | - $wrong['fields']['testDate']['default'] = $def; |
| 483 | + $wrong['fields'][0]['default'] = $def; |
428 | 484 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
429 | 485 | } |
430 | 486 | } |
— | — | @@ -432,7 +488,8 @@ |
433 | 489 | function testPrefsDescriptionsColor() { |
434 | 490 | $correct = array( |
435 | 491 | 'fields' => array( |
436 | | - 'testColor' => array( |
| 492 | + array( |
| 493 | + 'name' => 'testColor', |
437 | 494 | 'type' => 'color', |
438 | 495 | 'label' => 'some label', |
439 | 496 | 'default' => '#123456' |
— | — | @@ -448,7 +505,7 @@ |
449 | 506 | '#8ed36e', |
450 | 507 | ) as $def ) |
451 | 508 | { |
452 | | - $correct2['fields']['testColor']['default'] = $def; |
| 509 | + $correct2['fields'][0]['default'] = $def; |
453 | 510 | $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
454 | 511 | } |
455 | 512 | |
— | — | @@ -465,7 +522,7 @@ |
466 | 523 | 'red', //syntax not allowed |
467 | 524 | ) as $def ) |
468 | 525 | { |
469 | | - $wrong['fields']['testColor']['default'] = $def; |
| 526 | + $wrong['fields'][0]['default'] = $def; |
470 | 527 | $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
471 | 528 | } |
472 | 529 | } |
— | — | @@ -476,31 +533,36 @@ |
477 | 534 | return array( array( |
478 | 535 | array( |
479 | 536 | 'fields' => array( |
480 | | - 'testBoolean' => array( |
| 537 | + array( |
| 538 | + 'name' => 'testBoolean', |
481 | 539 | 'type' => 'boolean', |
482 | 540 | 'label' => '@foo', |
483 | 541 | 'default' => true |
484 | 542 | ), |
485 | | - 'testBoolean2' => array( |
| 543 | + array( |
| 544 | + 'name' => 'testBoolean2', |
486 | 545 | 'type' => 'boolean', |
487 | 546 | 'label' => '@@foo2', |
488 | 547 | 'default' => true |
489 | 548 | ), |
490 | | - 'testNumber' => array( |
| 549 | + array( |
| 550 | + 'name' => 'testNumber', |
491 | 551 | 'type' => 'number', |
492 | 552 | 'label' => '@foo3', |
493 | 553 | 'min' => 2.3, |
494 | 554 | 'max' => 13.94, |
495 | 555 | 'default' => 7 |
496 | 556 | ), |
497 | | - 'testNumber2' => array( |
| 557 | + array( |
| 558 | + 'name' => 'testNumber2', |
498 | 559 | 'type' => 'number', |
499 | 560 | 'label' => 'foo4', |
500 | 561 | 'min' => 2.3, |
501 | 562 | 'max' => 13.94, |
502 | 563 | 'default' => 7 |
503 | 564 | ), |
504 | | - 'testSelect' => array( |
| 565 | + array( |
| 566 | + 'name' => 'testSelect', |
505 | 567 | 'type' => 'select', |
506 | 568 | 'label' => 'foo', |
507 | 569 | 'default' => 3, |
— | — | @@ -511,7 +573,8 @@ |
512 | 574 | '@opt4' => 'opt4value' |
513 | 575 | ) |
514 | 576 | ), |
515 | | - 'testSelect2' => array( |
| 577 | + array( |
| 578 | + 'name' => 'testSelect2', |
516 | 579 | 'type' => 'select', |
517 | 580 | 'label' => 'foo', |
518 | 581 | 'default' => 3, |
— | — | @@ -557,9 +620,9 @@ |
558 | 621 | $this->assertEquals( $prefs2['testNumber'], $prefs['testNumber'] ); |
559 | 622 | $this->assertEquals( $prefs2['testSelect'], $prefs['testSelect'] ); |
560 | 623 | |
561 | | - $this->assertEquals( $prefs2['testBoolean2'], $prefsDescription['fields']['testBoolean2']['default'] ); |
562 | | - $this->assertEquals( $prefs2['testNumber2'], $prefsDescription['fields']['testNumber2']['default'] ); |
563 | | - $this->assertEquals( $prefs2['testSelect2'], $prefsDescription['fields']['testSelect2']['default'] ); |
| 624 | + $this->assertEquals( $prefs2['testBoolean2'], $prefsDescription['fields'][1]['default'] ); |
| 625 | + $this->assertEquals( $prefs2['testNumber2'], $prefsDescription['fields'][3]['default'] ); |
| 626 | + $this->assertEquals( $prefs2['testSelect2'], $prefsDescription['fields'][5]['default'] ); |
564 | 627 | |
565 | 628 | $g = $this->create( '*foo[ResourceLoader]| foo.css|foo.js|foo.bar' ); |
566 | 629 | $g->setPrefsDescription( $prefsDescription ); |
Index: branches/salvatoreingala/Gadgets/backend/GadgetPrefs.php |
— | — | @@ -238,12 +238,17 @@ |
239 | 239 | public static function isPrefsDescriptionValid( $prefsDescription ) { |
240 | 240 | if ( !is_array( $prefsDescription ) |
241 | 241 | || !isset( $prefsDescription['fields'] ) |
242 | | - || !is_array( $prefsDescription['fields'] ) |
243 | | - || count( $prefsDescription['fields'] ) == 0 ) |
| 242 | + || !is_array( $prefsDescription['fields'] ) ) |
244 | 243 | { |
245 | 244 | return false; |
246 | 245 | } |
247 | | - |
| 246 | + |
| 247 | + //Check if 'fields' is a regular (not-associative) array, and that it is not empty |
| 248 | + $count = count( $prefsDescription['fields'] ); |
| 249 | + if ( $count == 0 || array_keys( $prefsDescription['fields'] ) !== range( 0, $count - 1 ) ) { |
| 250 | + return false; |
| 251 | + } |
| 252 | + |
248 | 253 | //Count of mandatory members for each type |
249 | 254 | $mandatoryCount = array(); |
250 | 255 | foreach ( self::$prefsDescriptionSpecifications as $type => $typeSpec ) { |
— | — | @@ -256,22 +261,35 @@ |
257 | 262 | } |
258 | 263 | |
259 | 264 | //TODO: validation of members other than $prefs['fields'] |
| 265 | + |
| 266 | + //Map of encountered names |
| 267 | + $names = array(); |
260 | 268 | |
261 | | - foreach ( $prefsDescription['fields'] as $option => $optionDefinition ) { |
| 269 | + foreach ( $prefsDescription['fields'] as $optionDefinition ) { |
262 | 270 | |
263 | | - //Check if 'type' is set and valid |
264 | | - if ( !isset( $optionDefinition['type'] ) ) { |
| 271 | + //Check if 'name' and 'type' are set |
| 272 | + if ( !isset( $optionDefinition['type'] ) || !isset( $optionDefinition['name'] ) ) { |
265 | 273 | return false; |
266 | 274 | } |
267 | 275 | |
268 | 276 | $type = $optionDefinition['type']; |
269 | 277 | |
| 278 | + //check if 'type' is valid |
270 | 279 | if ( !isset( self::$prefsDescriptionSpecifications[$type] ) ) { |
271 | 280 | return false; |
272 | 281 | } |
273 | 282 | |
274 | | - //check $option name compliance |
275 | | - if ( strlen( $option ) > 40 |
| 283 | + $option = $optionDefinition['name']; |
| 284 | + |
| 285 | + //check that it's different from previous names |
| 286 | + if ( isset( $names[$option] ) ) { |
| 287 | + return false; |
| 288 | + } |
| 289 | + |
| 290 | + $names[$option] = true; |
| 291 | + |
| 292 | + //check option name compliance |
| 293 | + if ( strlen( $option ) > 40 |
276 | 294 | || !preg_match( '/^[a-zA-Z_][a-zA-Z0-9_]*$/', $option ) ) |
277 | 295 | { |
278 | 296 | return false; |
— | — | @@ -283,8 +301,8 @@ |
284 | 302 | $count = 0; //count of present mandatory members |
285 | 303 | foreach ( $optionDefinition as $fieldName => $fieldValue ) { |
286 | 304 | |
287 | | - if ( $fieldName == 'type' ) { |
288 | | - continue; //'type' must not be checked |
| 305 | + if ( $fieldName == 'type' || $fieldName == 'name' ) { |
| 306 | + continue; //'type' and 'name' must not be checked |
289 | 307 | } |
290 | 308 | |
291 | 309 | if ( !isset( $typeDescription[$fieldName] ) ) { |
— | — | @@ -468,16 +486,19 @@ |
469 | 487 | * @return boolean true if $prefs passes validation against $prefsDescription, false otherwise. |
470 | 488 | */ |
471 | 489 | public static function checkPrefsAgainstDescription( $prefsDescription, $prefs ) { |
| 490 | + $validPrefs = array(); |
472 | 491 | //Check that all the given preferences pass validation |
473 | | - foreach ( $prefsDescription['fields'] as $prefName => $prefDescription ) { |
| 492 | + foreach ( $prefsDescription['fields'] as $prefDescription ) { |
| 493 | + $prefName = $prefDescription['name']; |
474 | 494 | if ( !self::checkSinglePref( $prefDescription, $prefs, $prefName ) ) { |
475 | 495 | return false; |
476 | 496 | } |
| 497 | + $validPrefs[$prefName] = true; |
477 | 498 | } |
478 | 499 | |
479 | 500 | //Check that $prefs contains no preferences that are not described in $prefsDescription |
480 | 501 | foreach ( $prefs as $prefName => $value ) { |
481 | | - if ( !isset( $prefsDescription['fields'][$prefName] ) ) { |
| 502 | + if ( !isset( $validPrefs[$prefName] ) ) { |
482 | 503 | return false; |
483 | 504 | } |
484 | 505 | } |
— | — | @@ -493,19 +514,23 @@ |
494 | 515 | * @param &$prefs Array: reference of the array of preferences to match. |
495 | 516 | */ |
496 | 517 | public static function matchPrefsWithDescription( $prefsDescription, &$prefs ) { |
| 518 | + $validPrefs = array(); |
| 519 | + |
| 520 | + //Fix preferences that fail validation, by replacing their value with default |
| 521 | + foreach ( $prefsDescription['fields'] as $prefDescription ) { |
| 522 | + $prefName = $prefDescription['name']; |
| 523 | + if ( !self::checkSinglePref( $prefDescription, $prefs, $prefName ) ) { |
| 524 | + $prefs[$prefName] = $prefDescription['default']; |
| 525 | + } |
| 526 | + $validPrefs[$prefName] = true; |
| 527 | + } |
| 528 | + |
497 | 529 | //Remove unexisting preferences from $prefs |
498 | 530 | foreach ( $prefs as $prefName => $value ) { |
499 | | - if ( !isset( $prefsDescription['fields'][$prefName] ) ) { |
| 531 | + if ( !isset( $validPrefs[$prefName] ) ) { |
500 | 532 | unset( $prefs[$prefName] ); |
501 | 533 | } |
502 | 534 | } |
503 | | - |
504 | | - //Fix preferences that fail validation |
505 | | - foreach ( $prefsDescription['fields'] as $prefName => $prefDescription ) { |
506 | | - if ( !self::checkSinglePref( $prefDescription, $prefs, $prefName ) ) { |
507 | | - $prefs[$prefName] = $prefDescription['default']; |
508 | | - } |
509 | | - } |
510 | 535 | } |
511 | 536 | |
512 | 537 | /** |
Index: branches/salvatoreingala/Gadgets/api/ApiGetGadgetPrefs.php |
— | — | @@ -53,8 +53,9 @@ |
54 | 54 | } |
55 | 55 | |
56 | 56 | //Add user preferences to preference description |
57 | | - foreach ( $userPrefs as $pref => $value ) { |
58 | | - $prefsDescription['fields'][$pref]['value'] = $value; |
| 57 | + foreach ( $prefsDescription['fields'] as $prefIdx => $prefDescription ) { |
| 58 | + $prefName = $prefDescription['name']; |
| 59 | + $prefsDescription['fields'][$prefIdx]['value'] = $userPrefs[$prefName]; |
59 | 60 | } |
60 | 61 | |
61 | 62 | $this->getResult()->addValue( null, $this->getModuleName(), $prefsDescription ); |
Index: branches/salvatoreingala/Gadgets/ui/resources/jquery.formBuilder.js |
— | — | @@ -556,37 +556,35 @@ |
557 | 557 | |
558 | 558 | var settings = {}; //validator settings |
559 | 559 | |
560 | | - for ( var fieldName in description.fields ) { |
561 | | - if ( description.fields.hasOwnProperty( fieldName )) { |
562 | | - //TODO: validate fieldName |
563 | | - var field = description.fields[fieldName]; |
| 560 | + for ( var i = 0; i < description.fields.length; i++ ) { |
| 561 | + //TODO: validate fieldName |
| 562 | + var field = description.fields[i], |
| 563 | + fieldName = field.name, |
| 564 | + FieldConstructor = validFieldTypes[field.type]; |
564 | 565 | |
565 | | - var FieldConstructor = validFieldTypes[field.type]; |
| 566 | + if ( typeof FieldConstructor != 'function' ) { |
| 567 | + mw.log( "field with invalid type: " + field.type ); |
| 568 | + return null; |
| 569 | + } |
566 | 570 | |
567 | | - if ( typeof FieldConstructor != 'function' ) { |
568 | | - mw.log( "field with invalid type: " + field.type ); |
569 | | - return null; |
570 | | - } |
571 | | - |
572 | | - var f; |
573 | | - try { |
574 | | - f = new FieldConstructor( $form, fieldName, field ); |
575 | | - } catch ( e ) { |
576 | | - mw.log( e ); |
577 | | - return null; //constructor failed, wrong syntax in field description |
578 | | - } |
579 | | - |
580 | | - $form.append( f.getElement() ); |
581 | | - |
582 | | - //If this field has validation rules, add them to settings |
583 | | - var fieldSettings = f.getValidationSettings(); |
584 | | - |
585 | | - if ( fieldSettings ) { |
586 | | - $.extend( true, settings, fieldSettings ); |
587 | | - } |
588 | | - |
589 | | - fields.push( f ); |
| 571 | + var f; |
| 572 | + try { |
| 573 | + f = new FieldConstructor( $form, fieldName, field ); |
| 574 | + } catch ( e ) { |
| 575 | + mw.log( e ); |
| 576 | + return null; //constructor failed, wrong syntax in field description |
590 | 577 | } |
| 578 | + |
| 579 | + $form.append( f.getElement() ); |
| 580 | + |
| 581 | + //If this field has validation rules, add them to settings |
| 582 | + var fieldSettings = f.getValidationSettings(); |
| 583 | + |
| 584 | + if ( fieldSettings ) { |
| 585 | + $.extend( true, settings, fieldSettings ); |
| 586 | + } |
| 587 | + |
| 588 | + fields.push( f ); |
591 | 589 | } |
592 | 590 | |
593 | 591 | var validator = $form.validate( settings ); |