MediaWiki  master
ContentHandlerTest.php
Go to the documentation of this file.
1 <?php
3 
8 
9  protected function setUp() {
11  parent::setUp();
12 
13  $this->setMwGlobals( [
14  'wgExtraNamespaces' => [
15  12312 => 'Dummy',
16  12313 => 'Dummy_talk',
17  ],
18  // The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..)
19  // default to CONTENT_MODEL_WIKITEXT.
20  'wgNamespaceContentModels' => [
21  12312 => 'testing',
22  ],
23  'wgContentHandlers' => [
24  CONTENT_MODEL_WIKITEXT => 'WikitextContentHandler',
25  CONTENT_MODEL_JAVASCRIPT => 'JavaScriptContentHandler',
26  CONTENT_MODEL_JSON => 'JsonContentHandler',
27  CONTENT_MODEL_CSS => 'CssContentHandler',
28  CONTENT_MODEL_TEXT => 'TextContentHandler',
29  'testing' => 'DummyContentHandlerForTesting',
30  'testing-callbacks' => function( $modelId ) {
31  return new DummyContentHandlerForTesting( $modelId );
32  }
33  ],
34  ] );
35 
36  // Reset namespace cache
38  $wgContLang->resetNamespaces();
39  // And LinkCache
40  MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' );
41  }
42 
43  protected function tearDown() {
45 
46  // Reset namespace cache
48  $wgContLang->resetNamespaces();
49  // And LinkCache
50  MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' );
51 
52  parent::tearDown();
53  }
54 
55  public static function dataGetDefaultModelFor() {
56  return [
57  [ 'Help:Foo', CONTENT_MODEL_WIKITEXT ],
58  [ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ],
59  [ 'Help:Foo.css', CONTENT_MODEL_WIKITEXT ],
60  [ 'Help:Foo.json', CONTENT_MODEL_WIKITEXT ],
61  [ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ],
62  [ 'User:Foo', CONTENT_MODEL_WIKITEXT ],
63  [ 'User:Foo.js', CONTENT_MODEL_WIKITEXT ],
64  [ 'User:Foo.css', CONTENT_MODEL_WIKITEXT ],
65  [ 'User:Foo.json', CONTENT_MODEL_WIKITEXT ],
66  [ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
67  [ 'User:Foo/bar.css', CONTENT_MODEL_CSS ],
68  [ 'User:Foo/bar.json', CONTENT_MODEL_JSON ],
69  [ 'User:Foo/bar.json.nope', CONTENT_MODEL_WIKITEXT ],
70  [ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ],
71  [ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ],
72  [ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ],
73  [ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
74  [ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ],
75  [ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ],
76  [ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ],
77  [ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ],
78  [ 'MediaWiki:Foo.json', CONTENT_MODEL_JSON ],
79  [ 'MediaWiki:Foo.JSON', CONTENT_MODEL_WIKITEXT ],
80  ];
81  }
82 
87  public function testGetDefaultModelFor( $title, $expectedModelId ) {
89  $this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) );
90  }
91 
96  public function testGetForTitle( $title, $expectedContentModel ) {
98  LinkCache::singleton()->addBadLinkObj( $title );
99  $handler = ContentHandler::getForTitle( $title );
100  $this->assertEquals( $expectedContentModel, $handler->getModelID() );
101  }
102 
103  public static function dataGetLocalizedName() {
104  return [
105  [ null, null ],
106  [ "xyzzy", null ],
107 
108  // XXX: depends on content language
109  [ CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ],
110  ];
111  }
112 
117  public function testGetLocalizedName( $id, $expected ) {
118  $name = ContentHandler::getLocalizedName( $id );
119 
120  if ( $expected ) {
121  $this->assertNotNull( $name, "no name found for content model $id" );
122  $this->assertTrue( preg_match( $expected, $name ) > 0,
123  "content model name for #$id did not match pattern $expected"
124  );
125  } else {
126  $this->assertEquals( $id, $name, "localization of unknown model $id should have "
127  . "fallen back to use the model id directly."
128  );
129  }
130  }
131 
132  public static function dataGetPageLanguage() {
134 
135  return [
136  [ "Main", $wgLanguageCode ],
137  [ "Dummy:Foo", $wgLanguageCode ],
138  [ "MediaWiki:common.js", 'en' ],
139  [ "User:Foo/common.js", 'en' ],
140  [ "MediaWiki:common.css", 'en' ],
141  [ "User:Foo/common.css", 'en' ],
142  [ "User:Foo", $wgLanguageCode ],
143 
144  [ CONTENT_MODEL_JAVASCRIPT, 'javascript' ],
145  ];
146  }
147 
152  public function testGetPageLanguage( $title, $expected ) {
153  if ( is_string( $title ) ) {
155  LinkCache::singleton()->addBadLinkObj( $title );
156  }
157 
158  $expected = wfGetLangObj( $expected );
159 
160  $handler = ContentHandler::getForTitle( $title );
161  $lang = $handler->getPageLanguage( $title );
162 
163  $this->assertEquals( $expected->getCode(), $lang->getCode() );
164  }
165 
166  public static function dataGetContentText_Null() {
167  return [
168  [ 'fail' ],
169  [ 'serialize' ],
170  [ 'ignore' ],
171  ];
172  }
173 
178  public function testGetContentText_Null( $contentHandlerTextFallback ) {
179  $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
180 
181  $content = null;
182 
183  $text = ContentHandler::getContentText( $content );
184  $this->assertEquals( '', $text );
185  }
186 
187  public static function dataGetContentText_TextContent() {
188  return [
189  [ 'fail' ],
190  [ 'serialize' ],
191  [ 'ignore' ],
192  ];
193  }
194 
199  public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
200  $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
201 
202  $content = new WikitextContent( "hello world" );
203 
204  $text = ContentHandler::getContentText( $content );
205  $this->assertEquals( $content->getNativeData(), $text );
206  }
207 
214  $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
215 
216  $content = new DummyContentForTesting( "hello world" );
217 
218  ContentHandler::getContentText( $content );
219  }
220 
225  $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
226 
227  $content = new DummyContentForTesting( "hello world" );
228 
229  $text = ContentHandler::getContentText( $content );
230  $this->assertEquals( $content->serialize(), $text );
231  }
232 
237  $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
238 
239  $content = new DummyContentForTesting( "hello world" );
240 
241  $text = ContentHandler::getContentText( $content );
242  $this->assertNull( $text );
243  }
244 
245  /*
246  public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
247  */
248 
249  public static function dataMakeContent() {
250  return [
251  [ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ],
252  [ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ],
253  [ serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ],
254 
255  [
256  'hallo',
257  'Help:Test',
258  null,
261  'hallo',
262  false
263  ],
264  [
265  'hallo',
266  'MediaWiki:Test.js',
267  null,
270  'hallo',
271  false
272  ],
273  [ serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ],
274 
275  [ 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ],
276  [
277  'hallo',
278  'MediaWiki:Test.js',
280  null,
282  'hallo',
283  false
284  ],
285  [
286  serialize( 'hallo' ),
287  'Dummy:Test',
289  null,
291  serialize( 'hallo' ),
292  false
293  ],
294 
295  [ 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, null, true ],
296  [ 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, null, true ],
297  [ 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, null, true ],
298  ];
299  }
300 
305  public function testMakeContent( $data, $title, $modelId, $format,
306  $expectedModelId, $expectedNativeData, $shouldFail
307  ) {
309  LinkCache::singleton()->addBadLinkObj( $title );
310  try {
311  $content = ContentHandler::makeContent( $data, $title, $modelId, $format );
312 
313  if ( $shouldFail ) {
314  $this->fail( "ContentHandler::makeContent should have failed!" );
315  }
316 
317  $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
318  $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
319  } catch ( MWException $ex ) {
320  if ( !$shouldFail ) {
321  $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
322  } else {
323  // dummy, so we don't get the "test did not perform any assertions" message.
324  $this->assertTrue( true );
325  }
326  }
327  }
328 
329  /*
330  * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
331  * page.
332  */
333  public function testGetAutosummary() {
334  $this->setMwGlobals( 'wgContLang', Language::factory( 'en' ) );
335 
337  $title = Title::newFromText( 'Help:Test' );
338  // Create a new content object with no content
339  $newContent = ContentHandler::makeContent( '', $title, null, null, CONTENT_MODEL_WIKITEXT );
340  // first check, if we become a blank page created summary with the right bitmask
341  $autoSummary = $content->getAutosummary( null, $newContent, 97 );
342  $this->assertEquals( $autoSummary, 'Created blank page' );
343  // now check, what we become with another bitmask
344  $autoSummary = $content->getAutosummary( null, $newContent, 92 );
345  $this->assertEquals( $autoSummary, '' );
346  }
347 
348  /*
349  public function testSupportsSections() {
350  $this->markTestIncomplete( "not yet implemented" );
351  }
352  */
353 
354  public function testSupportsCategories() {
356  $this->assertTrue( $handler->supportsCategories(), 'content model supports categories' );
357  }
358 
359  public function testSupportsDirectEditing() {
361  $this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
362  }
363 
367  public function testRunLegacyHooks() {
368  Hooks::register( 'testRunLegacyHooks', __CLASS__ . '::dummyHookHandler' );
369 
370  $content = new WikitextContent( 'test text' );
371  $ok = ContentHandler::runLegacyHooks(
372  'testRunLegacyHooks',
373  [ 'foo', &$content, 'bar' ],
374  false
375  );
376 
377  $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
378  $this->assertEquals( "TEST TEXT", $content->getNativeData() );
379  }
380 
381  public static function dummyHookHandler( $foo, &$text, $bar ) {
382  if ( $text === null || $text === false ) {
383  return false;
384  }
385 
386  $text = strtoupper( $text );
387 
388  return true;
389  }
390 
391  public function provideGetModelForID() {
392  return [
393  [ CONTENT_MODEL_WIKITEXT, 'WikitextContentHandler' ],
394  [ CONTENT_MODEL_JAVASCRIPT, 'JavaScriptContentHandler' ],
395  [ CONTENT_MODEL_JSON, 'JsonContentHandler' ],
396  [ CONTENT_MODEL_CSS, 'CssContentHandler' ],
397  [ CONTENT_MODEL_TEXT, 'TextContentHandler' ],
398  [ 'testing', 'DummyContentHandlerForTesting' ],
399  [ 'testing-callbacks', 'DummyContentHandlerForTesting' ],
400  ];
401  }
402 
406  public function testGetModelForID( $modelId, $handlerClass ) {
407  $handler = ContentHandler::getForModelID( $modelId );
408 
409  $this->assertInstanceOf( $handlerClass, $handler );
410  }
411 
412 }
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:278
testGetContentText_NonTextContent_fail()
ContentHandler::getContentText should have thrown an exception for non-text Content object MWExceptio...
testRunLegacyHooks()
ContentHandler::runLegacyHooks.
static dummyHookHandler($foo, &$text, $bar)
testGetContentText_Null($contentHandlerTextFallback)
dataGetContentText_Null ContentHandler::getContentText
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testGetDefaultModelFor($title, $expectedModelId)
dataGetDefaultModelFor ContentHandler::getDefaultModelFor
if(!isset($args[0])) $lang
testGetModelForID($modelId, $handlerClass)
provideGetModelForID
testGetLocalizedName($id, $expected)
dataGetLocalizedName ContentHandler::getLocalizedName
testMakeContent($data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail)
dataMakeContent ContentHandler::makeContent
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
const CONTENT_MODEL_TEXT
Definition: Defines.php:281
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:256
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
testGetForTitle($title, $expectedContentModel)
dataGetDefaultModelFor ContentHandler::getForTitle
const CONTENT_MODEL_JSON
Definition: Defines.php:282
$wgLanguageCode
Site language code.
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 noclasses just before the function returns a value If you return true
Definition: hooks.txt:1816
static register($name, $callback)
Attach an event handler to a given hook.
Definition: Hooks.php:49
static singleton()
Get an instance of this class.
Definition: LinkCache.php:65
testGetPageLanguage($title, $expected)
dataGetPageLanguage ContentHandler::getPageLanguage
MediaWiki exception.
Definition: MWException.php:26
Allows to change the fields on the form that will be generated are created Can be used to omit specific feeds from being outputted You must not use this hook to add use OutputPage::addFeedLink() instead.&$feedLinks conditions will AND in the final query as a Content object as a Content object $title
Definition: hooks.txt:312
Content object for wiki text pages.
CONTENT_MODEL_JAVASCRIPT
Uploads have to be specially set up to be secure.
ContentHandler.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
testGetContentText_TextContent($contentHandlerTextFallback)
dataGetContentText_TextContent ContentHandler::getContentText
static dataGetContentText_TextContent()
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1020
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 local content language as $wgContLang
Definition: design.txt:56
const CONTENT_MODEL_CSS
Definition: Defines.php:280
const CONTENT_FORMAT_JAVASCRIPT
Definition: Defines.php:295
const CONTENT_FORMAT_WIKITEXT
Definition: Defines.php:293
serialize()
Definition: ApiMessage.php:94
testGetContentText_NonTextContent_serialize()
ContentHandler::getContentText.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves $handler
Definition: hooks.txt:776
static factory($code)
Get a cached or new language object for a given language code.
Definition: Language.php:179
setMwGlobals($pairs, $value=null)
static getCanonicalNamespaces($rebuild=false)
Returns array of all defined namespaces with their canonical (English) names.
testGetContentText_NonTextContent_ignore()
ContentHandler::getContentText.
wfGetLangObj($langcode=false)
Return a Language object from $langcode.
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310