Remove messages.inc, rebuildLanguage.php, writeMessagesArray.inc
[mediawiki.git] / tests / phpunit / includes / content / WikitextContentTest.php
blobbd4ae35d087fcb30f4db093a15ad6ad748a86633
1 <?php
3 /**
4 * @group ContentHandler
6 * @group Database
7 * ^--- needed, because we do need the database to test link updates
8 */
9 class WikitextContentTest extends TextContentTest {
10 public static $sections = "Intro
12 == stuff ==
13 hello world
15 == test ==
16 just a test
18 == foo ==
19 more stuff
22 public function newContent( $text ) {
23 return new WikitextContent( $text );
26 public static function dataGetParserOutput() {
27 return array(
28 array(
29 "WikitextContentTest_testGetParserOutput",
30 CONTENT_MODEL_WIKITEXT,
31 "hello ''world''\n",
32 "<p>hello <i>world</i>\n</p>"
34 // TODO: more...?
38 public static function dataGetSecondaryDataUpdates() {
39 return array(
40 array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
41 CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
42 array(
43 'LinksUpdate' => array(
44 'mRecursive' => true,
45 'mLinks' => array()
49 array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
50 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
51 array(
52 'LinksUpdate' => array(
53 'mRecursive' => true,
54 'mLinks' => array(
55 array( 'World_test_21344' => 0 )
60 // TODO: more...?
64 /**
65 * @dataProvider dataGetSecondaryDataUpdates
66 * @group Database
67 * @covers WikitextContent::getSecondaryDataUpdates
69 public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
70 $ns = $this->getDefaultWikitextNS();
71 $title = Title::newFromText( $title, $ns );
73 $content = ContentHandler::makeContent( $text, $title, $model );
75 $page = WikiPage::factory( $title );
76 $page->doEditContent( $content, '' );
78 $updates = $content->getSecondaryDataUpdates( $title );
80 // make updates accessible by class name
81 foreach ( $updates as $update ) {
82 $class = get_class( $update );
83 $updates[$class] = $update;
86 foreach ( $expectedStuff as $class => $fieldValues ) {
87 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
89 $update = $updates[$class];
91 foreach ( $fieldValues as $field => $value ) {
92 $v = $update->$field; #if the field doesn't exist, just crash and burn
93 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
97 $page->doDeleteArticle( '' );
100 public static function dataGetSection() {
101 return array(
102 array( WikitextContentTest::$sections,
103 "0",
104 "Intro"
106 array( WikitextContentTest::$sections,
107 "2",
108 "== test ==
109 just a test"
111 array( WikitextContentTest::$sections,
112 "8",
113 false
119 * @dataProvider dataGetSection
120 * @covers WikitextContent::getSection
122 public function testGetSection( $text, $sectionId, $expectedText ) {
123 $content = $this->newContent( $text );
125 $sectionContent = $content->getSection( $sectionId );
126 if ( is_object( $sectionContent ) ) {
127 $sectionText = $sectionContent->getNativeData();
128 } else {
129 $sectionText = $sectionContent;
132 $this->assertEquals( $expectedText, $sectionText );
135 public static function dataReplaceSection() {
136 return array(
137 array( WikitextContentTest::$sections,
138 "0",
139 "No more",
140 null,
141 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest::$sections ) )
143 array( WikitextContentTest::$sections,
145 "No more",
146 null,
147 "No more"
149 array( WikitextContentTest::$sections,
150 "2",
151 "== TEST ==\nmore fun",
152 null,
153 trim( preg_replace(
154 '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==",
155 WikitextContentTest::$sections
158 array( WikitextContentTest::$sections,
159 "8",
160 "No more",
161 null,
162 WikitextContentTest::$sections
164 array( WikitextContentTest::$sections,
165 "new",
166 "No more",
167 "New",
168 trim( WikitextContentTest::$sections ) . "\n\n\n== New ==\n\nNo more"
174 * @dataProvider dataReplaceSection
175 * @covers WikitextContent::replaceSection
177 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
178 $content = $this->newContent( $text );
179 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
181 $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() );
185 * @covers WikitextContent::addSectionHeader
187 public function testAddSectionHeader() {
188 $content = $this->newContent( 'hello world' );
189 $content = $content->addSectionHeader( 'test' );
191 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
194 public static function dataPreSaveTransform() {
195 return array(
196 array( 'hello this is ~~~',
197 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
199 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
200 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
202 array( // rtrim
203 " Foo \n ",
204 " Foo",
209 public static function dataPreloadTransform() {
210 return array(
211 array( 'hello this is ~~~',
212 "hello this is ~~~",
214 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
215 'hello \'\'this\'\' is bar',
220 public static function dataGetRedirectTarget() {
221 return array(
222 array( '#REDIRECT [[Test]]',
223 'Test',
225 array( '#REDIRECT Test',
226 null,
228 array( '* #REDIRECT [[Test]]',
229 null,
234 public static function dataGetTextForSummary() {
235 return array(
236 array( "hello\nworld.",
238 'hello world.',
240 array( 'hello world.',
242 'hello...',
244 array( '[[hello world]].',
246 'hel...',
251 public static function dataIsCountable() {
252 return array(
253 array( '',
254 null,
255 'any',
256 true
258 array( 'Foo',
259 null,
260 'any',
261 true
263 array( 'Foo',
264 null,
265 'comma',
266 false
268 array( 'Foo, bar',
269 null,
270 'comma',
271 true
273 array( 'Foo',
274 null,
275 'link',
276 false
278 array( 'Foo [[bar]]',
279 null,
280 'link',
281 true
283 array( 'Foo',
284 true,
285 'link',
286 true
288 array( 'Foo [[bar]]',
289 false,
290 'link',
291 false
293 array( '#REDIRECT [[bar]]',
294 true,
295 'any',
296 false
298 array( '#REDIRECT [[bar]]',
299 true,
300 'comma',
301 false
303 array( '#REDIRECT [[bar]]',
304 true,
305 'link',
306 false
312 * @covers WikitextContent::matchMagicWord
314 public function testMatchMagicWord() {
315 $mw = MagicWord::get( "staticredirect" );
317 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
318 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
320 $content = $this->newContent( "#REDIRECT [[FOO]]" );
321 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
325 * @covers WikitextContent::updateRedirect
327 public function testUpdateRedirect() {
328 $target = Title::newFromText( "testUpdateRedirect_target" );
330 // test with non-redirect page
331 $content = $this->newContent( "hello world." );
332 $newContent = $content->updateRedirect( $target );
334 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
336 // test with actual redirect
337 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
338 $newContent = $content->updateRedirect( $target );
340 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
341 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
343 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
347 * @covers WikitextContent::getModel
349 public function testGetModel() {
350 $content = $this->newContent( "hello world." );
352 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
356 * @covers WikitextContent::getContentHandler
358 public function testGetContentHandler() {
359 $content = $this->newContent( "hello world." );
361 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
364 public static function dataEquals() {
365 return array(
366 array( new WikitextContent( "hallo" ), null, false ),
367 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
368 array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
369 array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
370 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
374 public static function dataGetDeletionUpdates() {
375 return array(
376 array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
377 CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
378 array( 'LinksDeletionUpdate' => array() )
380 array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
381 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
382 array( 'LinksDeletionUpdate' => array() )
384 // @todo more...?