<?php
use MediaWiki\MediaWikiServices;
+use Wikimedia\TestingAccessWrapper;
/**
* @group ContentHandler
$handler = ContentHandler::getForTitle( $title );
$lang = $handler->getPageLanguage( $title );
+ $this->assertInstanceOf( Language::class, $lang );
$this->assertEquals( $expected->getCode(), $lang->getCode() );
}
$content = new WikitextContent( "hello world" );
$text = ContentHandler::getContentText( $content );
- $this->assertEquals( $content->getNativeData(), $text );
+ $this->assertEquals( $content->getText(), $text );
}
/**
public static function dataMakeContent() {
return [
- [ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ],
- [ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ],
- [ serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ],
+ [ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, false ],
+ [ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, false ],
+ [ serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", false ],
[
'hallo',
null,
CONTENT_FORMAT_WIKITEXT,
CONTENT_MODEL_WIKITEXT,
- 'hallo',
false
],
[
null,
CONTENT_FORMAT_JAVASCRIPT,
CONTENT_MODEL_JAVASCRIPT,
- 'hallo',
false
],
- [ serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ],
+ [ serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", false ],
- [ 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ],
+ [ 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, false ],
[
'hallo',
'MediaWiki:Test.js',
CONTENT_MODEL_CSS,
null,
CONTENT_MODEL_CSS,
- 'hallo',
false
],
[
CONTENT_MODEL_CSS,
null,
CONTENT_MODEL_CSS,
- serialize( 'hallo' ),
false
],
- [ 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, null, true ],
- [ 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, null, true ],
- [ 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, null, true ],
+ [ '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 ],
];
}
* @covers ContentHandler::makeContent
*/
public function testMakeContent( $data, $title, $modelId, $format,
- $expectedModelId, $expectedNativeData, $shouldFail
+ $expectedModelId, $shouldFail
) {
$title = Title::newFromText( $title );
MediaWikiServices::getInstance()->getLinkCache()->addBadLinkObj( $title );
}
$this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
- $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
+ $this->assertEquals( $data, $content->serialize(), 'bad serialized data' );
} catch ( MWException $ex ) {
if ( !$shouldFail ) {
$this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
} );
$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 );
+ $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->getMockBuilder( DifferenceEngine::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+ // hack to track object identity across cloning
+ $customDifferenceEngine->objectId = 12345;
+ $customContentHandler = $this->getMockBuilder( ContentHandler::class )
+ ->setConstructorArgs( [ 'foo', [] ] )
+ ->setMethods( [ 'createDifferenceEngine' ] )
+ ->getMockForAbstractClass();
+ $customContentHandler->expects( $this->any() )
+ ->method( 'createDifferenceEngine' )
+ ->willReturn( $customDifferenceEngine );
+ /** @var $customContentHandler ContentHandler */
+ $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->getMockBuilder( DifferenceEngine::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $customSlotDiffRenderer = $this->getMockBuilder( SlotDiffRenderer::class )
+ ->disableOriginalConstructor()
+ ->getMockForAbstractClass();
+ $customContentHandler2 = $this->getMockBuilder( ContentHandler::class )
+ ->setConstructorArgs( [ 'bar', [] ] )
+ ->setMethods( [ 'createDifferenceEngine', 'getSlotDiffRendererInternal' ] )
+ ->getMockForAbstractClass();
+ $customContentHandler2->expects( $this->any() )
+ ->method( 'createDifferenceEngine' )
+ ->willReturn( $customDifferenceEngine );
+ $customContentHandler2->expects( $this->any() )
+ ->method( 'getSlotDiffRendererInternal' )
+ ->willReturn( $customSlotDiffRenderer );
+ /** @var $customContentHandler2 ContentHandler */
+ $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->getMockBuilder( DifferenceEngine::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $customContentHandler = $this->getMockBuilder( ContentHandler::class )
+ ->setConstructorArgs( [ 'foo', [] ] )
+ ->setMethods( [ 'createDifferenceEngine' ] )
+ ->getMockForAbstractClass();
+ $customContentHandler->expects( $this->any() )
+ ->method( 'createDifferenceEngine' )
+ ->willReturn( $customDifferenceEngine );
+ /** @var $customContentHandler ContentHandler */
+
+ $customSlotDiffRenderer = $this->getMockBuilder( SlotDiffRenderer::class )
+ ->disableOriginalConstructor()
+ ->getMockForAbstractClass();
+ $customContentHandler2 = $this->getMockBuilder( ContentHandler::class )
+ ->setConstructorArgs( [ 'bar', [] ] )
+ ->setMethods( [ 'createDifferenceEngine', 'getSlotDiffRendererInternal' ] )
+ ->getMockForAbstractClass();
+ $customContentHandler2->expects( $this->any() )
+ ->method( 'createDifferenceEngine' )
+ ->willReturn( $customDifferenceEngine );
+ $customContentHandler2->expects( $this->any() )
+ ->method( 'getSlotDiffRendererInternal' )
+ ->willReturn( $customSlotDiffRenderer );
+ /** @var $customContentHandler2 ContentHandler */
+
+ $customSlotDiffRenderer2 = $this->getMockBuilder( SlotDiffRenderer::class )
+ ->disableOriginalConstructor()
+ ->getMockForAbstractClass();
+ $this->setTemporaryHook( 'GetSlotDiffRenderer',
+ function ( $handler, &$slotDiffRenderer ) use ( $customSlotDiffRenderer2 ) {
+ $slotDiffRenderer = $customSlotDiffRenderer2;
+ } );
+
+ $slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext::getMain() );
+ $this->assertSame( $customSlotDiffRenderer2, $slotDiffRenderer );
+ $slotDiffRenderer = $customContentHandler2->getSlotDiffRenderer( RequestContext::getMain() );
+ $this->assertSame( $customSlotDiffRenderer2, $slotDiffRenderer );
+ }
+
}