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