r106531 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106530‎ | r106531 | r106532 >
Date:22:13, 17 December 2011
Author:catrope
Status:ok
Tags:
Comment:
[RL2] Add test suite for the Gadget class, work in progress
Modified paths:
  • /branches/RL2/extensions/Gadgets/Gadgets.hooks.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/tests/GadgetTest.php (added) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/tests/GadgetTest.php
@@ -0,0 +1,83 @@
 2+<?php
 3+
 4+/**
 5+ * @group Gadgets
 6+ */
 7+class GadgetTest extends PHPUnit_Framework_TestCase {
 8+ /**
 9+ * @dataProvider provideValidatePropertiesArray
 10+ */
 11+ public function testValidatePropertiesArray( $input, $expectSuccess, $expectedErrors, $desc ) {
 12+ $status = Gadget::validatePropertiesArray( $input );
 13+ $this->assertEquals( $expectSuccess, $status->isGood(), $desc );
 14+ $this->assertEquals( $expectedErrors, $status->getErrorsArray(), $desc );
 15+ }
 16+
 17+ // Helper function because array_merge_recursive() doesn't work well for overriding arrays with other types
 18+ private static function buildPropertiesArray( $props ) {
 19+ $retval = Gadget::getPropertiesBase();
 20+ foreach ( $props as $outerKey => $arr ) {
 21+ if ( is_array( $arr ) ) {
 22+ foreach ( $arr as $innerKey => $value ) {
 23+ $retval[$outerKey][$innerKey] = $value;
 24+ }
 25+ } else {
 26+ $retval[$outerKey] = $arr;
 27+ }
 28+ }
 29+ return $retval;
 30+ }
 31+
 32+ public function provideValidatePropertiesArray() {
 33+ return array(
 34+ array( null, false, array( array( 'gadgets-validate-invalidjson' ) ), 'only arrays are accepted (testing null)' ),
 35+ array( 'foo', false, array( array( 'gadgets-validate-invalidjson' ) ), 'only arrays are accepted (testing string)' ),
 36+ array( '{"settings": {"rights":[], "default":false,"hidden":false,"shared":true,"category":"foo"},"module":{"scripts":["Foo.js", "Foo2.js"],"styles":["Foo.css"],"dependencies":[],"messages":["sessionfailure"]}}', false, array( array( 'gadgets-validate-invalidjson' ) ), 'only arrays are accepted (testing valid JSON string)' ),
 37+ array( self::buildPropertiesArray( array() ), true, array(), 'base array is accepted' ),
 38+ array( self::buildPropertiesArray( array( 'settings' => 'blah' ) ), false, array( array( 'gadgets-validate-wrongtype', 'settings', 'array', 'string' ) ), 'settings set to a string' ),
 39+ array( self::buildPropertiesArray( array( 'settings' => array( 'rights' => 'protect' ) ) ), false, array( array( 'gadgets-validate-wrongtype', 'settings.rights', 'array', 'string' ) ), 'rights set to a string' ),
 40+ array( self::buildPropertiesArray( array( 'settings' => array( 'default' => 42 ) ) ), false, array( array( 'gadgets-validate-wrongtype', 'settings.default', 'boolean', 'integer' ) ), 'default set to an integer' ),
 41+ array( self::buildPropertiesArray( array( 'settings' => array( 'hidden' => 3.14 ) ) ), false, array( array( 'gadgets-validate-wrongtype', 'settings.hidden', 'boolean', 'double' ) ), 'hidden set to a double' ),
 42+ array( self::buildPropertiesArray( array( 'settings' => array( 'shared' => array( 'foo' => 'bar' ) ) ) ), false, array( array( 'gadgets-validate-wrongtype', 'settings.shared', 'boolean', 'array' ) ), 'shared set to an array' ),
 43+ array( self::buildPropertiesArray( array( 'settings' => array( 'category' => null ) ) ), false, array( array( 'gadgets-validate-wrongtype', 'settings.category', 'string', 'NULL' ) ), 'category set to null' ),
 44+ array( self::buildPropertiesArray( array( 'module' => 'blah' ) ), false, array( array( 'gadgets-validate-wrongtype', 'module', 'array', 'string' ) ), 'module set to a string' ),
 45+ array( self::buildPropertiesArray( array( 'module' => array( 'scripts' => null ) ) ), false, array( array( 'gadgets-validate-wrongtype', 'module.scripts', 'array', 'NULL' ) ), 'scripts set to NULL' ),
 46+ array( self::buildPropertiesArray( array( 'module' => array( 'styles' => (object)array( 'Foo.css' ) ) ) ), false, array( array( 'gadgets-validate-wrongtype', 'module.styles', 'array', 'object' ) ), 'styles set to an object' ),
 47+ array( self::buildPropertiesArray( array( 'module' => array( 'messages' => array( 'foo', 'bar', 1, 'baz' ) ) ) ), false, array( array( 'gadgets-validate-wrongtype', 'module.messages[2]', 'string', 'integer' ) ), 'messages[2] set to an integer' ),
 48+ array( self::buildPropertiesArray( array( 'module' => array( 'dependencies' => array( null ) ) ) ), false, array( array( 'gadgets-validate-wrongtype', 'module.dependencies[0]', 'string', 'NULL' ) ), 'dependencies[0] set to null' ),
 49+ );
 50+ }
 51+
 52+ public function testGetters() {
 53+ $now = wfTimestampNow();
 54+ $data = array(
 55+ 'settings' => array(
 56+ 'rights' => array( 'protect' ),
 57+ 'default' => true,
 58+ 'hidden' => false,
 59+ 'shared' => true,
 60+ 'category' => 'foobar',
 61+ ),
 62+ 'module' => array(
 63+ 'scripts' => array( 'Foo.js', 'Bar.js' ),
 64+ 'styles' => array( 'Foo.css' ),
 65+ 'messages' => array( 'january', 'february' ),
 66+ 'dependencies' => array( 'jquery.ui.button' )
 67+ )
 68+ );
 69+ $g = new Gadget( 'GadgetTest', LocalGadgetRepo::singleton(), $data, wfTimestampNow() );
 70+ $this->assertEquals( $data, $g->getMetadata(), 'getMetadata' );
 71+ $this->assertEquals( FormatJson::encode( $data ), $g->getJSON(), 'getJSON' );
 72+ $this->assertEquals( 'GadgetTest', $g->getId(), 'getId' );
 73+ $this->assertEquals( LocalGadgetRepo::singleton(), $g->getRepo(), 'getRepo' );
 74+ $this->assertEquals( $now, $g->getTimestamp(), 'getTimestamp' );
 75+ $this->assertEquals( $data['settings']['category'], $g->getCategory(), 'getCategory' );
 76+ $this->assertEquals( $data['settings']['default'], $g->isEnabledByDefault(), 'isEnabledByDefault' );
 77+ $this->assertEquals( $data['settings']['rights'], $g->getRequiredRights(), 'getRequiredRights' );
 78+ $this->assertEquals( $data['settings']['hidden'], $g->isHidden(), 'isHidden' );
 79+ $this->assertEquals( $data['settings']['shared'], $g->isShared(), 'isShared' );
 80+ $this->assertEquals( $data['module']['scripts'], $g->getScripts(), 'getScripts' );
 81+ $this->assertEquals( $data['module']['styles'], $g->getStyles(), 'getStyles' );
 82+ $this->assertEquals( $data['module']['dependencies'], $g->getDependencies(), 'getDependencies' );
 83+ }
 84+}
Property changes on: branches/RL2/extensions/Gadgets/tests/GadgetTest.php
___________________________________________________________________
Added: svn:eol-style
185 + native
Index: branches/RL2/extensions/Gadgets/Gadgets.hooks.php
@@ -417,6 +417,7 @@
418418 public static function unitTestsList( &$files ) {
419419 //$files[] = dirname( __FILE__ ) . '/tests/GadgetsTest.php'; //FIXME broken
420420 $files[] = dirname( __FILE__ ) . '/tests/GadgetPrefsTest.php';
 421+ $files[] = dirname( __FILE__ ) . '/tests/GadgetTest.php';
421422 return true;
422423 }
423424

Status & tagging log