2013-04-27 01:49:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2013-11-15 23:45:43 +00:00
|
|
|
* Unit tests for the HTMLCheckMatrix
|
|
|
|
|
* @covers HTMLCheckMatrix
|
2013-04-27 01:49:02 +00:00
|
|
|
*/
|
2017-12-10 10:40:05 +00:00
|
|
|
class HTMLCheckMatrixTest extends MediaWikiTestCase {
|
2016-02-17 09:09:32 +00:00
|
|
|
static private $defaultOptions = [
|
|
|
|
|
'rows' => [ 'r1', 'r2' ],
|
|
|
|
|
'columns' => [ 'c1', 'c2' ],
|
2013-04-27 01:49:02 +00:00
|
|
|
'fieldname' => 'test',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-04-27 01:49:02 +00:00
|
|
|
|
|
|
|
|
public function testPlainInstantiation() {
|
|
|
|
|
try {
|
2016-02-17 09:09:32 +00:00
|
|
|
new HTMLCheckMatrix( [] );
|
2013-04-27 01:49:02 +00:00
|
|
|
} catch ( MWException $e ) {
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( HTMLFormFieldRequiredOptionsException::class, $e );
|
2013-04-27 01:49:02 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-17 14:47:00 +00:00
|
|
|
$this->fail( 'Expected MWException indicating missing parameters but none was thrown.' );
|
2013-04-27 01:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInstantiationWithMinimumRequiredParameters() {
|
2013-11-15 23:45:43 +00:00
|
|
|
new HTMLCheckMatrix( self::$defaultOptions );
|
2013-05-17 14:47:00 +00:00
|
|
|
$this->assertTrue( true ); // form instantiation must throw exception on failure
|
2013-04-27 01:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testValidateCallsUserDefinedValidationCallback() {
|
|
|
|
|
$called = false;
|
2016-02-17 09:09:32 +00:00
|
|
|
$field = new HTMLCheckMatrix( self::$defaultOptions + [
|
2014-04-24 17:51:33 +00:00
|
|
|
'validation-callback' => function () use ( &$called ) {
|
|
|
|
|
$called = true;
|
2014-04-24 16:06:46 +00:00
|
|
|
|
2014-04-24 17:51:33 +00:00
|
|
|
return false;
|
|
|
|
|
},
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
|
|
|
|
$this->assertEquals( false, $this->validate( $field, [] ) );
|
2013-04-27 01:49:02 +00:00
|
|
|
$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 ) );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( true, $this->validate( $field, [] ) );
|
2013-04-27 01:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testValidateAllowsOnlyKnownTags() {
|
|
|
|
|
$field = new HTMLCheckMatrix( self::$defaultOptions );
|
2016-11-02 17:13:43 +00:00
|
|
|
$this->assertInstanceOf( Message::class, $this->validate( $field, [ 'foo' ] ) );
|
2013-04-27 01:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testValidateAcceptsPartialTagList() {
|
|
|
|
|
$field = new HTMLCheckMatrix( self::$defaultOptions );
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertTrue( $this->validate( $field, [] ) );
|
|
|
|
|
$this->assertTrue( $this->validate( $field, [ 'c1-r1' ] ) );
|
|
|
|
|
$this->assertTrue( $this->validate( $field, [ 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ] ) );
|
2013-04-27 01:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$field = new HTMLCheckMatrix( self::$defaultOptions + [
|
|
|
|
|
'force-options-on' => [ 'c2-r1' ],
|
|
|
|
|
] );
|
|
|
|
|
$expected = [
|
2013-04-27 01:49:02 +00:00
|
|
|
'c1-r1' => false,
|
|
|
|
|
'c1-r2' => false,
|
|
|
|
|
'c2-r1' => true,
|
|
|
|
|
'c2-r2' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
|
|
|
|
$this->assertEquals( $expected, $field->filterDataForSubmit( [] ) );
|
2013-04-27 01:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testValuesForcedOffRemainOff() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$field = new HTMLCheckMatrix( self::$defaultOptions + [
|
|
|
|
|
'force-options-off' => [ 'c1-r2', 'c2-r2' ],
|
|
|
|
|
] );
|
|
|
|
|
$expected = [
|
2013-04-27 01:49:02 +00:00
|
|
|
'c1-r1' => true,
|
|
|
|
|
'c1-r2' => false,
|
|
|
|
|
'c2-r1' => true,
|
|
|
|
|
'c2-r2' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2013-04-27 01:49:02 +00:00
|
|
|
// array_keys on the result simulates submitting all fields checked
|
2013-05-17 14:47:00 +00:00
|
|
|
$this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) );
|
2013-04-27 01:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function validate( HTMLFormField $field, $submitted ) {
|
|
|
|
|
return $field->validate(
|
|
|
|
|
$submitted,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ self::$defaultOptions['fieldname'] => $submitted ]
|
2013-04-27 01:49:02 +00:00
|
|
|
);
|
|
|
|
|
}
|
2013-11-15 23:45:43 +00:00
|
|
|
|
2013-04-27 01:49:02 +00:00
|
|
|
}
|