MediaWiki REL1_33
TextContentTest.php
Go to the documentation of this file.
1<?php
2
4
11 protected $context;
12
13 protected function setUp() {
14 parent::setUp();
15
16 // trigger purging of all page related tables
17 $this->tablesUsed[] = 'page';
18 $this->tablesUsed[] = 'revision';
19
20 // Anon user
21 $user = new User();
22 $user->setName( '127.0.0.1' );
23
24 $this->context = new RequestContext( new FauxRequest() );
25 $this->context->setTitle( Title::newFromText( 'Test' ) );
26 $this->context->setUser( $user );
27
28 $this->setMwGlobals( [
29 'wgUser' => $user,
30 'wgTextModelsToParse' => [
34 ],
35 'wgTidyConfig' => [ 'driver' => 'RemexHtml' ],
36 'wgCapitalLinks' => true,
37 'wgHooks' => [], // bypass hook ContentGetParserOutput that force custom rendering
38 ] );
39
41 }
42
43 protected function tearDown() {
45 parent::tearDown();
46 }
47
52 public function newContent( $text ) {
53 return new TextContent( $text );
54 }
55
56 public static function dataGetParserOutput() {
57 return [
58 [
59 'TextContentTest_testGetParserOutput',
61 "hello ''world'' & [[stuff]]\n", "hello ''world'' &amp; [[stuff]]",
62 [
63 'Links' => []
64 ]
65 ],
66 // TODO: more...?
67 ];
68 }
69
74 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
75 $expectedFields = null
76 ) {
77 $title = Title::newFromText( $title );
78 $content = ContentHandler::makeContent( $text, $title, $model );
79
80 $po = $content->getParserOutput( $title );
81
82 $html = $po->getText();
83 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
84
85 $this->assertEquals( $expectedHtml, trim( $html ) );
86
87 if ( $expectedFields ) {
88 foreach ( $expectedFields as $field => $exp ) {
89 $f = 'get' . ucfirst( $field );
90 $v = call_user_func( [ $po, $f ] );
91
92 if ( is_array( $exp ) ) {
93 $this->assertArrayEquals( $exp, $v );
94 } else {
95 $this->assertEquals( $exp, $v );
96 }
97 }
98 }
99
100 // TODO: assert more properties
101 }
102
103 public static function dataPreSaveTransform() {
104 return [
105 [
106 # 0: no signature resolution
107 'hello this is ~~~',
108 'hello this is ~~~',
109 ],
110 [
111 # 1: rtrim
112 " Foo \n ",
113 ' Foo',
114 ],
115 [
116 # 2: newline normalization
117 "LF\n\nCRLF\r\n\r\nCR\r\rEND",
118 "LF\n\nCRLF\n\nCR\n\nEND",
119 ],
120 ];
121 }
122
127 public function testPreSaveTransform( $text, $expected ) {
128 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(),
129 MediaWikiServices::getInstance()->getContentLanguage() );
130
131 $content = $this->newContent( $text );
132 $content = $content->preSaveTransform(
133 $this->context->getTitle(),
134 $this->context->getUser(),
136 );
137
138 $this->assertEquals( $expected, $content->getText() );
139 }
140
141 public static function dataPreloadTransform() {
142 return [
143 [
144 'hello this is ~~~',
145 'hello this is ~~~',
146 ],
147 ];
148 }
149
154 public function testPreloadTransform( $text, $expected ) {
155 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(),
156 MediaWikiServices::getInstance()->getContentLanguage() );
157
158 $content = $this->newContent( $text );
159 $content = $content->preloadTransform( $this->context->getTitle(), $options );
160
161 $this->assertEquals( $expected, $content->getText() );
162 }
163
164 public static function dataGetRedirectTarget() {
165 return [
166 [ '#REDIRECT [[Test]]',
167 null,
168 ],
169 ];
170 }
171
176 public function testGetRedirectTarget( $text, $expected ) {
177 $content = $this->newContent( $text );
178 $t = $content->getRedirectTarget();
179
180 if ( is_null( $expected ) ) {
181 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
182 } else {
183 $this->assertEquals( $expected, $t->getPrefixedText() );
184 }
185 }
186
191 public function testIsRedirect( $text, $expected ) {
192 $content = $this->newContent( $text );
193
194 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
195 }
196
197 public static function dataIsCountable() {
198 return [
199 [ '',
200 null,
201 'any',
202 true
203 ],
204 [ 'Foo',
205 null,
206 'any',
207 true
208 ],
209 ];
210 }
211
216 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
217 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
218
219 $content = $this->newContent( $text );
220
221 $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
222
223 $this->assertEquals(
224 $expected,
225 $v,
226 'isCountable() returned unexpected value ' . var_export( $v, true )
227 . ' instead of ' . var_export( $expected, true )
228 . " in mode `$mode` for text \"$text\""
229 );
230 }
231
232 public static function dataGetTextForSummary() {
233 return [
234 [ "hello\nworld.",
235 16,
236 'hello world.',
237 ],
238 [ 'hello world.',
239 8,
240 'hello...',
241 ],
242 [ '[[hello world]].',
243 8,
244 '[[hel...',
245 ],
246 ];
247 }
248
253 public function testGetTextForSummary( $text, $maxlength, $expected ) {
254 $content = $this->newContent( $text );
255
256 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
257 }
258
262 public function testGetTextForSearchIndex() {
263 $content = $this->newContent( 'hello world.' );
264
265 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
266 }
267
271 public function testCopy() {
272 $content = $this->newContent( 'hello world.' );
273 $copy = $content->copy();
274
275 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
276 $this->assertEquals( 'hello world.', $copy->getText() );
277 }
278
282 public function testGetSize() {
283 $content = $this->newContent( 'hello world.' );
284
285 $this->assertEquals( 12, $content->getSize() );
286 }
287
291 public function testGetText() {
292 $content = $this->newContent( 'hello world.' );
293
294 $this->assertEquals( 'hello world.', $content->getText() );
295 }
296
300 public function testGetNativeData() {
301 $content = $this->newContent( 'hello world.' );
302
303 $this->assertEquals( 'hello world.', $content->getText() );
304 }
305
310 $content = $this->newContent( 'hello world.' );
311
312 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
313 }
314
318 public function testGetModel() {
319 $content = $this->newContent( "hello world." );
320
321 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
322 }
323
327 public function testGetContentHandler() {
328 $content = $this->newContent( "hello world." );
329
330 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
331 }
332
333 public static function dataIsEmpty() {
334 return [
335 [ '', true ],
336 [ ' ', false ],
337 [ '0', false ],
338 [ 'hallo welt.', false ],
339 ];
340 }
341
346 public function testIsEmpty( $text, $empty ) {
347 $content = $this->newContent( $text );
348
349 $this->assertEquals( $empty, $content->isEmpty() );
350 }
351
352 public static function dataEquals() {
353 return [
354 [ new TextContent( "hallo" ), null, false ],
355 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
356 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
357 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
358 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
359 ];
360 }
361
366 public function testEquals( Content $a, Content $b = null, $equal = false ) {
367 $this->assertEquals( $equal, $a->equals( $b ) );
368 }
369
370 public static function dataGetDeletionUpdates() {
371 return [
372 [
373 CONTENT_MODEL_TEXT, "hello ''world''\n",
374 []
375 ],
376 [
377 CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
378 []
379 ],
380 // TODO: more...?
381 ];
382 }
383
388 public function testDeletionUpdates( $model, $text, $expectedStuff ) {
389 $page = $this->getNonexistingTestPage( get_class( $this ) . '-' . $this->getName() );
390 $title = $page->getTitle();
391
392 $content = ContentHandler::makeContent( $text, $title, $model );
393 $page->doEditContent( $content, '' );
394
395 $updates = $content->getDeletionUpdates( $page );
396
397 // make updates accessible by class name
398 foreach ( $updates as $update ) {
399 $class = get_class( $update );
400 $updates[$class] = $update;
401 }
402
403 foreach ( $expectedStuff as $class => $fieldValues ) {
404 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
405
406 $update = $updates[$class];
407
408 foreach ( $fieldValues as $field => $value ) {
409 $v = $update->$field; # if the field doesn't exist, just crash and burn
410 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
411 }
412 }
413
414 // make phpunit happy even if $expectedStuff was empty
415 $this->assertTrue( true );
416 }
417
418 public static function provideConvert() {
419 return [
420 [ // #0
421 'Hallo Welt',
422 CONTENT_MODEL_WIKITEXT,
423 'lossless',
424 'Hallo Welt'
425 ],
426 [ // #1
427 'Hallo Welt',
428 CONTENT_MODEL_WIKITEXT,
429 'lossless',
430 'Hallo Welt'
431 ],
432 [ // #1
433 'Hallo Welt',
434 CONTENT_MODEL_CSS,
435 'lossless',
436 'Hallo Welt'
437 ],
438 [ // #1
439 'Hallo Welt',
440 CONTENT_MODEL_JAVASCRIPT,
441 'lossless',
442 'Hallo Welt'
443 ],
444 ];
445 }
446
451 public function testConvert( $text, $model, $lossy, $expectedNative ) {
452 $content = $this->newContent( $text );
453
455 $converted = $content->convert( $model, $lossy );
456
457 if ( $expectedNative === false ) {
458 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
459 } else {
460 $this->assertInstanceOf( Content::class, $converted );
461 $this->assertEquals( $expectedNative, $converted->getText() );
462 }
463 }
464
469 public function testNormalizeLineEndings( $input, $expected ) {
470 $this->assertEquals( $expected, TextContent::normalizeLineEndings( $input ) );
471 }
472
473 public static function provideNormalizeLineEndings() {
474 return [
475 [
476 "Foo\r\nbar",
477 "Foo\nbar"
478 ],
479 [
480 "Foo\rbar",
481 "Foo\nbar"
482 ],
483 [
484 "Foobar\n ",
485 "Foobar"
486 ]
487 ];
488 }
489
494 public function testSerialize() {
495 $cnt = $this->newContent( 'testing text' );
496
497 $this->assertSame( 'testing text', $cnt->serialize() );
498 }
499
500}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
WebRequest clone which takes values from a provided array.
Content for JavaScript pages.
static destroySingleton()
Destroy the current singleton instance.
Definition MWTidy.php:93
Base class that store and restore the Language objects.
getNonexistingTestPage( $title=null)
Returns a WikiPage representing a non-existing page.
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.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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.
testGetText()
TextContent::getText.
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.
testDeletionUpdates( $model, $text, $expectedStuff)
dataGetDeletionUpdates TextContent::getDeletionUpdates
testIsEmpty( $text, $empty)
dataIsEmpty TextContent::isEmpty
static dataGetRedirectTarget()
testIsRedirect( $text, $expected)
dataGetRedirectTarget TextContent::isRedirect
testGetRedirectTarget( $text, $expected)
dataGetRedirectTarget TextContent::getRedirectTarget
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:48
Content object for wiki text pages.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const CONTENT_MODEL_CSS
Definition Defines.php:246
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:244
const CONTENT_MODEL_TEXT
Definition Defines.php:247
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:245
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:1999
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
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:2004
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:2011
return true to allow those checks to and false if checking is done & $user
Definition hooks.txt:1510
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Base interface for content objects.
Definition Content.php:34
equals(Content $that=null)
Returns true if this Content objects is conceptually equivalent to the given Content object.
$content
$f
Definition router.php:79