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
|
|
|
*/
|
|
|
|
|
class HtmlCheckMatrixTest extends MediaWikiTestCase {
|
|
|
|
|
static private $defaultOptions = array(
|
|
|
|
|
'rows' => array( 'r1', 'r2' ),
|
|
|
|
|
'columns' => array( 'c1', 'c2' ),
|
|
|
|
|
'fieldname' => 'test',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
public function testPlainInstantiation() {
|
|
|
|
|
try {
|
2013-11-15 23:45:43 +00:00
|
|
|
new HTMLCheckMatrix( array() );
|
2013-04-27 01:49:02 +00:00
|
|
|
} catch ( MWException $e ) {
|
|
|
|
|
$this->assertInstanceOf( 'HTMLFormFieldRequiredOptionsException', $e );
|
|
|
|
|
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;
|
|
|
|
|
$field = new HTMLCheckMatrix( self::$defaultOptions + array(
|
2013-11-15 23:45:43 +00:00
|
|
|
'validation-callback' => function() use ( &$called ) {
|
|
|
|
|
$called = true;
|
|
|
|
|
return false;
|
|
|
|
|
},
|
|
|
|
|
) );
|
2013-04-27 01:49:02 +00:00
|
|
|
$this->assertEquals( false, $this->validate( $field, array() ) );
|
|
|
|
|
$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 ) );
|
|
|
|
|
$this->assertEquals( true, $this->validate( $field, array() ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testValidateAllowsOnlyKnownTags() {
|
|
|
|
|
$field = new HTMLCheckMatrix( self::$defaultOptions );
|
2013-05-17 14:47:00 +00:00
|
|
|
$this->assertInternalType( 'string', $this->validate( $field, array( 'foo' ) ) );
|
2013-04-27 01:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testValidateAcceptsPartialTagList() {
|
|
|
|
|
$field = new HTMLCheckMatrix( self::$defaultOptions );
|
|
|
|
|
$this->assertTrue( $this->validate( $field, array() ) );
|
|
|
|
|
$this->assertTrue( $this->validate( $field, array( 'c1-r1' ) ) );
|
|
|
|
|
$this->assertTrue( $this->validate( $field, array( 'c1-r1', 'c1-r2', 'c2-r1', 'c2-r2' ) ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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() {
|
|
|
|
|
$field = new HTMLCheckMatrix( self::$defaultOptions + array(
|
2013-11-15 23:45:43 +00:00
|
|
|
'force-options-on' => array( 'c2-r1' ),
|
|
|
|
|
) );
|
2013-04-27 01:49:02 +00:00
|
|
|
$expected = array(
|
|
|
|
|
'c1-r1' => false,
|
|
|
|
|
'c1-r2' => false,
|
|
|
|
|
'c2-r1' => true,
|
|
|
|
|
'c2-r2' => false,
|
|
|
|
|
);
|
2013-05-17 14:47:00 +00:00
|
|
|
$this->assertEquals( $expected, $field->filterDataForSubmit( array() ) );
|
2013-04-27 01:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testValuesForcedOffRemainOff() {
|
|
|
|
|
$field = new HTMLCheckMatrix( self::$defaultOptions + array(
|
2013-11-15 23:45:43 +00:00
|
|
|
'force-options-off' => array( 'c1-r2', 'c2-r2' ),
|
|
|
|
|
) );
|
2013-04-27 01:49:02 +00:00
|
|
|
$expected = array(
|
|
|
|
|
'c1-r1' => true,
|
|
|
|
|
'c1-r2' => false,
|
|
|
|
|
'c2-r1' => true,
|
|
|
|
|
'c2-r2' => false,
|
|
|
|
|
);
|
|
|
|
|
// 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,
|
|
|
|
|
array( self::$defaultOptions['fieldname'] => $submitted )
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-11-15 23:45:43 +00:00
|
|
|
|
2013-04-27 01:49:02 +00:00
|
|
|
}
|