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