MediaWiki REL1_31
WikitextContentHandlerTest.php
Go to the documentation of this file.
1<?php
2
10 private $handler;
11
12 protected function setUp() {
13 parent::setUp();
14
15 $this->handler = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT );
16 }
17
21 public function testSerializeContent() {
22 $content = new WikitextContent( 'hello world' );
23
24 $this->assertEquals( 'hello world', $this->handler->serializeContent( $content ) );
25 $this->assertEquals(
26 'hello world',
27 $this->handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT )
28 );
29
30 try {
31 $this->handler->serializeContent( $content, 'dummy/foo' );
32 $this->fail( "serializeContent() should have failed on unknown format" );
33 } catch ( MWException $e ) {
34 // ok, as expected
35 }
36 }
37
41 public function testUnserializeContent() {
42 $content = $this->handler->unserializeContent( 'hello world' );
43 $this->assertEquals( 'hello world', $content->getNativeData() );
44
45 $content = $this->handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
46 $this->assertEquals( 'hello world', $content->getNativeData() );
47
48 try {
49 $this->handler->unserializeContent( 'hello world', 'dummy/foo' );
50 $this->fail( "unserializeContent() should have failed on unknown format" );
51 } catch ( MWException $e ) {
52 // ok, as expected
53 }
54 }
55
59 public function testMakeEmptyContent() {
60 $content = $this->handler->makeEmptyContent();
61
62 $this->assertTrue( $content->isEmpty() );
63 $this->assertEquals( '', $content->getNativeData() );
64 }
65
66 public static function dataIsSupportedFormat() {
67 return [
68 [ null, true ],
70 [ 99887766, false ],
71 ];
72 }
73
80 public function testMakeRedirectContent( $title, $expected ) {
82 $wgContLang->resetNamespaces();
83
85
86 if ( is_string( $title ) ) {
87 $title = Title::newFromText( $title );
88 }
89 $content = $this->handler->makeRedirectContent( $title );
90 $this->assertEquals( $expected, $content->serialize() );
91 }
92
93 public static function provideMakeRedirectContent() {
94 return [
95 [ 'Hello', '#REDIRECT [[Hello]]' ],
96 [ 'Template:Hello', '#REDIRECT [[Template:Hello]]' ],
97 [ 'Hello#section', '#REDIRECT [[Hello#section]]' ],
98 [ 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ],
99 [ 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ],
100 [ 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ],
101 [ Title::makeTitle( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ],
102 [ Title::makeTitle( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ],
103 [
104 Title::makeTitle( NS_MAIN, 'Bar', 'fragment', 'google' ),
105 '#REDIRECT [[google:Bar#fragment]]'
106 ],
107 ];
108 }
109
114 public function testIsSupportedFormat( $format, $supported ) {
115 $this->assertEquals( $supported, $this->handler->isSupportedFormat( $format ) );
116 }
117
121 public function testSupportsDirectEditing() {
122 $handler = new WikiTextContentHandler();
123 $this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
124 }
125
126 public static function dataMerge3() {
127 return [
128 [
129 "first paragraph
130
131 second paragraph\n",
132
133 "FIRST paragraph
134
135 second paragraph\n",
136
137 "first paragraph
138
139 SECOND paragraph\n",
140
141 "FIRST paragraph
142
143 SECOND paragraph\n",
144 ],
145
146 [ "first paragraph
147 second paragraph\n",
148
149 "Bla bla\n",
150
151 "Blubberdibla\n",
152
153 false,
154 ],
155 ];
156 }
157
162 public function testMerge3( $old, $mine, $yours, $expected ) {
164
165 // test merge
166 $oldContent = new WikitextContent( $old );
167 $myContent = new WikitextContent( $mine );
168 $yourContent = new WikitextContent( $yours );
169
170 $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
171
172 $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
173 }
174
175 public static function dataGetAutosummary() {
176 return [
177 [
178 'Hello there, world!',
179 '#REDIRECT [[Foo]]',
180 0,
181 '/^Redirected page .*Foo/'
182 ],
183
184 [
185 null,
186 'Hello world!',
187 EDIT_NEW,
188 '/^Created page .*Hello/'
189 ],
190
191 [
192 null,
193 '',
194 EDIT_NEW,
195 '/^Created blank page$/'
196 ],
197
198 [
199 'Hello there, world!',
200 '',
201 0,
202 '/^Blanked/'
203 ],
204
205 [
206 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
207 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
208 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
209 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
210 'Hello world!',
211 0,
212 '/^Replaced .*Hello/'
213 ],
214
215 [
216 'foo',
217 'bar',
218 0,
219 '/^$/'
220 ],
221 ];
222 }
223
228 public function testGetAutosummary( $old, $new, $flags, $expected ) {
229 $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
230 $newContent = is_null( $new ) ? null : new WikitextContent( $new );
231
232 $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
233
234 $this->assertTrue(
235 (bool)preg_match( $expected, $summary ),
236 "Autosummary didn't match expected pattern $expected: $summary"
237 );
238 }
239
240 public static function dataGetChangeTag() {
241 return [
242 [
243 null,
244 '#REDIRECT [[Foo]]',
245 0,
246 'mw-new-redirect'
247 ],
248
249 [
250 'Lorem ipsum dolor',
251 '#REDIRECT [[Foo]]',
252 0,
253 'mw-new-redirect'
254 ],
255
256 [
257 '#REDIRECT [[Foo]]',
258 'Lorem ipsum dolor',
259 0,
260 'mw-removed-redirect'
261 ],
262
263 [
264 '#REDIRECT [[Foo]]',
265 '#REDIRECT [[Bar]]',
266 0,
267 'mw-changed-redirect-target'
268 ],
269
270 [
271 null,
272 'Lorem ipsum dolor',
273 EDIT_NEW,
274 null // mw-newpage is not defined as a tag
275 ],
276
277 [
278 null,
279 '',
280 EDIT_NEW,
281 null // mw-newblank is not defined as a tag
282 ],
283
284 [
285 'Lorem ipsum dolor',
286 '',
287 0,
288 'mw-blank'
289 ],
290
291 [
292 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
293 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
294 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
295 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
296 'Ipsum',
297 0,
298 'mw-replace'
299 ],
300
301 [
302 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
303 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
304 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
305 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
306 'Duis purus odio, rhoncus et finibus dapibus, facilisis ac urna. Pellentesque
307 arcu, tristique nec tempus nec, suscipit vel arcu. Sed non dolor nec ligula
308 congue tempor. Quisque pellentesque finibus orci a molestie. Nam maximus, purus
309 euismod finibus mollis, dui ante malesuada felis, dignissim rutrum diam sapien.',
310 0,
311 null
312 ],
313 ];
314 }
315
320 public function testGetChangeTag( $old, $new, $flags, $expected ) {
321 $this->setMwGlobals( 'wgSoftwareTags', [
322 'mw-new-redirect' => true,
323 'mw-removed-redirect' => true,
324 'mw-changed-redirect-target' => true,
325 'mw-newpage' => true,
326 'mw-newblank' => true,
327 'mw-blank' => true,
328 'mw-replace' => true,
329 ] );
330 $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
331 $newContent = is_null( $new ) ? null : new WikitextContent( $new );
332
333 $tag = $this->handler->getChangeTag( $oldContent, $newContent, $flags );
334
335 $this->assertSame( $expected, $tag );
336 }
337
341 public function testDataIndexFieldsFile() {
342 $mockEngine = $this->createMock( SearchEngine::class );
343 $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
344 $page = new WikiPage( $title );
345
346 $fileHandler = $this->getMockBuilder( FileContentHandler::class )
347 ->disableOriginalConstructor()
348 ->setMethods( [ 'getDataForSearchIndex' ] )
349 ->getMock();
350
351 $handler = $this->getMockBuilder( WikitextContentHandler::class )
352 ->disableOriginalConstructor()
353 ->setMethods( [ 'getFileHandler' ] )
354 ->getMock();
355
356 $handler->method( 'getFileHandler' )->will( $this->returnValue( $fileHandler ) );
357 $fileHandler->expects( $this->once() )
358 ->method( 'getDataForSearchIndex' )
359 ->will( $this->returnValue( [ 'file_text' => 'This is file content' ] ) );
360
361 $data = $handler->getDataForSearchIndex( $page, new ParserOutput(), $mockEngine );
362 $this->assertArrayHasKey( 'file_text', $data );
363 $this->assertEquals( 'This is file content', $data['file_text'] );
364 }
365}
A content handler knows how do deal with a specific type of content on a wiki page.
supportsDirectEditing()
Return true if this content model supports direct editing, such as via EditPage.
getDataForSearchIndex(WikiPage $page, ParserOutput $output, SearchEngine $engine)
Return fields to be indexed by search engine as representation of this document.
MediaWiki exception.
static clearCache()
Clear the self::$mObjects variable For use in parser tests.
Base class that store and restore the Language objects.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
markTestSkippedIfNoDiff3()
Check, if $wgDiff3 is set and ready to merge Will mark the calling test as skipped,...
Class representing a MediaWiki article and history.
Definition WikiPage.php:37
testMerge3( $old, $mine, $yours, $expected)
dataMerge3 WikitextContentHandler::merge3
testMakeRedirectContent( $title, $expected)
provideMakeRedirectContent
testGetChangeTag( $old, $new, $flags, $expected)
dataGetChangeTag WikitextContentHandler::getChangeTag
testSupportsDirectEditing()
WikitextContentHandler::supportsDirectEditing.
testGetAutosummary( $old, $new, $flags, $expected)
dataGetAutosummary WikitextContentHandler::getAutosummary
testMakeEmptyContent()
WikitextContentHandler::makeEmptyContent.
testDataIndexFieldsFile()
WikitextContentHandler::getDataForSearchIndex.
testSerializeContent()
WikitextContentHandler::serializeContent.
testIsSupportedFormat( $format, $supported)
dataIsSupportedFormat WikitextContentHandler::isSupportedFormat
testUnserializeContent()
WikitextContentHandler::unserializeContent.
Content object for wiki text pages.
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:57
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
const NS_FILE
Definition Defines.php:80
const NS_MAIN
Definition Defines.php:74
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:245
const CONTENT_FORMAT_WIKITEXT
Definition Defines.php:260
const EDIT_NEW
Definition Defines.php:162
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 set to a MediaTransformOutput the error message to be returned in an array you should do so by altering $wgNamespaceProtection and $wgNamespaceContentModels outside the handler
Definition hooks.txt:930
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:964
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:2006
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
returning false will NOT prevent logging $e
Definition hooks.txt:2176
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:37