Merge "(minor) use wfMemcKey when caching sites list."
[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 $title = Title::newFromText( $title );
73 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
74
75 $content = ContentHandler::makeContent( $text, $title, $model );
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
97 public static function dataGetSection() {
98 return array(
99 array( WikitextContentTest::$sections,
100 "0",
101 "Intro"
102 ),
103 array( WikitextContentTest::$sections,
104 "2",
105 "== test ==
106 just a test"
107 ),
108 array( WikitextContentTest::$sections,
109 "8",
110 false
111 ),
112 );
113 }
114
115 /**
116 * @dataProvider dataGetSection
117 */
118 public function testGetSection( $text, $sectionId, $expectedText ) {
119 $content = $this->newContent( $text );
120
121 $sectionContent = $content->getSection( $sectionId );
122 if ( is_object( $sectionContent ) ) {
123 $sectionText = $sectionContent->getNativeData();
124 } else {
125 $sectionText = $sectionContent;
126 }
127
128 $this->assertEquals( $expectedText, $sectionText );
129 }
130
131 public static function dataReplaceSection() {
132 return array(
133 array( WikitextContentTest::$sections,
134 "0",
135 "No more",
136 null,
137 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest::$sections ) )
138 ),
139 array( WikitextContentTest::$sections,
140 "",
141 "No more",
142 null,
143 "No more"
144 ),
145 array( WikitextContentTest::$sections,
146 "2",
147 "== TEST ==\nmore fun",
148 null,
149 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest::$sections ) )
150 ),
151 array( WikitextContentTest::$sections,
152 "8",
153 "No more",
154 null,
155 WikitextContentTest::$sections
156 ),
157 array( WikitextContentTest::$sections,
158 "new",
159 "No more",
160 "New",
161 trim( WikitextContentTest::$sections ) . "\n\n\n== New ==\n\nNo more"
162 ),
163 );
164 }
165
166 /**
167 * @dataProvider dataReplaceSection
168 */
169 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
170 $content = $this->newContent( $text );
171 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
172
173 $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() );
174 }
175
176 public function testAddSectionHeader( ) {
177 $content = $this->newContent( 'hello world' );
178 $content = $content->addSectionHeader( 'test' );
179
180 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
181 }
182
183 public static function dataPreSaveTransform() {
184 return array(
185 array( 'hello this is ~~~',
186 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
187 ),
188 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
189 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
190 ),
191 array( // rtrim
192 " Foo \n ",
193 " Foo",
194 ),
195 );
196 }
197
198 public static function dataPreloadTransform() {
199 return array(
200 array( 'hello this is ~~~',
201 "hello this is ~~~",
202 ),
203 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
204 'hello \'\'this\'\' is bar',
205 ),
206 );
207 }
208
209 public static function dataGetRedirectTarget() {
210 return array(
211 array( '#REDIRECT [[Test]]',
212 'Test',
213 ),
214 array( '#REDIRECT Test',
215 null,
216 ),
217 array( '* #REDIRECT [[Test]]',
218 null,
219 ),
220 );
221 }
222
223 public static function dataGetTextForSummary() {
224 return array(
225 array( "hello\nworld.",
226 16,
227 'hello world.',
228 ),
229 array( 'hello world.',
230 8,
231 'hello...',
232 ),
233 array( '[[hello world]].',
234 8,
235 'hel...',
236 ),
237 );
238 }
239
240 /**
241 * @todo: test needs database! Should be done by a test class in the Database group.
242 */
243 /*
244 public function getRedirectChain() {
245 $text = $this->getNativeData();
246 return Title::newFromRedirectArray( $text );
247 }
248 */
249
250 /**
251 * @todo: test needs database! Should be done by a test class in the Database group.
252 */
253 /*
254 public function getUltimateRedirectTarget() {
255 $text = $this->getNativeData();
256 return Title::newFromRedirectRecurse( $text );
257 }
258 */
259
260 public static function dataIsCountable() {
261 return array(
262 array( '',
263 null,
264 'any',
265 true
266 ),
267 array( 'Foo',
268 null,
269 'any',
270 true
271 ),
272 array( 'Foo',
273 null,
274 'comma',
275 false
276 ),
277 array( 'Foo, bar',
278 null,
279 'comma',
280 true
281 ),
282 array( 'Foo',
283 null,
284 'link',
285 false
286 ),
287 array( 'Foo [[bar]]',
288 null,
289 'link',
290 true
291 ),
292 array( 'Foo',
293 true,
294 'link',
295 true
296 ),
297 array( 'Foo [[bar]]',
298 false,
299 'link',
300 false
301 ),
302 array( '#REDIRECT [[bar]]',
303 true,
304 'any',
305 false
306 ),
307 array( '#REDIRECT [[bar]]',
308 true,
309 'comma',
310 false
311 ),
312 array( '#REDIRECT [[bar]]',
313 true,
314 'link',
315 false
316 ),
317 );
318 }
319
320 public function testMatchMagicWord( ) {
321 $mw = MagicWord::get( "staticredirect" );
322
323 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
324 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
325
326 $content = $this->newContent( "#REDIRECT [[FOO]]" );
327 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
328 }
329
330 public function testUpdateRedirect( ) {
331 $target = Title::newFromText( "testUpdateRedirect_target" );
332
333 // test with non-redirect page
334 $content = $this->newContent( "hello world." );
335 $newContent = $content->updateRedirect( $target );
336
337 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
338
339 // test with actual redirect
340 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
341 $newContent = $content->updateRedirect( $target );
342
343 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
344 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
345
346 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
347 }
348
349 public function testGetModel() {
350 $content = $this->newContent( "hello world." );
351
352 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
353 }
354
355 public function testGetContentHandler() {
356 $content = $this->newContent( "hello world." );
357
358 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
359 }
360
361 public static function dataEquals( ) {
362 return array(
363 array( new WikitextContent( "hallo" ), null, false ),
364 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
365 array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
366 array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
367 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
368 );
369 }
370
371 public static function dataGetDeletionUpdates() {
372 return array(
373 array("WikitextContentTest_testGetSecondaryDataUpdates_1",
374 CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
375 array( 'LinksDeletionUpdate' => array( ) )
376 ),
377 array("WikitextContentTest_testGetSecondaryDataUpdates_2",
378 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
379 array( 'LinksDeletionUpdate' => array( ) )
380 ),
381 // @todo: more...?
382 );
383 }
384
385 }