wiki.techinc.nl/tests/phpunit/integration/includes/htmlform/HTMLRestrictionsFieldTest.php
Siddharth VP d72917ea2f Refactor HTMLRestrictionsField to allow more restrictions to be added
Earlier, loadDataFromRequest() returned MWRestrictions object only in
case of valid input, and the original string if invalid. Now, an
MWRestrictions object is returned in all cases, on which we now have a
validity field. This also de-duplicates the check to find the invalid IP
address(es).

Bug: T349957
Change-Id: Iadb762b572cf0e7d2b92dbc4912804a3ddb48e74
2023-11-23 16:01:50 +00:00

80 lines
2.6 KiB
PHP

<?php
use MediaWiki\Request\FauxRequest;
use MediaWiki\Title\Title;
/**
* @covers HTMLRestrictionsField
*/
class HTMLRestrictionsFieldTest extends MediaWikiIntegrationTestCase {
use MediaWikiCoversValidator;
public function testConstruct() {
$htmlForm = $this->createMock( HTMLForm::class );
$htmlForm->method( 'msg' )->willReturnCallback( 'wfMessage' );
$languageMock = $this->createMock( Language::class );
$languageMock->method( 'getCode' )->willReturn( 'en' );
$titleMock = $this->createMock( Title::class );
$htmlForm->method( 'getLanguage' )->willReturn( $languageMock );
$htmlForm->method( 'getTitle' )->willReturn( $titleMock );
$field = new HTMLRestrictionsField( [ 'fieldname' => 'restrictions', 'parent' => $htmlForm ] );
$this->assertEquals( MWRestrictions::newDefault(), $field->getDefault(),
'defaults to the default MWRestrictions object' );
$field = new HTMLRestrictionsField( [
'fieldname' => 'restrictions',
'label' => 'foo',
'help' => 'bar',
'default' => 'baz',
'parent' => $htmlForm,
] );
$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( $ipText, $value ) {
$request = new FauxRequest( [ 'wprestrictions-ip' => $ipText ], true );
$context = new DerivativeContext( RequestContext::getMain() );
$context->setRequest( $request );
$form = HTMLForm::factory( 'ooui', [
'restrictions' => [ 'class' => HTMLRestrictionsField::class ],
], $context );
$form->setTitle( Title::makeTitle( NS_MAIN, 'Main Page' ) )->setSubmitCallback( static function () {
return true;
} )->prepareForm();
$status = $form->trySubmit();
if ( $status instanceof StatusValue ) {
$this->assertEquals( $value !== false, $status->isGood() );
} elseif ( $value === false ) {
$this->assertFalse( $status );
} else {
$this->assertTrue( $status );
}
if ( $value !== false ) {
$restrictions = $form->mFieldData['restrictions'];
$this->assertInstanceOf( MWRestrictions::class, $restrictions );
$this->assertEquals( $value, $restrictions->toArray()['IPAddresses'] );
}
$form->getHTML( $status );
}
public static function provideValidate() {
return [
// submitted text, value of 'IPAddresses' key or false for validation error
[ null, [ '0.0.0.0/0', '::/0' ] ],
[ '', [] ],
[ "1.2.3.4\n::0", [ '1.2.3.4', '::0' ] ],
[ "1.2.3.4\n::/x", false ],
];
}
}