MediaWiki  REL1_23
HTMLCheckMatrixTest.php
Go to the documentation of this file.
00001 <?php
00002 
00007 class HtmlCheckMatrixTest extends MediaWikiTestCase {
00008     static private $defaultOptions = array(
00009         'rows' => array( 'r1', 'r2' ),
00010         'columns' => array( 'c1', 'c2' ),
00011         'fieldname' => 'test',
00012     );
00013 
00014     public function testPlainInstantiation() {
00015         try {
00016             new HTMLCheckMatrix( array() );
00017         } catch ( MWException $e ) {
00018             $this->assertInstanceOf( 'HTMLFormFieldRequiredOptionsException', $e );
00019             return;
00020         }
00021 
00022         $this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
00023     }
00024 
00025     public function testInstantiationWithMinimumRequiredParameters() {
00026         new HTMLCheckMatrix( self::$defaultOptions );
00027         $this->assertTrue( true ); // form instantiation must throw exception on failure
00028     }
00029 
00030     public function testValidateCallsUserDefinedValidationCallback() {
00031         $called = false;
00032         $field = new HTMLCheckMatrix( self::$defaultOptions + array(
00033                 'validation-callback' => function() use ( &$called ) {
00034                         $called = true;
00035                         return false;
00036                     },
00037             ) );
00038         $this->assertEquals( false, $this->validate( $field, array() ) );
00039         $this->assertTrue( $called );
00040     }
00041 
00042     public function testValidateRequiresArrayInput() {
00043         $field = new HTMLCheckMatrix( self::$defaultOptions );
00044         $this->assertEquals( false, $this->validate( $field, null ) );
00045         $this->assertEquals( false, $this->validate( $field, true ) );
00046         $this->assertEquals( false, $this->validate( $field, 'abc' ) );
00047         $this->assertEquals( false, $this->validate( $field, new stdClass ) );
00048         $this->assertEquals( true, $this->validate( $field, array() ) );
00049     }
00050 
00051     public function testValidateAllowsOnlyKnownTags() {
00052         $field = new HTMLCheckMatrix( self::$defaultOptions );
00053         $this->assertInternalType( 'string', $this->validate( $field, array( 'foo' ) ) );
00054     }
00055 
00056     public function testValidateAcceptsPartialTagList() {
00057         $field = new HTMLCheckMatrix( self::$defaultOptions );
00058         $this->assertTrue( $this->validate( $field, array() ) );
00059         $this->assertTrue( $this->validate( $field, array( 'c1-r1' ) ) );
00060         $this->assertTrue( $this->validate( $field, array( 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ) ) );
00061     }
00062 
00070     public function testValuesForcedOnRemainOn() {
00071         $field = new HTMLCheckMatrix( self::$defaultOptions + array(
00072                 'force-options-on' => array( 'c2-r1' ),
00073             ) );
00074         $expected = array(
00075             'c1-r1' => false,
00076             'c1-r2' => false,
00077             'c2-r1' => true,
00078             'c2-r2' => false,
00079         );
00080         $this->assertEquals( $expected, $field->filterDataForSubmit( array() ) );
00081     }
00082 
00083     public function testValuesForcedOffRemainOff() {
00084         $field = new HTMLCheckMatrix( self::$defaultOptions + array(
00085                 'force-options-off' => array( 'c1-r2', 'c2-r2' ),
00086             ) );
00087         $expected = array(
00088             'c1-r1' => true,
00089             'c1-r2' => false,
00090             'c2-r1' => true,
00091             'c2-r2' => false,
00092         );
00093         // array_keys on the result simulates submitting all fields checked
00094         $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
00095     }
00096 
00097     protected function validate( HTMLFormField $field, $submitted ) {
00098         return $field->validate(
00099             $submitted,
00100             array( self::$defaultOptions['fieldname'] => $submitted )
00101         );
00102     }
00103 
00104 }