Merge "(bug 17602) fix Monobook action tabs not quite touching the page body"
[lhc/web/wiklou.git] / tests / phpunit / includes / content / WikitextContentTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 *
6 * @group Database
7 * ^--- needed, because we do need the database to test link updates
8 */
9 class WikitextContentTest extends TextContentTest {
10 static $sections = "Intro
11
12 == stuff ==
13 hello world
14
15 == test ==
16 just a test
17
18 == foo ==
19 more stuff
20 ";
21
22 public function newContent( $text ) {
23 return new WikitextContent( $text );
24 }
25
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>"
33 ),
34 // TODO: more...?
35 );
36 }
37
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()
46 )
47 )
48 ),
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 )
56 )
57 )
58 )
59 ),
60 // TODO: more...?
61 );
62 }
63
64 /**
65 * @dataProvider dataGetSecondaryDataUpdates
66 * @group Database
67 */
68 public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
69 $ns = $this->getDefaultWikitextNS();
70 $title = Title::newFromText( $title, $ns );
71
72 $content = ContentHandler::makeContent( $text, $title, $model );
73
74 $page = WikiPage::factory( $title );
75 $page->doEditContent( $content, '' );
76
77 $updates = $content->getSecondaryDataUpdates( $title );
78
79 // make updates accessible by class name
80 foreach ( $updates as $update ) {
81 $class = get_class( $update );
82 $updates[$class] = $update;
83 }
84
85 foreach ( $expectedStuff as $class => $fieldValues ) {
86 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
87
88 $update = $updates[$class];
89
90 foreach ( $fieldValues as $field => $value ) {
91 $v = $update->$field; #if the field doesn't exist, just crash and burn
92 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
93 }
94 }
95
96 $page->doDeleteArticle( '' );
97 }
98
99 public static function dataGetSection() {
100 return array(
101 array( WikitextContentTest::$sections,
102 "0",
103 "Intro"
104 ),
105 array( WikitextContentTest::$sections,
106 "2",
107 "== test ==
108 just a test"
109 ),
110 array( WikitextContentTest::$sections,
111 "8",
112 false
113 ),
114 );
115 }
116
117 /**
118 * @dataProvider dataGetSection
119 */
120 public function testGetSection( $text, $sectionId, $expectedText ) {
121 $content = $this->newContent( $text );
122
123 $sectionContent = $content->getSection( $sectionId );
124 if ( is_object( $sectionContent ) ) {
125 $sectionText = $sectionContent->getNativeData();
126 } else {
127 $sectionText = $sectionContent;
128 }
129
130 $this->assertEquals( $expectedText, $sectionText );
131 }
132
133 public static function dataReplaceSection() {
134 return array(
135 array( WikitextContentTest::$sections,
136 "0",
137 "No more",
138 null,
139 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest::$sections ) )
140 ),
141 array( WikitextContentTest::$sections,
142 "",
143 "No more",
144 null,
145 "No more"
146 ),
147 array( WikitextContentTest::$sections,
148 "2",
149 "== TEST ==\nmore fun",
150 null,
151 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest::$sections ) )
152 ),
153 array( WikitextContentTest::$sections,
154 "8",
155 "No more",
156 null,
157 WikitextContentTest::$sections
158 ),
159 array( WikitextContentTest::$sections,
160 "new",
161 "No more",
162 "New",
163 trim( WikitextContentTest::$sections ) . "\n\n\n== New ==\n\nNo more"
164 ),
165 );
166 }
167
168 /**
169 * @dataProvider dataReplaceSection
170 */
171 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
172 $content = $this->newContent( $text );
173 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
174
175 $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() );
176 }
177
178 public function testAddSectionHeader() {
179 $content = $this->newContent( 'hello world' );
180 $content = $content->addSectionHeader( 'test' );
181
182 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
183 }
184
185 public static function dataPreSaveTransform() {
186 return array(
187 array( 'hello this is ~~~',
188 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
189 ),
190 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
191 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
192 ),
193 array( // rtrim
194 " Foo \n ",
195 " Foo",
196 ),
197 );
198 }
199
200 public static function dataPreloadTransform() {
201 return array(
202 array( 'hello this is ~~~',
203 "hello this is ~~~",
204 ),
205 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
206 'hello \'\'this\'\' is bar',
207 ),
208 );
209 }
210
211 public static function dataGetRedirectTarget() {
212 return array(
213 array( '#REDIRECT [[Test]]',
214 'Test',
215 ),
216 array( '#REDIRECT Test',
217 null,
218 ),
219 array( '* #REDIRECT [[Test]]',
220 null,
221 ),
222 );
223 }
224
225 public static function dataGetTextForSummary() {
226 return array(
227 array( "hello\nworld.",
228 16,
229 'hello world.',
230 ),
231 array( 'hello world.',
232 8,
233 'hello...',
234 ),
235 array( '[[hello world]].',
236 8,
237 'hel...',
238 ),
239 );
240 }
241
242 /**
243 * @todo Test needs database! Should be done by a test class in the Database group.
244 */
245 /*
246 public function getRedirectChain() {
247 $text = $this->getNativeData();
248 return Title::newFromRedirectArray( $text );
249 }
250 */
251
252 /**
253 * @todo Test needs database! Should be done by a test class in the Database group.
254 */
255 /*
256 public function getUltimateRedirectTarget() {
257 $text = $this->getNativeData();
258 return Title::newFromRedirectRecurse( $text );
259 }
260 */
261
262 public static function dataIsCountable() {
263 return array(
264 array( '',
265 null,
266 'any',
267 true
268 ),
269 array( 'Foo',
270 null,
271 'any',
272 true
273 ),
274 array( 'Foo',
275 null,
276 'comma',
277 false
278 ),
279 array( 'Foo, bar',
280 null,
281 'comma',
282 true
283 ),
284 array( 'Foo',
285 null,
286 'link',
287 false
288 ),
289 array( 'Foo [[bar]]',
290 null,
291 'link',
292 true
293 ),
294 array( 'Foo',
295 true,
296 'link',
297 true
298 ),
299 array( 'Foo [[bar]]',
300 false,
301 'link',
302 false
303 ),
304 array( '#REDIRECT [[bar]]',
305 true,
306 'any',
307 false
308 ),
309 array( '#REDIRECT [[bar]]',
310 true,
311 'comma',
312 false
313 ),
314 array( '#REDIRECT [[bar]]',
315 true,
316 'link',
317 false
318 ),
319 );
320 }
321
322 public function testMatchMagicWord() {
323 $mw = MagicWord::get( "staticredirect" );
324
325 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
326 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
327
328 $content = $this->newContent( "#REDIRECT [[FOO]]" );
329 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
330 }
331
332 public function testUpdateRedirect() {
333 $target = Title::newFromText( "testUpdateRedirect_target" );
334
335 // test with non-redirect page
336 $content = $this->newContent( "hello world." );
337 $newContent = $content->updateRedirect( $target );
338
339 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
340
341 // test with actual redirect
342 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
343 $newContent = $content->updateRedirect( $target );
344
345 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
346 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
347
348 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
349 }
350
351 public function testGetModel() {
352 $content = $this->newContent( "hello world." );
353
354 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
355 }
356
357 public function testGetContentHandler() {
358 $content = $this->newContent( "hello world." );
359
360 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
361 }
362
363 public static function dataEquals() {
364 return array(
365 array( new WikitextContent( "hallo" ), null, false ),
366 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
367 array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
368 array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
369 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
370 );
371 }
372
373 public static function dataGetDeletionUpdates() {
374 return array(
375 array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
376 CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
377 array( 'LinksDeletionUpdate' => array() )
378 ),
379 array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
380 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
381 array( 'LinksDeletionUpdate' => array() )
382 ),
383 // @todo more...?
384 );
385 }
386 }