r90702 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90701‎ | r90702 | r90703 >
Date:09:40, 24 June 2011
Author:salvatoreingala
Status:deferred
Tags:
Comment:
- Code simplification in Gadget::setPrefs
- Added more tests
Modified paths:
  • /branches/salvatoreingala/Gadgets/Gadgets_tests.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/backend/Gadget.php (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/Gadgets_tests.php
@@ -279,4 +279,115 @@
280280 $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) );
281281 }
282282 }
 283+
 284+ //Tests Gadget::setPrefsDescription, Gadget::checkPrefsAgainstDescription,
 285+ //Gadget::matchPrefsWithDescription and Gadget::setPrefs.
 286+ function testSetPrefs() {
 287+ $prefsDescription = array(
 288+ 'fields' => array(
 289+ 'testBoolean' => array(
 290+ 'type' => 'boolean',
 291+ 'label' => 'foo',
 292+ 'default' => true
 293+ ),
 294+ 'testBoolean2' => array(
 295+ 'type' => 'boolean',
 296+ 'label' => 'foo',
 297+ 'default' => true
 298+ ),
 299+ 'testNumber' => array(
 300+ 'type' => 'number',
 301+ 'label' => 'foo',
 302+ 'min' => 2.3,
 303+ 'max' => 13.94,
 304+ 'default' => 7
 305+ ),
 306+ 'testNumber2' => array(
 307+ 'type' => 'number',
 308+ 'label' => 'foo',
 309+ 'min' => 2.3,
 310+ 'max' => 13.94,
 311+ 'default' => 7
 312+ ),
 313+ 'testSelect' => array(
 314+ 'type' => 'select',
 315+ 'label' => 'foo',
 316+ 'default' => 3,
 317+ 'options' => array(
 318+ 'opt1' => null,
 319+ 'opt2' => true,
 320+ 'opt3' => 3,
 321+ 'opt4' => 'opt4value'
 322+ )
 323+ ),
 324+ 'testSelect2' => array(
 325+ 'type' => 'select',
 326+ 'label' => 'foo',
 327+ 'default' => 3,
 328+ 'options' => array(
 329+ 'opt1' => null,
 330+ 'opt2' => true,
 331+ 'opt3' => 3,
 332+ 'opt4' => 'opt4value'
 333+ )
 334+ )
 335+ )
 336+ );
 337+
 338+ $this->assertTrue( Gadget::isPrefsDescriptionValid( $prefsDescription ) );
 339+
 340+ $prefs = array(
 341+ 'testBoolean' => false,
 342+ 'testBoolean2' => null, //wrong
 343+ 'testNumber' => 11,
 344+ 'testNumber2' => 45, //wrong
 345+ 'testSelect' => true,
 346+ 'testSelect2' => false //wrong
 347+ );
 348+
 349+ $this->assertFalse( Gadget::checkPrefsAgainstDescription( $prefsDescription, $prefs ) );
 350+
 351+ $prefs2 = $prefs;
 352+ Gadget::matchPrefsWithDescription( $prefsDescription, $prefs2 );
 353+ //Now $prefs2 should pass validation
 354+ $this->assertTrue( Gadget::checkPrefsAgainstDescription( $prefsDescription, $prefs2 ) );
 355+
 356+ //$prefs2 should have testBoolean, testNumber and testSelect unchanged, the other reset to defaults
 357+ $this->assertEquals( $prefs2['testBoolean'], $prefs['testBoolean'] );
 358+ $this->assertEquals( $prefs2['testNumber'], $prefs['testNumber'] );
 359+ $this->assertEquals( $prefs2['testSelect'], $prefs['testSelect'] );
 360+
 361+ $this->assertEquals( $prefs2['testBoolean2'], $prefsDescription['fields']['testBoolean2']['default'] );
 362+ $this->assertEquals( $prefs2['testNumber2'], $prefsDescription['fields']['testNumber2']['default'] );
 363+ $this->assertEquals( $prefs2['testSelect2'], $prefsDescription['fields']['testSelect2']['default'] );
 364+
 365+ $g = $this->create( '*foo[ResourceLoader]| foo.css|foo.js|foo.bar' );
 366+ $g->setPrefsDescription( $prefsDescription );
 367+ $this->assertTrue( $g->getPrefsDescription() !== null );
 368+
 369+ //Setting wrong preferences must fail
 370+ $this->assertFalse( $g->setPrefs( $prefs ) );
 371+
 372+ //Setting right preferences must succeed
 373+ $this->assertTrue( $g->setPrefs( $prefs2 ) );
 374+ }
 375+
 376+ /**
 377+ * @expectedException MWException
 378+ */
 379+ function testSetPrefsWithWrongParam() {
 380+ $g = $this->create( '*foo[ResourceLoader]| foo.css|foo.js|foo.bar' );
 381+ $g->setPrefsDescription( array(
 382+ 'fields' => array(
 383+ 'testBoolean' => array(
 384+ 'type' => 'boolean',
 385+ 'label' => 'foo',
 386+ 'default' => true
 387+ )
 388+ )
 389+ ) );
 390+
 391+ //Call with invalid param
 392+ $g->setPrefs( 'wrongparam' );
 393+ }
283394 }
Index: branches/salvatoreingala/Gadgets/backend/Gadget.php
@@ -799,18 +799,14 @@
800800 *
801801 * @param $prefs Array: the array of preferences.
802802 * @param $savePrefs boolean: if true, preferences are also saved back to the Database.
 803+ * @throws MWException when $prefs is not an array.
803804 *
804805 * @return boolean: true if validation is passed, false otherwise.
805806 */
806807 public function setPrefs( $prefs, $savePrefs = false ) {
807 -
808 - if ( is_string( $prefs ) ) {
809 - $prefs = FormatJson::decode( $prefs, true );
 808+ if ( !is_array( $prefs ) ) {
 809+ throw new MWException( __METHOD__ . ': $prefs must be an array' );
810810 }
811 -
812 - if ( $prefs === null || !is_array( $prefs ) ) {
813 - throw new MWException( __METHOD__ . ': $prefs must be an array or valid JSON' );
814 - }
815811
816812 $prefsDescription = $this->getPrefsDescription();
817813

Status & tagging log