diff options
Diffstat (limited to 'www/wiki/tests/phpunit/includes/htmlform')
4 files changed, 301 insertions, 0 deletions
diff --git a/www/wiki/tests/phpunit/includes/htmlform/HTMLAutoCompleteSelectFieldTest.php b/www/wiki/tests/phpunit/includes/htmlform/HTMLAutoCompleteSelectFieldTest.php new file mode 100644 index 00000000..99bea68d --- /dev/null +++ b/www/wiki/tests/phpunit/includes/htmlform/HTMLAutoCompleteSelectFieldTest.php @@ -0,0 +1,68 @@ +<?php +/** + * Unit tests for HTMLAutoCompleteSelectField + * + * @covers HTMLAutoCompleteSelectField + */ +class HTMLAutoCompleteSelectFieldTest extends MediaWikiTestCase { + + public $options = [ + 'Bulgaria' => 'BGR', + 'Burkina Faso' => 'BFA', + 'Burundi' => 'BDI', + ]; + + /** + * Verify that attempting to instantiate an HTMLAutoCompleteSelectField + * without providing any autocomplete options causes an exception to be + * thrown. + * + * @expectedException MWException + * @expectedExceptionMessage called without any autocompletions + */ + function testMissingAutocompletions() { + new HTMLAutoCompleteSelectField( [ 'fieldname' => 'Test' ] ); + } + + /** + * Verify that the autocomplete options are correctly encoded as + * the 'data-autocomplete' attribute of the field. + * + * @covers HTMLAutoCompleteSelectField::getAttributes + */ + function testGetAttributes() { + $field = new HTMLAutoCompleteSelectField( [ + 'fieldname' => 'Test', + 'autocomplete' => $this->options, + ] ); + + $attributes = $field->getAttributes( [] ); + $this->assertEquals( array_keys( $this->options ), + FormatJson::decode( $attributes['data-autocomplete'] ), + "The 'data-autocomplete' attribute encodes autocomplete option keys as a JSON array." + ); + } + + /** + * Test that the optional select dropdown is included or excluded based on + * the presence or absence of the 'options' parameter. + */ + function testOptionalSelectElement() { + $params = [ + 'fieldname' => 'Test', + 'autocomplete-data' => $this->options, + 'options' => $this->options, + ]; + + $field = new HTMLAutoCompleteSelectField( $params ); + $html = $field->getInputHTML( false ); + $this->assertRegExp( '/select/', $html, + "When the 'options' parameter is set, the HTML includes a <select>" ); + + unset( $params['options'] ); + $field = new HTMLAutoCompleteSelectField( $params ); + $html = $field->getInputHTML( false ); + $this->assertNotRegExp( '/select/', $html, + "When the 'options' parameter is not set, the HTML does not include a <select>" ); + } +} diff --git a/www/wiki/tests/phpunit/includes/htmlform/HTMLCheckMatrixTest.php b/www/wiki/tests/phpunit/includes/htmlform/HTMLCheckMatrixTest.php new file mode 100644 index 00000000..e7922fd2 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/htmlform/HTMLCheckMatrixTest.php @@ -0,0 +1,105 @@ +<?php + +/** + * Unit tests for the HTMLCheckMatrix + * @covers HTMLCheckMatrix + */ +class HTMLCheckMatrixTest extends MediaWikiTestCase { + static private $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 ] + ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/htmlform/HTMLFormTest.php b/www/wiki/tests/phpunit/includes/htmlform/HTMLFormTest.php new file mode 100644 index 00000000..e20cf942 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/htmlform/HTMLFormTest.php @@ -0,0 +1,57 @@ +<?php + +/** + * @covers HTMLForm + * + * @license GNU GPL v2+ + * @author Gergő Tisza + * @author Thiemo Mättig + */ +class HTMLFormTest extends MediaWikiTestCase { + + private function newInstance() { + $form = new HTMLForm( [] ); + $form->setTitle( Title::newFromText( 'Foo' ) ); + return $form; + } + + public function testGetHTML_empty() { + $form = $this->newInstance(); + $form->prepareForm(); + $html = $form->getHTML( false ); + $this->assertStringStartsWith( '<form ', $html ); + } + + /** + * @expectedException LogicException + */ + public function testGetHTML_noPrepare() { + $form = $this->newInstance(); + $form->getHTML( false ); + } + + public function testAutocompleteDefaultsToNull() { + $form = $this->newInstance(); + $this->assertNotContains( 'autocomplete', $form->wrapForm( '' ) ); + } + + public function testAutocompleteWhenSetToNull() { + $form = $this->newInstance(); + $form->setAutocomplete( null ); + $this->assertNotContains( 'autocomplete', $form->wrapForm( '' ) ); + } + + public function testAutocompleteWhenSetToFalse() { + $form = $this->newInstance(); + // Previously false was used instead of null to indicate the attribute should not be set + $form->setAutocomplete( false ); + $this->assertNotContains( 'autocomplete', $form->wrapForm( '' ) ); + } + + public function testAutocompleteWhenSetToOff() { + $form = $this->newInstance(); + $form->setAutocomplete( 'off' ); + $this->assertContains( ' autocomplete="off"', $form->wrapForm( '' ) ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/htmlform/HTMLRestrictionsFieldTest.php b/www/wiki/tests/phpunit/includes/htmlform/HTMLRestrictionsFieldTest.php new file mode 100644 index 00000000..c4290e1e --- /dev/null +++ b/www/wiki/tests/phpunit/includes/htmlform/HTMLRestrictionsFieldTest.php @@ -0,0 +1,71 @@ +<?php + +/** + * @covers HTMLRestrictionsField + */ +class HTMLRestrictionsFieldTest extends PHPUnit\Framework\TestCase { + + use MediaWikiCoversValidator; + + public function testConstruct() { + $field = new HTMLRestrictionsField( [ 'fieldname' => 'restrictions' ] ); + $this->assertNotEmpty( $field->getLabel(), 'has a default label' ); + $this->assertNotEmpty( $field->getHelpText(), 'has a default help text' ); + $this->assertEquals( MWRestrictions::newDefault(), $field->getDefault(), + 'defaults to the default MWRestrictions object' ); + + $field = new HTMLRestrictionsField( [ + 'fieldname' => 'restrictions', + 'label' => 'foo', + 'help' => 'bar', + 'default' => 'baz', + ] ); + $this->assertEquals( 'foo', $field->getLabel(), 'label can be customized' ); + $this->assertEquals( 'bar', $field->getHelpText(), 'help text can be customized' ); + $this->assertEquals( 'baz', $field->getDefault(), 'default can be customized' ); + } + + /** + * @dataProvider provideValidate + */ + public function testForm( $text, $value ) { + $form = HTMLForm::factory( 'ooui', [ + 'restrictions' => [ 'class' => HTMLRestrictionsField::class ], + ] ); + $request = new FauxRequest( [ 'wprestrictions' => $text ], true ); + $context = new DerivativeContext( RequestContext::getMain() ); + $context->setRequest( $request ); + $form->setContext( $context ); + $form->setTitle( Title::newFromText( 'Main Page' ) )->setSubmitCallback( function () { + return true; + } )->prepareForm(); + $status = $form->trySubmit(); + + if ( $status instanceof StatusValue ) { + $this->assertEquals( $value !== false, $status->isGood() ); + } elseif ( $value === false ) { + $this->assertNotSame( true, $status ); + } else { + $this->assertSame( true, $status ); + } + + if ( $value !== false ) { + $restrictions = $form->mFieldData['restrictions']; + $this->assertInstanceOf( MWRestrictions::class, $restrictions ); + $this->assertEquals( $value, $restrictions->toArray()['IPAddresses'] ); + } + + // sanity + $form->getHTML( $status ); + } + + public function provideValidate() { + return [ + // submitted text, value of 'IPAddresses' key or false for validation error + [ null, [ '0.0.0.0/0', '::/0' ] ], + [ '', [] ], + [ "1.2.3.4\n::/0", [ '1.2.3.4', '::/0' ] ], + [ "1.2.3.4\n::/x", false ], + ]; + } +} |