MediaWiki  1.33.0
HTMLCheckMatrixTest.php
Go to the documentation of this file.
1 <?php
2 
7  private static $defaultOptions = [
8  'rows' => [ 'r1', 'r2' ],
9  'columns' => [ 'c1', 'c2' ],
10  'fieldname' => 'test',
11  ];
12 
13  public function testPlainInstantiation() {
14  try {
15  new HTMLCheckMatrix( [] );
16  } catch ( MWException $e ) {
17  $this->assertInstanceOf( HTMLFormFieldRequiredOptionsException::class, $e );
18  return;
19  }
20 
21  $this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
22  }
23 
25  new HTMLCheckMatrix( self::$defaultOptions );
26  $this->assertTrue( true ); // form instantiation must throw exception on failure
27  }
28 
30  $called = false;
31  $field = new HTMLCheckMatrix( self::$defaultOptions + [
32  'validation-callback' => function () use ( &$called ) {
33  $called = true;
34 
35  return false;
36  },
37  ] );
38  $this->assertEquals( false, $this->validate( $field, [] ) );
39  $this->assertTrue( $called );
40  }
41 
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, [] ) );
49  }
50 
51  public function testValidateAllowsOnlyKnownTags() {
52  $field = new HTMLCheckMatrix( self::$defaultOptions );
53  $this->assertInstanceOf( Message::class, $this->validate( $field, [ 'foo' ] ) );
54  }
55 
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' ] ) );
61  }
62 
70  public function testValuesForcedOnRemainOn() {
71  $field = new HTMLCheckMatrix( self::$defaultOptions + [
72  'force-options-on' => [ 'c2-r1' ],
73  ] );
74  $expected = [
75  'c1-r1' => false,
76  'c1-r2' => false,
77  'c2-r1' => true,
78  'c2-r2' => false,
79  ];
80  $this->assertEquals( $expected, $field->filterDataForSubmit( [] ) );
81  }
82 
83  public function testValuesForcedOffRemainOff() {
84  $field = new HTMLCheckMatrix( self::$defaultOptions + [
85  'force-options-off' => [ 'c1-r2', 'c2-r2' ],
86  ] );
87  $expected = [
88  'c1-r1' => true,
89  'c1-r2' => false,
90  'c2-r1' => true,
91  'c2-r2' => false,
92  ];
93  // array_keys on the result simulates submitting all fields checked
94  $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
95  }
96 
97  protected function validate( HTMLFormField $field, $submitted ) {
98  return $field->validate(
99  $submitted,
100  [ self::$defaultOptions['fieldname'] => $submitted ]
101  );
102  }
103 
104 }
HTMLCheckMatrixTest\testValidateAcceptsPartialTagList
testValidateAcceptsPartialTagList()
Definition: HTMLCheckMatrixTest.php:56
HTMLCheckMatrixTest\testInstantiationWithMinimumRequiredParameters
testInstantiationWithMinimumRequiredParameters()
Definition: HTMLCheckMatrixTest.php:24
HTMLCheckMatrixTest\testValuesForcedOffRemainOff
testValuesForcedOffRemainOff()
Definition: HTMLCheckMatrixTest.php:83
HTMLCheckMatrixTest
HTMLCheckMatrix.
Definition: HTMLCheckMatrixTest.php:6
HTMLFormField\validate
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
Definition: HTMLFormField.php:301
HTMLCheckMatrixTest\testValidateCallsUserDefinedValidationCallback
testValidateCallsUserDefinedValidationCallback()
Definition: HTMLCheckMatrixTest.php:29
HTMLCheckMatrixTest\testValuesForcedOnRemainOn
testValuesForcedOnRemainOn()
This form object actually has no visibility into what happens later on, but essentially if the data s...
Definition: HTMLCheckMatrixTest.php:70
php
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
MediaWikiTestCase\$called
$called
$called tracks whether the setUp and tearDown method has been called.
Definition: MediaWikiTestCase.php:47
HTMLCheckMatrixTest\validate
validate(HTMLFormField $field, $submitted)
Definition: HTMLCheckMatrixTest.php:97
MWException
MediaWiki exception.
Definition: MWException.php:26
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
HTMLFormField
The parent class to generate form fields.
Definition: HTMLFormField.php:7
HTMLCheckMatrixTest\$defaultOptions
static $defaultOptions
Definition: HTMLCheckMatrixTest.php:7
HTMLCheckMatrixTest\testPlainInstantiation
testPlainInstantiation()
Definition: HTMLCheckMatrixTest.php:13
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
HTMLCheckMatrixTest\testValidateAllowsOnlyKnownTags
testValidateAllowsOnlyKnownTags()
Definition: HTMLCheckMatrixTest.php:51
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
HTMLCheckMatrix
A checkbox matrix Operates similarly to HTMLMultiSelectField, but instead of using an array of option...
Definition: HTMLCheckMatrix.php:25
HTMLCheckMatrixTest\testValidateRequiresArrayInput
testValidateRequiresArrayInput()
Definition: HTMLCheckMatrixTest.php:42
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52