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
->getTitle() );
32 $this->assertEquals( $expectedHtml, $po->getText() );
50 public function dataGetSection() {
52 array( WikitextContentTest
::$sections,
56 array( WikitextContentTest
::$sections,
61 array( WikitextContentTest
::$sections,
69 * @dataProvider dataGetSection
71 public function testGetSection( $text, $sectionId, $expectedText ) {
72 $content = $this->newContent( $text );
74 $sectionContent = $content->getSection( $sectionId );
76 $this->assertEquals( $expectedText, is_null( $sectionContent ) ?
null : $sectionContent->getNativeData() );
79 public function dataReplaceSection() {
81 array( WikitextContentTest
::$sections,
85 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest
::$sections ) )
87 array( WikitextContentTest
::$sections,
93 array( WikitextContentTest
::$sections,
95 "== TEST ==\nmore fun",
97 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest
::$sections ) )
99 array( WikitextContentTest
::$sections,
103 WikitextContentTest
::$sections
105 array( WikitextContentTest
::$sections,
109 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
115 * @dataProvider dataReplaceSection
117 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
118 $content = $this->newContent( $text );
119 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
121 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
124 public function testAddSectionHeader( ) {
125 $content = $this->newContent( 'hello world' );
126 $content = $content->addSectionHeader( 'test' );
128 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
131 public function dataPreSaveTransform() {
133 array( 'hello this is ~~~',
134 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
136 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
137 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
143 * @dataProvider dataPreSaveTransform
145 public function testPreSaveTransform( $text, $expected ) {
146 global $wgUser, $wgContLang;
147 $options = ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
149 $content = $this->newContent( $text );
150 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
152 $this->assertEquals( $expected, $content->getNativeData() );
155 public function dataPreloadTransform() {
157 array( 'hello this is ~~~',
160 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
161 'hello \'\'this\'\' is bar',
167 * @dataProvider dataPreloadTransform
169 public function testPreloadTransform( $text, $expected ) {
170 global $wgUser, $wgContLang;
171 $options = ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
173 $content = $this->newContent( $text );
174 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
176 $this->assertEquals( $expected, $content->getNativeData() );
179 public function dataGetRedirectTarget() {
181 array( '#REDIRECT [[Test]]',
184 array( '#REDIRECT Test',
187 array( '* #REDIRECT [[Test]]',
194 * @dataProvider dataGetRedirectTarget
196 public function testGetRedirectTarget( $text, $expected ) {
197 $content = $this->newContent( $text );
198 $t = $content->getRedirectTarget( );
200 if ( is_null( $expected ) ) $this->assertNull( $t, "text should not have generated a redirect target: $text" );
201 else $this->assertEquals( $expected, $t->getPrefixedText() );
205 * @dataProvider dataGetRedirectTarget
207 public function isRedirect( $text, $expected ) {
208 $content = $this->newContent( $text );
210 $this->assertEquals( !is_null($expected), $content->isRedirect() );
215 * @todo: test needs database!
218 public function getRedirectChain() {
219 $text = $this->getNativeData();
220 return Title::newFromRedirectArray( $text );
225 * @todo: test needs database!
228 public function getUltimateRedirectTarget() {
229 $text = $this->getNativeData();
230 return Title::newFromRedirectRecurse( $text );
235 public function dataIsCountable() {
262 array( 'Foo [[bar]]',
272 array( 'Foo [[bar]]',
277 array( '#REDIRECT [[bar]]',
282 array( '#REDIRECT [[bar]]',
287 array( '#REDIRECT [[bar]]',
297 * @dataProvider dataIsCountable
299 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
300 global $wgArticleCountMethod;
302 $old = $wgArticleCountMethod;
303 $wgArticleCountMethod = $mode;
305 $content = $this->newContent( $text );
307 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
308 $wgArticleCountMethod = $old;
310 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
311 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
314 public function dataGetTextForSummary() {
316 array( "hello\nworld.",
320 array( 'hello world.',
324 array( '[[hello world]].',
332 * @dataProvider dataGetTextForSummary
334 public function testGetTextForSummary( $text, $maxlength, $expected ) {
335 $content = $this->newContent( $text );
337 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
341 public function testGetTextForSearchIndex( ) {
342 $content = $this->newContent( "hello world." );
344 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
347 public function testCopy() {
348 $content = $this->newContent( "hello world." );
349 $copy = $content->copy();
351 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
352 $this->assertEquals( "hello world.", $copy->getNativeData() );
355 public function testGetSize( ) {
356 $content = $this->newContent( "hello world." );
358 $this->assertEquals( 12, $content->getSize() );
361 public function testGetNativeData( ) {
362 $content = $this->newContent( "hello world." );
364 $this->assertEquals( "hello world.", $content->getNativeData() );
367 public function testGetWikitextForTransclusion( ) {
368 $content = $this->newContent( "hello world." );
370 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
373 # =================================================================================================================
375 public function testGetModel() {
376 $content = $this->newContent( "hello world." );
378 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
381 public function testGetContentHandler() {
382 $content = $this->newContent( "hello world." );
384 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelID() );
387 public function dataIsEmpty( ) {
392 array( 'hallo welt.', false ),
397 * @dataProvider dataIsEmpty
399 public function testIsEmpty( $text, $empty ) {
400 $content = $this->newContent( $text );
402 $this->assertEquals( $empty, $content->isEmpty() );
405 public function dataEquals( ) {
407 array( new WikitextContent( "hallo" ), null, false ),
408 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
409 array( new WikitextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
410 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
415 * @dataProvider dataEquals
417 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
418 $this->assertEquals( $equal, $a->equals( $b ) );