diff options
author | Amir Sarabadani <Ladsgroup@gmail.com> | 2019-07-13 22:50:28 +0200 |
---|---|---|
committer | Amir Sarabadani <ladsgroup@gmail.com> | 2019-07-14 01:28:07 +0200 |
commit | 06f645c453ebd1a3bbd065abd4f29e909f5656c7 (patch) | |
tree | 61565db57ed8ff33cc6384a84374f0b7f67994cd /tests/phpunit/unit/includes/htmlform/HTMLCheckMatrixTest.php | |
parent | 3fda59f9621f55aed5602802d8e9d4dbc9d8dfa6 (diff) | |
download | mediawikicore-06f645c453ebd1a3bbd065abd4f29e909f5656c7.tar.gz mediawikicore-06f645c453ebd1a3bbd065abd4f29e909f5656c7.zip |
Load GlobalFunctions.php to tests/phpunit/bootstrap.php
That mostly enables testing global functions
Bug: T87781
Change-Id: Ib42c56a67926ebcdba53f4c6c54a5bff98cb77a3
Diffstat (limited to 'tests/phpunit/unit/includes/htmlform/HTMLCheckMatrixTest.php')
-rw-r--r-- | tests/phpunit/unit/includes/htmlform/HTMLCheckMatrixTest.php | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/tests/phpunit/unit/includes/htmlform/HTMLCheckMatrixTest.php b/tests/phpunit/unit/includes/htmlform/HTMLCheckMatrixTest.php new file mode 100644 index 000000000000..659d48d43d10 --- /dev/null +++ b/tests/phpunit/unit/includes/htmlform/HTMLCheckMatrixTest.php @@ -0,0 +1,104 @@ +<?php + +/** + * @covers HTMLCheckMatrix + */ +class HTMLCheckMatrixTest extends MediaWikiUnitTestCase { + private static $defaultOptions = [ + 'rows' => [ 'r1', 'r2' ], + 'columns' => [ 'c1', 'c2' ], + 'fieldname' => 'test', + ]; + + public function testPlainInstantiation() { + try { + new HTMLCheckMatrix( [] ); + } catch ( MWException $e ) { + $this->assertInstanceOf( HTMLFormFieldRequiredOptionsException::class, $e ); + return; + } + + $this->fail( 'Expected MWException indicating missing parameters but none was thrown.' ); + } + + public function testInstantiationWithMinimumRequiredParameters() { + new HTMLCheckMatrix( self::$defaultOptions ); + $this->assertTrue( true ); // form instantiation must throw exception on failure + } + + public function testValidateCallsUserDefinedValidationCallback() { + $called = false; + $field = new HTMLCheckMatrix( self::$defaultOptions + [ + 'validation-callback' => function () use ( &$called ) { + $called = true; + + return false; + }, + ] ); + $this->assertEquals( false, $this->validate( $field, [] ) ); + $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, [] ) ); + } + + public function testValidateAllowsOnlyKnownTags() { + $field = new HTMLCheckMatrix( self::$defaultOptions ); + $this->assertInstanceOf( Message::class, $this->validate( $field, [ 'foo' ] ) ); + } + + public function testValidateAcceptsPartialTagList() { + $field = new HTMLCheckMatrix( self::$defaultOptions ); + $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' ] ) ); + } + + /** + * 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 + [ + 'force-options-on' => [ 'c2-r1' ], + ] ); + $expected = [ + 'c1-r1' => false, + 'c1-r2' => false, + 'c2-r1' => true, + 'c2-r2' => false, + ]; + $this->assertEquals( $expected, $field->filterDataForSubmit( [] ) ); + } + + public function testValuesForcedOffRemainOff() { + $field = new HTMLCheckMatrix( self::$defaultOptions + [ + 'force-options-off' => [ 'c1-r2', 'c2-r2' ], + ] ); + $expected = [ + 'c1-r1' => true, + 'c1-r2' => false, + 'c2-r1' => true, + 'c2-r2' => false, + ]; + // array_keys on the result simulates submitting all fields checked + $this->assertEquals( $expected, $field->filterDataForSubmit( array_keys( $expected ) ) ); + } + + protected function validate( HTMLFormField $field, $submitted ) { + return $field->validate( + $submitted, + [ self::$defaultOptions['fieldname'] => $submitted ] + ); + } + +} |