Add @covers tags for more tests
[lhc/web/wiklou.git] / tests / phpunit / includes / htmlform / HTMLRestrictionsFieldTest.php
1 <?php
2
3 /**
4 * @covers HTMLRestrictionsField
5 */
6 class HTMLRestrictionsFieldTest extends PHPUnit_Framework_TestCase {
7 public function testConstruct() {
8 $field = new HTMLRestrictionsField( [ 'fieldname' => 'restrictions' ] );
9 $this->assertNotEmpty( $field->getLabel(), 'has a default label' );
10 $this->assertNotEmpty( $field->getHelpText(), 'has a default help text' );
11 $this->assertEquals( MWRestrictions::newDefault(), $field->getDefault(),
12 'defaults to the default MWRestrictions object' );
13
14 $field = new HTMLRestrictionsField( [
15 'fieldname' => 'restrictions',
16 'label' => 'foo',
17 'help' => 'bar',
18 'default' => 'baz',
19 ] );
20 $this->assertEquals( 'foo', $field->getLabel(), 'label can be customized' );
21 $this->assertEquals( 'bar', $field->getHelpText(), 'help text can be customized' );
22 $this->assertEquals( 'baz', $field->getDefault(), 'default can be customized' );
23 }
24
25 /**
26 * @dataProvider provideValidate
27 */
28 public function testForm( $text, $value ) {
29 $form = HTMLForm::factory( 'ooui', [
30 'restrictions' => [ 'class' => HTMLRestrictionsField::class ],
31 ] );
32 $request = new FauxRequest( [ 'wprestrictions' => $text ], true );
33 $context = new DerivativeContext( RequestContext::getMain() );
34 $context->setRequest( $request );
35 $form->setContext( $context );
36 $form->setTitle( Title::newFromText( 'Main Page' ) )->setSubmitCallback( function () {
37 return true;
38 } )->prepareForm();
39 $status = $form->trySubmit();
40
41 if ( $status instanceof StatusValue ) {
42 $this->assertEquals( $value !== false, $status->isGood() );
43 } elseif ( $value === false ) {
44 $this->assertNotSame( true, $status );
45 } else {
46 $this->assertSame( true, $status );
47 }
48
49 if ( $value !== false ) {
50 $restrictions = $form->mFieldData['restrictions'];
51 $this->assertInstanceOf( MWRestrictions::class, $restrictions );
52 $this->assertEquals( $value, $restrictions->toArray()['IPAddresses'] );
53 }
54
55 // sanity
56 $form->getHTML( $status );
57 }
58
59 public function provideValidate() {
60 return [
61 // submitted text, value of 'IPAddresses' key or false for validation error
62 [ null, [ '0.0.0.0/0', '::/0' ] ],
63 [ '', [] ],
64 [ "1.2.3.4\n::/0", [ '1.2.3.4', '::/0' ] ],
65 [ "1.2.3.4\n::/x", false ],
66 ];
67 }
68 }