wiki.techinc.nl/tests/phpunit/includes/htmlform/HTMLRestrictionsFieldTest.php
James D. Forrester ad06527fb4 Reorg: Namespace the Title class
This is moderately messy.

Process was principally:

* xargs rg --files-with-matches '^use Title;' | grep 'php$' | \
  xargs -P 1 -n 1 sed -i -z 's/use Title;/use MediaWiki\\Title\\Title;/1'
* rg --files-without-match 'MediaWiki\\Title\\Title;' . | grep 'php$' | \
  xargs rg --files-with-matches 'Title\b' | \
  xargs -P 1 -n 1 sed -i -z 's/\nuse /\nuse MediaWiki\\Title\\Title;\nuse /1'
* composer fix

Then manual fix-ups for a few files that don't have any use statements.

Bug: T166010
Follows-Up: Ia5d8cb759dc3bc9e9bbe217d0fb109e2f8c4101a
Change-Id: If8fc9d0d95fc1a114021e282a706fc3e7da3524b
2023-03-02 08:46:53 -05:00

76 lines
2.4 KiB
PHP

<?php
use MediaWiki\Request\FauxRequest;
use MediaWiki\Title\Title;
/**
* @covers HTMLRestrictionsField
*/
class HTMLRestrictionsFieldTest extends PHPUnit\Framework\TestCase {
use MediaWikiCoversValidator;
public function testConstruct() {
$htmlForm = $this->createMock( HTMLForm::class );
$htmlForm->method( 'msg' )->willReturnCallback( 'wfMessage' );
$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 ],
];
}
}