Merge "New getHTML() method for QuickTemplate to get the HTML of a template."
[lhc/web/wiklou.git] / tests / phpunit / includes / HTMLCheckMatrixTest.php
1 <?php
2
3 /**
4 * Unit tests for the HTMLCheckMatrix + HTMLFormField
5 * @todo the tests for the two classes could be split up
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 /**
15 * @covers HTMLCheckMatrix::__construct
16 */
17 public function testPlainInstantiation() {
18 try {
19 $form = new HTMLCheckMatrix( array() );
20 } catch ( MWException $e ) {
21 $this->assertInstanceOf( 'HTMLFormFieldRequiredOptionsException', $e );
22 return;
23 }
24
25 $this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
26 }
27
28 /**
29 * @covers HTMLCheckMatrix::__construct
30 */
31 public function testInstantiationWithMinimumRequiredParameters() {
32 $form = new HTMLCheckMatrix( self::$defaultOptions );
33 $this->assertTrue( true ); // form instantiation must throw exception on failure
34 }
35
36 /**
37 * @covers HTMLFormField::validate
38 */
39 public function testValidateCallsUserDefinedValidationCallback() {
40 $called = false;
41 $field = new HTMLCheckMatrix( self::$defaultOptions + array(
42 'validation-callback' => function() use ( &$called ) {
43 $called = true;
44 return false;
45 },
46 ) );
47 $this->assertEquals( false, $this->validate( $field, array() ) );
48 $this->assertTrue( $called );
49 }
50
51 /**
52 * @covers HTMLFormField::validate
53 */
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() ) );
61 }
62
63 /**
64 * @covers HTMLFormField::validate
65 */
66 public function testValidateAllowsOnlyKnownTags() {
67 $field = new HTMLCheckMatrix( self::$defaultOptions );
68 $this->assertInternalType( 'string', $this->validate( $field, array( 'foo' ) ) );
69 }
70
71 /**
72 * @covers HTMLFormField::validate
73 */
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' ) ) );
79 }
80
81 /**
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 );
86 * }
87 * @covers HTMLFormField::filterDataForSubmit
88 */
89 public function testValuesForcedOnRemainOn() {
90 $field = new HTMLCheckMatrix( self::$defaultOptions + array(
91 'force-options-on' => array( 'c2-r1' ),
92 ) );
93 $expected = array(
94 'c1-r1' => false,
95 'c1-r2' => false,
96 'c2-r1' => true,
97 'c2-r2' => false,
98 );
99 $this->assertEquals( $expected, $field->filterDataForSubmit( array() ) );
100 }
101
102 /**
103 * @covers HTMLFormField::filterDataForSubmit
104 */
105 public function testValuesForcedOffRemainOff() {
106 $field = new HTMLCheckMatrix( self::$defaultOptions + array(
107 'force-options-off' => array( 'c1-r2', 'c2-r2' ),
108 ) );
109 $expected = array(
110 'c1-r1' => true,
111 'c1-r2' => false,
112 'c2-r1' => true,
113 'c2-r2' => false,
114 );
115 // array_keys on the result simulates submitting all fields checked
116 $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
117 }
118
119 protected function validate( HTMLFormField $field, $submitted ) {
120 return $field->validate(
121 $submitted,
122 array( self::$defaultOptions['fieldname'] => $submitted )
123 );
124 }
125 }