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