http://www.mediawiki.org/wiki/Special:Code/MediaWiki/90702
Revision: 90702
Author: salvatoreingala
Date: 2011-06-24 09:40:45 +0000 (Fri, 24 Jun 2011)
Log Message:
-----------
- Code simplification in Gadget::setPrefs
- Added more tests
Modified Paths:
--------------
branches/salvatoreingala/Gadgets/Gadgets_tests.php
branches/salvatoreingala/Gadgets/backend/Gadget.php
Modified: branches/salvatoreingala/Gadgets/Gadgets_tests.php
===================================================================
--- branches/salvatoreingala/Gadgets/Gadgets_tests.php 2011-06-24 09:36:55 UTC
(rev 90701)
+++ branches/salvatoreingala/Gadgets/Gadgets_tests.php 2011-06-24 09:40:45 UTC
(rev 90702)
@@ -279,4 +279,115 @@
$this->assertFalse( Gadget::isPrefsDescriptionValid(
$wrong ) );
}
}
+
+ //Tests Gadget::setPrefsDescription,
Gadget::checkPrefsAgainstDescription,
+ //Gadget::matchPrefsWithDescription and Gadget::setPrefs.
+ function testSetPrefs() {
+ $prefsDescription = array(
+ 'fields' => array(
+ 'testBoolean' => array(
+ 'type' => 'boolean',
+ 'label' => 'foo',
+ 'default' => true
+ ),
+ 'testBoolean2' => array(
+ 'type' => 'boolean',
+ 'label' => 'foo',
+ 'default' => true
+ ),
+ 'testNumber' => array(
+ 'type' => 'number',
+ 'label' => 'foo',
+ 'min' => 2.3,
+ 'max' => 13.94,
+ 'default' => 7
+ ),
+ 'testNumber2' => array(
+ 'type' => 'number',
+ 'label' => 'foo',
+ 'min' => 2.3,
+ 'max' => 13.94,
+ 'default' => 7
+ ),
+ 'testSelect' => array(
+ 'type' => 'select',
+ 'label' => 'foo',
+ 'default' => 3,
+ 'options' => array(
+ 'opt1' => null,
+ 'opt2' => true,
+ 'opt3' => 3,
+ 'opt4' => 'opt4value'
+ )
+ ),
+ 'testSelect2' => array(
+ 'type' => 'select',
+ 'label' => 'foo',
+ 'default' => 3,
+ 'options' => array(
+ 'opt1' => null,
+ 'opt2' => true,
+ 'opt3' => 3,
+ 'opt4' => 'opt4value'
+ )
+ )
+ )
+ );
+
+ $this->assertTrue( Gadget::isPrefsDescriptionValid(
$prefsDescription ) );
+
+ $prefs = array(
+ 'testBoolean' => false,
+ 'testBoolean2' => null, //wrong
+ 'testNumber' => 11,
+ 'testNumber2' => 45, //wrong
+ 'testSelect' => true,
+ 'testSelect2' => false //wrong
+ );
+
+ $this->assertFalse( Gadget::checkPrefsAgainstDescription(
$prefsDescription, $prefs ) );
+
+ $prefs2 = $prefs;
+ Gadget::matchPrefsWithDescription( $prefsDescription, $prefs2 );
+ //Now $prefs2 should pass validation
+ $this->assertTrue( Gadget::checkPrefsAgainstDescription(
$prefsDescription, $prefs2 ) );
+
+ //$prefs2 should have testBoolean, testNumber and testSelect
unchanged, the other reset to defaults
+ $this->assertEquals( $prefs2['testBoolean'],
$prefs['testBoolean'] );
+ $this->assertEquals( $prefs2['testNumber'],
$prefs['testNumber'] );
+ $this->assertEquals( $prefs2['testSelect'],
$prefs['testSelect'] );
+
+ $this->assertEquals( $prefs2['testBoolean2'],
$prefsDescription['fields']['testBoolean2']['default'] );
+ $this->assertEquals( $prefs2['testNumber2'],
$prefsDescription['fields']['testNumber2']['default'] );
+ $this->assertEquals( $prefs2['testSelect2'],
$prefsDescription['fields']['testSelect2']['default'] );
+
+ $g = $this->create( '*foo[ResourceLoader]|
foo.css|foo.js|foo.bar' );
+ $g->setPrefsDescription( $prefsDescription );
+ $this->assertTrue( $g->getPrefsDescription() !== null );
+
+ //Setting wrong preferences must fail
+ $this->assertFalse( $g->setPrefs( $prefs ) );
+
+ //Setting right preferences must succeed
+ $this->assertTrue( $g->setPrefs( $prefs2 ) );
+ }
+
+ /**
+ * @expectedException MWException
+ */
+ function testSetPrefsWithWrongParam() {
+ $g = $this->create( '*foo[ResourceLoader]|
foo.css|foo.js|foo.bar' );
+ $g->setPrefsDescription( array(
+ 'fields' => array(
+ 'testBoolean' => array(
+ 'type' => 'boolean',
+ 'label' => 'foo',
+ 'default' => true
+ )
+ )
+ ) );
+
+ //Call with invalid param
+ $g->setPrefs( 'wrongparam' );
+ }
}
Modified: branches/salvatoreingala/Gadgets/backend/Gadget.php
===================================================================
--- branches/salvatoreingala/Gadgets/backend/Gadget.php 2011-06-24 09:36:55 UTC
(rev 90701)
+++ branches/salvatoreingala/Gadgets/backend/Gadget.php 2011-06-24 09:40:45 UTC
(rev 90702)
@@ -799,18 +799,14 @@
*
* @param $prefs Array: the array of preferences.
* @param $savePrefs boolean: if true, preferences are also saved back
to the Database.
+ * @throws MWException when $prefs is not an array.
*
* @return boolean: true if validation is passed, false otherwise.
*/
public function setPrefs( $prefs, $savePrefs = false ) {
-
- if ( is_string( $prefs ) ) {
- $prefs = FormatJson::decode( $prefs, true );
+ if ( !is_array( $prefs ) ) {
+ throw new MWException( __METHOD__ . ': $prefs must be
an array' );
}
-
- if ( $prefs === null || !is_array( $prefs ) ) {
- throw new MWException( __METHOD__ . ': $prefs must be
an array or valid JSON' );
- }
$prefsDescription = $this->getPrefsDescription();
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs