Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / htmlform / HTMLRestrictionsFieldTest.php
blob9ec4f97fd08d151e2a388dfb6b40127767553ae0
1 <?php
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',
13 'label' => 'foo',
14 'help' => 'bar',
15 'default' => 'baz',
16 ] );
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' );
22 /**
23 * @dataProvider provideValidate
25 public function testForm( $text, $value ) {
26 $form = HTMLForm::factory( 'ooui', [
27 'restrictions' => [ 'class' => HTMLRestrictionsField::class ],
28 ] );
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 () {
34 return true;
35 } )->prepareForm();
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 );
42 } else {
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'] );
52 // sanity
53 $form->getHTML( $status );
56 public function provideValidate() {
57 return [
58 // submitted text, value of 'IPAddresses' key or false for validation error
59 [ null, [ '0.0.0.0/0', '::/0' ] ],
60 [ '', [] ],
61 [ "1.2.3.4\n::/0", [ '1.2.3.4', '::/0' ] ],
62 [ "1.2.3.4\n::/x", false ],