4 * @covers HTMLRestrictionsField
6 class HTMLRestrictionsFieldTest
extends PHPUnit_Framework_TestCase
{
8 use MediaWikiCoversValidator
;
10 public function testConstruct() {
11 $field = new HTMLRestrictionsField( [ 'fieldname' => 'restrictions' ] );
12 $this->assertNotEmpty( $field->getLabel(), 'has a default label' );
13 $this->assertNotEmpty( $field->getHelpText(), 'has a default help text' );
14 $this->assertEquals( MWRestrictions
::newDefault(), $field->getDefault(),
15 'defaults to the default MWRestrictions object' );
17 $field = new HTMLRestrictionsField( [
18 'fieldname' => 'restrictions',
23 $this->assertEquals( 'foo', $field->getLabel(), 'label can be customized' );
24 $this->assertEquals( 'bar', $field->getHelpText(), 'help text can be customized' );
25 $this->assertEquals( 'baz', $field->getDefault(), 'default can be customized' );
29 * @dataProvider provideValidate
31 public function testForm( $text, $value ) {
32 $form = HTMLForm
::factory( 'ooui', [
33 'restrictions' => [ 'class' => HTMLRestrictionsField
::class ],
35 $request = new FauxRequest( [ 'wprestrictions' => $text ], true );
36 $context = new DerivativeContext( RequestContext
::getMain() );
37 $context->setRequest( $request );
38 $form->setContext( $context );
39 $form->setTitle( Title
::newFromText( 'Main Page' ) )->setSubmitCallback( function () {
42 $status = $form->trySubmit();
44 if ( $status instanceof StatusValue
) {
45 $this->assertEquals( $value !== false, $status->isGood() );
46 } elseif ( $value === false ) {
47 $this->assertNotSame( true, $status );
49 $this->assertSame( true, $status );
52 if ( $value !== false ) {
53 $restrictions = $form->mFieldData
['restrictions'];
54 $this->assertInstanceOf( MWRestrictions
::class, $restrictions );
55 $this->assertEquals( $value, $restrictions->toArray()['IPAddresses'] );
59 $form->getHTML( $status );
62 public function provideValidate() {
64 // submitted text, value of 'IPAddresses' key or false for validation error
65 [ null, [ '0.0.0.0/0', '::/0' ] ],
67 [ "1.2.3.4\n::/0", [ '1.2.3.4', '::/0' ] ],
68 [ "1.2.3.4\n::/x", false ],