Currently it is documented that mParent in HTMLFormField may be null. This can happen if the form element is constructed manually via new, instead of the normal way via HTMLForm methods. As it stands, much of the code assumes that mParent is always set despite the documentation. Lets mark creating form fields without parent set as deprecated. The current situation seems like a recipe for bugs, and after the deprecation period this would allow us to simplify some of the HTMLFormField code. Bug: T326456 Change-Id: Ica0740049f0a3e8ec764903c5b71825e4d628a3f Depends-On: I15a39605e3eec8a5c265c4a331039fa906eda036
78 lines
2.5 KiB
PHP
78 lines
2.5 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Request\FauxRequest;
|
|
|
|
/**
|
|
* @covers HTMLRestrictionsField
|
|
*/
|
|
class HTMLRestrictionsFieldTest extends PHPUnit\Framework\TestCase {
|
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
public function testConstruct() {
|
|
$htmlForm = $this->createMock( HTMLForm::class );
|
|
$htmlForm->method( 'msg' )
|
|
->willReturnCallback( static function ( ...$args ) {
|
|
return call_user_func_array( 'wfMessage', $args );
|
|
} );
|
|
|
|
$field = new HTMLRestrictionsField( [ 'fieldname' => 'restrictions', 'parent' => $htmlForm ] );
|
|
$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',
|
|
'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( $text, $value ) {
|
|
$request = new FauxRequest( [ 'wprestrictions' => $text ], 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 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 ],
|
|
];
|
|
}
|
|
}
|