Merge "Add $namespaceGenderAliases for 'eo'"
[lhc/web/wiklou.git] / tests / phpunit / includes / HTMLCheckMatrixTest.php
1 <?php
2
3 /**
4 * Unit tests for the HTMLCheckMatrix form field
5 */
6 class HtmlCheckMatrixTest extends MediaWikiTestCase {
7 static private $defaultOptions = array(
8 'rows' => array( 'r1', 'r2' ),
9 'columns' => array( 'c1', 'c2' ),
10 'fieldname' => 'test',
11 );
12
13 public function testPlainInstantiation() {
14 try {
15 $form = new HTMLCheckMatrix( array() );
16 } catch ( MWException $e ) {
17 $this->assertInstanceOf( 'HTMLFormFieldRequiredOptionsException', $e );
18 return;
19 }
20
21 $this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
22 }
23
24 public function testInstantiationWithMinimumRequiredParameters() {
25 $form = new HTMLCheckMatrix( self::$defaultOptions );
26 $this->assertTrue( true ); // form instantiation must throw exception on failure
27 }
28
29 public function testValidateCallsUserDefinedValidationCallback() {
30 $called = false;
31 $field = new HTMLCheckMatrix( self::$defaultOptions + array(
32 'validation-callback' => function() use ( &$called ) {
33 $called = true;
34 return false;
35 },
36 ) );
37 $this->assertEquals( false, $this->validate( $field, array() ) );
38 $this->assertTrue( $called );
39 }
40
41 public function testValidateRequiresArrayInput() {
42 $field = new HTMLCheckMatrix( self::$defaultOptions );
43 $this->assertEquals( false, $this->validate( $field, null ) );
44 $this->assertEquals( false, $this->validate( $field, true ) );
45 $this->assertEquals( false, $this->validate( $field, 'abc' ) );
46 $this->assertEquals( false, $this->validate( $field, new stdClass ) );
47 $this->assertEquals( true, $this->validate( $field, array() ) );
48 }
49
50 public function testValidateAllowsOnlyKnownTags() {
51 $field = new HTMLCheckMatrix( self::$defaultOptions );
52 $this->assertInternalType( 'string', $this->validate( $field, array( 'foo' ) ) );
53 }
54
55 public function testValidateAcceptsPartialTagList() {
56 $field = new HTMLCheckMatrix( self::$defaultOptions );
57 $this->assertTrue( $this->validate( $field, array() ) );
58 $this->assertTrue( $this->validate( $field, array( 'c1-r1' ) ) );
59 $this->assertTrue( $this->validate( $field, array( 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ) ) );
60 }
61
62 /**
63 * This form object actually has no visibility into what happens later on, but essentially
64 * if the data submitted by the user passes validate the following is run:
65 * foreach ( $field->filterDataForSubmit( $data ) as $k => $v ) {
66 * $user->setOption( $k, $v );
67 * }
68 */
69 public function testValuesForcedOnRemainOn() {
70 $field = new HTMLCheckMatrix( self::$defaultOptions + array(
71 'force-options-on' => array( 'c2-r1' ),
72 ) );
73 $expected = array(
74 'c1-r1' => false,
75 'c1-r2' => false,
76 'c2-r1' => true,
77 'c2-r2' => false,
78 );
79 $this->assertEquals( $expected, $field->filterDataForSubmit( array() ) );
80 }
81
82 public function testValuesForcedOffRemainOff() {
83 $field = new HTMLCheckMatrix( self::$defaultOptions + array(
84 'force-options-off' => array( 'c1-r2', 'c2-r2' ),
85 ) );
86 $expected = array(
87 'c1-r1' => true,
88 'c1-r2' => false,
89 'c2-r1' => true,
90 'c2-r2' => false,
91 );
92 // array_keys on the result simulates submitting all fields checked
93 $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
94 }
95
96 protected function validate( HTMLFormField $field, $submitted ) {
97 return $field->validate(
98 $submitted,
99 array( self::$defaultOptions['fieldname'] => $submitted )
100 );
101 }
102 }