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
72 lines
2.7 KiB
PHP
72 lines
2.7 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Interwiki\InterwikiLookupAdapter;
|
|
|
|
/**
|
|
* @covers HTMLTitleTextFieldTest
|
|
*/
|
|
class HTMLTitleTextFieldTest extends MediaWikiIntegrationTestCase {
|
|
|
|
/**
|
|
* @dataProvider provideInterwiki
|
|
*/
|
|
public function testInterwiki( array $config, string $value, $expected ) {
|
|
$this->setupInterwikiTable();
|
|
$htmlForm = $this->createMock( HTMLForm::class );
|
|
$htmlForm->method( 'msg' )
|
|
->willReturnCallback( static function ( ...$args ) {
|
|
return call_user_func_array( 'wfMessage', $args );
|
|
} );
|
|
|
|
$field = new HTMLTitleTextField( $config + [ 'fieldname' => 'foo', 'parent' => $htmlForm ] );
|
|
$result = $field->validate( $value, [ 'foo' => $value ] );
|
|
if ( $result instanceof Message ) {
|
|
$this->assertSame( $expected, $result->getKey() );
|
|
} else {
|
|
$this->assertSame( $expected, $result );
|
|
}
|
|
}
|
|
|
|
public function provideInterwiki() {
|
|
return [
|
|
'local title' => [ [ 'interwiki' => false ], 'SomeTitle', true ],
|
|
'interwiki title, default' => [ [], 'unittest_foo:SomeTitle', 'htmlform-title-interwiki' ],
|
|
'interwiki title, disallowed' => [ [ 'interwiki' => false ],
|
|
'unittest_foo:SomeTitle', 'htmlform-title-interwiki' ],
|
|
'interwiki title, allowed' => [ [ 'interwiki' => true ],
|
|
'unittest_foo:SomeTitle', true ],
|
|
'namespace safety check' => [ [ 'interwiki' => true, 'namespace' => NS_TALK ],
|
|
'SomeTitle', 'htmlform-title-badnamespace' ],
|
|
'interwiki ignores namespace' => [ [ 'interwiki' => true, 'namespace' => NS_TALK ],
|
|
'unittest_foo:SomeTitle', true ],
|
|
'creatable safety check' => [ [ 'interwiki' => true, 'creatable' => true ],
|
|
'Special:Version', 'htmlform-title-not-creatable' ],
|
|
'interwiki ignores creatable' => [ [ 'interwiki' => true, 'creatable' => true ],
|
|
'unittest_foo:Special:Version', true ],
|
|
'exists safety check' => [ [ 'interwiki' => true, 'exists' => true ],
|
|
'SomeTitle', 'htmlform-title-not-exists' ],
|
|
'interwiki ignores exists' => [ [ 'interwiki' => true, 'exists' => true ],
|
|
'unittest_foo:SomeTitle', true ],
|
|
];
|
|
}
|
|
|
|
public function testInterwiki_relative() {
|
|
$this->expectException( InvalidArgumentException::class );
|
|
$field = new HTMLTitleTextField( [
|
|
'fieldname' => 'foo',
|
|
'interwiki' => true,
|
|
'relative' => true,
|
|
'parent' => $this->createMock( HTMLForm::class )
|
|
] );
|
|
$field->validate( 'SomeTitle', [ 'foo' => 'SomeTitle' ] );
|
|
}
|
|
|
|
protected function setupInterwikiTable() {
|
|
$site = new Site( Site::TYPE_MEDIAWIKI );
|
|
$site->setGlobalId( 'unittest_foowiki' );
|
|
$site->addInterwikiId( 'unittest_foo' );
|
|
$this->setService( 'InterwikiLookup', new InterwikiLookupAdapter( new HashSiteStore( [ $site ] ) ) );
|
|
$this->assertTrue( Title::newFromText( 'unittest_foo:SomeTitle' )->isExternal() );
|
|
}
|
|
|
|
}
|