[Bug 41155] Record links on CSS/JS pages in the DB.
[lhc/web/wiklou.git] / tests / phpunit / includes / TextContentTest.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 TextContentTest extends MediaWikiTestCase {
10
11 public function setup() {
12 global $wgUser;
13
14 // anon user
15 $wgUser = new User();
16 $wgUser->setName( '127.0.0.1' );
17
18 $this->context = new RequestContext( new FauxRequest() );
19 $this->context->setTitle( Title::newFromText( "Test" ) );
20 $this->context->setUser( $wgUser );
21 }
22
23 public function newContent( $text ) {
24 return new TextContent( $text );
25 }
26
27
28 public function dataGetParserOutput() {
29 return array(
30 array(
31 "TextContentTest_testGetParserOutput",
32 CONTENT_MODEL_TEXT,
33 "hello ''world'' & [[stuff]]\n", "hello ''world'' &amp; [[stuff]]",
34 array( 'Links' => array() ) ),
35 // @todo: more...?
36 );
37 }
38
39 /**
40 * @dataProvider dataGetParserOutput
41 */
42 public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
43 $title = Title::newFromText( $title );
44 $content = ContentHandler::makeContent( $text, $title, $model );
45
46 $po = $content->getParserOutput( $title );
47
48 $html = $po->getText();
49 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
50
51 $this->assertEquals( $expectedHtml, trim( $html ) );
52
53 if ( $expectedFields ) {
54 foreach ( $expectedFields as $field => $exp ) {
55 $f = 'get' . ucfirst( $field );
56 $v = call_user_func( array( $po, $f ) );
57
58 if ( is_array( $exp ) ) {
59 $this->assertArrayEquals( $exp, $v );
60 } else {
61 $this->assertEquals( $exp, $v );
62 }
63 }
64 }
65
66 // @todo: assert more properties
67 }
68
69 public function dataPreSaveTransform() {
70 return array(
71 array( 'hello this is ~~~',
72 "hello this is ~~~",
73 ),
74 );
75 }
76
77 /**
78 * @dataProvider dataPreSaveTransform
79 */
80 public function testPreSaveTransform( $text, $expected ) {
81 global $wgContLang;
82
83 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
84
85 $content = $this->newContent( $text );
86 $content = $content->preSaveTransform( $this->context->getTitle(), $this->context->getUser(), $options );
87
88 $this->assertEquals( $expected, $content->getNativeData() );
89 }
90
91 public function dataPreloadTransform() {
92 return array(
93 array( 'hello this is ~~~',
94 "hello this is ~~~",
95 ),
96 );
97 }
98
99 /**
100 * @dataProvider dataPreloadTransform
101 */
102 public function testPreloadTransform( $text, $expected ) {
103 global $wgContLang;
104 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
105
106 $content = $this->newContent( $text );
107 $content = $content->preloadTransform( $this->context->getTitle(), $options );
108
109 $this->assertEquals( $expected, $content->getNativeData() );
110 }
111
112 public function dataGetRedirectTarget() {
113 return array(
114 array( '#REDIRECT [[Test]]',
115 null,
116 ),
117 );
118 }
119
120 /**
121 * @dataProvider dataGetRedirectTarget
122 */
123 public function testGetRedirectTarget( $text, $expected ) {
124 $content = $this->newContent( $text );
125 $t = $content->getRedirectTarget( );
126
127 if ( is_null( $expected ) ) {
128 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
129 } else {
130 $this->assertEquals( $expected, $t->getPrefixedText() );
131 }
132 }
133
134 /**
135 * @dataProvider dataGetRedirectTarget
136 */
137 public function isRedirect( $text, $expected ) {
138 $content = $this->newContent( $text );
139
140 $this->assertEquals( !is_null($expected), $content->isRedirect() );
141 }
142
143
144 /**
145 * @todo: test needs database! Should be done by a test class in the Database group.
146 */
147 /*
148 public function getRedirectChain() {
149 $text = $this->getNativeData();
150 return Title::newFromRedirectArray( $text );
151 }
152 */
153
154 /**
155 * @todo: test needs database! Should be done by a test class in the Database group.
156 */
157 /*
158 public function getUltimateRedirectTarget() {
159 $text = $this->getNativeData();
160 return Title::newFromRedirectRecurse( $text );
161 }
162 */
163
164
165 public function dataIsCountable() {
166 return array(
167 array( '',
168 null,
169 'any',
170 true
171 ),
172 array( 'Foo',
173 null,
174 'any',
175 true
176 ),
177 array( 'Foo',
178 null,
179 'comma',
180 false
181 ),
182 array( 'Foo, bar',
183 null,
184 'comma',
185 false
186 ),
187 );
188 }
189
190
191 /**
192 * @dataProvider dataIsCountable
193 * @group Database
194 */
195 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
196 global $wgArticleCountMethod;
197
198 $old = $wgArticleCountMethod;
199 $wgArticleCountMethod = $mode;
200
201 $content = $this->newContent( $text );
202
203 $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
204 $wgArticleCountMethod = $old;
205
206 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
207 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
208 }
209
210 public function dataGetTextForSummary() {
211 return array(
212 array( "hello\nworld.",
213 16,
214 'hello world.',
215 ),
216 array( 'hello world.',
217 8,
218 'hello...',
219 ),
220 array( '[[hello world]].',
221 8,
222 '[[hel...',
223 ),
224 );
225 }
226
227 /**
228 * @dataProvider dataGetTextForSummary
229 */
230 public function testGetTextForSummary( $text, $maxlength, $expected ) {
231 $content = $this->newContent( $text );
232
233 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
234 }
235
236
237 public function testGetTextForSearchIndex( ) {
238 $content = $this->newContent( "hello world." );
239
240 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
241 }
242
243 public function testCopy() {
244 $content = $this->newContent( "hello world." );
245 $copy = $content->copy();
246
247 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
248 $this->assertEquals( "hello world.", $copy->getNativeData() );
249 }
250
251 public function testGetSize( ) {
252 $content = $this->newContent( "hello world." );
253
254 $this->assertEquals( 12, $content->getSize() );
255 }
256
257 public function testGetNativeData( ) {
258 $content = $this->newContent( "hello world." );
259
260 $this->assertEquals( "hello world.", $content->getNativeData() );
261 }
262
263 public function testGetWikitextForTransclusion( ) {
264 $content = $this->newContent( "hello world." );
265
266 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
267 }
268
269 # =================================================================================================================
270
271 public function testGetModel() {
272 $content = $this->newContent( "hello world." );
273
274 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
275 }
276
277 public function testGetContentHandler() {
278 $content = $this->newContent( "hello world." );
279
280 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
281 }
282
283 public function dataIsEmpty( ) {
284 return array(
285 array( '', true ),
286 array( ' ', false ),
287 array( '0', false ),
288 array( 'hallo welt.', false ),
289 );
290 }
291
292 /**
293 * @dataProvider dataIsEmpty
294 */
295 public function testIsEmpty( $text, $empty ) {
296 $content = $this->newContent( $text );
297
298 $this->assertEquals( $empty, $content->isEmpty() );
299 }
300
301 public function dataEquals( ) {
302 return array(
303 array( new TextContent( "hallo" ), null, false ),
304 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
305 array( new TextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
306 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
307 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
308 );
309 }
310
311 /**
312 * @dataProvider dataEquals
313 */
314 public function testEquals( Content $a, Content $b = null, $equal = false ) {
315 $this->assertEquals( $equal, $a->equals( $b ) );
316 }
317
318 public function dataGetDeletionUpdates() {
319 return array(
320 array("TextContentTest_testGetSecondaryDataUpdates_1",
321 CONTENT_MODEL_TEXT, "hello ''world''\n",
322 array( )
323 ),
324 array("TextContentTest_testGetSecondaryDataUpdates_2",
325 CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
326 array( )
327 ),
328 // @todo: more...?
329 );
330 }
331
332 /**
333 * @dataProvider dataGetDeletionUpdates
334 */
335 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
336 $title = Title::newFromText( $title );
337 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
338
339 $content = ContentHandler::makeContent( $text, $title, $model );
340
341 $updates = $content->getDeletionUpdates( WikiPage::factory( $title ) );
342
343 // make updates accessible by class name
344 foreach ( $updates as $update ) {
345 $class = get_class( $update );
346 $updates[ $class ] = $update;
347 }
348
349 if ( !$expectedStuff ) {
350 $this->assertTrue( true ); // make phpunit happy
351 return;
352 }
353
354 foreach ( $expectedStuff as $class => $fieldValues ) {
355 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
356
357 $update = $updates[ $class ];
358
359 foreach ( $fieldValues as $field => $value ) {
360 $v = $update->$field; #if the field doesn't exist, just crash and burn
361 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
362 }
363 }
364 }
365
366 }