MediaWiki  1.32.5
WikitextContentHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
6 
14  private $handler;
15 
16  protected function setUp() {
17  parent::setUp();
18 
20  }
21 
25  public function testSerializeContent() {
26  $content = new WikitextContent( 'hello world' );
27 
28  $this->assertEquals( 'hello world', $this->handler->serializeContent( $content ) );
29  $this->assertEquals(
30  'hello world',
31  $this->handler->serializeContent( $content, CONTENT_FORMAT_WIKITEXT )
32  );
33 
34  try {
35  $this->handler->serializeContent( $content, 'dummy/foo' );
36  $this->fail( "serializeContent() should have failed on unknown format" );
37  } catch ( MWException $e ) {
38  // ok, as expected
39  }
40  }
41 
45  public function testUnserializeContent() {
46  $content = $this->handler->unserializeContent( 'hello world' );
47  $this->assertEquals( 'hello world', $content->getNativeData() );
48 
49  $content = $this->handler->unserializeContent( 'hello world', CONTENT_FORMAT_WIKITEXT );
50  $this->assertEquals( 'hello world', $content->getNativeData() );
51 
52  try {
53  $this->handler->unserializeContent( 'hello world', 'dummy/foo' );
54  $this->fail( "unserializeContent() should have failed on unknown format" );
55  } catch ( MWException $e ) {
56  // ok, as expected
57  }
58  }
59 
63  public function testMakeEmptyContent() {
64  $content = $this->handler->makeEmptyContent();
65 
66  $this->assertTrue( $content->isEmpty() );
67  $this->assertEquals( '', $content->getNativeData() );
68  }
69 
70  public static function dataIsSupportedFormat() {
71  return [
72  [ null, true ],
74  [ 99887766, false ],
75  ];
76  }
77 
84  public function testMakeRedirectContent( $title, $expected ) {
85  MediaWikiServices::getInstance()->getContentLanguage()->resetNamespaces();
86 
87  MediaWikiServices::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
88 
89  if ( is_string( $title ) ) {
91  }
92  $content = $this->handler->makeRedirectContent( $title );
93  $this->assertEquals( $expected, $content->serialize() );
94  }
95 
96  public static function provideMakeRedirectContent() {
97  return [
98  [ 'Hello', '#REDIRECT [[Hello]]' ],
99  [ 'Template:Hello', '#REDIRECT [[Template:Hello]]' ],
100  [ 'Hello#section', '#REDIRECT [[Hello#section]]' ],
101  [ 'user:john_doe#section', '#REDIRECT [[User:John doe#section]]' ],
102  [ 'MEDIAWIKI:FOOBAR', '#REDIRECT [[MediaWiki:FOOBAR]]' ],
103  [ 'Category:Foo', '#REDIRECT [[:Category:Foo]]' ],
104  [ Title::makeTitle( NS_MAIN, 'en:Foo' ), '#REDIRECT [[en:Foo]]' ],
105  [ Title::makeTitle( NS_MAIN, 'Foo', '', 'en' ), '#REDIRECT [[:en:Foo]]' ],
106  [
107  Title::makeTitle( NS_MAIN, 'Bar', 'fragment', 'google' ),
108  '#REDIRECT [[google:Bar#fragment]]'
109  ],
110  ];
111  }
112 
117  public function testIsSupportedFormat( $format, $supported ) {
118  $this->assertEquals( $supported, $this->handler->isSupportedFormat( $format ) );
119  }
120 
124  public function testSupportsDirectEditing() {
125  $handler = new WikiTextContentHandler();
126  $this->assertTrue( $handler->supportsDirectEditing(), 'direct editing is supported' );
127  }
128 
129  public static function dataMerge3() {
130  return [
131  [
132  "first paragraph
133 
134  second paragraph\n",
135 
136  "FIRST paragraph
137 
138  second paragraph\n",
139 
140  "first paragraph
141 
142  SECOND paragraph\n",
143 
144  "FIRST paragraph
145 
146  SECOND paragraph\n",
147  ],
148 
149  [ "first paragraph
150  second paragraph\n",
151 
152  "Bla bla\n",
153 
154  "Blubberdibla\n",
155 
156  false,
157  ],
158  ];
159  }
160 
165  public function testMerge3( $old, $mine, $yours, $expected ) {
166  $this->markTestSkippedIfNoDiff3();
167 
168  // test merge
169  $oldContent = new WikitextContent( $old );
170  $myContent = new WikitextContent( $mine );
171  $yourContent = new WikitextContent( $yours );
172 
173  $merged = $this->handler->merge3( $oldContent, $myContent, $yourContent );
174 
175  $this->assertEquals( $expected, $merged ? $merged->getNativeData() : $merged );
176  }
177 
178  public static function dataGetAutosummary() {
179  return [
180  [
181  'Hello there, world!',
182  '#REDIRECT [[Foo]]',
183  0,
184  '/^Redirected page .*Foo/'
185  ],
186 
187  [
188  null,
189  'Hello world!',
190  EDIT_NEW,
191  '/^Created page .*Hello/'
192  ],
193 
194  [
195  null,
196  '',
197  EDIT_NEW,
198  '/^Created blank page$/'
199  ],
200 
201  [
202  'Hello there, world!',
203  '',
204  0,
205  '/^Blanked/'
206  ],
207 
208  [
209  'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
210  eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
211  voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
212  clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
213  'Hello world!',
214  0,
215  '/^Replaced .*Hello/'
216  ],
217 
218  [
219  'foo',
220  'bar',
221  0,
222  '/^$/'
223  ],
224  ];
225  }
226 
231  public function testGetAutosummary( $old, $new, $flags, $expected ) {
232  $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
233  $newContent = is_null( $new ) ? null : new WikitextContent( $new );
234 
235  $summary = $this->handler->getAutosummary( $oldContent, $newContent, $flags );
236 
237  $this->assertTrue(
238  (bool)preg_match( $expected, $summary ),
239  "Autosummary didn't match expected pattern $expected: $summary"
240  );
241  }
242 
243  public static function dataGetChangeTag() {
244  return [
245  [
246  null,
247  '#REDIRECT [[Foo]]',
248  0,
249  'mw-new-redirect'
250  ],
251 
252  [
253  'Lorem ipsum dolor',
254  '#REDIRECT [[Foo]]',
255  0,
256  'mw-new-redirect'
257  ],
258 
259  [
260  '#REDIRECT [[Foo]]',
261  'Lorem ipsum dolor',
262  0,
263  'mw-removed-redirect'
264  ],
265 
266  [
267  '#REDIRECT [[Foo]]',
268  '#REDIRECT [[Bar]]',
269  0,
270  'mw-changed-redirect-target'
271  ],
272 
273  [
274  null,
275  'Lorem ipsum dolor',
276  EDIT_NEW,
277  null // mw-newpage is not defined as a tag
278  ],
279 
280  [
281  null,
282  '',
283  EDIT_NEW,
284  null // mw-newblank is not defined as a tag
285  ],
286 
287  [
288  'Lorem ipsum dolor',
289  '',
290  0,
291  'mw-blank'
292  ],
293 
294  [
295  'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
296  eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
297  voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
298  clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
299  'Ipsum',
300  0,
301  'mw-replace'
302  ],
303 
304  [
305  'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
306  eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
307  voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
308  clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
309  'Duis purus odio, rhoncus et finibus dapibus, facilisis ac urna. Pellentesque
310  arcu, tristique nec tempus nec, suscipit vel arcu. Sed non dolor nec ligula
311  congue tempor. Quisque pellentesque finibus orci a molestie. Nam maximus, purus
312  euismod finibus mollis, dui ante malesuada felis, dignissim rutrum diam sapien.',
313  0,
314  null
315  ],
316  ];
317  }
318 
323  public function testGetChangeTag( $old, $new, $flags, $expected ) {
324  $this->setMwGlobals( 'wgSoftwareTags', [
325  'mw-new-redirect' => true,
326  'mw-removed-redirect' => true,
327  'mw-changed-redirect-target' => true,
328  'mw-newpage' => true,
329  'mw-newblank' => true,
330  'mw-blank' => true,
331  'mw-replace' => true,
332  ] );
333  $oldContent = is_null( $old ) ? null : new WikitextContent( $old );
334  $newContent = is_null( $new ) ? null : new WikitextContent( $new );
335 
336  $tag = $this->handler->getChangeTag( $oldContent, $newContent, $flags );
337 
338  $this->assertSame( $expected, $tag );
339  }
340 
344  public function testDataIndexFieldsFile() {
345  $mockEngine = $this->createMock( SearchEngine::class );
346  $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
347  $page = new WikiPage( $title );
348 
349  $fileHandler = $this->getMockBuilder( FileContentHandler::class )
350  ->disableOriginalConstructor()
351  ->setMethods( [ 'getDataForSearchIndex' ] )
352  ->getMock();
353 
354  $handler = $this->getMockBuilder( WikitextContentHandler::class )
355  ->disableOriginalConstructor()
356  ->setMethods( [ 'getFileHandler' ] )
357  ->getMock();
358 
359  $handler->method( 'getFileHandler' )->will( $this->returnValue( $fileHandler ) );
360  $fileHandler->expects( $this->once() )
361  ->method( 'getDataForSearchIndex' )
362  ->will( $this->returnValue( [ 'file_text' => 'This is file content' ] ) );
363 
364  $data = $handler->getDataForSearchIndex( $page, new ParserOutput(), $mockEngine );
365  $this->assertArrayHasKey( 'file_text', $data );
366  $this->assertEquals( 'This is file content', $data['file_text'] );
367  }
368 
369  public function testGetSecondaryDataUpdates() {
370  $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
371  $content = new WikitextContent( '' );
372 
374  $srp = $this->getMock( SlotRenderingProvider::class );
375 
377  $updates = $handler->getSecondaryDataUpdates( $title, $content, SlotRecord::MAIN, $srp );
378 
379  $this->assertEquals( [], $updates );
380  }
381 
382  public function testGetDeletionUpdates() {
383  $title = Title::newFromText( 'Somefile.jpg', NS_FILE );
384  $content = new WikitextContent( '' );
385 
386  $srp = $this->getMock( SlotRenderingProvider::class );
387 
389  $updates = $handler->getDeletionUpdates( $title, SlotRecord::MAIN );
390 
391  $this->assertEquals( [], $updates );
392  }
393 
394 }
ContentHandler\getSecondaryDataUpdates
getSecondaryDataUpdates(Title $title, Content $content, $role, SlotRenderingProvider $slotOutput)
Returns a list of DeferrableUpdate objects for recording information about the given Content in some ...
Definition: ContentHandler.php:1444
ContentHandler
A content handler knows how do deal with a specific type of content on a wiki page.
Definition: ContentHandler.php:53
ContentHandler\getForModelID
static getForModelID( $modelId)
Returns the ContentHandler singleton for the given model ID.
Definition: ContentHandler.php:297
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:280
ParserOutput
Definition: ParserOutput.php:25
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
WikitextContentHandlerTest\testUnserializeContent
testUnserializeContent()
WikitextContentHandler::unserializeContent.
Definition: WikitextContentHandlerTest.php:45
ContentHandler\supportsDirectEditing
supportsDirectEditing()
Return true if this content model supports direct editing, such as via EditPage.
Definition: ContentHandler.php:1276
WikitextContentHandlerTest\testGetDeletionUpdates
testGetDeletionUpdates()
Definition: WikitextContentHandlerTest.php:382
WikitextContentHandlerTest\testGetAutosummary
testGetAutosummary( $old, $new, $flags, $expected)
dataGetAutosummary WikitextContentHandler::getAutosummary
Definition: WikitextContentHandlerTest.php:231
WikitextContentHandlerTest\dataIsSupportedFormat
static dataIsSupportedFormat()
Definition: WikitextContentHandlerTest.php:70
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:44
NS_FILE
const NS_FILE
Definition: Defines.php:70
WikitextContentHandlerTest\testMakeEmptyContent
testMakeEmptyContent()
WikitextContentHandler::makeEmptyContent.
Definition: WikitextContentHandlerTest.php:63
WikitextContentHandlerTest\testDataIndexFieldsFile
testDataIndexFieldsFile()
WikitextContentHandler::getDataForSearchIndex.
Definition: WikitextContentHandlerTest.php:344
WikitextContentHandlerTest\testGetChangeTag
testGetChangeTag( $old, $new, $flags, $expected)
dataGetChangeTag WikitextContentHandler::getChangeTag
Definition: WikitextContentHandlerTest.php:323
ContentHandler\getDeletionUpdates
getDeletionUpdates(Title $title, $role)
Returns a list of DeferrableUpdate objects for removing information about content in some secondary d...
Definition: ContentHandler.php:1481
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:235
WikitextContentHandlerTest\testMakeRedirectContent
testMakeRedirectContent( $title, $expected)
provideMakeRedirectContent
Definition: WikitextContentHandlerTest.php:84
php
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
Revision\SlotRenderingProvider
A lazy provider of ParserOutput objects for a revision's individual slots.
Definition: SlotRenderingProvider.php:17
WikitextContentHandlerTest\testSupportsDirectEditing
testSupportsDirectEditing()
WikitextContentHandler::supportsDirectEditing.
Definition: WikitextContentHandlerTest.php:124
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
handler
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:813
WikitextContentHandlerTest\testSerializeContent
testSerializeContent()
WikitextContentHandler::serializeContent.
Definition: WikitextContentHandlerTest.php:25
MWException
MediaWiki exception.
Definition: MWException.php:26
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
WikitextContentHandlerTest\dataGetChangeTag
static dataGetChangeTag()
Definition: WikitextContentHandlerTest.php:243
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:706
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
ContentHandler\getDataForSearchIndex
getDataForSearchIndex(WikiPage $page, ParserOutput $output, SearchEngine $engine)
Return fields to be indexed by search engine as representation of this document.
Definition: ContentHandler.php:1355
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:545
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:35
CONTENT_FORMAT_WIKITEXT
const CONTENT_FORMAT_WIKITEXT
Definition: Defines.php:250
WikitextContentHandlerTest\dataMerge3
static dataMerge3()
Definition: WikitextContentHandlerTest.php:129
MediaWikiTestCase\markTestSkippedIfNoDiff3
markTestSkippedIfNoDiff3()
Check, if $wgDiff3 is set and ready to merge Will mark the calling test as skipped,...
Definition: MediaWikiTestCase.php:2232
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2221
WikitextContentHandler
Content handler for wiki text pages.
Definition: WikitextContentHandler.php:33
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:8
WikitextContentHandlerTest\dataGetAutosummary
static dataGetAutosummary()
Definition: WikitextContentHandlerTest.php:178
EDIT_NEW
const EDIT_NEW
Definition: Defines.php:152
WikitextContentHandlerTest\testGetSecondaryDataUpdates
testGetSecondaryDataUpdates()
Definition: WikitextContentHandlerTest.php:369
true
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:2044
$content
$content
Definition: pageupdater.txt:72
WikitextContentHandlerTest\setUp
setUp()
Definition: WikitextContentHandlerTest.php:16
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWikiServices
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
WikitextContentHandlerTest\$handler
ContentHandler $handler
Definition: WikitextContentHandlerTest.php:14
WikitextContentHandlerTest\testMerge3
testMerge3( $old, $mine, $yours, $expected)
dataMerge3 WikitextContentHandler::merge3
Definition: WikitextContentHandlerTest.php:165
WikitextContentHandlerTest\provideMakeRedirectContent
static provideMakeRedirectContent()
Definition: WikitextContentHandlerTest.php:96
WikitextContentHandlerTest\testIsSupportedFormat
testIsSupportedFormat( $format, $supported)
dataIsSupportedFormat WikitextContentHandler::isSupportedFormat
Definition: WikitextContentHandlerTest.php:117
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
WikitextContentHandlerTest
ContentHandler.
Definition: WikitextContentHandlerTest.php:10