MediaWiki  1.33.0
WikitextContentTest.php
Go to the documentation of this file.
1 <?php
2 
4 
12  public static $sections = "Intro
13 
14 == stuff ==
15 hello world
16 
17 == test ==
18 just a test
19 
20 == foo ==
21 more stuff
22 ";
23 
24  public function newContent( $text ) {
25  return new WikitextContent( $text );
26  }
27 
28  public static function dataGetParserOutput() {
29  return [
30  [
31  "WikitextContentTest_testGetParserOutput",
33  "hello ''world''\n",
34  "<div class=\"mw-parser-output\"><p>hello <i>world</i>\n</p>\n\n\n</div>"
35  ],
36  // TODO: more...?
37  ];
38  }
39 
40  public static function dataGetSecondaryDataUpdates() {
41  return [
42  [ "WikitextContentTest_testGetSecondaryDataUpdates_1",
43  CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
44  [
46  'mRecursive' => true,
47  'mLinks' => []
48  ]
49  ]
50  ],
51  [ "WikitextContentTest_testGetSecondaryDataUpdates_2",
52  CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
53  [
55  'mRecursive' => true,
56  'mLinks' => [
57  [ 'World_test_21344' => 0 ]
58  ]
59  ]
60  ]
61  ],
62  // TODO: more...?
63  ];
64  }
65 
71  public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
72  $ns = $this->getDefaultWikitextNS();
74 
75  $content = ContentHandler::makeContent( $text, $title, $model );
76 
77  $page = WikiPage::factory( $title );
78  $page->doEditContent( $content, '' );
79 
80  $updates = $content->getSecondaryDataUpdates( $title );
81 
82  // make updates accessible by class name
83  foreach ( $updates as $update ) {
84  $class = get_class( $update );
85  $updates[$class] = $update;
86  }
87 
88  foreach ( $expectedStuff as $class => $fieldValues ) {
89  $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
90 
91  $update = $updates[$class];
92 
93  foreach ( $fieldValues as $field => $value ) {
94  $v = $update->$field; # if the field doesn't exist, just crash and burn
95  $this->assertEquals(
96  $value,
97  $v,
98  "unexpected value for field $field in instance of $class"
99  );
100  }
101  }
102 
103  $page->doDeleteArticle( '' );
104  }
105 
106  public static function dataGetSection() {
107  return [
108  [ self::$sections,
109  "0",
110  "Intro"
111  ],
112  [ self::$sections,
113  "2",
114  "== test ==
115 just a test"
116  ],
117  [ self::$sections,
118  "8",
119  false
120  ],
121  ];
122  }
123 
128  public function testGetSection( $text, $sectionId, $expectedText ) {
129  $content = $this->newContent( $text );
130 
131  $sectionContent = $content->getSection( $sectionId );
132  if ( is_object( $sectionContent ) ) {
133  $sectionText = $sectionContent->getText();
134  } else {
135  $sectionText = $sectionContent;
136  }
137 
138  $this->assertEquals( $expectedText, $sectionText );
139  }
140 
141  public static function dataReplaceSection() {
142  return [
143  [ self::$sections,
144  "0",
145  "No more",
146  null,
147  trim( preg_replace( '/^Intro/sm', 'No more', self::$sections ) )
148  ],
149  [ self::$sections,
150  "",
151  "No more",
152  null,
153  "No more"
154  ],
155  [ self::$sections,
156  "2",
157  "== TEST ==\nmore fun",
158  null,
159  trim( preg_replace(
160  '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==",
161  self::$sections
162  ) )
163  ],
164  [ self::$sections,
165  "8",
166  "No more",
167  null,
168  self::$sections
169  ],
170  [ self::$sections,
171  "new",
172  "No more",
173  "New",
174  trim( self::$sections ) . "\n\n\n== New ==\n\nNo more"
175  ],
176  ];
177  }
178 
183  public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
184  $content = $this->newContent( $text );
186  $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
187 
188  $this->assertEquals( $expected, $c ? $c->getText() : null );
189  }
190 
194  public function testAddSectionHeader() {
195  $content = $this->newContent( 'hello world' );
196  $content = $content->addSectionHeader( 'test' );
197 
198  $this->assertEquals( "== test ==\n\nhello world", $content->getText() );
199  }
200 
201  public static function dataPreSaveTransform() {
202  return [
203  [ 'hello this is ~~~',
204  "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
205  ],
206  [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
207  'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
208  ],
209  [ // rtrim
210  " Foo \n ",
211  " Foo",
212  ],
213  ];
214  }
215 
216  public static function dataPreloadTransform() {
217  return [
218  [
219  'hello this is ~~~',
220  "hello this is ~~~",
221  ],
222  [
223  'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
224  'hello \'\'this\'\' is bar',
225  ],
226  ];
227  }
228 
229  public static function dataGetRedirectTarget() {
230  return [
231  [ '#REDIRECT [[Test]]',
232  'Test',
233  ],
234  [ '#REDIRECT Test',
235  null,
236  ],
237  [ '* #REDIRECT [[Test]]',
238  null,
239  ],
240  ];
241  }
242 
243  public static function dataGetTextForSummary() {
244  return [
245  [ "hello\nworld.",
246  16,
247  'hello world.',
248  ],
249  [ 'hello world.',
250  8,
251  'hello...',
252  ],
253  [ '[[hello world]].',
254  8,
255  'hel...',
256  ],
257  ];
258  }
259 
260  public static function dataIsCountable() {
261  return [
262  [ '',
263  null,
264  'any',
265  true
266  ],
267  [ 'Foo',
268  null,
269  'any',
270  true
271  ],
272  [ 'Foo',
273  null,
274  'link',
275  false
276  ],
277  [ 'Foo [[bar]]',
278  null,
279  'link',
280  true
281  ],
282  [ 'Foo',
283  true,
284  'link',
285  true
286  ],
287  [ 'Foo [[bar]]',
288  false,
289  'link',
290  false
291  ],
292  [ '#REDIRECT [[bar]]',
293  true,
294  'any',
295  false
296  ],
297  [ '#REDIRECT [[bar]]',
298  true,
299  'link',
300  false
301  ],
302  ];
303  }
304 
308  public function testMatchMagicWord() {
309  $mw = MediaWikiServices::getInstance()->getMagicWordFactory()->get( "staticredirect" );
310 
311  $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
312  $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
313 
314  $content = $this->newContent( "#REDIRECT [[FOO]]" );
315  $this->assertFalse(
316  $content->matchMagicWord( $mw ),
317  "should not have matched magic word"
318  );
319  }
320 
324  public function testUpdateRedirect() {
325  $target = Title::newFromText( "testUpdateRedirect_target" );
326 
327  // test with non-redirect page
328  $content = $this->newContent( "hello world." );
329  $newContent = $content->updateRedirect( $target );
330 
331  $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
332 
333  // test with actual redirect
334  $content = $this->newContent( "#REDIRECT [[Someplace]]" );
335  $newContent = $content->updateRedirect( $target );
336 
337  $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
338  $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
339 
340  $this->assertEquals(
341  $target->getFullText(),
342  $newContent->getRedirectTarget()->getFullText()
343  );
344  }
345 
349  public function testGetModel() {
350  $content = $this->newContent( "hello world." );
351 
352  $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
353  }
354 
358  public function testGetContentHandler() {
359  $content = $this->newContent( "hello world." );
360 
361  $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
362  }
363 
368  public function testRedirectParserOption() {
369  $title = Title::newFromText( 'testRedirectParserOption' );
370 
371  // Set up hook and its reporting variables
372  $wikitext = null;
373  $redirectTarget = null;
374  $this->mergeMwGlobalArrayValue( 'wgHooks', [
375  'InternalParseBeforeLinks' => [
376  function ( &$parser, &$text, &$stripState ) use ( &$wikitext, &$redirectTarget ) {
377  $wikitext = $text;
378  $redirectTarget = $parser->getOptions()->getRedirectTarget();
379  }
380  ]
381  ] );
382 
383  // Test with non-redirect page
384  $wikitext = false;
385  $redirectTarget = false;
386  $content = $this->newContent( 'hello world.' );
387  $options = ParserOptions::newCanonical( 'canonical' );
388  $options->setRedirectTarget( $title );
389  $content->getParserOutput( $title, null, $options );
390  $this->assertEquals( 'hello world.', $wikitext,
391  'Wikitext passed to hook was not as expected'
392  );
393  $this->assertEquals( null, $redirectTarget, 'Redirect seen in hook was not null' );
394  $this->assertEquals( $title, $options->getRedirectTarget(),
395  'ParserOptions\' redirectTarget was changed'
396  );
397 
398  // Test with a redirect page
399  $wikitext = false;
400  $redirectTarget = false;
401  $content = $this->newContent(
402  "#REDIRECT [[TestRedirectParserOption/redir]]\nhello redirect."
403  );
404  $options = ParserOptions::newCanonical( 'canonical' );
405  $content->getParserOutput( $title, null, $options );
406  $this->assertEquals(
407  'hello redirect.',
408  $wikitext,
409  'Wikitext passed to hook was not as expected'
410  );
411  $this->assertNotEquals(
412  null,
413  $redirectTarget,
414  'Redirect seen in hook was null' );
415  $this->assertEquals(
416  'TestRedirectParserOption/redir',
417  $redirectTarget->getFullText(),
418  'Redirect seen in hook was not the expected title'
419  );
420  $this->assertEquals(
421  null,
422  $options->getRedirectTarget(),
423  'ParserOptions\' redirectTarget was changed'
424  );
425  }
426 
427  public static function dataEquals() {
428  return [
429  [ new WikitextContent( "hallo" ), null, false ],
430  [ new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ],
431  [ new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
432  [ new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ],
433  [ new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ],
434  ];
435  }
436 
437  public static function dataGetDeletionUpdates() {
438  return [
439  [
440  CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
442  ],
443  [
444  CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
446  ],
447  // @todo more...?
448  ];
449  }
450 
455  public function testHadSignature() {
456  $titleObj = Title::newFromText( __CLASS__ );
457 
458  $content = new WikitextContent( '~~~~' );
459  $pstContent = $content->preSaveTransform(
460  $titleObj, $this->getTestUser()->getUser(), new ParserOptions()
461  );
462 
463  $this->assertTrue( $pstContent->getParserOutput( $titleObj )->getFlag( 'user-signature' ) );
464  }
465 }
WikitextContentTest\testGetSecondaryDataUpdates
testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff)
dataGetSecondaryDataUpdates Database WikitextContent::getSecondaryDataUpdates
Definition: WikitextContentTest.php:71
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:42
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:306
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
MediaWikiTestCase\getTestUser
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
Definition: MediaWikiTestCase.php:180
WikitextContentTest\dataGetSecondaryDataUpdates
static dataGetSecondaryDataUpdates()
Definition: WikitextContentTest.php:40
TextContentTest
ContentHandler Database ^— needed, because we do need the database to test link updates.
Definition: TextContentTest.php:10
link
usually copyright or history_copyright This message must be in HTML not wikitext if the section is included from a template to be included in the link
Definition: hooks.txt:3053
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:235
is
This document provides an overview of the usage of PageUpdater and that is
Definition: pageupdater.txt:3
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
WikitextContentTest\newContent
newContent( $text)
Definition: WikitextContentTest.php:24
WikitextContentTest\$sections
static $sections
Definition: WikitextContentTest.php:12
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:138
in
null for the wiki Added in
Definition: hooks.txt:1588
not
if not
Definition: COPYING.txt:307
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
MediaWikiTestCase\getDefaultWikitextNS
getDefaultWikitextNS()
Returns the ID of a namespace that defaults to Wikitext.
Definition: MediaWikiTestCase.php:2211
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:36
WikitextContentTest\dataEquals
static dataEquals()
Definition: WikitextContentTest.php:427
hook
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 or an object and a method hook function The function part of a hook
Definition: hooks.txt:23
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:133
JavaScriptContent
Content for JavaScript pages.
Definition: JavaScriptContent.php:33
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
$value
$value
Definition: styleTest.css.php:49
WikitextContentTest\testRedirectParserOption
testRedirectParserOption()
ParserOptions::getRedirectTarget ParserOptions::setRedirectTarget.
Definition: WikitextContentTest.php:368
WikitextContentTest\testHadSignature
testHadSignature()
WikitextContent::preSaveTransform WikitextContent::fillParserOutput.
Definition: WikitextContentTest.php:455
WikitextContentTest\dataGetDeletionUpdates
static dataGetDeletionUpdates()
Definition: WikitextContentTest.php:437
ParserOptions\newCanonical
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
Definition: ParserOptions.php:1064
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:37
WikitextContentTest\dataGetParserOutput
static dataGetParserOutput()
Definition: WikitextContentTest.php:28
$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:1985
Test
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
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:1985
$content
$content
Definition: pageupdater.txt:72
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
seen
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be seen
Definition: globals.txt:25
WikitextContentTest
ContentHandler.
Definition: WikitextContentTest.php:11