MediaWiki  1.27.2
HTMLCheckMatrixTest.php
Go to the documentation of this file.
1 <?php
2 
8  static private $defaultOptions = [
9  'rows' => [ 'r1', 'r2' ],
10  'columns' => [ 'c1', 'c2' ],
11  'fieldname' => 'test',
12  ];
13 
14  public function testPlainInstantiation() {
15  try {
16  new HTMLCheckMatrix( [] );
17  } catch ( MWException $e ) {
18  $this->assertInstanceOf( 'HTMLFormFieldRequiredOptionsException', $e );
19  return;
20  }
21 
22  $this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
23  }
24 
26  new HTMLCheckMatrix( self::$defaultOptions );
27  $this->assertTrue( true ); // form instantiation must throw exception on failure
28  }
29 
31  $called = false;
32  $field = new HTMLCheckMatrix( self::$defaultOptions + [
33  'validation-callback' => function () use ( &$called ) {
34  $called = true;
35 
36  return false;
37  },
38  ] );
39  $this->assertEquals( false, $this->validate( $field, [] ) );
40  $this->assertTrue( $called );
41  }
42 
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, [] ) );
50  }
51 
52  public function testValidateAllowsOnlyKnownTags() {
53  $field = new HTMLCheckMatrix( self::$defaultOptions );
54  $this->assertInternalType( 'string', $this->validate( $field, [ 'foo' ] ) );
55  }
56 
58  $field = new HTMLCheckMatrix( self::$defaultOptions );
59  $this->assertTrue( $this->validate( $field, [] ) );
60  $this->assertTrue( $this->validate( $field, [ 'c1-r1' ] ) );
61  $this->assertTrue( $this->validate( $field, [ 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ] ) );
62  }
63 
71  public function testValuesForcedOnRemainOn() {
72  $field = new HTMLCheckMatrix( self::$defaultOptions + [
73  'force-options-on' => [ 'c2-r1' ],
74  ] );
75  $expected = [
76  'c1-r1' => false,
77  'c1-r2' => false,
78  'c2-r1' => true,
79  'c2-r2' => false,
80  ];
81  $this->assertEquals( $expected, $field->filterDataForSubmit( [] ) );
82  }
83 
84  public function testValuesForcedOffRemainOff() {
85  $field = new HTMLCheckMatrix( self::$defaultOptions + [
86  'force-options-off' => [ 'c1-r2', 'c2-r2' ],
87  ] );
88  $expected = [
89  'c1-r1' => true,
90  'c1-r2' => false,
91  'c2-r1' => true,
92  'c2-r2' => false,
93  ];
94  // array_keys on the result simulates submitting all fields checked
95  $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
96  }
97 
98  protected function validate( HTMLFormField $field, $submitted ) {
99  return $field->validate(
100  $submitted,
101  [ self::$defaultOptions['fieldname'] => $submitted ]
102  );
103  }
104 
105 }
$called
$called tracks whether the setUp and tearDown method has been called.
Unit tests for the HTMLCheckMatrix HTMLCheckMatrix.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testValuesForcedOnRemainOn()
This form object actually has no visibility into what happens later on, but essentially if the data s...
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:1932
validate(HTMLFormField $field, $submitted)
validate($value, $alldata)
Override this function to add specific validation checks on the field input.
A checkbox matrix Operates similarly to HTMLMultiSelectField, but instead of using an array of option...
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
The parent class to generate form fields.