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