2012-04-18 13:02:21 +00:00
|
|
|
<?php
|
2016-05-12 22:44:33 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2012-04-18 13:02:21 +00:00
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
/**
|
|
|
|
|
* @group ContentHandler
|
2016-05-16 20:24:10 +00:00
|
|
|
* @group Database
|
2012-05-13 22:02:29 +00:00
|
|
|
*/
|
2012-04-18 13:02:21 +00:00
|
|
|
class ContentHandlerTest extends MediaWikiTestCase {
|
|
|
|
|
|
2013-11-24 00:46:49 +00:00
|
|
|
protected function setUp() {
|
2013-03-22 16:44:34 +00:00
|
|
|
parent::setUp();
|
2012-09-12 11:43:52 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( [
|
|
|
|
|
'wgExtraNamespaces' => [
|
2012-10-20 01:51:15 +00:00
|
|
|
12312 => 'Dummy',
|
|
|
|
|
12313 => 'Dummy_talk',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-10-20 01:51:15 +00:00
|
|
|
// The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..)
|
|
|
|
|
// default to CONTENT_MODEL_WIKITEXT.
|
2016-02-17 09:09:32 +00:00
|
|
|
'wgNamespaceContentModels' => [
|
2012-10-20 01:51:15 +00:00
|
|
|
12312 => 'testing',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'wgContentHandlers' => [
|
2018-01-13 00:02:09 +00:00
|
|
|
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,
|
2017-06-26 16:35:31 +00:00
|
|
|
'testing-callbacks' => function ( $modelId ) {
|
2016-02-05 13:40:39 +00:00
|
|
|
return new DummyContentHandlerForTesting( $modelId );
|
|
|
|
|
}
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
] );
|
2012-10-20 01:51:15 +00:00
|
|
|
|
2018-08-01 12:40:47 +00:00
|
|
|
// Reset LinkCache
|
2016-05-12 22:44:33 +00:00
|
|
|
MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' );
|
2012-04-26 10:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
2013-11-24 00:46:49 +00:00
|
|
|
protected function tearDown() {
|
2018-08-01 12:40:47 +00:00
|
|
|
// Reset LinkCache
|
2016-05-12 22:44:33 +00:00
|
|
|
MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' );
|
2012-04-26 10:11:01 +00:00
|
|
|
|
2012-10-20 01:51:15 +00:00
|
|
|
parent::tearDown();
|
2012-04-26 10:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
2016-05-16 20:24:10 +00:00
|
|
|
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]]' );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-20 01:51:15 +00:00
|
|
|
public static function dataGetDefaultModelFor() {
|
2016-02-17 09:09:32 +00:00
|
|
|
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
|
2013-10-18 10:53:29 +00:00
|
|
|
* @covers ContentHandler::getDefaultModelFor
|
2012-04-25 16:35:36 +00:00
|
|
|
*/
|
2012-05-13 22:02:29 +00:00
|
|
|
public function testGetDefaultModelFor( $title, $expectedModelId ) {
|
2012-04-25 16:35:36 +00:00
|
|
|
$title = Title::newFromText( $title );
|
2012-05-13 22:02:29 +00:00
|
|
|
$this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) );
|
2012-04-25 16:35:36 +00:00
|
|
|
}
|
2012-10-20 01:51:15 +00:00
|
|
|
|
2012-04-25 16:35:36 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider dataGetDefaultModelFor
|
2013-10-18 10:53:29 +00:00
|
|
|
* @covers ContentHandler::getForTitle
|
2012-04-25 16:35:36 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetForTitle( $title, $expectedContentModel ) {
|
|
|
|
|
$title = Title::newFromText( $title );
|
2018-06-11 06:55:11 +00:00
|
|
|
MediaWikiServices::getInstance()->getLinkCache()->addBadLinkObj( $title );
|
2012-04-25 16:35:36 +00:00
|
|
|
$handler = ContentHandler::getForTitle( $title );
|
2012-05-13 22:02:29 +00:00
|
|
|
$this->assertEquals( $expectedContentModel, $handler->getModelID() );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-20 01:51:15 +00:00
|
|
|
public static function dataGetLocalizedName() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ null, null ],
|
|
|
|
|
[ "xyzzy", null ],
|
2012-05-13 22:02:29 +00:00
|
|
|
|
2012-10-20 01:51:15 +00:00
|
|
|
// XXX: depends on content language
|
2016-02-17 09:09:32 +00:00
|
|
|
[ CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ],
|
|
|
|
|
];
|
2012-05-13 22:02:29 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-19 12:45:25 +00:00
|
|
|
/**
|
2012-06-25 21:30:51 +00:00
|
|
|
* @dataProvider dataGetLocalizedName
|
2013-10-18 10:53:29 +00:00
|
|
|
* @covers ContentHandler::getLocalizedName
|
2012-06-19 12:45:25 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetLocalizedName( $id, $expected ) {
|
2012-06-25 21:30:51 +00:00
|
|
|
$name = ContentHandler::getLocalizedName( $id );
|
2012-06-19 12:45:25 +00:00
|
|
|
|
2012-06-25 21:30:51 +00:00
|
|
|
if ( $expected ) {
|
|
|
|
|
$this->assertNotNull( $name, "no name found for content model $id" );
|
2013-01-28 10:27:15 +00:00
|
|
|
$this->assertTrue( preg_match( $expected, $name ) > 0,
|
2012-10-20 01:51:15 +00:00
|
|
|
"content model name for #$id did not match pattern $expected"
|
|
|
|
|
);
|
2012-06-25 21:30:51 +00:00
|
|
|
} else {
|
2012-08-20 19:33:07 +00:00
|
|
|
$this->assertEquals( $id, $name, "localization of unknown model $id should have "
|
2012-10-20 01:51:15 +00:00
|
|
|
. "fallen back to use the model id directly."
|
|
|
|
|
);
|
2012-06-19 12:45:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-20 01:51:15 +00:00
|
|
|
public static function dataGetPageLanguage() {
|
2012-06-26 14:37:42 +00:00
|
|
|
global $wgLanguageCode;
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
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 ],
|
|
|
|
|
|
|
|
|
|
[ CONTENT_MODEL_JAVASCRIPT, 'javascript' ],
|
|
|
|
|
];
|
2012-06-26 14:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider dataGetPageLanguage
|
2013-10-18 10:53:29 +00:00
|
|
|
* @covers ContentHandler::getPageLanguage
|
2012-06-26 14:37:42 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetPageLanguage( $title, $expected ) {
|
|
|
|
|
if ( is_string( $title ) ) {
|
|
|
|
|
$title = Title::newFromText( $title );
|
2018-06-11 06:55:11 +00:00
|
|
|
MediaWikiServices::getInstance()->getLinkCache()->addBadLinkObj( $title );
|
2012-06-26 14:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$expected = wfGetLangObj( $expected );
|
|
|
|
|
|
|
|
|
|
$handler = ContentHandler::getForTitle( $title );
|
|
|
|
|
$lang = $handler->getPageLanguage( $title );
|
|
|
|
|
|
|
|
|
|
$this->assertEquals( $expected->getCode(), $lang->getCode() );
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-22 14:16:35 +00:00
|
|
|
public static function dataGetContentText_Null() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 'fail' ],
|
|
|
|
|
[ 'serialize' ],
|
|
|
|
|
[ 'ignore' ],
|
|
|
|
|
];
|
2013-03-22 14:16:35 +00:00
|
|
|
}
|
2012-04-25 16:35:36 +00:00
|
|
|
|
2013-03-22 14:16:35 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider dataGetContentText_Null
|
2013-10-18 10:53:29 +00:00
|
|
|
* @covers ContentHandler::getContentText
|
2013-03-22 14:16:35 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetContentText_Null( $contentHandlerTextFallback ) {
|
|
|
|
|
$this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
|
2012-04-25 16:35:36 +00:00
|
|
|
|
2013-03-22 14:16:35 +00:00
|
|
|
$content = null;
|
2012-04-25 16:35:36 +00:00
|
|
|
|
|
|
|
|
$text = ContentHandler::getContentText( $content );
|
|
|
|
|
$this->assertEquals( '', $text );
|
2013-03-22 14:16:35 +00:00
|
|
|
}
|
2012-04-25 16:35:36 +00:00
|
|
|
|
2013-03-22 14:16:35 +00:00
|
|
|
public static function dataGetContentText_TextContent() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 'fail' ],
|
|
|
|
|
[ 'serialize' ],
|
|
|
|
|
[ 'ignore' ],
|
|
|
|
|
];
|
2012-04-25 16:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-22 14:16:35 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider dataGetContentText_TextContent
|
2013-10-18 10:53:29 +00:00
|
|
|
* @covers ContentHandler::getContentText
|
2013-03-22 14:16:35 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
|
|
|
|
|
$this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
|
2012-04-25 16:35:36 +00:00
|
|
|
|
|
|
|
|
$content = new WikitextContent( "hello world" );
|
|
|
|
|
|
|
|
|
|
$text = ContentHandler::getContentText( $content );
|
|
|
|
|
$this->assertEquals( $content->getNativeData(), $text );
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-22 14:16:35 +00:00
|
|
|
/**
|
|
|
|
|
* ContentHandler::getContentText should have thrown an exception for non-text Content object
|
|
|
|
|
* @expectedException MWException
|
2013-10-18 10:53:29 +00:00
|
|
|
* @covers ContentHandler::getContentText
|
2013-03-22 14:16:35 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetContentText_NonTextContent_fail() {
|
|
|
|
|
$this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
|
2012-04-25 16:35:36 +00:00
|
|
|
|
|
|
|
|
$content = new DummyContentForTesting( "hello world" );
|
|
|
|
|
|
2013-03-22 14:16:35 +00:00
|
|
|
ContentHandler::getContentText( $content );
|
|
|
|
|
}
|
2012-04-25 16:35:36 +00:00
|
|
|
|
2013-10-18 10:53:29 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ContentHandler::getContentText
|
|
|
|
|
*/
|
2013-03-22 14:16:35 +00:00
|
|
|
public function testGetContentText_NonTextContent_serialize() {
|
|
|
|
|
$this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
|
2012-04-25 16:35:36 +00:00
|
|
|
|
2013-03-22 14:16:35 +00:00
|
|
|
$content = new DummyContentForTesting( "hello world" );
|
2012-04-25 16:35:36 +00:00
|
|
|
|
|
|
|
|
$text = ContentHandler::getContentText( $content );
|
|
|
|
|
$this->assertEquals( $content->serialize(), $text );
|
2013-03-22 14:16:35 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-18 10:53:29 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ContentHandler::getContentText
|
|
|
|
|
*/
|
2013-03-22 14:16:35 +00:00
|
|
|
public function testGetContentText_NonTextContent_ignore() {
|
|
|
|
|
$this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
|
|
|
|
|
|
|
|
|
|
$content = new DummyContentForTesting( "hello world" );
|
2012-04-25 16:35:36 +00:00
|
|
|
|
|
|
|
|
$text = ContentHandler::getContentText( $content );
|
|
|
|
|
$this->assertNull( $text );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-20 01:51:15 +00:00
|
|
|
public static function dataMakeContent() {
|
2016-02-17 09:09:32 +00:00
|
|
|
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 ],
|
2012-04-25 16:35:36 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-04-24 16:06:21 +00:00
|
|
|
'hallo',
|
|
|
|
|
'Help:Test',
|
|
|
|
|
null,
|
|
|
|
|
CONTENT_FORMAT_WIKITEXT,
|
|
|
|
|
CONTENT_MODEL_WIKITEXT,
|
|
|
|
|
'hallo',
|
|
|
|
|
false
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-04-24 16:06:21 +00:00
|
|
|
'hallo',
|
|
|
|
|
'MediaWiki:Test.js',
|
|
|
|
|
null,
|
|
|
|
|
CONTENT_FORMAT_JAVASCRIPT,
|
|
|
|
|
CONTENT_MODEL_JAVASCRIPT,
|
|
|
|
|
'hallo',
|
|
|
|
|
false
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ],
|
2012-04-25 16:35:36 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ],
|
|
|
|
|
[
|
2014-04-24 16:06:21 +00:00
|
|
|
'hallo',
|
|
|
|
|
'MediaWiki:Test.js',
|
|
|
|
|
CONTENT_MODEL_CSS,
|
|
|
|
|
null,
|
|
|
|
|
CONTENT_MODEL_CSS,
|
|
|
|
|
'hallo',
|
|
|
|
|
false
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-04-24 16:06:21 +00:00
|
|
|
serialize( 'hallo' ),
|
|
|
|
|
'Dummy:Test',
|
|
|
|
|
CONTENT_MODEL_CSS,
|
|
|
|
|
null,
|
|
|
|
|
CONTENT_MODEL_CSS,
|
|
|
|
|
serialize( 'hallo' ),
|
|
|
|
|
false
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-04-20 19:33:13 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '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 ],
|
|
|
|
|
];
|
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
|
2013-10-18 10:53:29 +00:00
|
|
|
* @covers ContentHandler::makeContent
|
2012-04-25 16:35:36 +00:00
|
|
|
*/
|
2014-04-24 16:06:21 +00:00
|
|
|
public function testMakeContent( $data, $title, $modelId, $format,
|
|
|
|
|
$expectedModelId, $expectedNativeData, $shouldFail
|
|
|
|
|
) {
|
2012-04-25 16:35:36 +00:00
|
|
|
$title = Title::newFromText( $title );
|
2018-06-11 06:55:11 +00:00
|
|
|
MediaWikiServices::getInstance()->getLinkCache()->addBadLinkObj( $title );
|
2012-04-25 16:35:36 +00:00
|
|
|
try {
|
2012-05-13 22:02:29 +00:00
|
|
|
$content = ContentHandler::makeContent( $data, $title, $modelId, $format );
|
2012-04-20 19:33:13 +00:00
|
|
|
|
2012-10-20 01:51:15 +00:00
|
|
|
if ( $shouldFail ) {
|
|
|
|
|
$this->fail( "ContentHandler::makeContent should have failed!" );
|
|
|
|
|
}
|
2012-04-20 19:33:13 +00:00
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
$this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
|
2012-04-25 16:35:36 +00:00
|
|
|
$this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
|
|
|
|
|
} catch ( MWException $ex ) {
|
2013-02-14 13:10:38 +00:00
|
|
|
if ( !$shouldFail ) {
|
|
|
|
|
$this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
|
2013-03-25 23:27:14 +00:00
|
|
|
} else {
|
2013-02-14 13:10:38 +00:00
|
|
|
// 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
|
|
|
|
2017-12-25 01:08:48 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ContentHandler::getAutosummary
|
|
|
|
|
*
|
2014-07-27 12:18:26 +00:00
|
|
|
* Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
|
|
|
|
|
* page.
|
|
|
|
|
*/
|
|
|
|
|
public function testGetAutosummary() {
|
2018-07-25 14:57:23 +00:00
|
|
|
$this->setContentLang( 'en' );
|
2014-09-27 09:53:02 +00:00
|
|
|
|
2014-07-27 12:18:26 +00:00
|
|
|
$content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT );
|
|
|
|
|
$title = Title::newFromText( 'Help:Test' );
|
|
|
|
|
// Create a new content object with no content
|
2017-11-09 11:13:16 +00:00
|
|
|
$newContent = ContentHandler::makeContent( '', $title, CONTENT_MODEL_WIKITEXT, null );
|
2014-07-27 12:18:26 +00:00
|
|
|
// first check, if we become a blank page created summary with the right bitmask
|
|
|
|
|
$autoSummary = $content->getAutosummary( null, $newContent, 97 );
|
2017-11-09 11:13:16 +00:00
|
|
|
$this->assertEquals( $autoSummary,
|
|
|
|
|
wfMessage( 'autosumm-newblank' )->inContentLanguage()->text() );
|
2014-07-27 12:18:26 +00:00
|
|
|
// now check, what we become with another bitmask
|
|
|
|
|
$autoSummary = $content->getAutosummary( null, $newContent, 92 );
|
|
|
|
|
$this->assertEquals( $autoSummary, '' );
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 11:13:16 +00:00
|
|
|
/**
|
|
|
|
|
* Test software tag that is added when content model of the page changes
|
|
|
|
|
* @covers ContentHandler::getChangeTag
|
|
|
|
|
*/
|
|
|
|
|
public function testGetChangeTag() {
|
|
|
|
|
$this->setMwGlobals( 'wgSoftwareTags', [ '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( $tag, 'mw-contentmodelchange' );
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-25 01:08:48 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ContentHandler::supportsCategories
|
|
|
|
|
*/
|
2016-02-15 15:19:58 +00:00
|
|
|
public function testSupportsCategories() {
|
|
|
|
|
$handler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT );
|
|
|
|
|
$this->assertTrue( $handler->supportsCategories(), 'content model supports categories' );
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-25 01:08:48 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ContentHandler::supportsDirectEditing
|
|
|
|
|
*/
|
2015-04-15 08:26:22 +00:00
|
|
|
public function testSupportsDirectEditing() {
|
|
|
|
|
$handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON );
|
|
|
|
|
$this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 17:28:20 +00:00
|
|
|
public static function dummyHookHandler( $foo, &$text, $bar ) {
|
|
|
|
|
if ( $text === null || $text === false ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$text = strtoupper( $text );
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2016-02-05 13:40:39 +00:00
|
|
|
|
|
|
|
|
public function provideGetModelForID() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2018-01-13 00:02:09 +00:00
|
|
|
[ 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 ],
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-05 13:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-12-25 01:08:48 +00:00
|
|
|
* @covers ContentHandler::getForModelID
|
2016-02-05 13:40:39 +00:00
|
|
|
* @dataProvider provideGetModelForID
|
|
|
|
|
*/
|
|
|
|
|
public function testGetModelForID( $modelId, $handlerClass ) {
|
|
|
|
|
$handler = ContentHandler::getForModelID( $modelId );
|
|
|
|
|
|
|
|
|
|
$this->assertInstanceOf( $handlerClass, $handler );
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-25 01:08:48 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ContentHandler::getFieldsForSearchIndex
|
|
|
|
|
*/
|
2016-08-09 17:22:09 +00:00
|
|
|
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 );
|
2017-01-26 19:15:03 +00:00
|
|
|
$this->assertArrayHasKey( 'content_model', $fields );
|
2016-08-09 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function newSearchEngine() {
|
2018-01-13 00:02:09 +00:00
|
|
|
$searchEngine = $this->getMockBuilder( SearchEngine::class )
|
2016-08-09 17:22:09 +00:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$searchEngine->expects( $this->any() )
|
|
|
|
|
->method( 'makeSearchFieldMapping' )
|
2017-06-26 16:35:31 +00:00
|
|
|
->will( $this->returnCallback( function ( $name, $type ) {
|
2016-08-09 17:22:09 +00:00
|
|
|
return new DummySearchIndexFieldDefinition( $name, $type );
|
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
|
|
return $searchEngine;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-16 20:24:10 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ContentHandler::getDataForSearchIndex
|
|
|
|
|
*/
|
|
|
|
|
public function testDataIndexFields() {
|
2018-01-13 00:02:09 +00:00
|
|
|
$mockEngine = $this->createMock( SearchEngine::class );
|
2016-05-16 20:24:10 +00:00
|
|
|
$title = Title::newFromText( 'Not_Main_Page', NS_MAIN );
|
|
|
|
|
$page = new WikiPage( $title );
|
|
|
|
|
|
|
|
|
|
$this->setTemporaryHook( 'SearchDataForIndex',
|
2017-02-25 21:53:36 +00:00
|
|
|
function (
|
|
|
|
|
&$fields,
|
|
|
|
|
ContentHandler $handler,
|
|
|
|
|
WikiPage $page,
|
|
|
|
|
ParserOutput $output,
|
|
|
|
|
SearchEngine $engine
|
|
|
|
|
) {
|
2016-05-16 20:24:10 +00:00
|
|
|
$fields['testDataField'] = 'test content';
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
$output = $page->getContent()->getParserOutput( $title );
|
|
|
|
|
$data = $page->getContentHandler()->getDataForSearchIndex( $page, $output, $mockEngine );
|
|
|
|
|
$this->assertArrayHasKey( 'text', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'text_bytes', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'language', $data );
|
|
|
|
|
$this->assertArrayHasKey( 'testDataField', $data );
|
|
|
|
|
$this->assertEquals( 'test content', $data['testDataField'] );
|
2017-01-26 19:15:03 +00:00
|
|
|
$this->assertEquals( 'wikitext', $data['content_model'] );
|
2016-05-16 20:24:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers ContentHandler::getParserOutputForIndexing
|
|
|
|
|
*/
|
|
|
|
|
public function testParserOutputForIndexing() {
|
|
|
|
|
$title = Title::newFromText( 'Smithee', NS_MAIN );
|
|
|
|
|
$page = new WikiPage( $title );
|
|
|
|
|
|
|
|
|
|
$out = $page->getContentHandler()->getParserOutputForIndexing( $page );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $out );
|
|
|
|
|
$this->assertContains( 'one who smiths', $out->getRawText() );
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-31 05:31:30 +00:00
|
|
|
/**
|
|
|
|
|
* @covers ContentHandler::getContentModels
|
|
|
|
|
*/
|
|
|
|
|
public function testGetContentModelsHook() {
|
|
|
|
|
$this->setTemporaryHook( 'GetContentModels', function ( &$models ) {
|
|
|
|
|
$models[] = 'Ferrari';
|
|
|
|
|
} );
|
|
|
|
|
$this->assertContains( 'Ferrari', ContentHandler::getContentModels() );
|
|
|
|
|
}
|
2012-04-20 19:33:13 +00:00
|
|
|
}
|