3 class HTMLRestrictionsFieldTest
extends PHPUnit_Framework_TestCase
{
4 public function testConstruct() {
5 $field = new HTMLRestrictionsField( [ 'fieldname' => 'restrictions' ] );
6 $this->assertNotEmpty( $field->getLabel(), 'has a default label' );
7 $this->assertNotEmpty( $field->getHelpText(), 'has a default help text' );
8 $this->assertEquals( MWRestrictions
::newDefault(), $field->getDefault(),
9 'defaults to the default MWRestrictions object' );
11 $field = new HTMLRestrictionsField( [
12 'fieldname' => 'restrictions',
17 $this->assertEquals( 'foo', $field->getLabel(), 'label can be customized' );
18 $this->assertEquals( 'bar', $field->getHelpText(), 'help text can be customized' );
19 $this->assertEquals( 'baz', $field->getDefault(), 'default can be customized' );
23 * @dataProvider provideValidate
25 public function testForm( $text, $value ) {
26 $form = HTMLForm
::factory( 'ooui', [
27 'restrictions' => [ 'class' => HTMLRestrictionsField
::class ],
29 $request = new FauxRequest( [ 'wprestrictions' => $text ], true );
30 $context = new DerivativeContext( RequestContext
::getMain() );
31 $context->setRequest( $request );
32 $form->setContext( $context );
33 $form->setTitle( Title
::newFromText( 'Main Page' ) )->setSubmitCallback( function () {
36 $status = $form->trySubmit();
38 if ( $status instanceof StatusValue
) {
39 $this->assertEquals( $value !== false, $status->isGood() );
40 } elseif ( $value === false ) {
41 $this->assertNotSame( true, $status );
43 $this->assertSame( true, $status );
46 if ( $value !== false ) {
47 $restrictions = $form->mFieldData
['restrictions'];
48 $this->assertInstanceOf( MWRestrictions
::class, $restrictions );
49 $this->assertEquals( $value, $restrictions->toArray()['IPAddresses'] );
53 $form->getHTML( $status );
56 public function provideValidate() {
58 // submitted text, value of 'IPAddresses' key or false for validation error
59 [ null, [ '0.0.0.0/0', '::/0' ] ],
61 [ "1.2.3.4\n::/0", [ '1.2.3.4', '::/0' ] ],
62 [ "1.2.3.4\n::/x", false ],