Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / htmlform / HTMLCheckMatrixTest.php
blob5a822f531854ff43d215dd3e667643e03d46531e
1 <?php
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',
14 public function testPlainInstantiation() {
15 try {
16 new HTMLCheckMatrix( array() );
17 } catch ( MWException $e ) {
18 $this->assertInstanceOf( 'HTMLFormFieldRequiredOptionsException', $e );
19 return;
22 $this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
25 public function testInstantiationWithMinimumRequiredParameters() {
26 new HTMLCheckMatrix( self::$defaultOptions );
27 $this->assertTrue( true ); // form instantiation must throw exception on failure
30 public function testValidateCallsUserDefinedValidationCallback() {
31 $called = false;
32 $field = new HTMLCheckMatrix( self::$defaultOptions + array(
33 'validation-callback' => function () use ( &$called ) {
34 $called = true;
36 return false;
38 ) );
39 $this->assertEquals( false, $this->validate( $field, array() ) );
40 $this->assertTrue( $called );
43 public function testValidateRequiresArrayInput() {
44 $field = new HTMLCheckMatrix( self::$defaultOptions );
45 $this->assertEquals( false, $this->validate( $field, null ) );
46 $this->assertEquals( false, $this->validate( $field, true ) );
47 $this->assertEquals( false, $this->validate( $field, 'abc' ) );
48 $this->assertEquals( false, $this->validate( $field, new stdClass ) );
49 $this->assertEquals( true, $this->validate( $field, array() ) );
52 public function testValidateAllowsOnlyKnownTags() {
53 $field = new HTMLCheckMatrix( self::$defaultOptions );
54 $this->assertInternalType( 'string', $this->validate( $field, array( 'foo' ) ) );
57 public function testValidateAcceptsPartialTagList() {
58 $field = new HTMLCheckMatrix( self::$defaultOptions );
59 $this->assertTrue( $this->validate( $field, array() ) );
60 $this->assertTrue( $this->validate( $field, array( 'c1-r1' ) ) );
61 $this->assertTrue( $this->validate( $field, array( 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ) ) );
64 /**
65 * This form object actually has no visibility into what happens later on, but essentially
66 * if the data submitted by the user passes validate the following is run:
67 * foreach ( $field->filterDataForSubmit( $data ) as $k => $v ) {
68 * $user->setOption( $k, $v );
69 * }
71 public function testValuesForcedOnRemainOn() {
72 $field = new HTMLCheckMatrix( self::$defaultOptions + array(
73 'force-options-on' => array( 'c2-r1' ),
74 ) );
75 $expected = array(
76 'c1-r1' => false,
77 'c1-r2' => false,
78 'c2-r1' => true,
79 'c2-r2' => false,
81 $this->assertEquals( $expected, $field->filterDataForSubmit( array() ) );
84 public function testValuesForcedOffRemainOff() {
85 $field = new HTMLCheckMatrix( self::$defaultOptions + array(
86 'force-options-off' => array( 'c1-r2', 'c2-r2' ),
87 ) );
88 $expected = array(
89 'c1-r1' => true,
90 'c1-r2' => false,
91 'c2-r1' => true,
92 'c2-r2' => false,
94 // array_keys on the result simulates submitting all fields checked
95 $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
98 protected function validate( HTMLFormField $field, $submitted ) {
99 return $field->validate(
100 $submitted,
101 array( self::$defaultOptions['fieldname'] => $submitted )