35e2e778cddc2daca393adbff54d89adfeced819
3 class WikitextContentTest
extends MediaWikiTestCase
{
5 public function setup() {
6 $this->context
= new RequestContext( new FauxRequest() );
7 $this->context
->setTitle( Title
::newFromText( "Test" ) );
10 public function newContent( $text ) {
11 return new WikitextContent( $text );
14 public function dataGetParserOutput() {
16 array("hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
22 * @dataProvider dataGetParserOutput
24 public function testGetParserOutput( $text, $expectedHtml ) {
25 $content = $this->newContent( $text );
27 $po = $content->getParserOutput( $this->context
);
29 $this->assertEquals( $expectedHtml, $po->getText() );
47 public function dataGetSection() {
49 array( WikitextContentTest
::$sections,
53 array( WikitextContentTest
::$sections,
58 array( WikitextContentTest
::$sections,
66 * @dataProvider dataGetSection
68 public function testGetSection( $text, $sectionId, $expectedText ) {
69 $content = $this->newContent( $text );
71 $sectionContent = $content->getSection( $sectionId );
73 $this->assertEquals( $expectedText, is_null( $sectionContent ) ?
null : $sectionContent->getNativeData() );
76 public function dataReplaceSection() {
78 array( WikitextContentTest
::$sections,
82 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest
::$sections ) )
84 array( WikitextContentTest
::$sections,
90 array( WikitextContentTest
::$sections,
92 "== TEST ==\nmore fun",
94 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest
::$sections ) )
96 array( WikitextContentTest
::$sections,
100 WikitextContentTest
::$sections
102 array( WikitextContentTest
::$sections,
106 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
112 * @dataProvider dataReplaceSection
114 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
115 $content = $this->newContent( $text );
116 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
118 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
121 public function testAddSectionHeader( ) {
122 $content = $this->newContent( 'hello world' );
123 $content = $content->addSectionHeader( 'test' );
125 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
128 public function dataPreSaveTransform() {
130 array( 'hello this is ~~~',
131 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
133 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
134 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
140 * @dataProvider dataPreSaveTransform
142 public function testPreSaveTransform( $text, $expected ) {
143 global $wgUser, $wgContLang;
144 $options = ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
146 $content = $this->newContent( $text );
147 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
149 $this->assertEquals( $expected, $content->getNativeData() );
152 public function dataPreloadTransform() {
154 array( 'hello this is ~~~',
157 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
158 'hello \'\'this\'\' is bar',
164 * @dataProvider dataPreloadTransform
166 public function testPreloadTransform( $text, $expected ) {
167 global $wgUser, $wgContLang;
168 $options = ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
170 $content = $this->newContent( $text );
171 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
173 $this->assertEquals( $expected, $content->getNativeData() );
176 public function dataGetRedirectTarget() {
178 array( '#REDIRECT [[Test]]',
181 array( '#REDIRECT Test',
184 array( '* #REDIRECT [[Test]]',
191 * @dataProvider dataGetRedirectTarget
193 public function testGetRedirectTarget( $text, $expected ) {
194 $content = $this->newContent( $text );
195 $t = $content->getRedirectTarget( );
197 if ( is_null( $expected ) ) $this->assertNull( $t, "text should not have generated a redirect target: $text" );
198 else $this->assertEquals( $expected, $t->getPrefixedText() );
202 * @dataProvider dataGetRedirectTarget
204 public function isRedirect( $text, $expected ) {
205 $content = $this->newContent( $text );
207 $this->assertEquals( !is_null($expected), $content->isRedirect() );
212 * @todo: test needs database!
215 public function getRedirectChain() {
216 $text = $this->getNativeData();
217 return Title::newFromRedirectArray( $text );
222 * @todo: test needs database!
225 public function getUltimateRedirectTarget() {
226 $text = $this->getNativeData();
227 return Title::newFromRedirectRecurse( $text );
232 public function dataIsCountable() {
259 array( 'Foo [[bar]]',
269 array( 'Foo [[bar]]',
274 array( '#REDIRECT [[bar]]',
279 array( '#REDIRECT [[bar]]',
284 array( '#REDIRECT [[bar]]',
294 * @dataProvider dataIsCountable
296 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
297 global $wgArticleCountMethod;
299 $old = $wgArticleCountMethod;
300 $wgArticleCountMethod = $mode;
302 $content = $this->newContent( $text );
304 $v = $content->isCountable( $hasLinks, $this->context
);
305 $wgArticleCountMethod = $old;
307 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
308 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
311 public function dataGetTextForSummary() {
313 array( "hello\nworld.",
317 array( 'hello world.',
321 array( '[[hello world]].',
329 * @dataProvider dataGetTextForSummary
331 public function testGetTextForSummary( $text, $maxlength, $expected ) {
332 $content = $this->newContent( $text );
334 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
338 public function testGetTextForSearchIndex( ) {
339 $content = $this->newContent( "hello world." );
341 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
344 public function testCopy() {
345 $content = $this->newContent( "hello world." );
346 $copy = $content->copy();
348 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
349 $this->assertEquals( "hello world.", $copy->getNativeData() );
352 public function testGetSize( ) {
353 $content = $this->newContent( "hello world." );
355 $this->assertEquals( 12, $content->getSize() );
358 public function testGetNativeData( ) {
359 $content = $this->newContent( "hello world." );
361 $this->assertEquals( "hello world.", $content->getNativeData() );
364 public function testGetWikitextForTransclusion( ) {
365 $content = $this->newContent( "hello world." );
367 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
370 # =================================================================================================================
372 public function getModelName() {
373 $content = $this->newContent( "hello world." );
375 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModelName() );
378 public function getContentHandler() {
379 $content = $this->newContent( "hello world." );
381 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelName() );
384 public function dataIsEmpty( ) {
389 array( 'hallo welt.', false ),
394 * @dataProvider dataIsEmpty
396 public function testIsEmpty( $text, $empty ) {
397 $content = $this->newContent( $text );
399 $this->assertEquals( $empty, $content->isEmpty() );
402 public function dataEquals( ) {
404 array( new WikitextContent( "hallo" ), null, false ),
405 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
406 array( new WikitextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
407 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
412 * @dataProvider dataEquals
414 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
415 $this->assertEquals( $equal, $a->equals( $b ) );