== Skin::wrapHTML ==
Skin::wrapHTML no longer has to perform any guessing of the
ParserOutput language. Nor does it have to special wiki pages vs
special pages in this regard. Yay, code removal.
== ImagePage ==
On URLs like /wiki/File:Example.jpg, the main output handler is
ImagePage::view. This calls the parent Article::view to handle most of
its output. Article::view obtains the ParserOptions, and then fetches
ParserOutput, and then adds `<div class=mw-parser-output>` and its
metadata to OutputPage.
Before this change, ImagePage::view was creating a wrapper based
on "predicting" what language the ParserOutput will contain. It
couldn't call the new OutputPage::getContentLanguage or some
equivalent as Article::view wouldn't have populated that yet.
This leaky abstraction is fixed by this change as now the `<div>`
from ParserOutput no longer comes with a "please wrap it properly"
contract that Article subclasses couldn't possibly implement correctly
(it coudln't wrap it after the fact because Article::view writes to
OutputPage directly).
RECENT (T310445):
A special case was recently added for file pages about translated SVGs.
For those, we decide which language to use for the "fullMedia" thumb
atop the page. This was recently changed as part of T310445 from a
hardcoded $wgLanguageCode (site content lang) to new problematic
Title::getPageViewLanguage, which tries to guestimate the page
language of the rendered ParserOutput and then gets the preferred
variant for the current user. The motivation for this was to support
language variants but used Title::getPageViewLanguage as a kitchen
sink to achieve that minor side-effect. The only part of this
now-deprecated method that we actually need is
LanguageConverter::getPreferredVariant().
Test plan: Covered by ImagePageTest.
== Skin mainpage-title ==
RECENT (T331095, T298715):
A special case was added to Skin::getTemplateData that powers the
mainpage-title interface message feature. This is empty by default,
but when created via MediaWiki:mainpage-title allows interface admins
to replace the H1 with a custom and localised page heading.
A few months ago, in Ifc9f0a7174, Title::getPageViewLanguage was
applied here to support language variants. Replace with the same
fix as for ImagePage. Revert back to Message::inContentLanguage()
but refactor to inLanguage() via MediaWikiServices::getContentLanguage
so that LanguageConverter::getPreferredVariant can be applied.
== EditPage ==
This was doing similar "predicting" of the ParserOutput language to
create an empty preview placeholder for use by preview.js. Now that
ApiParse (via ParserOutput::getText) returns a usable element without
any secret "you magically know the right class, lang, and dir" contract,
this placeholder is no longer needed.
Test Plan:
* EditPage: Default preview
1. index.php?title=Main_Page&action=edit
2. Show preview
3. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
* EditPage: JS preview
1. Preferences > Editing > Show preview without reload
2. index.php?title=Main_Page&action=edit
3. Show preview
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
5. Type something and 'Show preview' again
6. Assert old element gone, new text is shown, and new element
attributes are the same as the above.
== McrUndoAction ==
Same as EditPage basically, but without the JS preview use case.
== DifferenceEngine ==
Test:
1. Open /w/index.php?title=Main_Page&diff=0
(this shows the latest diff, can do manually by viewing
/wiki/Main_Page, click "View history", click "Compare selected revisions")
2. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
3. Open /w/index.php?title=Main_Page&diff=0&action=render
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
== Special:ExpandTemplates ==
Test:
1. /wiki/Special:ExpandTemplates
2. Write "Hello".
3. "OK"
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
Bug: T341244
Depends-On: Icd9c079f5896ee83d86b9c2699636dc81d25a14c
Depends-On: I4e7484b3b94f1cb6062e7cef9f20626b650bb4b1
Depends-On: I90b88f3b3a3bbeba4f48d118f92f54864997e105
Change-Id: Ib130a055e46764544af0f1a46d2bc2b3a7ee85b7
681 lines
22 KiB
PHP
681 lines
22 KiB
PHP
<?php
|
|
|
|
use MediaWiki\Content\ValidationParams;
|
|
use MediaWiki\Languages\LanguageNameUtils;
|
|
use MediaWiki\Linker\LinkRenderer;
|
|
use MediaWiki\MainConfigNames;
|
|
use MediaWiki\Page\Hook\OpportunisticLinksUpdateHook;
|
|
use MediaWiki\Page\PageIdentity;
|
|
use MediaWiki\Page\PageIdentityValue;
|
|
use MediaWiki\Parser\MagicWordFactory;
|
|
use MediaWiki\Parser\Parsoid\ParsoidParserFactory;
|
|
use MediaWiki\Title\Title;
|
|
use MediaWiki\Title\TitleFactory;
|
|
use Wikimedia\TestingAccessWrapper;
|
|
use Wikimedia\UUID\GlobalIdGenerator;
|
|
|
|
/**
|
|
* @group ContentHandler
|
|
* @group Database
|
|
*/
|
|
class ContentHandlerTest extends MediaWikiIntegrationTestCase {
|
|
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
|
|
$this->overrideConfigValues( [
|
|
MainConfigNames::ExtraNamespaces => [
|
|
12312 => 'Dummy',
|
|
12313 => 'Dummy_talk',
|
|
],
|
|
// The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..)
|
|
// default to CONTENT_MODEL_WIKITEXT.
|
|
MainConfigNames::NamespaceContentModels => [
|
|
12312 => 'testing',
|
|
],
|
|
MainConfigNames::ContentHandlers => [
|
|
CONTENT_MODEL_WIKITEXT => [
|
|
'class' => WikitextContentHandler::class,
|
|
'services' => [
|
|
'TitleFactory',
|
|
'ParserFactory',
|
|
'GlobalIdGenerator',
|
|
'LanguageNameUtils',
|
|
'LinkRenderer',
|
|
'MagicWordFactory',
|
|
'ParsoidParserFactory',
|
|
],
|
|
],
|
|
CONTENT_MODEL_JAVASCRIPT => JavaScriptContentHandler::class,
|
|
CONTENT_MODEL_JSON => JsonContentHandler::class,
|
|
CONTENT_MODEL_CSS => CssContentHandler::class,
|
|
CONTENT_MODEL_TEXT => TextContentHandler::class,
|
|
'testing' => DummyContentHandlerForTesting::class,
|
|
'testing-callbacks' => static function ( $modelId ) {
|
|
return new DummyContentHandlerForTesting( $modelId );
|
|
}
|
|
],
|
|
] );
|
|
}
|
|
|
|
public function addDBDataOnce() {
|
|
$this->insertPage( 'Not_Main_Page', 'This is not a main page' );
|
|
$this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]' );
|
|
}
|
|
|
|
public static function dataGetDefaultModelFor() {
|
|
return [
|
|
[ 'Help:Foo', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'Help:Foo.css', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'Help:Foo.json', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'User:Foo', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'User:Foo.js', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'User:Foo.css', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'User:Foo.json', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
|
|
[ 'User:Foo/bar.css', CONTENT_MODEL_CSS ],
|
|
[ 'User:Foo/bar.json', CONTENT_MODEL_JSON ],
|
|
[ 'User:Foo/bar.json.nope', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
|
|
[ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ],
|
|
[ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ],
|
|
[ 'MediaWiki:Foo.json', CONTENT_MODEL_JSON ],
|
|
[ 'MediaWiki:Foo.JSON', CONTENT_MODEL_WIKITEXT ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider dataGetDefaultModelFor
|
|
* @covers ContentHandler::getDefaultModelFor
|
|
*/
|
|
public function testGetDefaultModelFor( $title, $expectedModelId ) {
|
|
$title = Title::newFromText( $title );
|
|
$this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) );
|
|
}
|
|
|
|
public static function dataGetLocalizedName() {
|
|
return [
|
|
[ null, null ],
|
|
[ "xyzzy", null ],
|
|
|
|
// XXX: depends on content language
|
|
[ CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider dataGetLocalizedName
|
|
* @covers ContentHandler::getLocalizedName
|
|
*/
|
|
public function testGetLocalizedName( $id, $expected ) {
|
|
$name = ContentHandler::getLocalizedName( $id );
|
|
|
|
if ( $expected ) {
|
|
$this->assertNotNull( $name, "no name found for content model $id" );
|
|
$this->assertTrue( preg_match( $expected, $name ) > 0,
|
|
"content model name for #$id did not match pattern $expected"
|
|
);
|
|
} else {
|
|
$this->assertEquals( $id, $name, "localization of unknown model $id should have "
|
|
. "fallen back to use the model id directly."
|
|
);
|
|
}
|
|
}
|
|
|
|
public static function dataGetPageLanguage() {
|
|
global $wgLanguageCode;
|
|
|
|
return [
|
|
[ "Main", $wgLanguageCode ],
|
|
[ "Dummy:Foo", $wgLanguageCode ],
|
|
[ "MediaWiki:common.js", 'en' ],
|
|
[ "User:Foo/common.js", 'en' ],
|
|
[ "MediaWiki:common.css", 'en' ],
|
|
[ "User:Foo/common.css", 'en' ],
|
|
[ "User:Foo", $wgLanguageCode ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider dataGetPageLanguage
|
|
* @covers ContentHandler::getPageLanguage
|
|
*/
|
|
public function testGetPageLanguage( $title, $expected ) {
|
|
$title = Title::newFromText( $title );
|
|
$this->getServiceContainer()->getLinkCache()->addBadLinkObj( $title );
|
|
|
|
$handler = $this->getServiceContainer()
|
|
->getContentHandlerFactory()
|
|
->getContentHandler( $title->getContentModel() );
|
|
$lang = $handler->getPageLanguage( $title );
|
|
|
|
$this->assertInstanceOf( Language::class, $lang );
|
|
$this->assertEquals( $expected, $lang->getCode() );
|
|
}
|
|
|
|
public static function dataGetContentText_Null() {
|
|
return [
|
|
[ 'fail' ],
|
|
[ 'serialize' ],
|
|
[ 'ignore' ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider dataGetContentText_Null
|
|
* @covers ContentHandler::getContentText
|
|
*/
|
|
public function testGetContentText_Null( $contentHandlerTextFallback ) {
|
|
$this->overrideConfigValue( MainConfigNames::ContentHandlerTextFallback, $contentHandlerTextFallback );
|
|
|
|
$content = null;
|
|
|
|
$text = ContentHandler::getContentText( $content );
|
|
$this->assertSame( '', $text );
|
|
}
|
|
|
|
public static function dataGetContentText_TextContent() {
|
|
return [
|
|
[ 'fail' ],
|
|
[ 'serialize' ],
|
|
[ 'ignore' ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider dataGetContentText_TextContent
|
|
* @covers ContentHandler::getContentText
|
|
*/
|
|
public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
|
|
$this->overrideConfigValue( MainConfigNames::ContentHandlerTextFallback, $contentHandlerTextFallback );
|
|
|
|
$content = new WikitextContent( "hello world" );
|
|
|
|
$text = ContentHandler::getContentText( $content );
|
|
$this->assertEquals( $content->getText(), $text );
|
|
}
|
|
|
|
/**
|
|
* ContentHandler::getContentText should have thrown an exception for non-text Content object
|
|
*
|
|
* @covers ContentHandler::getContentText
|
|
*/
|
|
public function testGetContentText_NonTextContent_fail() {
|
|
$this->overrideConfigValue( MainConfigNames::ContentHandlerTextFallback, 'fail' );
|
|
|
|
$content = new DummyContentForTesting( "hello world" );
|
|
|
|
$this->expectException( MWException::class );
|
|
ContentHandler::getContentText( $content );
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getContentText
|
|
*/
|
|
public function testGetContentText_NonTextContent_serialize() {
|
|
$this->overrideConfigValue( MainConfigNames::ContentHandlerTextFallback, 'serialize' );
|
|
|
|
$content = new DummyContentForTesting( "hello world" );
|
|
|
|
$text = ContentHandler::getContentText( $content );
|
|
$this->assertEquals( $content->serialize(), $text );
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getContentText
|
|
*/
|
|
public function testGetContentText_NonTextContent_ignore() {
|
|
$this->overrideConfigValue( MainConfigNames::ContentHandlerTextFallback, 'ignore' );
|
|
|
|
$content = new DummyContentForTesting( "hello world" );
|
|
|
|
$text = ContentHandler::getContentText( $content );
|
|
$this->assertNull( $text );
|
|
}
|
|
|
|
public static function dataMakeContent() {
|
|
return [
|
|
[ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, false ],
|
|
[ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, false ],
|
|
[ 'hallo', 'Dummy:Test', null, null, "testing", false ],
|
|
|
|
[
|
|
'hallo',
|
|
'Help:Test',
|
|
null,
|
|
CONTENT_FORMAT_WIKITEXT,
|
|
CONTENT_MODEL_WIKITEXT,
|
|
false
|
|
],
|
|
[
|
|
'hallo',
|
|
'MediaWiki:Test.js',
|
|
null,
|
|
CONTENT_FORMAT_JAVASCRIPT,
|
|
CONTENT_MODEL_JAVASCRIPT,
|
|
false
|
|
],
|
|
[ 'hallo', 'Dummy:Test', null, "testing", "testing", false ],
|
|
|
|
[ 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, false ],
|
|
[
|
|
'hallo',
|
|
'MediaWiki:Test.js',
|
|
CONTENT_MODEL_CSS,
|
|
null,
|
|
CONTENT_MODEL_CSS,
|
|
false
|
|
],
|
|
[
|
|
serialize( 'hallo' ),
|
|
'Dummy:Test',
|
|
CONTENT_MODEL_CSS,
|
|
null,
|
|
CONTENT_MODEL_CSS,
|
|
false
|
|
],
|
|
|
|
[ 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, true ],
|
|
[ 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, true ],
|
|
[ 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, true ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider dataMakeContent
|
|
* @covers ContentHandler::makeContent
|
|
*/
|
|
public function testMakeContent( $data, $title, $modelId, $format,
|
|
$expectedModelId, $shouldFail
|
|
) {
|
|
$title = Title::newFromText( $title );
|
|
$this->getServiceContainer()->getLinkCache()->addBadLinkObj( $title );
|
|
try {
|
|
$content = ContentHandler::makeContent( $data, $title, $modelId, $format );
|
|
|
|
if ( $shouldFail ) {
|
|
$this->fail( "ContentHandler::makeContent should have failed!" );
|
|
}
|
|
|
|
$this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
|
|
$this->assertEquals( $data, $content->serialize(), 'bad serialized data' );
|
|
} catch ( MWException $ex ) {
|
|
if ( !$shouldFail ) {
|
|
$this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
|
|
} else {
|
|
// dummy, so we don't get the "test did not perform any assertions" message.
|
|
$this->assertTrue( true );
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getAutosummary
|
|
*
|
|
* Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
|
|
* page.
|
|
*/
|
|
public function testGetAutosummary() {
|
|
$this->setContentLang( 'en' );
|
|
|
|
$content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT );
|
|
$title = Title::makeTitle( NS_HELP, 'Test' );
|
|
// Create a new content object with no content
|
|
$newContent = ContentHandler::makeContent( '', $title, CONTENT_MODEL_WIKITEXT, null );
|
|
// first check, if we become a blank page created summary with the right bitmask
|
|
$autoSummary = $content->getAutosummary( null, $newContent, 97 );
|
|
$this->assertEquals(
|
|
wfMessage( 'autosumm-newblank' )->inContentLanguage()->text(),
|
|
$autoSummary
|
|
);
|
|
// now check, what we become with another bitmask
|
|
$autoSummary = $content->getAutosummary( null, $newContent, 92 );
|
|
$this->assertSame( '', $autoSummary );
|
|
}
|
|
|
|
/**
|
|
* Test software tag that is added when content model of the page changes
|
|
* @covers ContentHandler::getChangeTag
|
|
*/
|
|
public function testGetChangeTag() {
|
|
$this->overrideConfigValue( MainConfigNames::SoftwareTags, [ 'mw-contentmodelchange' => true ] );
|
|
$wikitextContentHandler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT );
|
|
// Create old content object with javascript content model
|
|
$oldContent = ContentHandler::makeContent( '', null, CONTENT_MODEL_JAVASCRIPT, null );
|
|
// Create new content object with wikitext content model
|
|
$newContent = ContentHandler::makeContent( '', null, CONTENT_MODEL_WIKITEXT, null );
|
|
// Get the tag for this edit
|
|
$tag = $wikitextContentHandler->getChangeTag( $oldContent, $newContent, EDIT_UPDATE );
|
|
$this->assertSame( 'mw-contentmodelchange', $tag );
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::supportsCategories
|
|
*/
|
|
public function testSupportsCategories() {
|
|
$handler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT );
|
|
$this->assertTrue( $handler->supportsCategories(), 'content model supports categories' );
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::supportsDirectEditing
|
|
*/
|
|
public function testSupportsDirectEditing() {
|
|
$handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON );
|
|
$this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
|
|
}
|
|
|
|
public static function dummyHookHandler( $foo, &$text, $bar ) {
|
|
if ( $text === null || $text === false ) {
|
|
return false;
|
|
}
|
|
|
|
$text = strtoupper( $text );
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function provideGetModelForID() {
|
|
return [
|
|
[ CONTENT_MODEL_WIKITEXT, WikitextContentHandler::class ],
|
|
[ CONTENT_MODEL_JAVASCRIPT, JavaScriptContentHandler::class ],
|
|
[ CONTENT_MODEL_JSON, JsonContentHandler::class ],
|
|
[ CONTENT_MODEL_CSS, CssContentHandler::class ],
|
|
[ CONTENT_MODEL_TEXT, TextContentHandler::class ],
|
|
[ 'testing', DummyContentHandlerForTesting::class ],
|
|
[ 'testing-callbacks', DummyContentHandlerForTesting::class ],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getForModelID
|
|
* @dataProvider provideGetModelForID
|
|
*/
|
|
public function testGetModelForID( $modelId, $handlerClass ) {
|
|
$handler = ContentHandler::getForModelID( $modelId );
|
|
|
|
$this->assertInstanceOf( $handlerClass, $handler );
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getFieldsForSearchIndex
|
|
*/
|
|
public function testGetFieldsForSearchIndex() {
|
|
$searchEngine = $this->newSearchEngine();
|
|
|
|
$handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
|
|
|
|
$fields = $handler->getFieldsForSearchIndex( $searchEngine );
|
|
|
|
$this->assertArrayHasKey( 'category', $fields );
|
|
$this->assertArrayHasKey( 'external_link', $fields );
|
|
$this->assertArrayHasKey( 'outgoing_link', $fields );
|
|
$this->assertArrayHasKey( 'template', $fields );
|
|
$this->assertArrayHasKey( 'content_model', $fields );
|
|
}
|
|
|
|
private function newSearchEngine() {
|
|
$searchEngine = $this->createMock( SearchEngine::class );
|
|
|
|
$searchEngine->method( 'makeSearchFieldMapping' )
|
|
->willReturnCallback( static function ( $name, $type ) {
|
|
return new DummySearchIndexFieldDefinition( $name, $type );
|
|
} );
|
|
|
|
return $searchEngine;
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getDataForSearchIndex
|
|
*/
|
|
public function testDataIndexFields() {
|
|
$mockEngine = $this->createMock( SearchEngine::class );
|
|
$title = Title::makeTitle( NS_MAIN, 'Not_Main_Page' );
|
|
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
|
|
|
|
$this->setTemporaryHook( 'SearchDataForIndex',
|
|
static function (
|
|
&$fields,
|
|
ContentHandler $handler,
|
|
WikiPage $page,
|
|
ParserOutput $output,
|
|
SearchEngine $engine
|
|
) {
|
|
$fields['testDataField'] = 'test content';
|
|
} );
|
|
|
|
$revision = $page->getRevisionRecord();
|
|
$output = $page->getContentHandler()->getParserOutputForIndexing( $page, null, $revision );
|
|
$data = $page->getContentHandler()->getDataForSearchIndex( $page, $output, $mockEngine, $revision );
|
|
$this->assertArrayHasKey( 'text', $data );
|
|
$this->assertArrayHasKey( 'text_bytes', $data );
|
|
$this->assertArrayHasKey( 'language', $data );
|
|
$this->assertArrayHasKey( 'testDataField', $data );
|
|
$this->assertEquals( 'test content', $data['testDataField'] );
|
|
$this->assertEquals( 'wikitext', $data['content_model'] );
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getParserOutputForIndexing
|
|
*/
|
|
public function testParserOutputForIndexing() {
|
|
$opportunisticUpdateHook =
|
|
$this->createMock( OpportunisticLinksUpdateHook::class );
|
|
// WikiPage::triggerOpportunisticLinksUpdate should not be triggered when
|
|
// getParserOutputForIndexing is called
|
|
$opportunisticUpdateHook->expects( $this->never() )
|
|
->method( 'onOpportunisticLinksUpdate' )
|
|
->willReturn( false );
|
|
$this->setTemporaryHook( 'OpportunisticLinksUpdate', $opportunisticUpdateHook );
|
|
|
|
$title = Title::makeTitle( NS_MAIN, 'Smithee' );
|
|
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
|
|
$revision = $page->getRevisionRecord();
|
|
|
|
$out = $page->getContentHandler()->getParserOutputForIndexing( $page, null, $revision );
|
|
$this->assertInstanceOf( ParserOutput::class, $out );
|
|
$this->assertStringContainsString( 'one who smiths', $out->getRawText() );
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getContentModels
|
|
*/
|
|
public function testGetContentModelsHook() {
|
|
$this->setTemporaryHook( 'GetContentModels', static function ( &$models ) {
|
|
$models[] = 'Ferrari';
|
|
} );
|
|
$this->assertContains( 'Ferrari', ContentHandler::getContentModels() );
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getSlotDiffRenderer
|
|
*/
|
|
public function testGetSlotDiffRenderer_default() {
|
|
$this->mergeMwGlobalArrayValue( 'wgHooks', [
|
|
'GetSlotDiffRenderer' => [],
|
|
] );
|
|
|
|
// test default renderer
|
|
$contentHandler = new WikitextContentHandler(
|
|
CONTENT_MODEL_WIKITEXT,
|
|
$this->createMock( TitleFactory::class ),
|
|
$this->createMock( ParserFactory::class ),
|
|
$this->createMock( GlobalIdGenerator::class ),
|
|
$this->createMock( LanguageNameUtils::class ),
|
|
$this->createMock( LinkRenderer::class ),
|
|
$this->createMock( MagicWordFactory::class ),
|
|
$this->createMock( ParsoidParserFactory::class )
|
|
);
|
|
$slotDiffRenderer = $contentHandler->getSlotDiffRenderer( RequestContext::getMain() );
|
|
$this->assertInstanceOf( TextSlotDiffRenderer::class, $slotDiffRenderer );
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getSlotDiffRenderer
|
|
*/
|
|
public function testGetSlotDiffRenderer_bc() {
|
|
$this->mergeMwGlobalArrayValue( 'wgHooks', [
|
|
'GetSlotDiffRenderer' => [],
|
|
] );
|
|
|
|
// test B/C renderer
|
|
$customDifferenceEngine = $this->createMock( DifferenceEngine::class );
|
|
// hack to track object identity across cloning
|
|
$customDifferenceEngine->objectId = 12345;
|
|
$customContentHandler = $this->getMockBuilder( ContentHandler::class )
|
|
->setConstructorArgs( [ 'foo', [] ] )
|
|
->onlyMethods( [ 'createDifferenceEngine' ] )
|
|
->getMockForAbstractClass();
|
|
$customContentHandler->method( 'createDifferenceEngine' )
|
|
->willReturn( $customDifferenceEngine );
|
|
/** @var ContentHandler $customContentHandler */
|
|
$slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext::getMain() );
|
|
$this->assertInstanceOf( DifferenceEngineSlotDiffRenderer::class, $slotDiffRenderer );
|
|
$this->assertSame(
|
|
$customDifferenceEngine->objectId,
|
|
TestingAccessWrapper::newFromObject( $slotDiffRenderer )->differenceEngine->objectId
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getSlotDiffRenderer
|
|
*/
|
|
public function testGetSlotDiffRenderer_nobc() {
|
|
$this->mergeMwGlobalArrayValue( 'wgHooks', [
|
|
'GetSlotDiffRenderer' => [],
|
|
] );
|
|
|
|
// test that B/C renderer does not get used when getSlotDiffRendererInternal is overridden
|
|
$customDifferenceEngine = $this->createMock( DifferenceEngine::class );
|
|
$customSlotDiffRenderer = $this->getMockBuilder( SlotDiffRenderer::class )
|
|
->disableOriginalConstructor()
|
|
->getMockForAbstractClass();
|
|
$customContentHandler2 = $this->getMockBuilder( ContentHandler::class )
|
|
->setConstructorArgs( [ 'bar', [] ] )
|
|
->onlyMethods( [ 'createDifferenceEngine', 'getSlotDiffRendererInternal' ] )
|
|
->getMockForAbstractClass();
|
|
$customContentHandler2->method( 'createDifferenceEngine' )
|
|
->willReturn( $customDifferenceEngine );
|
|
$customContentHandler2->method( 'getSlotDiffRendererInternal' )
|
|
->willReturn( $customSlotDiffRenderer );
|
|
/** @var ContentHandler $customContentHandler2 */
|
|
$this->hideDeprecated( 'ContentHandler::getSlotDiffRendererInternal' );
|
|
$slotDiffRenderer = $customContentHandler2->getSlotDiffRenderer( RequestContext::getMain() );
|
|
$this->assertSame( $customSlotDiffRenderer, $slotDiffRenderer );
|
|
}
|
|
|
|
/**
|
|
* @covers ContentHandler::getSlotDiffRenderer
|
|
*/
|
|
public function testGetSlotDiffRenderer_hook() {
|
|
$this->mergeMwGlobalArrayValue( 'wgHooks', [
|
|
'GetSlotDiffRenderer' => [],
|
|
] );
|
|
|
|
// test that the hook handler takes precedence
|
|
$customDifferenceEngine = $this->createMock( DifferenceEngine::class );
|
|
$customContentHandler = $this->getMockBuilder( ContentHandler::class )
|
|
->setConstructorArgs( [ 'foo', [] ] )
|
|
->onlyMethods( [ 'createDifferenceEngine' ] )
|
|
->getMockForAbstractClass();
|
|
$customContentHandler->method( 'createDifferenceEngine' )
|
|
->willReturn( $customDifferenceEngine );
|
|
/** @var ContentHandler $customContentHandler */
|
|
|
|
$customSlotDiffRenderer = $this->getMockBuilder( SlotDiffRenderer::class )
|
|
->disableOriginalConstructor()
|
|
->getMockForAbstractClass();
|
|
$customContentHandler2 = $this->getMockBuilder( ContentHandler::class )
|
|
->setConstructorArgs( [ 'bar', [] ] )
|
|
->onlyMethods( [ 'createDifferenceEngine', 'getSlotDiffRendererInternal' ] )
|
|
->getMockForAbstractClass();
|
|
$customContentHandler2->method( 'createDifferenceEngine' )
|
|
->willReturn( $customDifferenceEngine );
|
|
$customContentHandler2->method( 'getSlotDiffRendererInternal' )
|
|
->willReturn( $customSlotDiffRenderer );
|
|
/** @var ContentHandler $customContentHandler2 */
|
|
|
|
$customSlotDiffRenderer2 = $this->getMockBuilder( SlotDiffRenderer::class )
|
|
->disableOriginalConstructor()
|
|
->getMockForAbstractClass();
|
|
$this->setTemporaryHook( 'GetSlotDiffRenderer',
|
|
static function ( $handler, &$slotDiffRenderer ) use ( $customSlotDiffRenderer2 ) {
|
|
$slotDiffRenderer = $customSlotDiffRenderer2;
|
|
} );
|
|
|
|
$this->hideDeprecated( 'ContentHandler::getSlotDiffRendererInternal' );
|
|
$slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext::getMain() );
|
|
$this->assertSame( $customSlotDiffRenderer2, $slotDiffRenderer );
|
|
|
|
$this->hideDeprecated( 'ContentHandler::getSlotDiffRendererInternal' );
|
|
$slotDiffRenderer = $customContentHandler2->getSlotDiffRenderer( RequestContext::getMain() );
|
|
$this->assertSame( $customSlotDiffRenderer2, $slotDiffRenderer );
|
|
}
|
|
|
|
public static function providerGetPageViewLanguage() {
|
|
yield [ NS_FILE, 'sr', 'sr-ec', 'sr-ec' ];
|
|
yield [ NS_FILE, 'sr', 'sr', 'sr' ];
|
|
yield [ NS_MEDIAWIKI, 'sr-ec', 'sr', 'sr-ec' ];
|
|
yield [ NS_MEDIAWIKI, 'sr', 'sr-ec', 'sr' ];
|
|
}
|
|
|
|
/**
|
|
* Superseded by OutputPageTest::testGetJsVarsAboutPageLang
|
|
*
|
|
* @dataProvider providerGetPageViewLanguage
|
|
* @covers ContentHandler::getPageViewLanguage
|
|
*/
|
|
public function testGetPageViewLanguage( $namespace, $lang, $variant, $expected ) {
|
|
$contentHandler = $this->getMockBuilder( ContentHandler::class )
|
|
->disableOriginalConstructor()
|
|
->getMockForAbstractClass();
|
|
|
|
$title = Title::makeTitle( $namespace, 'SimpleTitle' );
|
|
|
|
$this->overrideConfigValue( MainConfigNames::DefaultLanguageVariant, $variant );
|
|
|
|
$this->setUserLang( $lang );
|
|
$this->setContentLang( $lang );
|
|
|
|
$pageViewLanguage = $contentHandler->getPageViewLanguage( $title );
|
|
$this->assertEquals( $expected, $pageViewLanguage->getCode() );
|
|
}
|
|
|
|
public static function provideValidateSave() {
|
|
yield 'wikitext' => [
|
|
new WikitextContent( 'hello world' ),
|
|
true
|
|
];
|
|
|
|
yield 'valid json' => [
|
|
new JsonContent( '{ "0": "bar" }' ),
|
|
true
|
|
];
|
|
|
|
yield 'invalid json' => [
|
|
new JsonContent( 'foo' ),
|
|
false
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider provideValidateSave
|
|
* @covers ContentHandler::validateSave
|
|
*/
|
|
public function testValidateSave( $content, $expectedResult ) {
|
|
$page = new PageIdentityValue( 0, 1, 'Foo', PageIdentity::LOCAL );
|
|
$contentHandlerFactory = $this->getServiceContainer()->getContentHandlerFactory();
|
|
$contentHandler = $contentHandlerFactory->getContentHandler( $content->getModel() );
|
|
$validateParams = new ValidationParams( $page, 0 );
|
|
|
|
$status = $contentHandler->validateSave( $content, $validateParams );
|
|
$this->assertEquals( $expectedResult, $status->isOK() );
|
|
}
|
|
}
|