http://www.mediawiki.org/wiki/Special:Code/MediaWiki/90660
Revision: 90660 Author: salvatoreingala Date: 2011-06-23 14:04:05 +0000 (Thu, 23 Jun 2011) Log Message: ----------- - Minor fixes in Gadget::isPrefsDescriptionValid - Added unit tests for Gadget::isPrefsDescriptionValid 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-23 09:52:37 UTC (rev 90659) +++ branches/salvatoreingala/Gadgets/Gadgets_tests.php 2011-06-23 14:04:05 UTC (rev 90660) @@ -44,7 +44,7 @@ $this->assertEquals( array( 'jquery.ui' ), $g->getDependencies() ); } - function testPreferences() { + function testOptions() { global $wgUser; // This test makes call to the parser which requires valids Outputpage @@ -79,4 +79,204 @@ $wgOut = $old_wgOut; $wgTitle = $old_wgTitle; } + + //Test preferences descriptions validator (generic) + function testPrefsDescriptions() { + $this->assertFalse( Gadget::isPrefsDescriptionValid( null ) ); + $this->assertFalse( Gadget::isPrefsDescriptionValid( array() ) ); + $this->assertFalse( Gadget::isPrefsDescriptionValid( array( 'fields' => array() ) ) ); + + //Test with wrong type + $this->assertFalse( Gadget::isPrefsDescriptionValid( array( + 'fields' => array( + 'testUnexisting' => array( + 'type' => 'unexistingtype', + 'label' => 'foo', + 'default' => 'bar' + ) + ) + ) ) ); + + //Test with wrong preference name + $this->assertFalse( Gadget::isPrefsDescriptionValid( array( + 'fields' => array( + 'testWrongN@me' => array( + 'type' => 'boolean', + 'label' => 'foo', + 'default' => true + ) + ) + ) ) ); + + //Test with an unexisting field parameter + $this->assertFalse( Gadget::isPrefsDescriptionValid( array( + 'fields' => array( + 'testBoolean' => array( + 'type' => 'boolean', + 'label' => 'foo', + 'default' => true, + 'unexistingParamThatMustFail' => 'foo' + ) + ) + ) ) ); + } + + //Tests for 'boolean' type preferences + function testPrefsDescriptionsBoolean() { + $correct = array( + 'fields' => array( + 'testBoolean' => array( + 'type' => 'boolean', + 'label' => 'some label', + 'default' => true + ) + ) + ); + + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) ); + + $correct2 = array( + 'fields' => array( + 'testBoolean' => array( + 'type' => 'boolean', + 'label' => 'some label', + 'default' => false + ) + ) + ); + + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) ); + + //Tests with wrong default values + $wrong = $correct; + foreach ( array( 0, 1, '', 'false', 'true', null, array() ) as $def ) { + $wrong['fields']['testBoolean']['default'] = $def; + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); + } + } + + //Tests for 'string' type preferences + function testPrefsDescriptionsString() { + $correct = array( + 'fields' => array( + 'testString' => array( + 'type' => 'string', + 'label' => 'some label', + 'minlength' => 6, + 'maxlength' => 10, + 'required' => false, + 'default' => 'default' + ) + ) + ); + + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) ); + + //Tests with wrong default values + $wrong = $correct; + foreach ( array( null, true, false, 0, 1, array(), 'short', 'veryverylongstring' ) as $def ) { + $wrong['fields']['testString']['default'] = $def; + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); + } + + //Tests with correct default values (when required is false) + $correct2 = $correct; + foreach ( array( '', '6chars', '1234567890' ) as $def ) { + $correct2['fields']['testString']['default'] = $def; + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) ); + } + + //Test with empty default when "required" is true + $wrong = $correct; + $wrong['fields']['testString']['required'] = true; + $wrong['fields']['testString']['default'] = ''; + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); + } + + //Tests for 'number' type preferences + function testPrefsDescriptionsNumber() { + $correctFloat = array( + 'fields' => array( + 'testNumber' => array( + 'type' => 'number', + 'label' => 'some label', + 'min' => -15, + 'max' => 36, + 'required' => true, + 'default' => 3.14 + ) + ) + ); + + $correctInt = array( + 'fields' => array( + 'testNumber' => array( + 'type' => 'number', + 'label' => 'some label', + 'min' => -15, + 'max' => 36, + 'integer' => true, + 'required' => true, + 'default' => 12 + ) + ) + ); + + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correctFloat ) ); + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correctInt ) ); + + //Tests with wrong default values (with 'required' = true) + $wrongFloat = $correctFloat; + foreach ( array( '', false, true, null, array(), -100, +100 ) as $def ) { + $wrongFloat['fields']['testNumber']['default'] = $def; + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrongFloat ) ); + } + + $wrongInt = $correctInt; + foreach ( array( '', false, true, null, array(), -100, +100, 2.7182818 ) as $def ) { + $wrongInt['fields']['testNumber']['default'] = $def; + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrongInt ) ); + } + + //If required=false, default=null must be accepted, too + foreach ( array( $correctFloat, $correctInt ) as $correct ) { + $correct['fields']['testNumber']['required'] = false; + $correct['fields']['testNumber']['default'] = null; + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) ); + } + } + + //Tests for 'select' type preferences + function testPrefsDescriptionsSelect() { + $correct = array( + 'fields' => array( + 'testSelect' => array( + 'type' => 'select', + 'label' => 'some label', + 'default' => 3, + 'options' => array( + 'opt1' => null, + 'opt2' => true, + 'opt3' => 3, + 'opt4' => 'test' + ) + ) + ) + ); + + + //Tests with correct default values + $correct2 = $correct; + foreach ( array( null, true, 3, 'test' ) as $def ) { + $correct2['fields']['testSelect']['default'] = $def; + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) ); + } + + //Tests with wrong default values + $wrong = $correct; + foreach ( array( '', 'true', 'null', false, array(), 0, 1, 3.0001 ) as $def ) { + $wrong['fields']['testSelect']['default'] = $def; + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); + } + } } Modified: branches/salvatoreingala/Gadgets/backend/Gadget.php =================================================================== --- branches/salvatoreingala/Gadgets/backend/Gadget.php 2011-06-23 09:52:37 UTC (rev 90659) +++ branches/salvatoreingala/Gadgets/backend/Gadget.php 2011-06-23 14:04:05 UTC (rev 90660) @@ -73,7 +73,7 @@ 'number' => array( 'default' => array( 'isMandatory' => true, - 'checker' => 'Gadget::isFloatOrInt' + 'checker' => 'Gadget::isFloatOrIntOrNull' ), 'label' => array( 'isMandatory' => true, @@ -156,9 +156,24 @@ private static function isFloatOrInt( $param ) { return is_float( $param ) || is_int( $param ); } + + private static function isFloatOrIntOrNull( $param ) { + return is_float( $param ) || is_int( $param ) || $param === null; + } //Further checks for 'number' options private static function checkNumberOptionDefinition( $option ) { + + if ( $option['default'] === null ) { + if ( isset( $option['required'] ) && $option['required'] === false ) { + //Accept null default if "required" is false + return true; + } else { + //Reject null default if "required" is true + return false; + } + } + if ( isset( $option['integer'] ) && $option['integer'] === true ) { //Check if 'min', 'max' and 'default' are integers (if given) if ( intval( $option['default'] ) != $option['default'] ) { @@ -575,8 +590,11 @@ //Checks if the given description of the preferences is valid public static function isPrefsDescriptionValid( $prefsDescription ) { - - if ( $prefsDescription === null || !isset( $prefsDescription['fields'] ) ) { + if ( !is_array( $prefsDescription ) + || !isset( $prefsDescription['fields'] ) + || !is_array( $prefsDescription['fields'] ) + || count( $prefsDescription['fields'] ) == 0 ) + { return false; } @@ -606,7 +624,10 @@ return false; } - //TODO: check $option name compliance + //check $option name compliance + if ( !preg_match( '/^[a-zA-Z_][a-zA-Z0-9_]*$/', $option ) ) { + return false; + } //Check if all fields satisfy specification $typeSpec = self::$prefsDescriptionSpecifications[$type]; _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs