MediaWiki REL1_31
TextContentTest.php
Go to the documentation of this file.
1<?php
2
9 protected $context;
10
11 protected function setUp() {
12 parent::setUp();
13
14 // Anon user
15 $user = new User();
16 $user->setName( '127.0.0.1' );
17
18 $this->context = new RequestContext( new FauxRequest() );
19 $this->context->setTitle( Title::newFromText( 'Test' ) );
20 $this->context->setUser( $user );
21
22 $this->setMwGlobals( [
23 'wgUser' => $user,
24 'wgTextModelsToParse' => [
28 ],
29 'wgUseTidy' => false,
30 'wgCapitalLinks' => true,
31 'wgHooks' => [], // bypass hook ContentGetParserOutput that force custom rendering
32 ] );
33
35 }
36
37 protected function tearDown() {
39 parent::tearDown();
40 }
41
42 public function newContent( $text ) {
43 return new TextContent( $text );
44 }
45
46 public static function dataGetParserOutput() {
47 return [
48 [
49 'TextContentTest_testGetParserOutput',
51 "hello ''world'' & [[stuff]]\n", "hello ''world'' &amp; [[stuff]]",
52 [
53 'Links' => []
54 ]
55 ],
56 // TODO: more...?
57 ];
58 }
59
64 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
65 $expectedFields = null
66 ) {
67 $title = Title::newFromText( $title );
68 $content = ContentHandler::makeContent( $text, $title, $model );
69
70 $po = $content->getParserOutput( $title );
71
72 $html = $po->getText();
73 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
74
75 $this->assertEquals( $expectedHtml, trim( $html ) );
76
77 if ( $expectedFields ) {
78 foreach ( $expectedFields as $field => $exp ) {
79 $f = 'get' . ucfirst( $field );
80 $v = call_user_func( [ $po, $f ] );
81
82 if ( is_array( $exp ) ) {
83 $this->assertArrayEquals( $exp, $v );
84 } else {
85 $this->assertEquals( $exp, $v );
86 }
87 }
88 }
89
90 // TODO: assert more properties
91 }
92
93 public static function dataPreSaveTransform() {
94 return [
95 [
96 # 0: no signature resolution
97 'hello this is ~~~',
98 'hello this is ~~~',
99 ],
100 [
101 # 1: rtrim
102 " Foo \n ",
103 ' Foo',
104 ],
105 [
106 # 2: newline normalization
107 "LF\n\nCRLF\r\n\r\nCR\r\rEND",
108 "LF\n\nCRLF\n\nCR\n\nEND",
109 ],
110 ];
111 }
112
117 public function testPreSaveTransform( $text, $expected ) {
118 global $wgContLang;
119
120 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
121
122 $content = $this->newContent( $text );
123 $content = $content->preSaveTransform(
124 $this->context->getTitle(),
125 $this->context->getUser(),
127 );
128
129 $this->assertEquals( $expected, $content->getNativeData() );
130 }
131
132 public static function dataPreloadTransform() {
133 return [
134 [
135 'hello this is ~~~',
136 'hello this is ~~~',
137 ],
138 ];
139 }
140
145 public function testPreloadTransform( $text, $expected ) {
146 global $wgContLang;
147 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
148
149 $content = $this->newContent( $text );
150 $content = $content->preloadTransform( $this->context->getTitle(), $options );
151
152 $this->assertEquals( $expected, $content->getNativeData() );
153 }
154
155 public static function dataGetRedirectTarget() {
156 return [
157 [ '#REDIRECT [[Test]]',
158 null,
159 ],
160 ];
161 }
162
167 public function testGetRedirectTarget( $text, $expected ) {
168 $content = $this->newContent( $text );
169 $t = $content->getRedirectTarget();
170
171 if ( is_null( $expected ) ) {
172 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
173 } else {
174 $this->assertEquals( $expected, $t->getPrefixedText() );
175 }
176 }
177
182 public function testIsRedirect( $text, $expected ) {
183 $content = $this->newContent( $text );
184
185 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
186 }
187
188 public static function dataIsCountable() {
189 return [
190 [ '',
191 null,
192 'any',
193 true
194 ],
195 [ 'Foo',
196 null,
197 'any',
198 true
199 ],
200 ];
201 }
202
207 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
208 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
209
210 $content = $this->newContent( $text );
211
212 $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
213
214 $this->assertEquals(
215 $expected,
216 $v,
217 'isCountable() returned unexpected value ' . var_export( $v, true )
218 . ' instead of ' . var_export( $expected, true )
219 . " in mode `$mode` for text \"$text\""
220 );
221 }
222
223 public static function dataGetTextForSummary() {
224 return [
225 [ "hello\nworld.",
226 16,
227 'hello world.',
228 ],
229 [ 'hello world.',
230 8,
231 'hello...',
232 ],
233 [ '[[hello world]].',
234 8,
235 '[[hel...',
236 ],
237 ];
238 }
239
244 public function testGetTextForSummary( $text, $maxlength, $expected ) {
245 $content = $this->newContent( $text );
246
247 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
248 }
249
253 public function testGetTextForSearchIndex() {
254 $content = $this->newContent( 'hello world.' );
255
256 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
257 }
258
262 public function testCopy() {
263 $content = $this->newContent( 'hello world.' );
264 $copy = $content->copy();
265
266 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
267 $this->assertEquals( 'hello world.', $copy->getNativeData() );
268 }
269
273 public function testGetSize() {
274 $content = $this->newContent( 'hello world.' );
275
276 $this->assertEquals( 12, $content->getSize() );
277 }
278
282 public function testGetNativeData() {
283 $content = $this->newContent( 'hello world.' );
284
285 $this->assertEquals( 'hello world.', $content->getNativeData() );
286 }
287
292 $content = $this->newContent( 'hello world.' );
293
294 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
295 }
296
300 public function testGetModel() {
301 $content = $this->newContent( "hello world." );
302
303 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
304 }
305
309 public function testGetContentHandler() {
310 $content = $this->newContent( "hello world." );
311
312 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
313 }
314
315 public static function dataIsEmpty() {
316 return [
317 [ '', true ],
318 [ ' ', false ],
319 [ '0', false ],
320 [ 'hallo welt.', false ],
321 ];
322 }
323
328 public function testIsEmpty( $text, $empty ) {
329 $content = $this->newContent( $text );
330
331 $this->assertEquals( $empty, $content->isEmpty() );
332 }
333
334 public static function dataEquals() {
335 return [
336 [ new TextContent( "hallo" ), null, false ],
337 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
338 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
339 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
340 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
341 ];
342 }
343
348 public function testEquals( Content $a, Content $b = null, $equal = false ) {
349 $this->assertEquals( $equal, $a->equals( $b ) );
350 }
351
352 public static function dataGetDeletionUpdates() {
353 return [
354 [ "TextContentTest_testGetSecondaryDataUpdates_1",
355 CONTENT_MODEL_TEXT, "hello ''world''\n",
356 []
357 ],
358 [ "TextContentTest_testGetSecondaryDataUpdates_2",
359 CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
360 []
361 ],
362 // TODO: more...?
363 ];
364 }
365
370 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
371 $ns = $this->getDefaultWikitextNS();
372 $title = Title::newFromText( $title, $ns );
373
374 $content = ContentHandler::makeContent( $text, $title, $model );
375
376 $page = WikiPage::factory( $title );
377 $page->doEditContent( $content, '' );
378
379 $updates = $content->getDeletionUpdates( $page );
380
381 // make updates accessible by class name
382 foreach ( $updates as $update ) {
383 $class = get_class( $update );
384 $updates[$class] = $update;
385 }
386
387 if ( !$expectedStuff ) {
388 $this->assertTrue( true ); // make phpunit happy
389 return;
390 }
391
392 foreach ( $expectedStuff as $class => $fieldValues ) {
393 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
394
395 $update = $updates[$class];
396
397 foreach ( $fieldValues as $field => $value ) {
398 $v = $update->$field; # if the field doesn't exist, just crash and burn
399 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
400 }
401 }
402
403 $page->doDeleteArticle( '' );
404 }
405
406 public static function provideConvert() {
407 return [
408 [ // #0
409 'Hallo Welt',
410 CONTENT_MODEL_WIKITEXT,
411 'lossless',
412 'Hallo Welt'
413 ],
414 [ // #1
415 'Hallo Welt',
416 CONTENT_MODEL_WIKITEXT,
417 'lossless',
418 'Hallo Welt'
419 ],
420 [ // #1
421 'Hallo Welt',
422 CONTENT_MODEL_CSS,
423 'lossless',
424 'Hallo Welt'
425 ],
426 [ // #1
427 'Hallo Welt',
428 CONTENT_MODEL_JAVASCRIPT,
429 'lossless',
430 'Hallo Welt'
431 ],
432 ];
433 }
434
439 public function testConvert( $text, $model, $lossy, $expectedNative ) {
440 $content = $this->newContent( $text );
441
442 $converted = $content->convert( $model, $lossy );
443
444 if ( $expectedNative === false ) {
445 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
446 } else {
447 $this->assertInstanceOf( Content::class, $converted );
448 $this->assertEquals( $expectedNative, $converted->getNativeData() );
449 }
450 }
451
456 public function testNormalizeLineEndings( $input, $expected ) {
457 $this->assertEquals( $expected, TextContent::normalizeLineEndings( $input ) );
458 }
459
460 public static function provideNormalizeLineEndings() {
461 return [
462 [
463 "Foo\r\nbar",
464 "Foo\nbar"
465 ],
466 [
467 "Foo\rbar",
468 "Foo\nbar"
469 ],
470 [
471 "Foobar\n ",
472 "Foobar"
473 ]
474 ];
475 }
476
477}
WebRequest clone which takes values from a provided array.
Content for JavaScript pages.
static destroySingleton()
Destroy the current singleton instance.
Definition MWTidy.php:142
Base class that store and restore the Language objects.
getDefaultWikitextNS()
Returns the ID of a namespace that defaults to Wikitext.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Group all the pieces relevant to the context of a request into one instance.
ContentHandler Database ^— needed, because we do need the database to test link updates.
testGetContentHandler()
TextContent::getContentHandler.
testGetTextForSearchIndex()
TextContent::getTextForSearchIndex.
testGetModel()
TextContent::getModel.
testGetNativeData()
TextContent::getNativeData.
testGetSize()
TextContent::getSize.
testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields=null)
dataGetParserOutput TextContent::getParserOutput
testPreloadTransform( $text, $expected)
dataPreloadTransform TextContent::preloadTransform
testCopy()
TextContent::copy.
static dataGetParserOutput()
testEquals(Content $a, Content $b=null, $equal=false)
dataEquals TextContent::equals
testGetWikitextForTransclusion()
TextContent::getWikitextForTransclusion.
testIsEmpty( $text, $empty)
dataIsEmpty TextContent::isEmpty
static dataGetRedirectTarget()
testIsRedirect( $text, $expected)
dataGetRedirectTarget TextContent::isRedirect
testGetRedirectTarget( $text, $expected)
dataGetRedirectTarget TextContent::getRedirectTarget
testDeletionUpdates( $title, $model, $text, $expectedStuff)
dataGetDeletionUpdates TextContent::getDeletionUpdates
static dataGetDeletionUpdates()
testIsCountable( $text, $hasLinks, $mode, $expected)
dataIsCountable TextContent::isCountable
testGetTextForSummary( $text, $maxlength, $expected)
dataGetTextForSummary TextContent::getTextForSummary
static dataPreSaveTransform()
static dataPreloadTransform()
testPreSaveTransform( $text, $expected)
dataPreSaveTransform TextContent::preSaveTransform
static dataGetTextForSummary()
Content object implementation for representing flat text.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
Content object for wiki text pages.
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition design.txt:57
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:2001
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2006
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2013
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
const CONTENT_MODEL_CSS
Definition Defines.php:247
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:245
const CONTENT_MODEL_TEXT
Definition Defines.php:248
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:246
Base interface for content objects.
Definition Content.php:34