wiki.techinc.nl/tests/phpunit/includes/content/ContentHandlerTest.php

685 lines
23 KiB
PHP
Raw Normal View History

<?php
use MediaWiki\Content\ValidationParams;
use MediaWiki\Languages\LanguageNameUtils;
use MediaWiki\MainConfigNames;
use MediaWiki\Page\Hook\OpportunisticLinksUpdateHook;
use MediaWiki\Page\PageIdentity;
use MediaWiki\Page\PageIdentityValue;
use MediaWiki\Parser\MagicWordFactory;
Allow setting a ParserOption to generate Parsoid HTML This is an initial quick-and-dirty implementation. The ParsoidParser class will eventually inherit from \Parser, but this is an initial placeholder to unblock other Parsoid read views work. Currently Parsoid does not fully implement all the ParserOutput metadata set by the legacy parser, but we're working on it. This patch also addresses T300325 by ensuring the the Page HTML APIs use ParserOutput::getRawText(), which will return the entire Parsoid HTML document without post-processing. This is what the Parsoid team refers to as "edit mode" HTML. The ParserOutput::getText() method returns only the <body> contents of the HTML, and applies several transformations, including inserting Table of Contents and style deduplication; this is the "read views" flavor of the Parsoid HTML. We need to be careful of the interaction of the `useParsoid` flag with the ParserCacheMetadata. Effectively `useParsoid` should *always* be marked as "used" or else the ParserCache will assume its value doesn't matter and will serve legacy content for parsoid requests and vice-versa. T330677 is a follow up to address this more thoroughly by splitting the parser cache in ParserOutputAccess; the stop gap in this patch is fragile and, because it doesn't fork the ParserCacheMetadata cache, may corrupt the ParserCacheMetadata in the case when Parsoid and the legacy parser consult different sets of options to render a page. Bug: T300191 Bug: T330677 Bug: T300325 Change-Id: Ica09a4284c00d7917f8b6249e946232b2fb38011
2022-05-27 16:38:32 +00:00
use MediaWiki\Parser\Parsoid\ParsoidParserFactory;
use MediaWiki\Title\Title;
use MediaWiki\Title\TitleFactory;
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
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',
'MagicWordFactory',
Allow setting a ParserOption to generate Parsoid HTML This is an initial quick-and-dirty implementation. The ParsoidParser class will eventually inherit from \Parser, but this is an initial placeholder to unblock other Parsoid read views work. Currently Parsoid does not fully implement all the ParserOutput metadata set by the legacy parser, but we're working on it. This patch also addresses T300325 by ensuring the the Page HTML APIs use ParserOutput::getRawText(), which will return the entire Parsoid HTML document without post-processing. This is what the Parsoid team refers to as "edit mode" HTML. The ParserOutput::getText() method returns only the <body> contents of the HTML, and applies several transformations, including inserting Table of Contents and style deduplication; this is the "read views" flavor of the Parsoid HTML. We need to be careful of the interaction of the `useParsoid` flag with the ParserCacheMetadata. Effectively `useParsoid` should *always* be marked as "used" or else the ParserCache will assume its value doesn't matter and will serve legacy content for parsoid requests and vice-versa. T330677 is a follow up to address this more thoroughly by splitting the parser cache in ParserOutputAccess; the stop gap in this patch is fragile and, because it doesn't fork the ParserCacheMetadata cache, may corrupt the ParserCacheMetadata in the case when Parsoid and the legacy parser consult different sets of options to render a page. Bug: T300191 Bug: T330677 Bug: T300325 Change-Id: Ica09a4284c00d7917f8b6249e946232b2fb38011
2022-05-27 16:38:32 +00:00
'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 ],
];
2012-04-25 16:35:36 +00:00
}
/**
* @dataProvider dataGetDefaultModelFor
* @covers ContentHandler::getDefaultModelFor
2012-04-25 16:35:36 +00:00
*/
public function testGetDefaultModelFor( $title, $expectedModelId ) {
2012-04-25 16:35:36 +00:00
$title = Title::newFromText( $title );
$this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) );
2012-04-25 16:35:36 +00:00
}
2012-04-25 16:35:36 +00:00
/**
* @dataProvider dataGetDefaultModelFor
* @covers ContentHandler::getForTitle
2012-04-25 16:35:36 +00:00
*/
public function testGetForTitle( $title, $expectedContentModel ) {
$this->hideDeprecated( 'ContentHandler::getForTitle' );
2012-04-25 16:35:36 +00:00
$title = Title::newFromText( $title );
$this->getServiceContainer()->getLinkCache()->addBadLinkObj( $title );
2012-04-25 16:35:36 +00:00
$handler = ContentHandler::getForTitle( $title );
$this->assertEquals( $expectedContentModel, $handler->getModelID() );
}
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' ],
];
}
2012-04-25 16:35:36 +00:00
/**
* @dataProvider dataGetContentText_Null
* @covers ContentHandler::getContentText
*/
public function testGetContentText_Null( $contentHandlerTextFallback ) {
$this->overrideConfigValue( MainConfigNames::ContentHandlerTextFallback, $contentHandlerTextFallback );
2012-04-25 16:35:36 +00:00
$content = null;
2012-04-25 16:35:36 +00:00
$text = ContentHandler::getContentText( $content );
$this->assertSame( '', $text );
}
2012-04-25 16:35:36 +00:00
public static function dataGetContentText_TextContent() {
return [
[ 'fail' ],
[ 'serialize' ],
[ 'ignore' ],
];
2012-04-25 16:35:36 +00:00
}
/**
* @dataProvider dataGetContentText_TextContent
* @covers ContentHandler::getContentText
*/
public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
$this->overrideConfigValue( MainConfigNames::ContentHandlerTextFallback, $contentHandlerTextFallback );
2012-04-25 16:35:36 +00:00
$content = new WikitextContent( "hello world" );
$text = ContentHandler::getContentText( $content );
$this->assertEquals( $content->getText(), $text );
2012-04-25 16:35:36 +00:00
}
/**
* 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' );
2012-04-25 16:35:36 +00:00
$content = new DummyContentForTesting( "hello world" );
$this->expectException( MWException::class );
ContentHandler::getContentText( $content );
}
2012-04-25 16:35:36 +00:00
/**
* @covers ContentHandler::getContentText
*/
public function testGetContentText_NonTextContent_serialize() {
$this->overrideConfigValue( MainConfigNames::ContentHandlerTextFallback, 'serialize' );
2012-04-25 16:35:36 +00:00
$content = new DummyContentForTesting( "hello world" );
2012-04-25 16:35:36 +00:00
$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" );
2012-04-25 16:35:36 +00:00
$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 ],
2012-04-25 16:35:36 +00:00
[
'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 ],
2012-04-25 16:35:36 +00:00
[ '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
],
2012-04-20 19:33:13 +00:00
[ '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 ],
];
2012-04-25 16:35:36 +00:00
}
2012-04-20 19:33:13 +00:00
2012-04-25 16:35:36 +00:00
/**
* @dataProvider dataMakeContent
* @covers ContentHandler::makeContent
2012-04-25 16:35:36 +00:00
*/
public function testMakeContent( $data, $title, $modelId, $format,
$expectedModelId, $shouldFail
) {
2012-04-25 16:35:36 +00:00
$title = Title::newFromText( $title );
$this->getServiceContainer()->getLinkCache()->addBadLinkObj( $title );
2012-04-25 16:35:36 +00:00
try {
$content = ContentHandler::makeContent( $data, $title, $modelId, $format );
2012-04-20 19:33:13 +00:00
if ( $shouldFail ) {
$this->fail( "ContentHandler::makeContent should have failed!" );
}
2012-04-20 19:33:13 +00:00
$this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
$this->assertEquals( $data, $content->serialize(), 'bad serialized data' );
2012-04-25 16:35:36 +00:00
} 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 );
}
2012-04-25 16:35:36 +00:00
}
}
2012-04-20 19:33:13 +00:00
/**
* @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::newFromText( '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::newFromText( 'Not_Main_Page', NS_MAIN );
$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::newFromText( 'Smithee', NS_MAIN );
$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() );
}
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
/**
* @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 ),
Allow setting a ParserOption to generate Parsoid HTML This is an initial quick-and-dirty implementation. The ParsoidParser class will eventually inherit from \Parser, but this is an initial placeholder to unblock other Parsoid read views work. Currently Parsoid does not fully implement all the ParserOutput metadata set by the legacy parser, but we're working on it. This patch also addresses T300325 by ensuring the the Page HTML APIs use ParserOutput::getRawText(), which will return the entire Parsoid HTML document without post-processing. This is what the Parsoid team refers to as "edit mode" HTML. The ParserOutput::getText() method returns only the <body> contents of the HTML, and applies several transformations, including inserting Table of Contents and style deduplication; this is the "read views" flavor of the Parsoid HTML. We need to be careful of the interaction of the `useParsoid` flag with the ParserCacheMetadata. Effectively `useParsoid` should *always* be marked as "used" or else the ParserCache will assume its value doesn't matter and will serve legacy content for parsoid requests and vice-versa. T330677 is a follow up to address this more thoroughly by splitting the parser cache in ParserOutputAccess; the stop gap in this patch is fragile and, because it doesn't fork the ParserCacheMetadata cache, may corrupt the ParserCacheMetadata in the case when Parsoid and the legacy parser consult different sets of options to render a page. Bug: T300191 Bug: T330677 Bug: T300325 Change-Id: Ica09a4284c00d7917f8b6249e946232b2fb38011
2022-05-27 16:38:32 +00:00
$this->createMock( MagicWordFactory::class ),
$this->createMock( ParsoidParserFactory::class )
);
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
$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 );
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
// hack to track object identity across cloning
$customDifferenceEngine->objectId = 12345;
$customContentHandler = $this->getMockBuilder( ContentHandler::class )
->setConstructorArgs( [ 'foo', [] ] )
->onlyMethods( [ 'createDifferenceEngine' ] )
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
->getMockForAbstractClass();
$customContentHandler->method( 'createDifferenceEngine' )
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
->willReturn( $customDifferenceEngine );
/** @var ContentHandler $customContentHandler */
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
$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 );
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
$customSlotDiffRenderer = $this->getMockBuilder( SlotDiffRenderer::class )
->disableOriginalConstructor()
->getMockForAbstractClass();
$customContentHandler2 = $this->getMockBuilder( ContentHandler::class )
->setConstructorArgs( [ 'bar', [] ] )
->onlyMethods( [ 'createDifferenceEngine', 'getSlotDiffRendererInternal' ] )
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
->getMockForAbstractClass();
$customContentHandler2->method( 'createDifferenceEngine' )
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
->willReturn( $customDifferenceEngine );
$customContentHandler2->method( 'getSlotDiffRendererInternal' )
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
->willReturn( $customSlotDiffRenderer );
/** @var ContentHandler $customContentHandler2 */
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
$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 );
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
$customContentHandler = $this->getMockBuilder( ContentHandler::class )
->setConstructorArgs( [ 'foo', [] ] )
->onlyMethods( [ 'createDifferenceEngine' ] )
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
->getMockForAbstractClass();
$customContentHandler->method( 'createDifferenceEngine' )
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
->willReturn( $customDifferenceEngine );
/** @var ContentHandler $customContentHandler */
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
$customSlotDiffRenderer = $this->getMockBuilder( SlotDiffRenderer::class )
->disableOriginalConstructor()
->getMockForAbstractClass();
$customContentHandler2 = $this->getMockBuilder( ContentHandler::class )
->setConstructorArgs( [ 'bar', [] ] )
->onlyMethods( [ 'createDifferenceEngine', 'getSlotDiffRendererInternal' ] )
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
->getMockForAbstractClass();
$customContentHandler2->method( 'createDifferenceEngine' )
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
->willReturn( $customDifferenceEngine );
$customContentHandler2->method( 'getSlotDiffRendererInternal' )
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
->willReturn( $customSlotDiffRenderer );
/** @var ContentHandler $customContentHandler2 */
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
$customSlotDiffRenderer2 = $this->getMockBuilder( SlotDiffRenderer::class )
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->setTemporaryHook( 'GetSlotDiffRenderer',
static function ( $handler, &$slotDiffRenderer ) use ( $customSlotDiffRenderer2 ) {
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
$slotDiffRenderer = $customSlotDiffRenderer2;
} );
$slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext::getMain() );
$this->assertSame( $customSlotDiffRenderer2, $slotDiffRenderer );
$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' ];
}
/**
* @dataProvider providerGetPageViewLanguage
* @covers ContentHandler::getPageViewLanguage
*/
public function testGetPageViewLanguage( $namespace, $lang, $variant, $expected ) {
$contentHandler = $this->getMockBuilder( ContentHandler::class )
->disableOriginalConstructor()
->getMockForAbstractClass();
$title = Title::newFromText( "SimpleTitle", $namespace );
$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() );
}
2012-04-20 19:33:13 +00:00
}