Debian Codegraph @

tests/phpunit/unit/includes/htmlform/HTMLCheckMatrixTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\HTMLForm\Field\HTMLCheckMatrix;
4 use MediaWiki\HTMLForm\HTMLForm;
5 use MediaWiki\HTMLForm\HTMLFormField;
6 use MediaWiki\HTMLForm\HTMLFormFieldRequiredOptionsException;
7 use MediaWiki\Message\Message;
8
9 /**
10 * @covers \MediaWiki\HTMLForm\Field\HTMLCheckMatrix
11 */
12 class HTMLCheckMatrixTest extends MediaWikiUnitTestCase {
13 /** @var array */
14 private $defaultOptions = [
15 'rows' => [ 'r1', 'r2' ],
16 'columns' => [ 'c1', 'c2' ],
17 'fieldname' => 'test',
18 ];
19
20 protected function setUp(): void {
21 parent::setUp();
22 $htmlForm = $this->createMock( HTMLForm::class );
23 $htmlForm->method( 'msg' )->willReturnCallback( 'wfMessage' );
24 $this->defaultOptions['parent'] = $htmlForm;
25 }
26
27 public function testPlainInstantiation() {
28 $this->expectException( HTMLFormFieldRequiredOptionsException::class );
29 new HTMLCheckMatrix( [] );
30 }
31
32 public function testInstantiationWithMinimumRequiredParameters() {
33 new HTMLCheckMatrix( $this->defaultOptions );
34 $this->assertTrue( true ); // form instantiation must throw exception on failure
35 }
36
37 public function testValidateCallsUserDefinedValidationCallback() {
38 $called = false;
39 $field = new HTMLCheckMatrix( $this->defaultOptions + [
40 'validation-callback' => static function () use ( &$called ) {
41 $called = true;
42
43 return false;
44 },
45 ] );
46 $this->assertFalse( $this->validate( $field, [] ) );
47 $this->assertTrue( $called );
48 }
49
50 /**
51 * @dataProvider provideValidate
52 */
53 public function testValidate( $expected, $submitted ) {
54 $field = new HTMLCheckMatrix( $this->defaultOptions );
55 $this->assertSame( $expected, $this->validate( $field, $submitted ) );
56 }
57
58 public static function provideValidate() {
59 // $expected, $submitted
60 yield [ false, null ];
61 yield [ false, true ];
62 yield [ false, 'abc' ];
63 yield [ false, (object)[] ];
64 yield [ true, [] ];
65 yield [ true, [ 'c1-r1' ] ];
66 yield [ true, [ 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ] ];
67 }
68
69 public function testValidateAllowsOnlyKnownTags() {
70 $field = new HTMLCheckMatrix( $this->defaultOptions );
71 $this->assertInstanceOf( Message::class, $this->validate( $field, [ 'foo' ] ) );
72 }
73
74 /**
75 * This form object actually has no visibility into what happens later on, but essentially
76 * if the data submitted by the user passes validate the following is run:
77 * foreach ( $field->filterDataForSubmit( $data ) as $k => $v ) {
78 * $userOptionsManager->setOption( $user, $k, $v );
79 * }
80 */
81 public function testValuesForcedOnRemainOn() {
82 $field = new HTMLCheckMatrix( $this->defaultOptions + [
83 'force-options-on' => [ 'c2-r1' ],
84 ] );
85 $expected = [
86 'c1-r1' => false,
87 'c1-r2' => false,
88 'c2-r1' => true,
89 'c2-r2' => false,
90 ];
91 $this->assertEquals( $expected, $field->filterDataForSubmit( [] ) );
92 }
93
94 public function testValuesForcedOffRemainOff() {
95 $field = new HTMLCheckMatrix( $this->defaultOptions + [
96 'force-options-off' => [ 'c1-r2', 'c2-r2' ],
97 ] );
98 $expected = [
99 'c1-r1' => true,
100 'c1-r2' => false,
101 'c2-r1' => true,
102 'c2-r2' => false,
103 ];
104 // array_keys on the result simulates submitting all fields checked
105 $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
106 }
107
108 protected function validate( HTMLFormField $field, $submitted ) {
109 return $field->validate(
110 $submitted,
111 [ $this->defaultOptions['fieldname'] => $submitted ]
112 );
113 }
114
115 }