4 * @covers HTMLCheckMatrix
6 class HTMLCheckMatrixTest
extends MediaWikiTestCase
{
7 private static $defaultOptions = [
8 'rows' => [ 'r1', 'r2' ],
9 'columns' => [ 'c1', 'c2' ],
10 'fieldname' => 'test',
13 public function testPlainInstantiation() {
15 new HTMLCheckMatrix( [] );
16 } catch ( MWException
$e ) {
17 $this->assertInstanceOf( HTMLFormFieldRequiredOptionsException
::class, $e );
21 $this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
24 public function testInstantiationWithMinimumRequiredParameters() {
25 new HTMLCheckMatrix( self
::$defaultOptions );
26 $this->assertTrue( true ); // form instantiation must throw exception on failure
29 public function testValidateCallsUserDefinedValidationCallback() {
31 $field = new HTMLCheckMatrix( self
::$defaultOptions +
[
32 'validation-callback' => function () use ( &$called ) {
38 $this->assertEquals( false, $this->validate( $field, [] ) );
39 $this->assertTrue( $called );
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, [] ) );
51 public function testValidateAllowsOnlyKnownTags() {
52 $field = new HTMLCheckMatrix( self
::$defaultOptions );
53 $this->assertInstanceOf( Message
::class, $this->validate( $field, [ 'foo' ] ) );
56 public function testValidateAcceptsPartialTagList() {
57 $field = new HTMLCheckMatrix( self
::$defaultOptions );
58 $this->assertTrue( $this->validate( $field, [] ) );
59 $this->assertTrue( $this->validate( $field, [ 'c1-r1' ] ) );
60 $this->assertTrue( $this->validate( $field, [ 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ] ) );
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 );
70 public function testValuesForcedOnRemainOn() {
71 $field = new HTMLCheckMatrix( self
::$defaultOptions +
[
72 'force-options-on' => [ 'c2-r1' ],
80 $this->assertEquals( $expected, $field->filterDataForSubmit( [] ) );
83 public function testValuesForcedOffRemainOff() {
84 $field = new HTMLCheckMatrix( self
::$defaultOptions +
[
85 'force-options-off' => [ 'c1-r2', 'c2-r2' ],
93 // array_keys on the result simulates submitting all fields checked
94 $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
97 protected function validate( HTMLFormField
$field, $submitted ) {
98 return $field->validate(
100 [ self
::$defaultOptions['fieldname'] => $submitted ]