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