2016-09-21 01:33:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-10-28 10:04:25 +00:00
|
|
|
use MediaWiki\Request\FauxRequest;
|
|
|
|
|
|
2017-12-29 23:44:13 +00:00
|
|
|
/**
|
|
|
|
|
* @covers HTMLRestrictionsField
|
|
|
|
|
*/
|
2018-02-17 12:29:13 +00:00
|
|
|
class HTMLRestrictionsFieldTest extends PHPUnit\Framework\TestCase {
|
2017-12-29 23:22:37 +00:00
|
|
|
|
|
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
|
2016-09-21 01:33:26 +00:00
|
|
|
public function testConstruct() {
|
2023-01-07 07:48:03 +00:00
|
|
|
$htmlForm = $this->createMock( HTMLForm::class );
|
2023-01-10 20:33:47 +00:00
|
|
|
$htmlForm->method( 'msg' )->willReturnCallback( 'wfMessage' );
|
2023-01-07 07:48:03 +00:00
|
|
|
|
|
|
|
|
$field = new HTMLRestrictionsField( [ 'fieldname' => 'restrictions', 'parent' => $htmlForm ] );
|
2016-09-21 01:33:26 +00:00
|
|
|
$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',
|
2023-01-07 07:48:03 +00:00
|
|
|
'parent' => $htmlForm,
|
2016-09-21 01:33:26 +00:00
|
|
|
] );
|
|
|
|
|
$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 ) {
|
|
|
|
|
$request = new FauxRequest( [ 'wprestrictions' => $text ], true );
|
|
|
|
|
$context = new DerivativeContext( RequestContext::getMain() );
|
|
|
|
|
$context->setRequest( $request );
|
2022-05-15 22:52:01 +00:00
|
|
|
$form = HTMLForm::factory( 'ooui', [
|
|
|
|
|
'restrictions' => [ 'class' => HTMLRestrictionsField::class ],
|
|
|
|
|
], $context );
|
2022-09-23 19:53:11 +00:00
|
|
|
$form->setTitle( Title::makeTitle( NS_MAIN, 'Main Page' ) )->setSubmitCallback( static function () {
|
2016-09-21 01:33:26 +00:00
|
|
|
return true;
|
|
|
|
|
} )->prepareForm();
|
|
|
|
|
$status = $form->trySubmit();
|
|
|
|
|
|
|
|
|
|
if ( $status instanceof StatusValue ) {
|
|
|
|
|
$this->assertEquals( $value !== false, $status->isGood() );
|
|
|
|
|
} elseif ( $value === false ) {
|
2020-10-28 19:57:14 +00:00
|
|
|
$this->assertFalse( $status );
|
2016-09-21 01:33:26 +00:00
|
|
|
} else {
|
2020-10-28 19:57:14 +00:00
|
|
|
$this->assertTrue( $status );
|
2016-09-21 01:33:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $value !== false ) {
|
|
|
|
|
$restrictions = $form->mFieldData['restrictions'];
|
|
|
|
|
$this->assertInstanceOf( MWRestrictions::class, $restrictions );
|
|
|
|
|
$this->assertEquals( $value, $restrictions->toArray()['IPAddresses'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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' ] ],
|
|
|
|
|
[ '', [] ],
|
2020-11-10 12:06:29 +00:00
|
|
|
[ "1.2.3.4\n::0", [ '1.2.3.4', '::0' ] ],
|
2016-09-21 01:33:26 +00:00
|
|
|
[ "1.2.3.4\n::/x", false ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|