4 * Unit tests for the HTMLCheckMatrix + HTMLFormField
5 * @todo the tests for the two classes could be split up
7 class HtmlCheckMatrixTest
extends MediaWikiTestCase
{
8 static private $defaultOptions = array(
9 'rows' => array( 'r1', 'r2' ),
10 'columns' => array( 'c1', 'c2' ),
11 'fieldname' => 'test',
15 * @covers HTMLCheckMatrix::__construct
17 public function testPlainInstantiation() {
19 $form = new HTMLCheckMatrix( array() );
20 } catch ( MWException
$e ) {
21 $this->assertInstanceOf( 'HTMLFormFieldRequiredOptionsException', $e );
25 $this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
29 * @covers HTMLCheckMatrix::__construct
31 public function testInstantiationWithMinimumRequiredParameters() {
32 $form = new HTMLCheckMatrix( self
::$defaultOptions );
33 $this->assertTrue( true ); // form instantiation must throw exception on failure
37 * @covers HTMLFormField::validate
39 public function testValidateCallsUserDefinedValidationCallback() {
41 $field = new HTMLCheckMatrix( self
::$defaultOptions +
array(
42 'validation-callback' => function() use ( &$called ) {
47 $this->assertEquals( false, $this->validate( $field, array() ) );
48 $this->assertTrue( $called );
52 * @covers HTMLFormField::validate
54 public function testValidateRequiresArrayInput() {
55 $field = new HTMLCheckMatrix( self
::$defaultOptions );
56 $this->assertEquals( false, $this->validate( $field, null ) );
57 $this->assertEquals( false, $this->validate( $field, true ) );
58 $this->assertEquals( false, $this->validate( $field, 'abc' ) );
59 $this->assertEquals( false, $this->validate( $field, new stdClass
) );
60 $this->assertEquals( true, $this->validate( $field, array() ) );
64 * @covers HTMLFormField::validate
66 public function testValidateAllowsOnlyKnownTags() {
67 $field = new HTMLCheckMatrix( self
::$defaultOptions );
68 $this->assertInternalType( 'string', $this->validate( $field, array( 'foo' ) ) );
72 * @covers HTMLFormField::validate
74 public function testValidateAcceptsPartialTagList() {
75 $field = new HTMLCheckMatrix( self
::$defaultOptions );
76 $this->assertTrue( $this->validate( $field, array() ) );
77 $this->assertTrue( $this->validate( $field, array( 'c1-r1' ) ) );
78 $this->assertTrue( $this->validate( $field, array( 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ) ) );
82 * This form object actually has no visibility into what happens later on, but essentially
83 * if the data submitted by the user passes validate the following is run:
84 * foreach ( $field->filterDataForSubmit( $data ) as $k => $v ) {
85 * $user->setOption( $k, $v );
87 * @covers HTMLFormField::filterDataForSubmit
89 public function testValuesForcedOnRemainOn() {
90 $field = new HTMLCheckMatrix( self
::$defaultOptions +
array(
91 'force-options-on' => array( 'c2-r1' ),
99 $this->assertEquals( $expected, $field->filterDataForSubmit( array() ) );
103 * @covers HTMLFormField::filterDataForSubmit
105 public function testValuesForcedOffRemainOff() {
106 $field = new HTMLCheckMatrix( self
::$defaultOptions +
array(
107 'force-options-off' => array( 'c1-r2', 'c2-r2' ),
115 // array_keys on the result simulates submitting all fields checked
116 $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
119 protected function validate( HTMLFormField
$field, $submitted ) {
120 return $field->validate(
122 array( self
::$defaultOptions['fieldname'] => $submitted )