summaryrefslogtreecommitdiff
path: root/tests/phpunit/includes/HTMLCheckMatrixTest.php
blob: 5bbafd37c1795e1e200e65bd6578a2069ba2fbd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

/**
 * Unit tests for the HTMLCheckMatrix form field
 */
class HtmlCheckMatrixTest extends MediaWikiTestCase {
	static private $defaultOptions = array(
		'rows' => array( 'r1', 'r2' ),
		'columns' => array( 'c1', 'c2' ),
		'fieldname' => 'test',
	);

	public function testPlainInstantiation() {
		try {
			$form = new HTMLCheckMatrix( array() );
		} catch ( MWException $e ) {
			$this->assertInstanceOf( 'HTMLFormFieldRequiredOptionsException', $e );
			return;
		}

		$this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
	}

	public function testInstantiationWithMinimumRequiredParameters() {
		$form = new HTMLCheckMatrix( self::$defaultOptions );
		$this->assertTrue( true ); // form instantiation must throw exception on failure
	}

	public function testValidateCallsUserDefinedValidationCallback() {
		$called = false;
		$field = new HTMLCheckMatrix( self::$defaultOptions + array(
			'validation-callback' => function() use ( &$called ) {
				$called = true;
				return false;
			},
		) );
		$this->assertEquals( false, $this->validate( $field, array() ) );
		$this->assertTrue( $called );
	}

	public function testValidateRequiresArrayInput() {
		$field = new HTMLCheckMatrix( self::$defaultOptions );
		$this->assertEquals( false, $this->validate( $field, null ) );
		$this->assertEquals( false, $this->validate( $field, true ) );
		$this->assertEquals( false, $this->validate( $field, 'abc' ) );
		$this->assertEquals( false, $this->validate( $field, new stdClass ) );
		$this->assertEquals( true, $this->validate( $field, array() ) );
	}

	public function testValidateAllowsOnlyKnownTags() {
		$field = new HTMLCheckMatrix( self::$defaultOptions );
		$this->assertInternalType( 'string', $this->validate( $field, array( 'foo' ) ) );
	}

	public function testValidateAcceptsPartialTagList() {
		$field = new HTMLCheckMatrix( self::$defaultOptions );
		$this->assertTrue( $this->validate( $field, array() ) );
		$this->assertTrue( $this->validate( $field, array( 'c1-r1' ) ) );
		$this->assertTrue( $this->validate( $field, array( 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ) ) );
	}

	/**
	 * This form object actually has no visibility into what happens later on, but essentially
	 * if the data submitted by the user passes validate the following is run:
	 * foreach ( $field->filterDataForSubmit( $data ) as $k => $v ) {
	 *     $user->setOption( $k, $v );
	 * }
	 */
	public function testValuesForcedOnRemainOn() {
		$field = new HTMLCheckMatrix( self::$defaultOptions + array(
			'force-options-on' => array( 'c2-r1' ),
		) );
		$expected = array(
			'c1-r1' => false,
			'c1-r2' => false,
			'c2-r1' => true,
			'c2-r2' => false,
		);
		$this->assertEquals( $expected, $field->filterDataForSubmit( array() ) );
	}

	public function testValuesForcedOffRemainOff() {
		$field = new HTMLCheckMatrix( self::$defaultOptions + array(
			'force-options-off' => array( 'c1-r2', 'c2-r2' ),
		) );
		$expected = array(
			'c1-r1' => true,
			'c1-r2' => false,
			'c2-r1' => true,
			'c2-r2' => false,
		);
		// array_keys on the result simulates submitting all fields checked
		$this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
	}

	protected function validate( HTMLFormField $field, $submitted ) {
		return $field->validate(
			$submitted,
			array( self::$defaultOptions['fieldname'] => $submitted )
		);
	}
}