Index: branches/salvatoreingala/Gadgets/Gadgets_tests.php |
— | — | @@ -44,7 +44,7 @@ |
45 | 45 | $this->assertEquals( array( 'jquery.ui' ), $g->getDependencies() ); |
46 | 46 | } |
47 | 47 | |
48 | | - function testPreferences() { |
| 48 | + function testOptions() { |
49 | 49 | global $wgUser; |
50 | 50 | |
51 | 51 | // This test makes call to the parser which requires valids Outputpage |
— | — | @@ -79,4 +79,204 @@ |
80 | 80 | $wgOut = $old_wgOut; |
81 | 81 | $wgTitle = $old_wgTitle; |
82 | 82 | } |
| 83 | + |
| 84 | + //Test preferences descriptions validator (generic) |
| 85 | + function testPrefsDescriptions() { |
| 86 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( null ) ); |
| 87 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( array() ) ); |
| 88 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( array( 'fields' => array() ) ) ); |
| 89 | + |
| 90 | + //Test with wrong type |
| 91 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( array( |
| 92 | + 'fields' => array( |
| 93 | + 'testUnexisting' => array( |
| 94 | + 'type' => 'unexistingtype', |
| 95 | + 'label' => 'foo', |
| 96 | + 'default' => 'bar' |
| 97 | + ) |
| 98 | + ) |
| 99 | + ) ) ); |
| 100 | + |
| 101 | + //Test with wrong preference name |
| 102 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( array( |
| 103 | + 'fields' => array( |
| 104 | + 'testWrongN@me' => array( |
| 105 | + 'type' => 'boolean', |
| 106 | + 'label' => 'foo', |
| 107 | + 'default' => true |
| 108 | + ) |
| 109 | + ) |
| 110 | + ) ) ); |
| 111 | + |
| 112 | + //Test with an unexisting field parameter |
| 113 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( array( |
| 114 | + 'fields' => array( |
| 115 | + 'testBoolean' => array( |
| 116 | + 'type' => 'boolean', |
| 117 | + 'label' => 'foo', |
| 118 | + 'default' => true, |
| 119 | + 'unexistingParamThatMustFail' => 'foo' |
| 120 | + ) |
| 121 | + ) |
| 122 | + ) ) ); |
| 123 | + } |
| 124 | + |
| 125 | + //Tests for 'boolean' type preferences |
| 126 | + function testPrefsDescriptionsBoolean() { |
| 127 | + $correct = array( |
| 128 | + 'fields' => array( |
| 129 | + 'testBoolean' => array( |
| 130 | + 'type' => 'boolean', |
| 131 | + 'label' => 'some label', |
| 132 | + 'default' => true |
| 133 | + ) |
| 134 | + ) |
| 135 | + ); |
| 136 | + |
| 137 | + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) ); |
| 138 | + |
| 139 | + $correct2 = array( |
| 140 | + 'fields' => array( |
| 141 | + 'testBoolean' => array( |
| 142 | + 'type' => 'boolean', |
| 143 | + 'label' => 'some label', |
| 144 | + 'default' => false |
| 145 | + ) |
| 146 | + ) |
| 147 | + ); |
| 148 | + |
| 149 | + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) ); |
| 150 | + |
| 151 | + //Tests with wrong default values |
| 152 | + $wrong = $correct; |
| 153 | + foreach ( array( 0, 1, '', 'false', 'true', null, array() ) as $def ) { |
| 154 | + $wrong['fields']['testBoolean']['default'] = $def; |
| 155 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + //Tests for 'string' type preferences |
| 160 | + function testPrefsDescriptionsString() { |
| 161 | + $correct = array( |
| 162 | + 'fields' => array( |
| 163 | + 'testString' => array( |
| 164 | + 'type' => 'string', |
| 165 | + 'label' => 'some label', |
| 166 | + 'minlength' => 6, |
| 167 | + 'maxlength' => 10, |
| 168 | + 'required' => false, |
| 169 | + 'default' => 'default' |
| 170 | + ) |
| 171 | + ) |
| 172 | + ); |
| 173 | + |
| 174 | + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) ); |
| 175 | + |
| 176 | + //Tests with wrong default values |
| 177 | + $wrong = $correct; |
| 178 | + foreach ( array( null, true, false, 0, 1, array(), 'short', 'veryverylongstring' ) as $def ) { |
| 179 | + $wrong['fields']['testString']['default'] = $def; |
| 180 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); |
| 181 | + } |
| 182 | + |
| 183 | + //Tests with correct default values (when required is false) |
| 184 | + $correct2 = $correct; |
| 185 | + foreach ( array( '', '6chars', '1234567890' ) as $def ) { |
| 186 | + $correct2['fields']['testString']['default'] = $def; |
| 187 | + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) ); |
| 188 | + } |
| 189 | + |
| 190 | + //Test with empty default when "required" is true |
| 191 | + $wrong = $correct; |
| 192 | + $wrong['fields']['testString']['required'] = true; |
| 193 | + $wrong['fields']['testString']['default'] = ''; |
| 194 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); |
| 195 | + } |
| 196 | + |
| 197 | + //Tests for 'number' type preferences |
| 198 | + function testPrefsDescriptionsNumber() { |
| 199 | + $correctFloat = array( |
| 200 | + 'fields' => array( |
| 201 | + 'testNumber' => array( |
| 202 | + 'type' => 'number', |
| 203 | + 'label' => 'some label', |
| 204 | + 'min' => -15, |
| 205 | + 'max' => 36, |
| 206 | + 'required' => true, |
| 207 | + 'default' => 3.14 |
| 208 | + ) |
| 209 | + ) |
| 210 | + ); |
| 211 | + |
| 212 | + $correctInt = array( |
| 213 | + 'fields' => array( |
| 214 | + 'testNumber' => array( |
| 215 | + 'type' => 'number', |
| 216 | + 'label' => 'some label', |
| 217 | + 'min' => -15, |
| 218 | + 'max' => 36, |
| 219 | + 'integer' => true, |
| 220 | + 'required' => true, |
| 221 | + 'default' => 12 |
| 222 | + ) |
| 223 | + ) |
| 224 | + ); |
| 225 | + |
| 226 | + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correctFloat ) ); |
| 227 | + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correctInt ) ); |
| 228 | + |
| 229 | + //Tests with wrong default values (with 'required' = true) |
| 230 | + $wrongFloat = $correctFloat; |
| 231 | + foreach ( array( '', false, true, null, array(), -100, +100 ) as $def ) { |
| 232 | + $wrongFloat['fields']['testNumber']['default'] = $def; |
| 233 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrongFloat ) ); |
| 234 | + } |
| 235 | + |
| 236 | + $wrongInt = $correctInt; |
| 237 | + foreach ( array( '', false, true, null, array(), -100, +100, 2.7182818 ) as $def ) { |
| 238 | + $wrongInt['fields']['testNumber']['default'] = $def; |
| 239 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrongInt ) ); |
| 240 | + } |
| 241 | + |
| 242 | + //If required=false, default=null must be accepted, too |
| 243 | + foreach ( array( $correctFloat, $correctInt ) as $correct ) { |
| 244 | + $correct['fields']['testNumber']['required'] = false; |
| 245 | + $correct['fields']['testNumber']['default'] = null; |
| 246 | + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct ) ); |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + //Tests for 'select' type preferences |
| 251 | + function testPrefsDescriptionsSelect() { |
| 252 | + $correct = array( |
| 253 | + 'fields' => array( |
| 254 | + 'testSelect' => array( |
| 255 | + 'type' => 'select', |
| 256 | + 'label' => 'some label', |
| 257 | + 'default' => 3, |
| 258 | + 'options' => array( |
| 259 | + 'opt1' => null, |
| 260 | + 'opt2' => true, |
| 261 | + 'opt3' => 3, |
| 262 | + 'opt4' => 'test' |
| 263 | + ) |
| 264 | + ) |
| 265 | + ) |
| 266 | + ); |
| 267 | + |
| 268 | + |
| 269 | + //Tests with correct default values |
| 270 | + $correct2 = $correct; |
| 271 | + foreach ( array( null, true, 3, 'test' ) as $def ) { |
| 272 | + $correct2['fields']['testSelect']['default'] = $def; |
| 273 | + $this->assertTrue( Gadget::isPrefsDescriptionValid( $correct2 ) ); |
| 274 | + } |
| 275 | + |
| 276 | + //Tests with wrong default values |
| 277 | + $wrong = $correct; |
| 278 | + foreach ( array( '', 'true', 'null', false, array(), 0, 1, 3.0001 ) as $def ) { |
| 279 | + $wrong['fields']['testSelect']['default'] = $def; |
| 280 | + $this->assertFalse( Gadget::isPrefsDescriptionValid( $wrong ) ); |
| 281 | + } |
| 282 | + } |
83 | 283 | } |
Index: branches/salvatoreingala/Gadgets/backend/Gadget.php |
— | — | @@ -73,7 +73,7 @@ |
74 | 74 | 'number' => array( |
75 | 75 | 'default' => array( |
76 | 76 | 'isMandatory' => true, |
77 | | - 'checker' => 'Gadget::isFloatOrInt' |
| 77 | + 'checker' => 'Gadget::isFloatOrIntOrNull' |
78 | 78 | ), |
79 | 79 | 'label' => array( |
80 | 80 | 'isMandatory' => true, |
— | — | @@ -156,9 +156,24 @@ |
157 | 157 | private static function isFloatOrInt( $param ) { |
158 | 158 | return is_float( $param ) || is_int( $param ); |
159 | 159 | } |
| 160 | + |
| 161 | + private static function isFloatOrIntOrNull( $param ) { |
| 162 | + return is_float( $param ) || is_int( $param ) || $param === null; |
| 163 | + } |
160 | 164 | |
161 | 165 | //Further checks for 'number' options |
162 | 166 | private static function checkNumberOptionDefinition( $option ) { |
| 167 | + |
| 168 | + if ( $option['default'] === null ) { |
| 169 | + if ( isset( $option['required'] ) && $option['required'] === false ) { |
| 170 | + //Accept null default if "required" is false |
| 171 | + return true; |
| 172 | + } else { |
| 173 | + //Reject null default if "required" is true |
| 174 | + return false; |
| 175 | + } |
| 176 | + } |
| 177 | + |
163 | 178 | if ( isset( $option['integer'] ) && $option['integer'] === true ) { |
164 | 179 | //Check if 'min', 'max' and 'default' are integers (if given) |
165 | 180 | if ( intval( $option['default'] ) != $option['default'] ) { |
— | — | @@ -575,8 +590,11 @@ |
576 | 591 | |
577 | 592 | //Checks if the given description of the preferences is valid |
578 | 593 | public static function isPrefsDescriptionValid( $prefsDescription ) { |
579 | | - |
580 | | - if ( $prefsDescription === null || !isset( $prefsDescription['fields'] ) ) { |
| 594 | + if ( !is_array( $prefsDescription ) |
| 595 | + || !isset( $prefsDescription['fields'] ) |
| 596 | + || !is_array( $prefsDescription['fields'] ) |
| 597 | + || count( $prefsDescription['fields'] ) == 0 ) |
| 598 | + { |
581 | 599 | return false; |
582 | 600 | } |
583 | 601 | |
— | — | @@ -606,7 +624,10 @@ |
607 | 625 | return false; |
608 | 626 | } |
609 | 627 | |
610 | | - //TODO: check $option name compliance |
| 628 | + //check $option name compliance |
| 629 | + if ( !preg_match( '/^[a-zA-Z_][a-zA-Z0-9_]*$/', $option ) ) { |
| 630 | + return false; |
| 631 | + } |
611 | 632 | |
612 | 633 | //Check if all fields satisfy specification |
613 | 634 | $typeSpec = self::$prefsDescriptionSpecifications[$type]; |