diff options
author | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
---|---|---|
committer | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
commit | fc7369835258467bf97eb64f184b93691f9a9fd5 (patch) | |
tree | daabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/tests/phpunit/includes/htmlform/HTMLRestrictionsFieldTest.php |
first commit
Diffstat (limited to 'www/wiki/tests/phpunit/includes/htmlform/HTMLRestrictionsFieldTest.php')
-rw-r--r-- | www/wiki/tests/phpunit/includes/htmlform/HTMLRestrictionsFieldTest.php | 71 |
1 files changed, 71 insertions, 0 deletions
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 ], + ]; + } +} |