Index: branches/RL2/extensions/Gadgets/tests/GadgetPrefsTest.php |
— | — | @@ -0,0 +1,1077 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * @group Gadgets |
| 5 | + */ |
| 6 | +class GadgetPrefsTest extends PHPUnit_Framework_TestCase { |
| 7 | + function testPreferences() { |
| 8 | + // FIXME this test is broken |
| 9 | + $this->markTestIncomplete( 'Broken for now' ); |
| 10 | + return; |
| 11 | + |
| 12 | + global $wgUser; |
| 13 | + |
| 14 | + // This test makes call to the parser which requires valids Outputpage |
| 15 | + // and Title objects. Set them up there, they will be released at the |
| 16 | + // end of the test. |
| 17 | + global $wgOut, $wgTitle; |
| 18 | + $old_wgOut = $wgOut; |
| 19 | + $old_wgTitle = $wgTitle; |
| 20 | + $wgTitle = Title::newFromText( 'Parser test for Gadgets extension' ); |
| 21 | + |
| 22 | + // Proceed with test setup: |
| 23 | + $prefs = array(); |
| 24 | + $context = new RequestContext(); |
| 25 | + $wgOut = $context->getOutput(); |
| 26 | + $wgOut->setTitle( Title::newFromText( 'test' ) ); |
| 27 | + |
| 28 | + Gadget::loadStructuredList( '* foo | foo.js |
| 29 | +==keep-section1== |
| 30 | +* bar| bar.js |
| 31 | +==remove-section== |
| 32 | +* baz [rights=embezzle] |baz.js |
| 33 | +==keep-section2== |
| 34 | +* quux [rights=read] | quux.js' ); |
| 35 | + $this->assertTrue( GadgetsHooks::getPreferences( $wgUser, $prefs ), 'GetPrefences hook should return true' ); |
| 36 | + |
| 37 | + $options = $prefs['gadgets']['options']; |
| 38 | + $this->assertFalse( isset( $options['<gadget-section-remove-section>'] ), 'Must not show empty sections' ); |
| 39 | + $this->assertTrue( isset( $options['<gadget-section-keep-section1>'] ) ); |
| 40 | + $this->assertTrue( isset( $options['<gadget-section-keep-section2>'] ) ); |
| 41 | + |
| 42 | + // Restore globals |
| 43 | + $wgOut = $old_wgOut; |
| 44 | + $wgTitle = $old_wgTitle; |
| 45 | + } |
| 46 | + |
| 47 | + //Test preferences descriptions validator (generic) |
| 48 | + function testPrefsDescriptions() { |
| 49 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( null ) ); |
| 50 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array() ) ); |
| 51 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( 'fields' => array() ) ) ); |
| 52 | + |
| 53 | + //Test with stdClass instead if array |
| 54 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( (object)array( |
| 55 | + 'fields' => array( |
| 56 | + array( |
| 57 | + 'name' => 'testBoolean', |
| 58 | + 'type' => 'boolean', |
| 59 | + 'label' => 'foo', |
| 60 | + 'default' => 'bar' |
| 61 | + ) |
| 62 | + ) |
| 63 | + ) ) ); |
| 64 | + |
| 65 | + //Test with wrong type |
| 66 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
| 67 | + 'fields' => array( |
| 68 | + array( |
| 69 | + 'name' => 'testUnexisting', |
| 70 | + 'type' => 'unexistingtype', |
| 71 | + 'label' => 'foo', |
| 72 | + 'default' => 'bar' |
| 73 | + ) |
| 74 | + ) |
| 75 | + ) ) ); |
| 76 | + |
| 77 | + //Test with missing name |
| 78 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
| 79 | + 'fields' => array( |
| 80 | + array( |
| 81 | + 'type' => 'boolean', |
| 82 | + 'label' => 'foo', |
| 83 | + 'default' => true |
| 84 | + ) |
| 85 | + ) |
| 86 | + ) ) ); |
| 87 | + |
| 88 | + //Test with wrong preference name |
| 89 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
| 90 | + 'fields' => array( |
| 91 | + array( |
| 92 | + 'name' => 'testWrongN@me', |
| 93 | + 'type' => 'boolean', |
| 94 | + 'label' => 'foo', |
| 95 | + 'default' => true |
| 96 | + ) |
| 97 | + ) |
| 98 | + ) ) ); |
| 99 | + |
| 100 | + //Test with two fields with the same name |
| 101 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
| 102 | + 'fields' => array( |
| 103 | + array( |
| 104 | + 'name' => 'testBoolean', |
| 105 | + 'type' => 'boolean', |
| 106 | + 'label' => 'foo', |
| 107 | + 'default' => true |
| 108 | + ), |
| 109 | + array( |
| 110 | + 'name' => 'testBoolean', |
| 111 | + 'type' => 'string', |
| 112 | + 'label' => 'foo', |
| 113 | + 'default' => 'bar' |
| 114 | + ) |
| 115 | + ) |
| 116 | + ) ) ); |
| 117 | + |
| 118 | + //Test with fields encoded as associative array instead of regular array |
| 119 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
| 120 | + 'fields' => array( |
| 121 | + 'testBoolean' => array( |
| 122 | + 'name' => 'testBoolean', |
| 123 | + 'type' => 'string', |
| 124 | + 'label' => 'foo', |
| 125 | + 'default' => 'bar' |
| 126 | + ) |
| 127 | + ) |
| 128 | + ) ) ); |
| 129 | + |
| 130 | + //Test with too long preference name (41 characters) |
| 131 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
| 132 | + 'fields' => array( |
| 133 | + array( |
| 134 | + 'name' => 'aPreferenceNameExceedingTheLimitOf40Chars', |
| 135 | + 'type' => 'boolean', |
| 136 | + 'label' => 'foo', |
| 137 | + 'default' => true |
| 138 | + ) |
| 139 | + ) |
| 140 | + ) ) ); |
| 141 | + |
| 142 | + //This must pass, instead (40 characters is fine) |
| 143 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( array( |
| 144 | + 'fields' => array( |
| 145 | + array( |
| 146 | + 'name' => 'otherPreferenceNameThatS40CharactersLong', |
| 147 | + 'type' => 'boolean', |
| 148 | + 'label' => 'foo', |
| 149 | + 'default' => true |
| 150 | + ) |
| 151 | + ) |
| 152 | + ) ) ); |
| 153 | + |
| 154 | + |
| 155 | + //Test with an unexisting field parameter |
| 156 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
| 157 | + 'fields' => array( |
| 158 | + array( |
| 159 | + 'name' => 'testBoolean', |
| 160 | + 'type' => 'boolean', |
| 161 | + 'label' => 'foo', |
| 162 | + 'default' => true, |
| 163 | + 'unexistingParamThatMustFail' => 'foo' |
| 164 | + ) |
| 165 | + ) |
| 166 | + ) ) ); |
| 167 | + } |
| 168 | + |
| 169 | + //Tests for 'label' type preferences |
| 170 | + function testPrefsDescriptionsLabel() { |
| 171 | + $correct = array( |
| 172 | + 'fields' => array( |
| 173 | + array( |
| 174 | + 'type' => 'label', |
| 175 | + 'label' => 'foo' |
| 176 | + ) |
| 177 | + ) |
| 178 | + ); |
| 179 | + |
| 180 | + //Tests with correct values for 'label' |
| 181 | + foreach ( array( '', '@', '@message', 'foo', '@@not message' ) as $def ) { |
| 182 | + $correct['fields'][0]['label'] = $def; |
| 183 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
| 184 | + } |
| 185 | + |
| 186 | + //Tests with wrong values for 'label' |
| 187 | + $wrong = $correct; |
| 188 | + foreach ( array( 0, 1, true, false, null, array() ) as $label ) { |
| 189 | + $wrong['fields'][0]['label'] = $label; |
| 190 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 191 | + } |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + //Tests for 'boolean' type preferences |
| 196 | + function testPrefsDescriptionsBoolean() { |
| 197 | + $correct = array( |
| 198 | + 'fields' => array( |
| 199 | + array( |
| 200 | + 'name' => 'testBoolean', |
| 201 | + 'type' => 'boolean', |
| 202 | + 'label' => 'some label', |
| 203 | + 'default' => true |
| 204 | + ) |
| 205 | + ) |
| 206 | + ); |
| 207 | + |
| 208 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
| 209 | + |
| 210 | + $correct2 = array( |
| 211 | + 'fields' => array( |
| 212 | + array( |
| 213 | + 'name' => 'testBoolean', |
| 214 | + 'type' => 'boolean', |
| 215 | + 'label' => 'some label', |
| 216 | + 'default' => false |
| 217 | + ) |
| 218 | + ) |
| 219 | + ); |
| 220 | + |
| 221 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
| 222 | + |
| 223 | + //Tests with wrong default values |
| 224 | + $wrong = $correct; |
| 225 | + foreach ( array( 0, 1, '', 'false', 'true', null, array() ) as $def ) { |
| 226 | + $wrong['fields'][0]['default'] = $def; |
| 227 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + //Tests for 'string' type preferences |
| 232 | + function testPrefsDescriptionsString() { |
| 233 | + $correct = array( |
| 234 | + 'fields' => array( |
| 235 | + array( |
| 236 | + 'name' => 'testString', |
| 237 | + 'type' => 'string', |
| 238 | + 'label' => 'some label', |
| 239 | + 'minlength' => 6, |
| 240 | + 'maxlength' => 10, |
| 241 | + 'default' => 'default' |
| 242 | + ) |
| 243 | + ) |
| 244 | + ); |
| 245 | + |
| 246 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
| 247 | + |
| 248 | + //Tests with wrong default values (when 'required' is not given) |
| 249 | + $wrong = $correct; |
| 250 | + foreach ( array( null, '', true, false, 0, 1, array(), 'short', 'veryverylongstring' ) as $def ) { |
| 251 | + $wrong['fields'][0]['default'] = $def; |
| 252 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 253 | + } |
| 254 | + |
| 255 | + //Tests with correct default values (when required is not given) |
| 256 | + $correct2 = $correct; |
| 257 | + foreach ( array( '6chars', '1234567890' ) as $def ) { |
| 258 | + $correct2['fields'][0]['default'] = $def; |
| 259 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
| 260 | + } |
| 261 | + |
| 262 | + //Tests with wrong default values (when 'required' is false) |
| 263 | + $wrong = $correct; |
| 264 | + $wrong['fields'][0]['required'] = false; |
| 265 | + foreach ( array( null, true, false, 0, 1, array(), 'short', 'veryverylongstring' ) as $def ) { |
| 266 | + $wrong['fields'][0]['default'] = $def; |
| 267 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 268 | + } |
| 269 | + |
| 270 | + //Tests with correct default values (when required is false) |
| 271 | + $correct2 = $correct; |
| 272 | + $correct2['fields'][0]['required'] = false; |
| 273 | + foreach ( array( '', '6chars', '1234567890' ) as $def ) { |
| 274 | + $correct2['fields'][0]['default'] = $def; |
| 275 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
| 276 | + } |
| 277 | + |
| 278 | + $correct = array( |
| 279 | + 'fields' => array( |
| 280 | + array( |
| 281 | + 'name' => 'testString', |
| 282 | + 'type' => 'string', |
| 283 | + 'label' => 'some label', |
| 284 | + 'default' => '' |
| 285 | + ) |
| 286 | + ) |
| 287 | + ); |
| 288 | + |
| 289 | + //Test with empty default when "required" is true |
| 290 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
| 291 | + |
| 292 | + //Test with empty default when "required" is true |
| 293 | + $wrong = $correct; |
| 294 | + $wrong['fields'][0]['required'] = true; |
| 295 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 296 | + |
| 297 | + //Test with empty default when "required" is false and minlength is given |
| 298 | + $correct2 = $correct; |
| 299 | + $correct2['fields'][0]['required'] = false; |
| 300 | + $correct2['fields'][0]['minlength'] = 3; |
| 301 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
| 302 | + } |
| 303 | + |
| 304 | + //Tests for 'number' type preferences |
| 305 | + function testPrefsDescriptionsNumber() { |
| 306 | + $correctFloat = array( |
| 307 | + 'fields' => array( |
| 308 | + array( |
| 309 | + 'name' => 'testNumber', |
| 310 | + 'type' => 'number', |
| 311 | + 'label' => 'some label', |
| 312 | + 'min' => -15, |
| 313 | + 'max' => 36, |
| 314 | + 'required' => true, |
| 315 | + 'default' => 3.14 |
| 316 | + ) |
| 317 | + ) |
| 318 | + ); |
| 319 | + |
| 320 | + $correctInt = array( |
| 321 | + 'fields' => array( |
| 322 | + array( |
| 323 | + 'name' => 'testNumber', |
| 324 | + 'type' => 'number', |
| 325 | + 'label' => 'some label', |
| 326 | + 'min' => -15, |
| 327 | + 'max' => 36, |
| 328 | + 'integer' => true, |
| 329 | + 'required' => true, |
| 330 | + 'default' => 12 |
| 331 | + ) |
| 332 | + ) |
| 333 | + ); |
| 334 | + |
| 335 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correctFloat ) ); |
| 336 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correctInt ) ); |
| 337 | + |
| 338 | + //Tests with wrong default values (with 'required' = true) |
| 339 | + $wrongFloat = $correctFloat; |
| 340 | + foreach ( array( '', false, true, null, array(), -100, +100 ) as $def ) { |
| 341 | + $wrongFloat['fields'][0]['default'] = $def; |
| 342 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrongFloat ) ); |
| 343 | + } |
| 344 | + |
| 345 | + $wrongInt = $correctInt; |
| 346 | + foreach ( array( '', false, true, null, array(), -100, +100, 2.7182818 ) as $def ) { |
| 347 | + $wrongInt['fields'][0]['default'] = $def; |
| 348 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrongInt ) ); |
| 349 | + } |
| 350 | + |
| 351 | + //If required=false, default=null must be accepted, too |
| 352 | + foreach ( array( $correctFloat, $correctInt ) as $correct ) { |
| 353 | + $correct['fields'][0]['required'] = false; |
| 354 | + $correct['fields'][0]['default'] = null; |
| 355 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
| 356 | + } |
| 357 | + } |
| 358 | + |
| 359 | + //Tests for 'select' type preferences |
| 360 | + function testPrefsDescriptionsSelect() { |
| 361 | + $correct = array( |
| 362 | + 'fields' => array( |
| 363 | + array( |
| 364 | + 'name' => 'testSelect', |
| 365 | + 'type' => 'select', |
| 366 | + 'label' => 'some label', |
| 367 | + 'default' => 3, |
| 368 | + 'options' => array( |
| 369 | + array( 'name' => 'opt1', 'value' => null ), |
| 370 | + array( 'name' => 'opt2', 'value' => true ), |
| 371 | + array( 'name' => 'opt3', 'value' => 3 ), |
| 372 | + array( 'name' => 'opt4', 'value' => 'test' ) |
| 373 | + ) |
| 374 | + ) |
| 375 | + ) |
| 376 | + ); |
| 377 | + |
| 378 | + |
| 379 | + //Tests with correct default values |
| 380 | + $correct2 = $correct; |
| 381 | + foreach ( array( null, true, 3, 'test' ) as $def ) { |
| 382 | + $correct2['fields'][0]['default'] = $def; |
| 383 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
| 384 | + } |
| 385 | + |
| 386 | + //Tests with wrong default values |
| 387 | + $wrong = $correct; |
| 388 | + foreach ( array( '', 'true', 'null', false, array(), 0, 1, 3.0001 ) as $def ) { |
| 389 | + $wrong['fields'][0]['default'] = $def; |
| 390 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 391 | + } |
| 392 | + } |
| 393 | + |
| 394 | + //Tests for 'range' type preferences |
| 395 | + function testPrefsDescriptionsRange() { |
| 396 | + $correct = array( |
| 397 | + 'fields' => array( |
| 398 | + array( |
| 399 | + 'name' => 'testRange', |
| 400 | + 'type' => 'range', |
| 401 | + 'label' => 'some label', |
| 402 | + 'default' => 35, |
| 403 | + 'min' => 15, |
| 404 | + 'max' => 45 |
| 405 | + ) |
| 406 | + ) |
| 407 | + ); |
| 408 | + |
| 409 | + //Tests with correct default values |
| 410 | + $correct2 = $correct; |
| 411 | + foreach ( array( 15, 33, 45 ) as $def ) { |
| 412 | + $correct2['fields'][0]['default'] = $def; |
| 413 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
| 414 | + } |
| 415 | + |
| 416 | + //Tests with wrong default values |
| 417 | + $wrong = $correct; |
| 418 | + foreach ( array( '', true, false, null, array(), '35', 14, 46, 30.2 ) as $def ) { |
| 419 | + $wrong['fields'][0]['default'] = $def; |
| 420 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 421 | + } |
| 422 | + |
| 423 | + //Test with max not in the set min + k*step (step not given, so it's 1) |
| 424 | + $wrong = $correct; |
| 425 | + $wrong['fields'][0]['max'] = 45.5; |
| 426 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 427 | + |
| 428 | + |
| 429 | + //Tests with floating point min, max and step |
| 430 | + $correct = array( |
| 431 | + 'fields' => array( |
| 432 | + array( |
| 433 | + 'name' => 'testRange', |
| 434 | + 'type' => 'range', |
| 435 | + 'label' => 'some label', |
| 436 | + 'default' => 0.20, |
| 437 | + 'min' => -2.8, |
| 438 | + 'max' => 4.2, |
| 439 | + 'step' => 0.25 |
| 440 | + ) |
| 441 | + ) |
| 442 | + ); |
| 443 | + |
| 444 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
| 445 | + |
| 446 | + //Tests with correct default values |
| 447 | + $correct2 = $correct; |
| 448 | + foreach ( array( -2.8, -2.55, 0.20, 4.2 ) as $def ) { |
| 449 | + $correct2['fields'][0]['default'] = $def; |
| 450 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
| 451 | + } |
| 452 | + |
| 453 | + //Tests with wrong default values |
| 454 | + $wrong = $correct; |
| 455 | + foreach ( array( '', true, false, null, array(), '0.20', -2.7, 0, 4.199999 ) as $def ) { |
| 456 | + $wrong['fields'][0]['default'] = $def; |
| 457 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 458 | + } |
| 459 | + } |
| 460 | + |
| 461 | + //Tests for 'date' type preferences |
| 462 | + function testPrefsDescriptionsDate() { |
| 463 | + $correct = array( |
| 464 | + 'fields' => array( |
| 465 | + array( |
| 466 | + 'name' => 'testDate', |
| 467 | + 'type' => 'date', |
| 468 | + 'label' => 'some label', |
| 469 | + 'default' => null |
| 470 | + ) |
| 471 | + ) |
| 472 | + ); |
| 473 | + |
| 474 | + //Tests with correct default values |
| 475 | + $correct2 = $correct; |
| 476 | + foreach ( array( |
| 477 | + null, |
| 478 | + '2011-07-05T15:00:00Z', |
| 479 | + '2011-01-01T00:00:00Z', |
| 480 | + '2011-12-31T23:59:59Z', |
| 481 | + ) as $def ) |
| 482 | + { |
| 483 | + $correct2['fields'][0]['default'] = $def; |
| 484 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
| 485 | + } |
| 486 | + |
| 487 | + //Tests with wrong default values |
| 488 | + $wrong = $correct; |
| 489 | + foreach ( array( |
| 490 | + '', true, false, array(), 0, |
| 491 | + '2011-07-05T15:00:00', |
| 492 | + '2011-07-05T15:00:61Z', |
| 493 | + '2011-07-05T15:61:00Z', |
| 494 | + '2011-07-05T25:00:00Z', |
| 495 | + '2011-07-32T15:00:00Z', |
| 496 | + '2011-07-5T15:00:00Z', |
| 497 | + '2011-7-05T15:00:00Z', |
| 498 | + '2011-13-05T15:00:00Z', |
| 499 | + '2011-07-05T15:00-00Z', |
| 500 | + '2011-07-05T15-00:00Z', |
| 501 | + '2011-07-05S15:00:00Z', |
| 502 | + '2011-07:05T15:00:00Z', |
| 503 | + '2011:07-05T15:00:00Z' |
| 504 | + ) as $def ) |
| 505 | + { |
| 506 | + $wrong['fields'][0]['default'] = $def; |
| 507 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 508 | + } |
| 509 | + } |
| 510 | + |
| 511 | + //Tests for 'color' type preferences |
| 512 | + function testPrefsDescriptionsColor() { |
| 513 | + $correct = array( |
| 514 | + 'fields' => array( |
| 515 | + array( |
| 516 | + 'name' => 'testColor', |
| 517 | + 'type' => 'color', |
| 518 | + 'label' => 'some label', |
| 519 | + 'default' => '#123456' |
| 520 | + ) |
| 521 | + ) |
| 522 | + ); |
| 523 | + |
| 524 | + //Tests with correct default values |
| 525 | + $correct2 = $correct; |
| 526 | + foreach ( array( |
| 527 | + '#000000', |
| 528 | + '#ffffff', |
| 529 | + '#8ed36e', |
| 530 | + ) as $def ) |
| 531 | + { |
| 532 | + $correct2['fields'][0]['default'] = $def; |
| 533 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
| 534 | + } |
| 535 | + |
| 536 | + //Tests with wrong default values |
| 537 | + $wrong = $correct; |
| 538 | + foreach ( array( |
| 539 | + '', true, false, null, 0, array(), |
| 540 | + '123456', |
| 541 | + '#629af', |
| 542 | + '##123456', |
| 543 | + '#1aefdq', |
| 544 | + '#145aeF', //uppercase letters not allowed |
| 545 | + '#179', //syntax not allowed |
| 546 | + 'red', //syntax not allowed |
| 547 | + ) as $def ) |
| 548 | + { |
| 549 | + $wrong['fields'][0]['default'] = $def; |
| 550 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 551 | + } |
| 552 | + } |
| 553 | + |
| 554 | + //Tests for 'composite' type fields |
| 555 | + function testPrefsDescriptionsComposite() { |
| 556 | + $correct = array( |
| 557 | + 'fields' => array( |
| 558 | + array( |
| 559 | + 'name' => 'foo', |
| 560 | + 'type' => 'composite', |
| 561 | + 'fields' => array( |
| 562 | + array( |
| 563 | + 'name' => 'bar', |
| 564 | + 'type' => 'boolean', |
| 565 | + 'label' => '@msg1', |
| 566 | + 'default' => true |
| 567 | + ), |
| 568 | + array( |
| 569 | + 'name' => 'car', |
| 570 | + 'type' => 'color', |
| 571 | + 'label' => '@msg2', |
| 572 | + 'default' => '#123456' |
| 573 | + ) |
| 574 | + ) |
| 575 | + ) |
| 576 | + ) |
| 577 | + ); |
| 578 | + |
| 579 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
| 580 | + $this->assertEquals( |
| 581 | + GadgetPrefs::getDefaults( $correct ), |
| 582 | + array( 'foo' => array( 'bar' => true, 'car' => '#123456' ) ) |
| 583 | + ); |
| 584 | + $this->assertEquals( GadgetPrefs::getMessages( $correct ), array( 'msg1', 'msg2' ) ); |
| 585 | + |
| 586 | + $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( |
| 587 | + $correct, |
| 588 | + array( 'foo' => array( 'bar' => false, 'car' => '#00aaff' ) ) |
| 589 | + ) ); |
| 590 | + |
| 591 | + $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
| 592 | + $correct, |
| 593 | + array( 'foo' => array( 'bar' => null, 'car' => '#00aaff' ) ) |
| 594 | + ) ); |
| 595 | + |
| 596 | + $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
| 597 | + $correct, |
| 598 | + array( 'foo' => array( 'bar' => false, 'car' => '#00aafz' ) ) |
| 599 | + ) ); |
| 600 | + |
| 601 | + $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
| 602 | + $correct, |
| 603 | + array( 'bar' => false, 'car' => '#00aaff' ) |
| 604 | + ) ); |
| 605 | + |
| 606 | + $prefs = array( |
| 607 | + 'foo' => array( |
| 608 | + 'bar' => false, |
| 609 | + 'car' => null //wrong |
| 610 | + ) |
| 611 | + ); |
| 612 | + |
| 613 | + GadgetPrefs::matchPrefsWithDescription( $correct, $prefs ); |
| 614 | + //Check if only the wrong subfield has been reset to default value |
| 615 | + $this->assertEquals( $prefs, array( 'foo' => array( 'bar' => false, 'car' => '#123456' ) ) ); |
| 616 | + } |
| 617 | + |
| 618 | + //Tests for 'list' type fields |
| 619 | + function testPrefsDescriptionsList() { |
| 620 | + $correct = array( |
| 621 | + 'fields' => array( |
| 622 | + array( |
| 623 | + 'name' => 'foo', |
| 624 | + 'type' => 'list', |
| 625 | + 'default' => array(), |
| 626 | + 'field' => array( |
| 627 | + 'type' => 'composite', |
| 628 | + 'fields' => array( |
| 629 | + array( |
| 630 | + 'name' => 'bar', |
| 631 | + 'type' => 'boolean', |
| 632 | + 'label' => '@msg1', |
| 633 | + 'default' => true |
| 634 | + ), |
| 635 | + array( |
| 636 | + 'name' => 'car', |
| 637 | + 'type' => 'color', |
| 638 | + 'label' => '@msg2', |
| 639 | + 'default' => '#123456' |
| 640 | + ) |
| 641 | + ) |
| 642 | + ) |
| 643 | + ) |
| 644 | + ) |
| 645 | + ); |
| 646 | + |
| 647 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
| 648 | + |
| 649 | + //Specifying the 'name' member for field must fail |
| 650 | + $wrong = $correct; |
| 651 | + $wrong['fields'][0]['field']['name'] = 'composite'; |
| 652 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 653 | + |
| 654 | + |
| 655 | + $this->assertEquals( |
| 656 | + GadgetPrefs::getDefaults( $correct ), |
| 657 | + array( 'foo' => array() ) |
| 658 | + ); |
| 659 | + |
| 660 | + $this->assertEquals( GadgetPrefs::getMessages( $correct ), array( 'msg1', 'msg2' ) ); |
| 661 | + |
| 662 | + //Tests with correct pref values |
| 663 | + $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( |
| 664 | + $correct, |
| 665 | + array( 'foo' => array() ) |
| 666 | + ) ); |
| 667 | + |
| 668 | + $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( |
| 669 | + $correct, |
| 670 | + array( 'foo' => array( |
| 671 | + array( |
| 672 | + 'bar' => true, |
| 673 | + 'car' => '#115599' |
| 674 | + ), |
| 675 | + array( |
| 676 | + 'bar' => false, |
| 677 | + 'car' => '#123456' |
| 678 | + ), |
| 679 | + array( |
| 680 | + 'bar' => true, |
| 681 | + 'car' => '#ffffff' |
| 682 | + ) |
| 683 | + ) |
| 684 | + ) |
| 685 | + ) ); |
| 686 | + |
| 687 | + //Tests with wrong pref values |
| 688 | + $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
| 689 | + $correct, |
| 690 | + array( 'foo' => array( |
| 691 | + array( |
| 692 | + 'bar' => null, //wrong |
| 693 | + 'car' => '#115599' |
| 694 | + ) |
| 695 | + ) |
| 696 | + ) |
| 697 | + ) ); |
| 698 | + |
| 699 | + $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
| 700 | + $correct, |
| 701 | + array( 'foo' => array( //wrong, not enclosed in array |
| 702 | + 'bar' => null, |
| 703 | + 'car' => '#115599' |
| 704 | + ) |
| 705 | + ) |
| 706 | + ) ); |
| 707 | + |
| 708 | + |
| 709 | + //Tests with 'minlength' and 'maxlength' options |
| 710 | + $wrong = $correct; |
| 711 | + $wrong['fields'][0]['minlength'] = 4; |
| 712 | + $wrong['fields'][0]['maxlength'] = 3; //maxlength < minlength, wrong |
| 713 | + $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
| 714 | + |
| 715 | + $correct2 = $correct; |
| 716 | + $correct2['fields'][0]['minlength'] = 2; |
| 717 | + $correct2['fields'][0]['maxlength'] = 3; |
| 718 | + $correct2['fields'][0]['default'] = array( |
| 719 | + array( 'bar' => true, 'car' => '#115599' ), |
| 720 | + array( 'bar' => false, 'car' => '#123456' ) |
| 721 | + ); |
| 722 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
| 723 | + |
| 724 | + $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
| 725 | + $correct2, |
| 726 | + array( 'foo' => array( //less than minlength items |
| 727 | + array( 'bar' => true, 'car' => '#115599' ) |
| 728 | + ) |
| 729 | + ) |
| 730 | + ) ); |
| 731 | + |
| 732 | + $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
| 733 | + $correct2, |
| 734 | + array( 'foo' => array() ) //empty array, must fail because "required" is not false |
| 735 | + ) ); |
| 736 | + |
| 737 | + $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
| 738 | + $correct2, |
| 739 | + array( 'foo' => array( //more than minlength items |
| 740 | + array( 'bar' => true, 'car' => '#115599' ), |
| 741 | + array( 'bar' => false, 'car' => '#123456' ), |
| 742 | + array( 'bar' => true, 'car' => '#ffffff' ), |
| 743 | + array( 'bar' => false, 'car' => '#2357bd' ) |
| 744 | + ) |
| 745 | + ) |
| 746 | + ) ); |
| 747 | + |
| 748 | + //Test with 'required' |
| 749 | + $correct2['fields'][0]['required'] = false; |
| 750 | + $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( |
| 751 | + $correct2, |
| 752 | + array( 'foo' => array() ) //empty array, must be accepted because "required" is false |
| 753 | + ) ); |
| 754 | + |
| 755 | + //Tests matchPrefsWithDescription |
| 756 | + $prefs = array( 'foo' => array( |
| 757 | + array( |
| 758 | + 'bar' => null, |
| 759 | + 'car' => '#115599' |
| 760 | + ), |
| 761 | + array( |
| 762 | + 'bar' => false, |
| 763 | + 'car' => '' |
| 764 | + ), |
| 765 | + array( |
| 766 | + 'bar' => true, |
| 767 | + 'car' => '#ffffff' |
| 768 | + ) |
| 769 | + ) |
| 770 | + ); |
| 771 | + |
| 772 | + |
| 773 | + GadgetPrefs::matchPrefsWithDescription( $correct, $prefs ); |
| 774 | + $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( $correct, $prefs ) ); |
| 775 | + } |
| 776 | + |
| 777 | + //Data provider to be able to reuse a complex preference description for several tests. |
| 778 | + function prefsDescProvider() { |
| 779 | + return array( array( |
| 780 | + array( |
| 781 | + 'fields' => array( |
| 782 | + array( |
| 783 | + 'type' => 'bundle', |
| 784 | + 'sections' => array( |
| 785 | + array( |
| 786 | + 'title' => '@section1', |
| 787 | + 'fields' => array ( |
| 788 | + array( |
| 789 | + 'name' => 'testBoolean', |
| 790 | + 'type' => 'boolean', |
| 791 | + 'label' => '@foo', |
| 792 | + 'default' => true |
| 793 | + ), |
| 794 | + array( |
| 795 | + 'name' => 'testBoolean2', |
| 796 | + 'type' => 'boolean', |
| 797 | + 'label' => '@@foo2', |
| 798 | + 'default' => true |
| 799 | + ), |
| 800 | + array( |
| 801 | + 'name' => 'testNumber', |
| 802 | + 'type' => 'number', |
| 803 | + 'label' => '@foo3', |
| 804 | + 'min' => 2.3, |
| 805 | + 'max' => 13.94, |
| 806 | + 'default' => 7 |
| 807 | + ) |
| 808 | + ) |
| 809 | + ), |
| 810 | + array( |
| 811 | + 'title' => 'Section2', |
| 812 | + 'fields' => array( |
| 813 | + array( |
| 814 | + 'name' => 'testNumber2', |
| 815 | + 'type' => 'number', |
| 816 | + 'label' => 'foo4', |
| 817 | + 'min' => 2.3, |
| 818 | + 'max' => 13.94, |
| 819 | + 'default' => 7 |
| 820 | + ), |
| 821 | + array( |
| 822 | + 'name' => 'testSelect', |
| 823 | + 'type' => 'select', |
| 824 | + 'label' => 'foo', |
| 825 | + 'default' => 3, |
| 826 | + 'options' => array( |
| 827 | + array( 'name' => '@opt1', 'value' => null ), |
| 828 | + array( 'name' => '@opt2', 'value' => true ), |
| 829 | + array( 'name' => 'opt3', 'value' => 3 ), |
| 830 | + array( 'name' => '@opt4', 'value' => 'opt4value' ) |
| 831 | + ) |
| 832 | + ), |
| 833 | + array( |
| 834 | + 'name' => 'testSelect2', |
| 835 | + 'type' => 'select', |
| 836 | + 'label' => 'foo', |
| 837 | + 'default' => 3, |
| 838 | + 'options' => array( |
| 839 | + array( 'name' => '@opt1', 'value' => null ), |
| 840 | + array( 'name' => 'opt2', 'value' => true ), |
| 841 | + array( 'name' => 'opt3', 'value' => 3 ), |
| 842 | + array( 'name' => 'opt4', 'value' => 'opt4value' ) |
| 843 | + ) |
| 844 | + ) |
| 845 | + ) |
| 846 | + ) |
| 847 | + ) |
| 848 | + ) |
| 849 | + ) |
| 850 | + ) |
| 851 | + ) ); |
| 852 | + } |
| 853 | + |
| 854 | + /** |
| 855 | + * Tests Gadget::getDefaults |
| 856 | + * |
| 857 | + * @dataProvider prefsDescProvider |
| 858 | + */ |
| 859 | + function testGetDefaults( $prefsDescription ) { |
| 860 | + $this->assertEquals( GadgetPrefs::getDefaults( $prefsDescription ), array( |
| 861 | + 'testBoolean' => true, |
| 862 | + 'testBoolean2' => true, |
| 863 | + 'testNumber' => 7, |
| 864 | + 'testNumber2' => 7, |
| 865 | + 'testSelect' => 3, |
| 866 | + 'testSelect2' => 3 |
| 867 | + ) ); |
| 868 | + } |
| 869 | + |
| 870 | + /** |
| 871 | + * Tests Gadget::setPrefsDescription, GadgetPrefs::checkPrefsAgainstDescription, |
| 872 | + * GadgetPrefs::matchPrefsWithDescription and Gadget::setPrefs. |
| 873 | + * |
| 874 | + * @dataProvider prefsDescProvider |
| 875 | + */ |
| 876 | + function testSetPrefs( $prefsDescription ) { |
| 877 | + // FIXME this test is broken |
| 878 | + $this->markTestIncomplete( 'Broken for now' ); |
| 879 | + return; |
| 880 | + |
| 881 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $prefsDescription ) ); |
| 882 | + |
| 883 | + $prefs = array( |
| 884 | + 'testBoolean' => false, |
| 885 | + 'testBoolean2' => null, //wrong |
| 886 | + 'testNumber' => 11, |
| 887 | + 'testNumber2' => 45, //wrong |
| 888 | + 'testSelect' => true, |
| 889 | + 'testSelect2' => false //wrong |
| 890 | + ); |
| 891 | + |
| 892 | + $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( $prefsDescription, $prefs ) ); |
| 893 | + |
| 894 | + $prefs2 = $prefs; |
| 895 | + GadgetPrefs::matchPrefsWithDescription( $prefsDescription, $prefs2 ); |
| 896 | + //Now $prefs2 should pass validation |
| 897 | + $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( $prefsDescription, $prefs2 ) ); |
| 898 | + |
| 899 | + //$prefs2 should have testBoolean, testNumber and testSelect unchanged, the other reset to defaults |
| 900 | + $this->assertEquals( $prefs2['testBoolean'], $prefs['testBoolean'] ); |
| 901 | + $this->assertEquals( $prefs2['testNumber'], $prefs['testNumber'] ); |
| 902 | + $this->assertEquals( $prefs2['testSelect'], $prefs['testSelect'] ); |
| 903 | + |
| 904 | + $defaults = GadgetPrefs::getDefaults( $prefsDescription ); |
| 905 | + $this->assertEquals( $prefs2['testBoolean2'], $defaults['testBoolean2'] ); |
| 906 | + $this->assertEquals( $prefs2['testNumber2'], $defaults['testNumber2'] ); |
| 907 | + $this->assertEquals( $prefs2['testSelect2'], $defaults['testSelect2'] ); |
| 908 | + |
| 909 | + $g = $this->create( '*foo[ResourceLoader]| foo.css|foo.js|foo.bar' ); //FIXME |
| 910 | + $g->setPrefsDescription( $prefsDescription ); |
| 911 | + $this->assertTrue( $g->getPrefsDescription() !== null ); |
| 912 | + |
| 913 | + //Setting wrong preferences must fail |
| 914 | + $this->assertFalse( $g->setPrefs( $prefs ) ); |
| 915 | + |
| 916 | + //Setting right preferences must succeed |
| 917 | + $this->assertTrue( $g->setPrefs( $prefs2 ) ); |
| 918 | + |
| 919 | + //Adding a field not in the description must fail |
| 920 | + $prefs2['someUnexistingPreference'] = 'bar'; |
| 921 | + $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( $prefsDescription, $prefs2 ) ); |
| 922 | + } |
| 923 | + |
| 924 | + /** |
| 925 | + * @expectedException MWException |
| 926 | + */ |
| 927 | + function testSetPrefsWithWrongParam() { |
| 928 | + // FIXME this test is broken |
| 929 | + $this->markTestIncomplete( 'Broken for now' ); |
| 930 | + return; |
| 931 | + |
| 932 | + $g = $this->create( '*foo[ResourceLoader]| foo.css|foo.js|foo.bar' ); //FIXME |
| 933 | + $g->setPrefsDescription( array( |
| 934 | + 'fields' => array( |
| 935 | + 'testBoolean' => array( |
| 936 | + 'type' => 'boolean', |
| 937 | + 'label' => 'foo', |
| 938 | + 'default' => true |
| 939 | + ) |
| 940 | + ) |
| 941 | + ) ); |
| 942 | + |
| 943 | + //Call with invalid param |
| 944 | + $g->setPrefs( 'wrongparam' ); |
| 945 | + } |
| 946 | + |
| 947 | + /** |
| 948 | + * Tests GadgetPrefs::simplifyPrefs. |
| 949 | + */ |
| 950 | + function testSimplifyPrefs() { |
| 951 | + $prefsDescription = array( |
| 952 | + 'fields' => array( |
| 953 | + array( |
| 954 | + 'type' => 'boolean', |
| 955 | + 'name' => 'foo', |
| 956 | + 'label' => 'some label', |
| 957 | + 'default' => true |
| 958 | + ), |
| 959 | + array( |
| 960 | + 'type' => 'bundle', |
| 961 | + 'sections' => array( |
| 962 | + array( |
| 963 | + 'name' => 'Section 1', |
| 964 | + 'fields' => array( |
| 965 | + array( |
| 966 | + 'type' => 'boolean', |
| 967 | + 'name' => 'bar', |
| 968 | + 'label' => 'dummy label', |
| 969 | + 'default' => false |
| 970 | + ), |
| 971 | + ) |
| 972 | + ), |
| 973 | + array( |
| 974 | + 'name' => 'Section 2', |
| 975 | + 'fields' => array( |
| 976 | + array( |
| 977 | + 'type' => 'string', |
| 978 | + 'name' => 'baz', |
| 979 | + 'label' => 'A string', |
| 980 | + 'default' => 'qwerty' |
| 981 | + ) |
| 982 | + ) |
| 983 | + ) |
| 984 | + ) |
| 985 | + ), |
| 986 | + array( |
| 987 | + 'type' => 'composite', |
| 988 | + 'name' => 'cmp', |
| 989 | + 'fields' => array( |
| 990 | + array( |
| 991 | + 'type' => 'number', |
| 992 | + 'name' => 'aNumber', |
| 993 | + 'label' => 'A number', |
| 994 | + 'default' => 3.14 |
| 995 | + ), |
| 996 | + array( |
| 997 | + 'type' => 'color', |
| 998 | + 'name' => 'aColor', |
| 999 | + 'label' => 'A color', |
| 1000 | + 'default' => '#a023e2' |
| 1001 | + ) |
| 1002 | + ) |
| 1003 | + ), |
| 1004 | + array( |
| 1005 | + 'type' => 'list', |
| 1006 | + 'name' => 'aList', |
| 1007 | + 'default' => array( 2, 3, 5, 7 ), |
| 1008 | + 'field' => array( |
| 1009 | + 'type' => 'range', |
| 1010 | + 'label' => 'A range', |
| 1011 | + 'min' => 0, |
| 1012 | + 'max' => 256, |
| 1013 | + 'default' => 128 |
| 1014 | + ) |
| 1015 | + ) |
| 1016 | + ) |
| 1017 | + ); |
| 1018 | + |
| 1019 | + $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $prefsDescription ) ); |
| 1020 | + |
| 1021 | + $prefs = array( |
| 1022 | + 'foo' => true, //=default |
| 1023 | + 'bar' => true, |
| 1024 | + 'baz' => 'asdfgh', |
| 1025 | + 'cmp' => array( |
| 1026 | + 'aNumber' => 2.81, |
| 1027 | + 'aColor' => '#a023e2' //=default |
| 1028 | + ), |
| 1029 | + 'aList' => array( 2, 3, 5, 9 ) |
| 1030 | + ); |
| 1031 | + |
| 1032 | + GadgetPrefs::simplifyPrefs( $prefsDescription, $prefs ); |
| 1033 | + $this->assertEquals( |
| 1034 | + $prefs, |
| 1035 | + array( |
| 1036 | + 'bar' => true, |
| 1037 | + 'baz' => 'asdfgh', |
| 1038 | + 'cmp' => array( |
| 1039 | + 'aNumber' => 2.81, |
| 1040 | + ), |
| 1041 | + 'aList' => array( 2, 3, 5, 9 ) |
| 1042 | + ) |
| 1043 | + ); |
| 1044 | + |
| 1045 | + |
| 1046 | + $prefs = array( |
| 1047 | + 'foo' => false, |
| 1048 | + 'bar' => false, //=default |
| 1049 | + 'baz' => 'asdfgh', |
| 1050 | + 'cmp' => array( |
| 1051 | + 'aNumber' => 3.14, //=default |
| 1052 | + 'aColor' => '#a023e2' //=default |
| 1053 | + ), |
| 1054 | + 'aList' => array( 2, 3, 5, 7 ) //=default |
| 1055 | + ); |
| 1056 | + GadgetPrefs::simplifyPrefs( $prefsDescription, $prefs ); |
| 1057 | + $this->assertEquals( |
| 1058 | + $prefs, |
| 1059 | + array( |
| 1060 | + 'foo' => false, |
| 1061 | + 'baz' => 'asdfgh' |
| 1062 | + ) |
| 1063 | + ); |
| 1064 | + } |
| 1065 | + |
| 1066 | + /** |
| 1067 | + * Tests GadgetPrefs::getMessages. |
| 1068 | + * |
| 1069 | + * @dataProvider prefsDescProvider |
| 1070 | + */ |
| 1071 | + function testGetMessages( $prefsDescription ) { |
| 1072 | + $msgs = GadgetPrefs::getMessages( $prefsDescription ); |
| 1073 | + sort( $msgs ); |
| 1074 | + $this->assertEquals( $msgs, array( |
| 1075 | + 'foo', 'foo3', 'opt1', 'opt2', 'opt4', 'section1' |
| 1076 | + ) ); |
| 1077 | + } |
| 1078 | +} |
Property changes on: branches/RL2/extensions/Gadgets/tests/GadgetPrefsTest.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 1079 | + native |
Index: branches/RL2/extensions/Gadgets/tests/GadgetsTest.php |
— | — | @@ -44,1065 +44,4 @@ |
45 | 45 | $this->assertTrue( $g->supportsResourceLoader() ); |
46 | 46 | $this->assertEquals( array( 'jquery.ui' ), $g->getDependencies() ); |
47 | 47 | } |
48 | | - |
49 | | - function testPreferences() { |
50 | | - global $wgUser; |
51 | | - |
52 | | - // This test makes call to the parser which requires valids Outputpage |
53 | | - // and Title objects. Set them up there, they will be released at the |
54 | | - // end of the test. |
55 | | - global $wgOut, $wgTitle; |
56 | | - $old_wgOut = $wgOut; |
57 | | - $old_wgTitle = $wgTitle; |
58 | | - $wgTitle = Title::newFromText( 'Parser test for Gadgets extension' ); |
59 | | - |
60 | | - // Proceed with test setup: |
61 | | - $prefs = array(); |
62 | | - $context = new RequestContext(); |
63 | | - $wgOut = $context->getOutput(); |
64 | | - $wgOut->setTitle( Title::newFromText( 'test' ) ); |
65 | | - |
66 | | - Gadget::loadStructuredList( '* foo | foo.js |
67 | | -==keep-section1== |
68 | | -* bar| bar.js |
69 | | -==remove-section== |
70 | | -* baz [rights=embezzle] |baz.js |
71 | | -==keep-section2== |
72 | | -* quux [rights=read] | quux.js' ); |
73 | | - $this->assertTrue( GadgetsHooks::getPreferences( $wgUser, $prefs ), 'GetPrefences hook should return true' ); |
74 | | - |
75 | | - $options = $prefs['gadgets']['options']; |
76 | | - $this->assertFalse( isset( $options['<gadget-section-remove-section>'] ), 'Must not show empty sections' ); |
77 | | - $this->assertTrue( isset( $options['<gadget-section-keep-section1>'] ) ); |
78 | | - $this->assertTrue( isset( $options['<gadget-section-keep-section2>'] ) ); |
79 | | - |
80 | | - // Restore globals |
81 | | - $wgOut = $old_wgOut; |
82 | | - $wgTitle = $old_wgTitle; |
83 | | - } |
84 | | - |
85 | | - //Test preferences descriptions validator (generic) |
86 | | - function testPrefsDescriptions() { |
87 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( null ) ); |
88 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array() ) ); |
89 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( 'fields' => array() ) ) ); |
90 | | - |
91 | | - //Test with stdClass instead if array |
92 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( (object)array( |
93 | | - 'fields' => array( |
94 | | - array( |
95 | | - 'name' => 'testBoolean', |
96 | | - 'type' => 'boolean', |
97 | | - 'label' => 'foo', |
98 | | - 'default' => 'bar' |
99 | | - ) |
100 | | - ) |
101 | | - ) ) ); |
102 | | - |
103 | | - //Test with wrong type |
104 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
105 | | - 'fields' => array( |
106 | | - array( |
107 | | - 'name' => 'testUnexisting', |
108 | | - 'type' => 'unexistingtype', |
109 | | - 'label' => 'foo', |
110 | | - 'default' => 'bar' |
111 | | - ) |
112 | | - ) |
113 | | - ) ) ); |
114 | | - |
115 | | - //Test with missing name |
116 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
117 | | - 'fields' => array( |
118 | | - array( |
119 | | - 'type' => 'boolean', |
120 | | - 'label' => 'foo', |
121 | | - 'default' => true |
122 | | - ) |
123 | | - ) |
124 | | - ) ) ); |
125 | | - |
126 | | - //Test with wrong preference name |
127 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
128 | | - 'fields' => array( |
129 | | - array( |
130 | | - 'name' => 'testWrongN@me', |
131 | | - 'type' => 'boolean', |
132 | | - 'label' => 'foo', |
133 | | - 'default' => true |
134 | | - ) |
135 | | - ) |
136 | | - ) ) ); |
137 | | - |
138 | | - //Test with two fields with the same name |
139 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
140 | | - 'fields' => array( |
141 | | - array( |
142 | | - 'name' => 'testBoolean', |
143 | | - 'type' => 'boolean', |
144 | | - 'label' => 'foo', |
145 | | - 'default' => true |
146 | | - ), |
147 | | - array( |
148 | | - 'name' => 'testBoolean', |
149 | | - 'type' => 'string', |
150 | | - 'label' => 'foo', |
151 | | - 'default' => 'bar' |
152 | | - ) |
153 | | - ) |
154 | | - ) ) ); |
155 | | - |
156 | | - //Test with fields encoded as associative array instead of regular array |
157 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
158 | | - 'fields' => array( |
159 | | - 'testBoolean' => array( |
160 | | - 'name' => 'testBoolean', |
161 | | - 'type' => 'string', |
162 | | - 'label' => 'foo', |
163 | | - 'default' => 'bar' |
164 | | - ) |
165 | | - ) |
166 | | - ) ) ); |
167 | | - |
168 | | - //Test with too long preference name (41 characters) |
169 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
170 | | - 'fields' => array( |
171 | | - array( |
172 | | - 'name' => 'aPreferenceNameExceedingTheLimitOf40Chars', |
173 | | - 'type' => 'boolean', |
174 | | - 'label' => 'foo', |
175 | | - 'default' => true |
176 | | - ) |
177 | | - ) |
178 | | - ) ) ); |
179 | | - |
180 | | - //This must pass, instead (40 characters is fine) |
181 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( array( |
182 | | - 'fields' => array( |
183 | | - array( |
184 | | - 'name' => 'otherPreferenceNameThatS40CharactersLong', |
185 | | - 'type' => 'boolean', |
186 | | - 'label' => 'foo', |
187 | | - 'default' => true |
188 | | - ) |
189 | | - ) |
190 | | - ) ) ); |
191 | | - |
192 | | - |
193 | | - //Test with an unexisting field parameter |
194 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( array( |
195 | | - 'fields' => array( |
196 | | - array( |
197 | | - 'name' => 'testBoolean', |
198 | | - 'type' => 'boolean', |
199 | | - 'label' => 'foo', |
200 | | - 'default' => true, |
201 | | - 'unexistingParamThatMustFail' => 'foo' |
202 | | - ) |
203 | | - ) |
204 | | - ) ) ); |
205 | | - } |
206 | | - |
207 | | - //Tests for 'label' type preferences |
208 | | - function testPrefsDescriptionsLabel() { |
209 | | - $correct = array( |
210 | | - 'fields' => array( |
211 | | - array( |
212 | | - 'type' => 'label', |
213 | | - 'label' => 'foo' |
214 | | - ) |
215 | | - ) |
216 | | - ); |
217 | | - |
218 | | - //Tests with correct values for 'label' |
219 | | - foreach ( array( '', '@', '@message', 'foo', '@@not message' ) as $def ) { |
220 | | - $correct['fields'][0]['label'] = $def; |
221 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
222 | | - } |
223 | | - |
224 | | - //Tests with wrong values for 'label' |
225 | | - $wrong = $correct; |
226 | | - foreach ( array( 0, 1, true, false, null, array() ) as $label ) { |
227 | | - $wrong['fields'][0]['label'] = $label; |
228 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
229 | | - } |
230 | | - |
231 | | - } |
232 | | - |
233 | | - //Tests for 'boolean' type preferences |
234 | | - function testPrefsDescriptionsBoolean() { |
235 | | - $correct = array( |
236 | | - 'fields' => array( |
237 | | - array( |
238 | | - 'name' => 'testBoolean', |
239 | | - 'type' => 'boolean', |
240 | | - 'label' => 'some label', |
241 | | - 'default' => true |
242 | | - ) |
243 | | - ) |
244 | | - ); |
245 | | - |
246 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
247 | | - |
248 | | - $correct2 = array( |
249 | | - 'fields' => array( |
250 | | - array( |
251 | | - 'name' => 'testBoolean', |
252 | | - 'type' => 'boolean', |
253 | | - 'label' => 'some label', |
254 | | - 'default' => false |
255 | | - ) |
256 | | - ) |
257 | | - ); |
258 | | - |
259 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
260 | | - |
261 | | - //Tests with wrong default values |
262 | | - $wrong = $correct; |
263 | | - foreach ( array( 0, 1, '', 'false', 'true', null, array() ) as $def ) { |
264 | | - $wrong['fields'][0]['default'] = $def; |
265 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
266 | | - } |
267 | | - } |
268 | | - |
269 | | - //Tests for 'string' type preferences |
270 | | - function testPrefsDescriptionsString() { |
271 | | - $correct = array( |
272 | | - 'fields' => array( |
273 | | - array( |
274 | | - 'name' => 'testString', |
275 | | - 'type' => 'string', |
276 | | - 'label' => 'some label', |
277 | | - 'minlength' => 6, |
278 | | - 'maxlength' => 10, |
279 | | - 'default' => 'default' |
280 | | - ) |
281 | | - ) |
282 | | - ); |
283 | | - |
284 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
285 | | - |
286 | | - //Tests with wrong default values (when 'required' is not given) |
287 | | - $wrong = $correct; |
288 | | - foreach ( array( null, '', true, false, 0, 1, array(), 'short', 'veryverylongstring' ) as $def ) { |
289 | | - $wrong['fields'][0]['default'] = $def; |
290 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
291 | | - } |
292 | | - |
293 | | - //Tests with correct default values (when required is not given) |
294 | | - $correct2 = $correct; |
295 | | - foreach ( array( '6chars', '1234567890' ) as $def ) { |
296 | | - $correct2['fields'][0]['default'] = $def; |
297 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
298 | | - } |
299 | | - |
300 | | - //Tests with wrong default values (when 'required' is false) |
301 | | - $wrong = $correct; |
302 | | - $wrong['fields'][0]['required'] = false; |
303 | | - foreach ( array( null, true, false, 0, 1, array(), 'short', 'veryverylongstring' ) as $def ) { |
304 | | - $wrong['fields'][0]['default'] = $def; |
305 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
306 | | - } |
307 | | - |
308 | | - //Tests with correct default values (when required is false) |
309 | | - $correct2 = $correct; |
310 | | - $correct2['fields'][0]['required'] = false; |
311 | | - foreach ( array( '', '6chars', '1234567890' ) as $def ) { |
312 | | - $correct2['fields'][0]['default'] = $def; |
313 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
314 | | - } |
315 | | - |
316 | | - $correct = array( |
317 | | - 'fields' => array( |
318 | | - array( |
319 | | - 'name' => 'testString', |
320 | | - 'type' => 'string', |
321 | | - 'label' => 'some label', |
322 | | - 'default' => '' |
323 | | - ) |
324 | | - ) |
325 | | - ); |
326 | | - |
327 | | - //Test with empty default when "required" is true |
328 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
329 | | - |
330 | | - //Test with empty default when "required" is true |
331 | | - $wrong = $correct; |
332 | | - $wrong['fields'][0]['required'] = true; |
333 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
334 | | - |
335 | | - //Test with empty default when "required" is false and minlength is given |
336 | | - $correct2 = $correct; |
337 | | - $correct2['fields'][0]['required'] = false; |
338 | | - $correct2['fields'][0]['minlength'] = 3; |
339 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
340 | | - } |
341 | | - |
342 | | - //Tests for 'number' type preferences |
343 | | - function testPrefsDescriptionsNumber() { |
344 | | - $correctFloat = array( |
345 | | - 'fields' => array( |
346 | | - array( |
347 | | - 'name' => 'testNumber', |
348 | | - 'type' => 'number', |
349 | | - 'label' => 'some label', |
350 | | - 'min' => -15, |
351 | | - 'max' => 36, |
352 | | - 'required' => true, |
353 | | - 'default' => 3.14 |
354 | | - ) |
355 | | - ) |
356 | | - ); |
357 | | - |
358 | | - $correctInt = array( |
359 | | - 'fields' => array( |
360 | | - array( |
361 | | - 'name' => 'testNumber', |
362 | | - 'type' => 'number', |
363 | | - 'label' => 'some label', |
364 | | - 'min' => -15, |
365 | | - 'max' => 36, |
366 | | - 'integer' => true, |
367 | | - 'required' => true, |
368 | | - 'default' => 12 |
369 | | - ) |
370 | | - ) |
371 | | - ); |
372 | | - |
373 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correctFloat ) ); |
374 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correctInt ) ); |
375 | | - |
376 | | - //Tests with wrong default values (with 'required' = true) |
377 | | - $wrongFloat = $correctFloat; |
378 | | - foreach ( array( '', false, true, null, array(), -100, +100 ) as $def ) { |
379 | | - $wrongFloat['fields'][0]['default'] = $def; |
380 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrongFloat ) ); |
381 | | - } |
382 | | - |
383 | | - $wrongInt = $correctInt; |
384 | | - foreach ( array( '', false, true, null, array(), -100, +100, 2.7182818 ) as $def ) { |
385 | | - $wrongInt['fields'][0]['default'] = $def; |
386 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrongInt ) ); |
387 | | - } |
388 | | - |
389 | | - //If required=false, default=null must be accepted, too |
390 | | - foreach ( array( $correctFloat, $correctInt ) as $correct ) { |
391 | | - $correct['fields'][0]['required'] = false; |
392 | | - $correct['fields'][0]['default'] = null; |
393 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
394 | | - } |
395 | | - } |
396 | | - |
397 | | - //Tests for 'select' type preferences |
398 | | - function testPrefsDescriptionsSelect() { |
399 | | - $correct = array( |
400 | | - 'fields' => array( |
401 | | - array( |
402 | | - 'name' => 'testSelect', |
403 | | - 'type' => 'select', |
404 | | - 'label' => 'some label', |
405 | | - 'default' => 3, |
406 | | - 'options' => array( |
407 | | - array( 'name' => 'opt1', 'value' => null ), |
408 | | - array( 'name' => 'opt2', 'value' => true ), |
409 | | - array( 'name' => 'opt3', 'value' => 3 ), |
410 | | - array( 'name' => 'opt4', 'value' => 'test' ) |
411 | | - ) |
412 | | - ) |
413 | | - ) |
414 | | - ); |
415 | | - |
416 | | - |
417 | | - //Tests with correct default values |
418 | | - $correct2 = $correct; |
419 | | - foreach ( array( null, true, 3, 'test' ) as $def ) { |
420 | | - $correct2['fields'][0]['default'] = $def; |
421 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
422 | | - } |
423 | | - |
424 | | - //Tests with wrong default values |
425 | | - $wrong = $correct; |
426 | | - foreach ( array( '', 'true', 'null', false, array(), 0, 1, 3.0001 ) as $def ) { |
427 | | - $wrong['fields'][0]['default'] = $def; |
428 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
429 | | - } |
430 | | - } |
431 | | - |
432 | | - //Tests for 'range' type preferences |
433 | | - function testPrefsDescriptionsRange() { |
434 | | - $correct = array( |
435 | | - 'fields' => array( |
436 | | - array( |
437 | | - 'name' => 'testRange', |
438 | | - 'type' => 'range', |
439 | | - 'label' => 'some label', |
440 | | - 'default' => 35, |
441 | | - 'min' => 15, |
442 | | - 'max' => 45 |
443 | | - ) |
444 | | - ) |
445 | | - ); |
446 | | - |
447 | | - //Tests with correct default values |
448 | | - $correct2 = $correct; |
449 | | - foreach ( array( 15, 33, 45 ) as $def ) { |
450 | | - $correct2['fields'][0]['default'] = $def; |
451 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
452 | | - } |
453 | | - |
454 | | - //Tests with wrong default values |
455 | | - $wrong = $correct; |
456 | | - foreach ( array( '', true, false, null, array(), '35', 14, 46, 30.2 ) as $def ) { |
457 | | - $wrong['fields'][0]['default'] = $def; |
458 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
459 | | - } |
460 | | - |
461 | | - //Test with max not in the set min + k*step (step not given, so it's 1) |
462 | | - $wrong = $correct; |
463 | | - $wrong['fields'][0]['max'] = 45.5; |
464 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
465 | | - |
466 | | - |
467 | | - //Tests with floating point min, max and step |
468 | | - $correct = array( |
469 | | - 'fields' => array( |
470 | | - array( |
471 | | - 'name' => 'testRange', |
472 | | - 'type' => 'range', |
473 | | - 'label' => 'some label', |
474 | | - 'default' => 0.20, |
475 | | - 'min' => -2.8, |
476 | | - 'max' => 4.2, |
477 | | - 'step' => 0.25 |
478 | | - ) |
479 | | - ) |
480 | | - ); |
481 | | - |
482 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
483 | | - |
484 | | - //Tests with correct default values |
485 | | - $correct2 = $correct; |
486 | | - foreach ( array( -2.8, -2.55, 0.20, 4.2 ) as $def ) { |
487 | | - $correct2['fields'][0]['default'] = $def; |
488 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
489 | | - } |
490 | | - |
491 | | - //Tests with wrong default values |
492 | | - $wrong = $correct; |
493 | | - foreach ( array( '', true, false, null, array(), '0.20', -2.7, 0, 4.199999 ) as $def ) { |
494 | | - $wrong['fields'][0]['default'] = $def; |
495 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
496 | | - } |
497 | | - } |
498 | | - |
499 | | - //Tests for 'date' type preferences |
500 | | - function testPrefsDescriptionsDate() { |
501 | | - $correct = array( |
502 | | - 'fields' => array( |
503 | | - array( |
504 | | - 'name' => 'testDate', |
505 | | - 'type' => 'date', |
506 | | - 'label' => 'some label', |
507 | | - 'default' => null |
508 | | - ) |
509 | | - ) |
510 | | - ); |
511 | | - |
512 | | - //Tests with correct default values |
513 | | - $correct2 = $correct; |
514 | | - foreach ( array( |
515 | | - null, |
516 | | - '2011-07-05T15:00:00Z', |
517 | | - '2011-01-01T00:00:00Z', |
518 | | - '2011-12-31T23:59:59Z', |
519 | | - ) as $def ) |
520 | | - { |
521 | | - $correct2['fields'][0]['default'] = $def; |
522 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
523 | | - } |
524 | | - |
525 | | - //Tests with wrong default values |
526 | | - $wrong = $correct; |
527 | | - foreach ( array( |
528 | | - '', true, false, array(), 0, |
529 | | - '2011-07-05T15:00:00', |
530 | | - '2011-07-05T15:00:61Z', |
531 | | - '2011-07-05T15:61:00Z', |
532 | | - '2011-07-05T25:00:00Z', |
533 | | - '2011-07-32T15:00:00Z', |
534 | | - '2011-07-5T15:00:00Z', |
535 | | - '2011-7-05T15:00:00Z', |
536 | | - '2011-13-05T15:00:00Z', |
537 | | - '2011-07-05T15:00-00Z', |
538 | | - '2011-07-05T15-00:00Z', |
539 | | - '2011-07-05S15:00:00Z', |
540 | | - '2011-07:05T15:00:00Z', |
541 | | - '2011:07-05T15:00:00Z' |
542 | | - ) as $def ) |
543 | | - { |
544 | | - $wrong['fields'][0]['default'] = $def; |
545 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
546 | | - } |
547 | | - } |
548 | | - |
549 | | - //Tests for 'color' type preferences |
550 | | - function testPrefsDescriptionsColor() { |
551 | | - $correct = array( |
552 | | - 'fields' => array( |
553 | | - array( |
554 | | - 'name' => 'testColor', |
555 | | - 'type' => 'color', |
556 | | - 'label' => 'some label', |
557 | | - 'default' => '#123456' |
558 | | - ) |
559 | | - ) |
560 | | - ); |
561 | | - |
562 | | - //Tests with correct default values |
563 | | - $correct2 = $correct; |
564 | | - foreach ( array( |
565 | | - '#000000', |
566 | | - '#ffffff', |
567 | | - '#8ed36e', |
568 | | - ) as $def ) |
569 | | - { |
570 | | - $correct2['fields'][0]['default'] = $def; |
571 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
572 | | - } |
573 | | - |
574 | | - //Tests with wrong default values |
575 | | - $wrong = $correct; |
576 | | - foreach ( array( |
577 | | - '', true, false, null, 0, array(), |
578 | | - '123456', |
579 | | - '#629af', |
580 | | - '##123456', |
581 | | - '#1aefdq', |
582 | | - '#145aeF', //uppercase letters not allowed |
583 | | - '#179', //syntax not allowed |
584 | | - 'red', //syntax not allowed |
585 | | - ) as $def ) |
586 | | - { |
587 | | - $wrong['fields'][0]['default'] = $def; |
588 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
589 | | - } |
590 | | - } |
591 | | - |
592 | | - //Tests for 'composite' type fields |
593 | | - function testPrefsDescriptionsComposite() { |
594 | | - $correct = array( |
595 | | - 'fields' => array( |
596 | | - array( |
597 | | - 'name' => 'foo', |
598 | | - 'type' => 'composite', |
599 | | - 'fields' => array( |
600 | | - array( |
601 | | - 'name' => 'bar', |
602 | | - 'type' => 'boolean', |
603 | | - 'label' => '@msg1', |
604 | | - 'default' => true |
605 | | - ), |
606 | | - array( |
607 | | - 'name' => 'car', |
608 | | - 'type' => 'color', |
609 | | - 'label' => '@msg2', |
610 | | - 'default' => '#123456' |
611 | | - ) |
612 | | - ) |
613 | | - ) |
614 | | - ) |
615 | | - ); |
616 | | - |
617 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
618 | | - $this->assertEquals( |
619 | | - GadgetPrefs::getDefaults( $correct ), |
620 | | - array( 'foo' => array( 'bar' => true, 'car' => '#123456' ) ) |
621 | | - ); |
622 | | - $this->assertEquals( GadgetPrefs::getMessages( $correct ), array( 'msg1', 'msg2' ) ); |
623 | | - |
624 | | - $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( |
625 | | - $correct, |
626 | | - array( 'foo' => array( 'bar' => false, 'car' => '#00aaff' ) ) |
627 | | - ) ); |
628 | | - |
629 | | - $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
630 | | - $correct, |
631 | | - array( 'foo' => array( 'bar' => null, 'car' => '#00aaff' ) ) |
632 | | - ) ); |
633 | | - |
634 | | - $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
635 | | - $correct, |
636 | | - array( 'foo' => array( 'bar' => false, 'car' => '#00aafz' ) ) |
637 | | - ) ); |
638 | | - |
639 | | - $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
640 | | - $correct, |
641 | | - array( 'bar' => false, 'car' => '#00aaff' ) |
642 | | - ) ); |
643 | | - |
644 | | - $prefs = array( |
645 | | - 'foo' => array( |
646 | | - 'bar' => false, |
647 | | - 'car' => null //wrong |
648 | | - ) |
649 | | - ); |
650 | | - |
651 | | - GadgetPrefs::matchPrefsWithDescription( $correct, $prefs ); |
652 | | - //Check if only the wrong subfield has been reset to default value |
653 | | - $this->assertEquals( $prefs, array( 'foo' => array( 'bar' => false, 'car' => '#123456' ) ) ); |
654 | | - } |
655 | | - |
656 | | - //Tests for 'list' type fields |
657 | | - function testPrefsDescriptionsList() { |
658 | | - $correct = array( |
659 | | - 'fields' => array( |
660 | | - array( |
661 | | - 'name' => 'foo', |
662 | | - 'type' => 'list', |
663 | | - 'default' => array(), |
664 | | - 'field' => array( |
665 | | - 'type' => 'composite', |
666 | | - 'fields' => array( |
667 | | - array( |
668 | | - 'name' => 'bar', |
669 | | - 'type' => 'boolean', |
670 | | - 'label' => '@msg1', |
671 | | - 'default' => true |
672 | | - ), |
673 | | - array( |
674 | | - 'name' => 'car', |
675 | | - 'type' => 'color', |
676 | | - 'label' => '@msg2', |
677 | | - 'default' => '#123456' |
678 | | - ) |
679 | | - ) |
680 | | - ) |
681 | | - ) |
682 | | - ) |
683 | | - ); |
684 | | - |
685 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct ) ); |
686 | | - |
687 | | - //Specifying the 'name' member for field must fail |
688 | | - $wrong = $correct; |
689 | | - $wrong['fields'][0]['field']['name'] = 'composite'; |
690 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
691 | | - |
692 | | - |
693 | | - $this->assertEquals( |
694 | | - GadgetPrefs::getDefaults( $correct ), |
695 | | - array( 'foo' => array() ) |
696 | | - ); |
697 | | - |
698 | | - $this->assertEquals( GadgetPrefs::getMessages( $correct ), array( 'msg1', 'msg2' ) ); |
699 | | - |
700 | | - //Tests with correct pref values |
701 | | - $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( |
702 | | - $correct, |
703 | | - array( 'foo' => array() ) |
704 | | - ) ); |
705 | | - |
706 | | - $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( |
707 | | - $correct, |
708 | | - array( 'foo' => array( |
709 | | - array( |
710 | | - 'bar' => true, |
711 | | - 'car' => '#115599' |
712 | | - ), |
713 | | - array( |
714 | | - 'bar' => false, |
715 | | - 'car' => '#123456' |
716 | | - ), |
717 | | - array( |
718 | | - 'bar' => true, |
719 | | - 'car' => '#ffffff' |
720 | | - ) |
721 | | - ) |
722 | | - ) |
723 | | - ) ); |
724 | | - |
725 | | - //Tests with wrong pref values |
726 | | - $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
727 | | - $correct, |
728 | | - array( 'foo' => array( |
729 | | - array( |
730 | | - 'bar' => null, //wrong |
731 | | - 'car' => '#115599' |
732 | | - ) |
733 | | - ) |
734 | | - ) |
735 | | - ) ); |
736 | | - |
737 | | - $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
738 | | - $correct, |
739 | | - array( 'foo' => array( //wrong, not enclosed in array |
740 | | - 'bar' => null, |
741 | | - 'car' => '#115599' |
742 | | - ) |
743 | | - ) |
744 | | - ) ); |
745 | | - |
746 | | - |
747 | | - //Tests with 'minlength' and 'maxlength' options |
748 | | - $wrong = $correct; |
749 | | - $wrong['fields'][0]['minlength'] = 4; |
750 | | - $wrong['fields'][0]['maxlength'] = 3; //maxlength < minlength, wrong |
751 | | - $this->assertFalse( GadgetPrefs::isPrefsDescriptionValid( $wrong ) ); |
752 | | - |
753 | | - $correct2 = $correct; |
754 | | - $correct2['fields'][0]['minlength'] = 2; |
755 | | - $correct2['fields'][0]['maxlength'] = 3; |
756 | | - $correct2['fields'][0]['default'] = array( |
757 | | - array( 'bar' => true, 'car' => '#115599' ), |
758 | | - array( 'bar' => false, 'car' => '#123456' ) |
759 | | - ); |
760 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $correct2 ) ); |
761 | | - |
762 | | - $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
763 | | - $correct2, |
764 | | - array( 'foo' => array( //less than minlength items |
765 | | - array( 'bar' => true, 'car' => '#115599' ) |
766 | | - ) |
767 | | - ) |
768 | | - ) ); |
769 | | - |
770 | | - $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
771 | | - $correct2, |
772 | | - array( 'foo' => array() ) //empty array, must fail because "required" is not false |
773 | | - ) ); |
774 | | - |
775 | | - $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( |
776 | | - $correct2, |
777 | | - array( 'foo' => array( //more than minlength items |
778 | | - array( 'bar' => true, 'car' => '#115599' ), |
779 | | - array( 'bar' => false, 'car' => '#123456' ), |
780 | | - array( 'bar' => true, 'car' => '#ffffff' ), |
781 | | - array( 'bar' => false, 'car' => '#2357bd' ) |
782 | | - ) |
783 | | - ) |
784 | | - ) ); |
785 | | - |
786 | | - //Test with 'required' |
787 | | - $correct2['fields'][0]['required'] = false; |
788 | | - $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( |
789 | | - $correct2, |
790 | | - array( 'foo' => array() ) //empty array, must be accepted because "required" is false |
791 | | - ) ); |
792 | | - |
793 | | - //Tests matchPrefsWithDescription |
794 | | - $prefs = array( 'foo' => array( |
795 | | - array( |
796 | | - 'bar' => null, |
797 | | - 'car' => '#115599' |
798 | | - ), |
799 | | - array( |
800 | | - 'bar' => false, |
801 | | - 'car' => '' |
802 | | - ), |
803 | | - array( |
804 | | - 'bar' => true, |
805 | | - 'car' => '#ffffff' |
806 | | - ) |
807 | | - ) |
808 | | - ); |
809 | | - |
810 | | - |
811 | | - GadgetPrefs::matchPrefsWithDescription( $correct, $prefs ); |
812 | | - $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( $correct, $prefs ) ); |
813 | | - } |
814 | | - |
815 | | - //Data provider to be able to reuse a complex preference description for several tests. |
816 | | - function prefsDescProvider() { |
817 | | - return array( array( |
818 | | - array( |
819 | | - 'fields' => array( |
820 | | - array( |
821 | | - 'type' => 'bundle', |
822 | | - 'sections' => array( |
823 | | - array( |
824 | | - 'title' => '@section1', |
825 | | - 'fields' => array ( |
826 | | - array( |
827 | | - 'name' => 'testBoolean', |
828 | | - 'type' => 'boolean', |
829 | | - 'label' => '@foo', |
830 | | - 'default' => true |
831 | | - ), |
832 | | - array( |
833 | | - 'name' => 'testBoolean2', |
834 | | - 'type' => 'boolean', |
835 | | - 'label' => '@@foo2', |
836 | | - 'default' => true |
837 | | - ), |
838 | | - array( |
839 | | - 'name' => 'testNumber', |
840 | | - 'type' => 'number', |
841 | | - 'label' => '@foo3', |
842 | | - 'min' => 2.3, |
843 | | - 'max' => 13.94, |
844 | | - 'default' => 7 |
845 | | - ) |
846 | | - ) |
847 | | - ), |
848 | | - array( |
849 | | - 'title' => 'Section2', |
850 | | - 'fields' => array( |
851 | | - array( |
852 | | - 'name' => 'testNumber2', |
853 | | - 'type' => 'number', |
854 | | - 'label' => 'foo4', |
855 | | - 'min' => 2.3, |
856 | | - 'max' => 13.94, |
857 | | - 'default' => 7 |
858 | | - ), |
859 | | - array( |
860 | | - 'name' => 'testSelect', |
861 | | - 'type' => 'select', |
862 | | - 'label' => 'foo', |
863 | | - 'default' => 3, |
864 | | - 'options' => array( |
865 | | - array( 'name' => '@opt1', 'value' => null ), |
866 | | - array( 'name' => '@opt2', 'value' => true ), |
867 | | - array( 'name' => 'opt3', 'value' => 3 ), |
868 | | - array( 'name' => '@opt4', 'value' => 'opt4value' ) |
869 | | - ) |
870 | | - ), |
871 | | - array( |
872 | | - 'name' => 'testSelect2', |
873 | | - 'type' => 'select', |
874 | | - 'label' => 'foo', |
875 | | - 'default' => 3, |
876 | | - 'options' => array( |
877 | | - array( 'name' => '@opt1', 'value' => null ), |
878 | | - array( 'name' => 'opt2', 'value' => true ), |
879 | | - array( 'name' => 'opt3', 'value' => 3 ), |
880 | | - array( 'name' => 'opt4', 'value' => 'opt4value' ) |
881 | | - ) |
882 | | - ) |
883 | | - ) |
884 | | - ) |
885 | | - ) |
886 | | - ) |
887 | | - ) |
888 | | - ) |
889 | | - ) ); |
890 | | - } |
891 | | - |
892 | | - /** |
893 | | - * Tests Gadget::getDefaults |
894 | | - * |
895 | | - * @dataProvider prefsDescProvider |
896 | | - */ |
897 | | - function testGetDefaults( $prefsDescription ) { |
898 | | - $this->assertEquals( GadgetPrefs::getDefaults( $prefsDescription ), array( |
899 | | - 'testBoolean' => true, |
900 | | - 'testBoolean2' => true, |
901 | | - 'testNumber' => 7, |
902 | | - 'testNumber2' => 7, |
903 | | - 'testSelect' => 3, |
904 | | - 'testSelect2' => 3 |
905 | | - ) ); |
906 | | - } |
907 | | - |
908 | | - /** |
909 | | - * Tests Gadget::setPrefsDescription, GadgetPrefs::checkPrefsAgainstDescription, |
910 | | - * GadgetPrefs::matchPrefsWithDescription and Gadget::setPrefs. |
911 | | - * |
912 | | - * @dataProvider prefsDescProvider |
913 | | - */ |
914 | | - function testSetPrefs( $prefsDescription ) { |
915 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $prefsDescription ) ); |
916 | | - |
917 | | - $prefs = array( |
918 | | - 'testBoolean' => false, |
919 | | - 'testBoolean2' => null, //wrong |
920 | | - 'testNumber' => 11, |
921 | | - 'testNumber2' => 45, //wrong |
922 | | - 'testSelect' => true, |
923 | | - 'testSelect2' => false //wrong |
924 | | - ); |
925 | | - |
926 | | - $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( $prefsDescription, $prefs ) ); |
927 | | - |
928 | | - $prefs2 = $prefs; |
929 | | - GadgetPrefs::matchPrefsWithDescription( $prefsDescription, $prefs2 ); |
930 | | - //Now $prefs2 should pass validation |
931 | | - $this->assertTrue( GadgetPrefs::checkPrefsAgainstDescription( $prefsDescription, $prefs2 ) ); |
932 | | - |
933 | | - //$prefs2 should have testBoolean, testNumber and testSelect unchanged, the other reset to defaults |
934 | | - $this->assertEquals( $prefs2['testBoolean'], $prefs['testBoolean'] ); |
935 | | - $this->assertEquals( $prefs2['testNumber'], $prefs['testNumber'] ); |
936 | | - $this->assertEquals( $prefs2['testSelect'], $prefs['testSelect'] ); |
937 | | - |
938 | | - $defaults = GadgetPrefs::getDefaults( $prefsDescription ); |
939 | | - $this->assertEquals( $prefs2['testBoolean2'], $defaults['testBoolean2'] ); |
940 | | - $this->assertEquals( $prefs2['testNumber2'], $defaults['testNumber2'] ); |
941 | | - $this->assertEquals( $prefs2['testSelect2'], $defaults['testSelect2'] ); |
942 | | - |
943 | | - $g = $this->create( '*foo[ResourceLoader]| foo.css|foo.js|foo.bar' ); //FIXME |
944 | | - $g->setPrefsDescription( $prefsDescription ); |
945 | | - $this->assertTrue( $g->getPrefsDescription() !== null ); |
946 | | - |
947 | | - //Setting wrong preferences must fail |
948 | | - $this->assertFalse( $g->setPrefs( $prefs ) ); |
949 | | - |
950 | | - //Setting right preferences must succeed |
951 | | - $this->assertTrue( $g->setPrefs( $prefs2 ) ); |
952 | | - |
953 | | - //Adding a field not in the description must fail |
954 | | - $prefs2['someUnexistingPreference'] = 'bar'; |
955 | | - $this->assertFalse( GadgetPrefs::checkPrefsAgainstDescription( $prefsDescription, $prefs2 ) ); |
956 | | - } |
957 | | - |
958 | | - /** |
959 | | - * @expectedException MWException |
960 | | - */ |
961 | | - function testSetPrefsWithWrongParam() { |
962 | | - $g = $this->create( '*foo[ResourceLoader]| foo.css|foo.js|foo.bar' ); //FIXME |
963 | | - $g->setPrefsDescription( array( |
964 | | - 'fields' => array( |
965 | | - 'testBoolean' => array( |
966 | | - 'type' => 'boolean', |
967 | | - 'label' => 'foo', |
968 | | - 'default' => true |
969 | | - ) |
970 | | - ) |
971 | | - ) ); |
972 | | - |
973 | | - //Call with invalid param |
974 | | - $g->setPrefs( 'wrongparam' ); |
975 | | - } |
976 | | - |
977 | | - |
978 | | - /** |
979 | | - * Tests GadgetPrefs::simplifyPrefs. |
980 | | - */ |
981 | | - function testSimplifyPrefs() { |
982 | | - $prefsDescription = array( |
983 | | - 'fields' => array( |
984 | | - array( |
985 | | - 'type' => 'boolean', |
986 | | - 'name' => 'foo', |
987 | | - 'label' => 'some label', |
988 | | - 'default' => true |
989 | | - ), |
990 | | - array( |
991 | | - 'type' => 'bundle', |
992 | | - 'sections' => array( |
993 | | - array( |
994 | | - 'name' => 'Section 1', |
995 | | - 'fields' => array( |
996 | | - array( |
997 | | - 'type' => 'boolean', |
998 | | - 'name' => 'bar', |
999 | | - 'label' => 'dummy label', |
1000 | | - 'default' => false |
1001 | | - ), |
1002 | | - ) |
1003 | | - ), |
1004 | | - array( |
1005 | | - 'name' => 'Section 2', |
1006 | | - 'fields' => array( |
1007 | | - array( |
1008 | | - 'type' => 'string', |
1009 | | - 'name' => 'baz', |
1010 | | - 'label' => 'A string', |
1011 | | - 'default' => 'qwerty' |
1012 | | - ) |
1013 | | - ) |
1014 | | - ) |
1015 | | - ) |
1016 | | - ), |
1017 | | - array( |
1018 | | - 'type' => 'composite', |
1019 | | - 'name' => 'cmp', |
1020 | | - 'fields' => array( |
1021 | | - array( |
1022 | | - 'type' => 'number', |
1023 | | - 'name' => 'aNumber', |
1024 | | - 'label' => 'A number', |
1025 | | - 'default' => 3.14 |
1026 | | - ), |
1027 | | - array( |
1028 | | - 'type' => 'color', |
1029 | | - 'name' => 'aColor', |
1030 | | - 'label' => 'A color', |
1031 | | - 'default' => '#a023e2' |
1032 | | - ) |
1033 | | - ) |
1034 | | - ), |
1035 | | - array( |
1036 | | - 'type' => 'list', |
1037 | | - 'name' => 'aList', |
1038 | | - 'default' => array( 2, 3, 5, 7 ), |
1039 | | - 'field' => array( |
1040 | | - 'type' => 'range', |
1041 | | - 'label' => 'A range', |
1042 | | - 'min' => 0, |
1043 | | - 'max' => 256, |
1044 | | - 'default' => 128 |
1045 | | - ) |
1046 | | - ) |
1047 | | - ) |
1048 | | - ); |
1049 | | - |
1050 | | - $this->assertTrue( GadgetPrefs::isPrefsDescriptionValid( $prefsDescription ) ); |
1051 | | - |
1052 | | - $prefs = array( |
1053 | | - 'foo' => true, //=default |
1054 | | - 'bar' => true, |
1055 | | - 'baz' => 'asdfgh', |
1056 | | - 'cmp' => array( |
1057 | | - 'aNumber' => 2.81, |
1058 | | - 'aColor' => '#a023e2' //=default |
1059 | | - ), |
1060 | | - 'aList' => array( 2, 3, 5, 9 ) |
1061 | | - ); |
1062 | | - |
1063 | | - GadgetPrefs::simplifyPrefs( $prefsDescription, $prefs ); |
1064 | | - $this->assertEquals( |
1065 | | - $prefs, |
1066 | | - array( |
1067 | | - 'bar' => true, |
1068 | | - 'baz' => 'asdfgh', |
1069 | | - 'cmp' => array( |
1070 | | - 'aNumber' => 2.81, |
1071 | | - ), |
1072 | | - 'aList' => array( 2, 3, 5, 9 ) |
1073 | | - ) |
1074 | | - ); |
1075 | | - |
1076 | | - |
1077 | | - $prefs = array( |
1078 | | - 'foo' => false, |
1079 | | - 'bar' => false, //=default |
1080 | | - 'baz' => 'asdfgh', |
1081 | | - 'cmp' => array( |
1082 | | - 'aNumber' => 3.14, //=default |
1083 | | - 'aColor' => '#a023e2' //=default |
1084 | | - ), |
1085 | | - 'aList' => array( 2, 3, 5, 7 ) //=default |
1086 | | - ); |
1087 | | - GadgetPrefs::simplifyPrefs( $prefsDescription, $prefs ); |
1088 | | - $this->assertEquals( |
1089 | | - $prefs, |
1090 | | - array( |
1091 | | - 'foo' => false, |
1092 | | - 'baz' => 'asdfgh' |
1093 | | - ) |
1094 | | - ); |
1095 | | - } |
1096 | | - |
1097 | | - /** |
1098 | | - * Tests GadgetPrefs::getMessages. |
1099 | | - * |
1100 | | - * @dataProvider prefsDescProvider |
1101 | | - */ |
1102 | | - function testGetMessages( $prefsDescription ) { |
1103 | | - $msgs = GadgetPrefs::getMessages( $prefsDescription ); |
1104 | | - sort( $msgs ); |
1105 | | - $this->assertEquals( $msgs, array( |
1106 | | - 'foo', 'foo3', 'opt1', 'opt2', 'opt4', 'section1' |
1107 | | - ) ); |
1108 | | - } |
1109 | 48 | } |
Index: branches/RL2/extensions/Gadgets/Gadgets.php |
— | — | @@ -117,7 +117,7 @@ |
118 | 118 | $wgHooks['TitleIsMovable'][] = 'GadgetsHooks::titleIsMovable'; |
119 | 119 | $wgHooks['TitleMoveComplete'][] = 'GadgetsHooks::cssOrJsPageMove'; |
120 | 120 | $wgHooks['getUserPermissionsErrors'][] = 'GadgetsHooks::getUserPermissionsErrors'; |
121 | | -#$wgHooks['UnitTestsList'][] = 'GadgetsHooks::unitTestsList'; // FIXME: broken |
| 121 | +$wgHooks['UnitTestsList'][] = 'GadgetsHooks::unitTestsList'; |
122 | 122 | |
123 | 123 | $dir = dirname(__FILE__) . '/'; |
124 | 124 | $wgExtensionMessagesFiles['Gadgets'] = $dir . 'Gadgets.i18n.php'; |
Index: branches/RL2/extensions/Gadgets/Gadgets.hooks.php |
— | — | @@ -415,7 +415,8 @@ |
416 | 416 | * @param $files Array: List of extension test files |
417 | 417 | */ |
418 | 418 | public static function unitTestsList( &$files ) { |
419 | | - $files[] = dirname( __FILE__ ) . '/tests/GadgetsTest.php'; |
| 419 | + //$files[] = dirname( __FILE__ ) . '/tests/GadgetsTest.php'; //FIXME broken |
| 420 | + $files[] = dirname( __FILE__ ) . '/tests/GadgetPrefsTest.php'; |
420 | 421 | return true; |
421 | 422 | } |
422 | 423 | |