MediaWiki  1.23.2
ContentHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
12 
13  protected function setUp() {
15  parent::setUp();
16 
17  $this->setMwGlobals( array(
18  'wgExtraNamespaces' => array(
19  12312 => 'Dummy',
20  12313 => 'Dummy_talk',
21  ),
22  // The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..)
23  // default to CONTENT_MODEL_WIKITEXT.
24  'wgNamespaceContentModels' => array(
25  12312 => 'testing',
26  ),
27  'wgContentHandlers' => array(
28  CONTENT_MODEL_WIKITEXT => 'WikitextContentHandler',
29  CONTENT_MODEL_JAVASCRIPT => 'JavaScriptContentHandler',
30  CONTENT_MODEL_CSS => 'CssContentHandler',
31  CONTENT_MODEL_TEXT => 'TextContentHandler',
32  'testing' => 'DummyContentHandlerForTesting',
33  ),
34  ) );
35 
36  // Reset namespace cache
38  $wgContLang->resetNamespaces();
39  }
40 
41  protected function tearDown() {
43 
44  // Reset namespace cache
46  $wgContLang->resetNamespaces();
47 
48  parent::tearDown();
49  }
50 
51  public static function dataGetDefaultModelFor() {
52  return array(
53  array( 'Help:Foo', CONTENT_MODEL_WIKITEXT ),
54  array( 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ),
55  array( 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ),
56  array( 'User:Foo', CONTENT_MODEL_WIKITEXT ),
57  array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT ),
58  array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ),
59  array( 'User:Foo/bar.css', CONTENT_MODEL_CSS ),
60  array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ),
61  array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ),
62  array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ),
63  array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ),
64  array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ),
65  array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ),
66  array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ),
67  array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ),
68  );
69  }
70 
75  public function testGetDefaultModelFor( $title, $expectedModelId ) {
77  $this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) );
78  }
79 
84  public function testGetForTitle( $title, $expectedContentModel ) {
86  $handler = ContentHandler::getForTitle( $title );
87  $this->assertEquals( $expectedContentModel, $handler->getModelID() );
88  }
89 
90  public static function dataGetLocalizedName() {
91  return array(
92  array( null, null ),
93  array( "xyzzy", null ),
94 
95  // XXX: depends on content language
96  array( CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ),
97  );
98  }
99 
104  public function testGetLocalizedName( $id, $expected ) {
106 
107  if ( $expected ) {
108  $this->assertNotNull( $name, "no name found for content model $id" );
109  $this->assertTrue( preg_match( $expected, $name ) > 0,
110  "content model name for #$id did not match pattern $expected"
111  );
112  } else {
113  $this->assertEquals( $id, $name, "localization of unknown model $id should have "
114  . "fallen back to use the model id directly."
115  );
116  }
117  }
118 
119  public static function dataGetPageLanguage() {
120  global $wgLanguageCode;
121 
122  return array(
123  array( "Main", $wgLanguageCode ),
124  array( "Dummy:Foo", $wgLanguageCode ),
125  array( "MediaWiki:common.js", 'en' ),
126  array( "User:Foo/common.js", 'en' ),
127  array( "MediaWiki:common.css", 'en' ),
128  array( "User:Foo/common.css", 'en' ),
129  array( "User:Foo", $wgLanguageCode ),
130 
131  array( CONTENT_MODEL_JAVASCRIPT, 'javascript' ),
132  );
133  }
134 
139  public function testGetPageLanguage( $title, $expected ) {
140  if ( is_string( $title ) ) {
142  }
143 
144  $expected = wfGetLangObj( $expected );
145 
146  $handler = ContentHandler::getForTitle( $title );
147  $lang = $handler->getPageLanguage( $title );
148 
149  $this->assertEquals( $expected->getCode(), $lang->getCode() );
150  }
151 
152  public static function dataGetContentText_Null() {
153  return array(
154  array( 'fail' ),
155  array( 'serialize' ),
156  array( 'ignore' ),
157  );
158  }
159 
164  public function testGetContentText_Null( $contentHandlerTextFallback ) {
165  $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
166 
167  $content = null;
168 
169  $text = ContentHandler::getContentText( $content );
170  $this->assertEquals( '', $text );
171  }
172 
173  public static function dataGetContentText_TextContent() {
174  return array(
175  array( 'fail' ),
176  array( 'serialize' ),
177  array( 'ignore' ),
178  );
179  }
180 
185  public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
186  $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
187 
188  $content = new WikitextContent( "hello world" );
189 
190  $text = ContentHandler::getContentText( $content );
191  $this->assertEquals( $content->getNativeData(), $text );
192  }
193 
200  $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
201 
202  $content = new DummyContentForTesting( "hello world" );
203 
204  ContentHandler::getContentText( $content );
205  }
206 
211  $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
212 
213  $content = new DummyContentForTesting( "hello world" );
214 
215  $text = ContentHandler::getContentText( $content );
216  $this->assertEquals( $content->serialize(), $text );
217  }
218 
223  $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
224 
225  $content = new DummyContentForTesting( "hello world" );
226 
227  $text = ContentHandler::getContentText( $content );
228  $this->assertNull( $text );
229  }
230 
231  /*
232  public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
233  */
234 
235  public static function dataMakeContent() {
236  return array(
237  array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
238  array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
239  array( serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ),
240 
241  array( 'hallo', 'Help:Test', null, CONTENT_FORMAT_WIKITEXT, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
242  array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
243  array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
244 
245  array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
246  array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
247  array( serialize( 'hallo' ), 'Dummy:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, serialize( 'hallo' ), false ),
248 
249  array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, null, true ),
250  array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, null, true ),
251  array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, null, true ),
252  );
253  }
254 
259  public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) {
261 
262  try {
263  $content = ContentHandler::makeContent( $data, $title, $modelId, $format );
264 
265  if ( $shouldFail ) {
266  $this->fail( "ContentHandler::makeContent should have failed!" );
267  }
268 
269  $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
270  $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
271  } catch ( MWException $ex ) {
272  if ( !$shouldFail ) {
273  $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
274  } else {
275  // dummy, so we don't get the "test did not perform any assertions" message.
276  $this->assertTrue( true );
277  }
278  }
279  }
280 
281  /*
282  public function testSupportsSections() {
283  $this->markTestIncomplete( "not yet implemented" );
284  }
285  */
286 
290  public function testRunLegacyHooks() {
291  Hooks::register( 'testRunLegacyHooks', __CLASS__ . '::dummyHookHandler' );
292 
293  $content = new WikitextContent( 'test text' );
294  $ok = ContentHandler::runLegacyHooks( 'testRunLegacyHooks', array( 'foo', &$content, 'bar' ), false );
295 
296  $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
297  $this->assertEquals( "TEST TEXT", $content->getNativeData() );
298  }
299 
300  public static function dummyHookHandler( $foo, &$text, $bar ) {
301  if ( $text === null || $text === false ) {
302  return false;
303  }
304 
305  $text = strtoupper( $text );
306 
307  return true;
308  }
309 }
310 
312 
313  public function __construct( $dataModel ) {
314  parent::__construct( $dataModel, array( "testing" ) );
315  }
316 
325  public function serializeContent( Content $content, $format = null ) {
326  return $content->serialize();
327  }
328 
337  public function unserializeContent( $blob, $format = null ) {
338  $d = unserialize( $blob );
339 
340  return new DummyContentForTesting( $d );
341  }
342 
347  public function makeEmptyContent() {
348  return new DummyContentForTesting( '' );
349  }
350 }
351 
353 
354  public function __construct( $data ) {
355  parent::__construct( "testing" );
356 
357  $this->data = $data;
358  }
359 
360  public function serialize( $format = null ) {
361  return serialize( $this->data );
362  }
363 
368  public function getTextForSearchIndex() {
369  return '';
370  }
371 
376  public function getWikitextForTransclusion() {
377  return false;
378  }
379 
386  public function getTextForSummary( $maxlength = 250 ) {
387  return '';
388  }
389 
397  public function getNativeData() {
398  return $this->data;
399  }
400 
406  public function getSize() {
407  return strlen( $this->data );
408  }
409 
424  public function copy() {
425  return $this;
426  }
427 
436  public function isCountable( $hasLinks = null ) {
437  return false;
438  }
439 
450  public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
451  return new ParserOutput( $this->getNativeData() );
452  }
453 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:31
ContentHandler
A content handler knows how do deal with a specific type of content on a wiki page.
Definition: ContentHandler.php:55
ContentHandlerTest\setUp
setUp()
Definition: ContentHandlerTest.php:13
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
data
and how to run hooks for an and one after Each event has a preferably in CamelCase For ArticleDelete hook A clump of code and data that should be run when an event happens This can be either a function and a chunk of data
Definition: hooks.txt:6
Content\serialize
serialize( $format=null)
Convenience method for serializing this Content object.
ParserOutput
Definition: ParserOutput.php:24
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
DummyContentForTesting\getParserOutput
getParserOutput(Title $title, $revId=null, ParserOptions $options=null, $generateHtml=true)
Definition: ContentHandlerTest.php:450
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:285
ContentHandlerTest\testGetPageLanguage
testGetPageLanguage( $title, $expected)
@dataProvider dataGetPageLanguage @covers ContentHandler::getPageLanguage
Definition: ContentHandlerTest.php:139
ContentHandlerTest\tearDown
tearDown()
Definition: ContentHandlerTest.php:41
ContentHandlerTest\dataGetDefaultModelFor
static dataGetDefaultModelFor()
Definition: ContentHandlerTest.php:51
fail
as a message key or array as accepted by ApiBase::dieUsageMsg after processing request parameters Return false to let the request fail
Definition: hooks.txt:375
ContentHandler\getForTitle
static getForTitle(Title $title)
Returns the appropriate ContentHandler singleton for the given title.
Definition: ContentHandler.php:259
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:56
DummyContentForTesting\__construct
__construct( $data)
Definition: ContentHandlerTest.php:354
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:283
DummyContentForTesting\getTextForSummary
getTextForSummary( $maxlength=250)
Returns a textual representation of the content suitable for use in edit summaries and log messages.
Definition: ContentHandlerTest.php:386
DummyContentForTesting\getTextForSearchIndex
getTextForSearchIndex()
Definition: ContentHandlerTest.php:368
ContentHandler\runLegacyHooks
static runLegacyHooks( $event, $args=array(), $warn=null)
Call a legacy hook that uses text instead of Content objects.
Definition: ContentHandler.php:1053
ContentHandlerTest\dataGetPageLanguage
static dataGetPageLanguage()
Definition: ContentHandlerTest.php:119
DummyContentForTesting\serialize
serialize( $format=null)
Definition: ContentHandlerTest.php:360
ContentHandler\getLocalizedName
static getLocalizedName( $name)
Returns the localized name for a given content model.
Definition: ContentHandler.php:360
ContentHandlerTest\dataGetContentText_Null
static dataGetContentText_Null()
Definition: ContentHandlerTest.php:152
DummyContentForTesting\isCountable
isCountable( $hasLinks=null)
Returns true if this content is countable as a "real" wiki page, provided that it's also in a countab...
Definition: ContentHandlerTest.php:436
ContentHandler\getDefaultModelFor
static getDefaultModelFor(Title $title)
Returns the name of the default content model to be used for the page with the given title.
Definition: ContentHandler.php:193
MWException
MediaWiki exception.
Definition: MWException.php:26
$blob
$blob
Definition: testCompression.php:61
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
ContentHandlerTest\testGetContentText_NonTextContent_ignore
testGetContentText_NonTextContent_ignore()
@covers ContentHandler::getContentText
Definition: ContentHandlerTest.php:222
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
ContentHandlerTest\testGetDefaultModelFor
testGetDefaultModelFor( $title, $expectedModelId)
@dataProvider dataGetDefaultModelFor @covers ContentHandler::getDefaultModelFor
Definition: ContentHandlerTest.php:75
wfGetLangObj
wfGetLangObj( $langcode=false)
Return a Language object from $langcode.
Definition: GlobalFunctions.php:1352
ContentHandlerTest\testRunLegacyHooks
testRunLegacyHooks()
@covers ContentHandler::runLegacyHooks
Definition: ContentHandlerTest.php:290
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:33
CONTENT_FORMAT_WIKITEXT
const CONTENT_FORMAT_WIKITEXT
Definition: Defines.php:297
ContentHandlerTest\testGetContentText_NonTextContent_fail
testGetContentText_NonTextContent_fail()
ContentHandler::getContentText should have thrown an exception for non-text Content object @expectedE...
Definition: ContentHandlerTest.php:199
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:144
ContentHandlerTest\testGetLocalizedName
testGetLocalizedName( $id, $expected)
@dataProvider dataGetLocalizedName @covers ContentHandler::getLocalizedName
Definition: ContentHandlerTest.php:104
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1530
$ok
$ok
Definition: UtfNormalTest.php:71
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:336
Hooks\register
static register( $name, $callback)
Attach an event handler to a given hook.
Definition: Hooks.php:55
DummyContentHandlerForTesting\makeEmptyContent
makeEmptyContent()
Creates an empty Content object of the type supported by this ContentHandler.
Definition: ContentHandlerTest.php:347
AbstractContent
Base implementation for content objects.
Definition: AbstractContent.php:34
DummyContentHandlerForTesting\serializeContent
serializeContent(Content $content, $format=null)
Definition: ContentHandlerTest.php:325
DummyContentForTesting\getWikitextForTransclusion
getWikitextForTransclusion()
Definition: ContentHandlerTest.php:376
DummyContentForTesting
Definition: ContentHandlerTest.php:352
Content
Base interface for content objects.
Definition: Content.php:34
Title
Represents a title within MediaWiki.
Definition: Title.php:35
ContentHandlerTest\dataMakeContent
static dataMakeContent()
Definition: ContentHandlerTest.php:235
ContentHandler\getContentText
static getContentText(Content $content=null)
Convenience function for getting flat text from a Content object.
Definition: ContentHandler.php:94
ContentHandlerTest\testGetContentText_Null
testGetContentText_Null( $contentHandlerTextFallback)
@dataProvider dataGetContentText_Null @covers ContentHandler::getContentText
Definition: ContentHandlerTest.php:164
ContentHandlerTest\dataGetLocalizedName
static dataGetLocalizedName()
Definition: ContentHandlerTest.php:90
MWNamespace\getCanonicalNamespaces
static getCanonicalNamespaces( $rebuild=false)
Returns array of all defined namespaces with their canonical (English) names.
Definition: Namespace.php:218
DummyContentHandlerForTesting\__construct
__construct( $dataModel)
Definition: ContentHandlerTest.php:313
ContentHandlerTest\dataGetContentText_TextContent
static dataGetContentText_TextContent()
Definition: ContentHandlerTest.php:173
ContentHandlerTest\testGetContentText_TextContent
testGetContentText_TextContent( $contentHandlerTextFallback)
@dataProvider dataGetContentText_TextContent @covers ContentHandler::getContentText
Definition: ContentHandlerTest.php:185
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:284
CONTENT_FORMAT_JAVASCRIPT
const CONTENT_FORMAT_JAVASCRIPT
Definition: Defines.php:299
ContentHandlerTest\testGetForTitle
testGetForTitle( $title, $expectedContentModel)
@dataProvider dataGetDefaultModelFor @covers ContentHandler::getForTitle
Definition: ContentHandlerTest.php:84
DummyContentForTesting\getSize
getSize()
returns the content's nominal size in bogo-bytes.
Definition: ContentHandlerTest.php:406
DummyContentForTesting\copy
copy()
Return a copy of this Content object.
Definition: ContentHandlerTest.php:424
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:286
ContentHandlerTest
@group ContentHandler @group Database
Definition: ContentHandlerTest.php:11
DummyContentHandlerForTesting\unserializeContent
unserializeContent( $blob, $format=null)
Definition: ContentHandlerTest.php:337
ContentHandlerTest\testGetContentText_NonTextContent_serialize
testGetContentText_NonTextContent_serialize()
@covers ContentHandler::getContentText
Definition: ContentHandlerTest.php:210
DummyContentHandlerForTesting
Definition: ContentHandlerTest.php:311
DummyContentForTesting\getNativeData
getNativeData()
Returns native represenation of the data.
Definition: ContentHandlerTest.php:397
ContentHandlerTest\dummyHookHandler
static dummyHookHandler( $foo, &$text, $bar)
Definition: ContentHandlerTest.php:300
ContentHandlerTest\testMakeContent
testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail)
@dataProvider dataMakeContent @covers ContentHandler::makeContent
Definition: ContentHandlerTest.php:259