4 * @group ContentHandler
6 class WikitextContentTest extends MediaWikiTestCase {
8 public function setup() {
9 $this->context = new RequestContext( new FauxRequest() );
10 $this->context->setTitle( Title::newFromText( "Test" ) );
13 public function newContent( $text ) {
14 return new WikitextContent( $text );
17 public function dataGetParserOutput() {
19 array("hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
25 * @dataProvider dataGetParserOutput
27 public function testGetParserOutput( $text, $expectedHtml ) {
28 $content = $this->newContent( $text );
30 $po = $content->getParserOutput( $this->context );
32 $this->assertEquals( $expectedHtml, $po->getText() );
35 public function dataGetSecondaryDataUpdates() {
42 * @dataProvider dataGetParserOutput
44 public function testGetSecondaryDataUpdates( $text, $expectedLinks ) {
45 $content = $this->newContent( "hello [[world]]\n" );
47 $updates = $content->getSecondaryDataUpdates( $this->context );
49 $this->assertEquals( 1, count( $updates ) );
50 $this->assertEquals( "LinksUpdate", get_class( $updates[0] ) );
67 public function dataGetSection() {
69 array( WikitextContentTest::$sections,
73 array( WikitextContentTest::$sections,
78 array( WikitextContentTest::$sections,
86 * @dataProvider dataGetSection
88 public function testGetSection( $text, $sectionId, $expectedText ) {
89 $content = $this->newContent( $text );
91 $sectionContent = $content->getSection( $sectionId );
93 $this->assertEquals( $expectedText, is_null( $sectionContent ) ? null : $sectionContent->getNativeData() );
96 public function dataReplaceSection() {
98 array( WikitextContentTest::$sections,
102 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest::$sections ) )
104 array( WikitextContentTest::$sections,
110 array( WikitextContentTest::$sections,
112 "== TEST ==\nmore fun",
114 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest::$sections ) )
116 array( WikitextContentTest::$sections,
120 WikitextContentTest::$sections
122 array( WikitextContentTest::$sections,
126 trim( WikitextContentTest::$sections ) . "\n\n\n== New ==\n\nNo more"
132 * @dataProvider dataReplaceSection
134 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
135 $content = $this->newContent( $text );
136 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
138 $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() );
141 public function testAddSectionHeader( ) {
142 $content = $this->newContent( 'hello world' );
143 $content = $content->addSectionHeader( 'test' );
145 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
148 public function dataPreSaveTransform() {
150 array( 'hello this is ~~~',
151 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
153 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
154 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
160 * @dataProvider dataPreSaveTransform
162 public function testPreSaveTransform( $text, $expected ) {
163 global $wgUser, $wgContLang;
164 $options = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
166 $content = $this->newContent( $text );
167 $content = $content->preSaveTransform( $this->context->getTitle(), $this->context->getUser(), $options );
169 $this->assertEquals( $expected, $content->getNativeData() );
172 public function dataPreloadTransform() {
174 array( 'hello this is ~~~',
177 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
178 'hello \'\'this\'\' is bar',
184 * @dataProvider dataPreloadTransform
186 public function testPreloadTransform( $text, $expected ) {
187 global $wgUser, $wgContLang;
188 $options = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
190 $content = $this->newContent( $text );
191 $content = $content->preloadTransform( $this->context->getTitle(), $options );
193 $this->assertEquals( $expected, $content->getNativeData() );
196 public function dataGetRedirectTarget() {
198 array( '#REDIRECT [[Test]]',
201 array( '#REDIRECT Test',
204 array( '* #REDIRECT [[Test]]',
211 * @dataProvider dataGetRedirectTarget
213 public function testGetRedirectTarget( $text, $expected ) {
214 $content = $this->newContent( $text );
215 $t = $content->getRedirectTarget( );
217 if ( is_null( $expected ) ) $this->assertNull( $t, "text should not have generated a redirect target: $text" );
218 else $this->assertEquals( $expected, $t->getPrefixedText() );
222 * @dataProvider dataGetRedirectTarget
224 public function isRedirect( $text, $expected ) {
225 $content = $this->newContent( $text );
227 $this->assertEquals( !is_null($expected), $content->isRedirect() );
232 * @todo: test needs database!
235 public function getRedirectChain() {
236 $text = $this->getNativeData();
237 return Title::newFromRedirectArray( $text );
242 * @todo: test needs database!
245 public function getUltimateRedirectTarget() {
246 $text = $this->getNativeData();
247 return Title::newFromRedirectRecurse( $text );
252 public function dataIsCountable() {
279 array( 'Foo [[bar]]',
289 array( 'Foo [[bar]]',
294 array( '#REDIRECT [[bar]]',
299 array( '#REDIRECT [[bar]]',
304 array( '#REDIRECT [[bar]]',
314 * @dataProvider dataIsCountable
316 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
317 global $wgArticleCountMethod;
319 $old = $wgArticleCountMethod;
320 $wgArticleCountMethod = $mode;
322 $content = $this->newContent( $text );
324 $v = $content->isCountable( $hasLinks, $this->context );
325 $wgArticleCountMethod = $old;
327 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
328 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
331 public function dataGetTextForSummary() {
333 array( "hello\nworld.",
337 array( 'hello world.',
341 array( '[[hello world]].',
349 * @dataProvider dataGetTextForSummary
351 public function testGetTextForSummary( $text, $maxlength, $expected ) {
352 $content = $this->newContent( $text );
354 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
358 public function testGetTextForSearchIndex( ) {
359 $content = $this->newContent( "hello world." );
361 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
364 public function testCopy() {
365 $content = $this->newContent( "hello world." );
366 $copy = $content->copy();
368 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
369 $this->assertEquals( "hello world.", $copy->getNativeData() );
372 public function testGetSize( ) {
373 $content = $this->newContent( "hello world." );
375 $this->assertEquals( 12, $content->getSize() );
378 public function testGetNativeData( ) {
379 $content = $this->newContent( "hello world." );
381 $this->assertEquals( "hello world.", $content->getNativeData() );
384 public function testGetWikitextForTransclusion( ) {
385 $content = $this->newContent( "hello world." );
387 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
390 # =================================================================================================================
392 public function testGetModel() {
393 $content = $this->newContent( "hello world." );
395 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
398 public function testGetContentHandler() {
399 $content = $this->newContent( "hello world." );
401 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
404 public function dataIsEmpty( ) {
409 array( 'hallo welt.', false ),
414 * @dataProvider dataIsEmpty
416 public function testIsEmpty( $text, $empty ) {
417 $content = $this->newContent( $text );
419 $this->assertEquals( $empty, $content->isEmpty() );
422 public function dataEquals( ) {
424 array( new WikitextContent( "hallo" ), null, false ),
425 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
426 array( new WikitextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
427 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
432 * @dataProvider dataEquals
434 public function testEquals( Content $a, Content $b = null, $equal = false ) {
435 $this->assertEquals( $equal, $a->equals( $b ) );