MediaWiki  REL1_24
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 
00036                 return false;
00037             },
00038         ) );
00039         $this->assertEquals( false, $this->validate( $field, array() ) );
00040         $this->assertTrue( $called );
00041     }
00042 
00043     public function testValidateRequiresArrayInput() {
00044         $field = new HTMLCheckMatrix( self::$defaultOptions );
00045         $this->assertEquals( false, $this->validate( $field, null ) );
00046         $this->assertEquals( false, $this->validate( $field, true ) );
00047         $this->assertEquals( false, $this->validate( $field, 'abc' ) );
00048         $this->assertEquals( false, $this->validate( $field, new stdClass ) );
00049         $this->assertEquals( true, $this->validate( $field, array() ) );
00050     }
00051 
00052     public function testValidateAllowsOnlyKnownTags() {
00053         $field = new HTMLCheckMatrix( self::$defaultOptions );
00054         $this->assertInternalType( 'string', $this->validate( $field, array( 'foo' ) ) );
00055     }
00056 
00057     public function testValidateAcceptsPartialTagList() {
00058         $field = new HTMLCheckMatrix( self::$defaultOptions );
00059         $this->assertTrue( $this->validate( $field, array() ) );
00060         $this->assertTrue( $this->validate( $field, array( 'c1-r1' ) ) );
00061         $this->assertTrue( $this->validate( $field, array( 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ) ) );
00062     }
00063 
00071     public function testValuesForcedOnRemainOn() {
00072         $field = new HTMLCheckMatrix( self::$defaultOptions + array(
00073                 'force-options-on' => array( 'c2-r1' ),
00074             ) );
00075         $expected = array(
00076             'c1-r1' => false,
00077             'c1-r2' => false,
00078             'c2-r1' => true,
00079             'c2-r2' => false,
00080         );
00081         $this->assertEquals( $expected, $field->filterDataForSubmit( array() ) );
00082     }
00083 
00084     public function testValuesForcedOffRemainOff() {
00085         $field = new HTMLCheckMatrix( self::$defaultOptions + array(
00086                 'force-options-off' => array( 'c1-r2', 'c2-r2' ),
00087             ) );
00088         $expected = array(
00089             'c1-r1' => true,
00090             'c1-r2' => false,
00091             'c2-r1' => true,
00092             'c2-r2' => false,
00093         );
00094         // array_keys on the result simulates submitting all fields checked
00095         $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
00096     }
00097 
00098     protected function validate( HTMLFormField $field, $submitted ) {
00099         return $field->validate(
00100             $submitted,
00101             array( self::$defaultOptions['fieldname'] => $submitted )
00102         );
00103     }
00104 
00105 }