MediaWiki REL1_32
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
48 public function newContent( $text ) {
49 return new TextContent( $text );
50 }
51
52 public static function dataGetParserOutput() {
53 return [
54 [
55 'TextContentTest_testGetParserOutput',
57 "hello ''world'' & [[stuff]]\n", "hello ''world'' &amp; [[stuff]]",
58 [
59 'Links' => []
60 ]
61 ],
62 // TODO: more...?
63 ];
64 }
65
70 public function testGetParserOutput( $title, $model, $text, $expectedHtml,
71 $expectedFields = null
72 ) {
73 $title = Title::newFromText( $title );
74 $content = ContentHandler::makeContent( $text, $title, $model );
75
76 $po = $content->getParserOutput( $title );
77
78 $html = $po->getText();
79 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
80
81 $this->assertEquals( $expectedHtml, trim( $html ) );
82
83 if ( $expectedFields ) {
84 foreach ( $expectedFields as $field => $exp ) {
85 $f = 'get' . ucfirst( $field );
86 $v = call_user_func( [ $po, $f ] );
87
88 if ( is_array( $exp ) ) {
89 $this->assertArrayEquals( $exp, $v );
90 } else {
91 $this->assertEquals( $exp, $v );
92 }
93 }
94 }
95
96 // TODO: assert more properties
97 }
98
99 public static function dataPreSaveTransform() {
100 return [
101 [
102 # 0: no signature resolution
103 'hello this is ~~~',
104 'hello this is ~~~',
105 ],
106 [
107 # 1: rtrim
108 " Foo \n ",
109 ' Foo',
110 ],
111 [
112 # 2: newline normalization
113 "LF\n\nCRLF\r\n\r\nCR\r\rEND",
114 "LF\n\nCRLF\n\nCR\n\nEND",
115 ],
116 ];
117 }
118
123 public function testPreSaveTransform( $text, $expected ) {
124 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(),
125 MediaWikiServices::getInstance()->getContentLanguage() );
126
127 $content = $this->newContent( $text );
128 $content = $content->preSaveTransform(
129 $this->context->getTitle(),
130 $this->context->getUser(),
132 );
133
134 $this->assertEquals( $expected, $content->getNativeData() );
135 }
136
137 public static function dataPreloadTransform() {
138 return [
139 [
140 'hello this is ~~~',
141 'hello this is ~~~',
142 ],
143 ];
144 }
145
150 public function testPreloadTransform( $text, $expected ) {
151 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(),
152 MediaWikiServices::getInstance()->getContentLanguage() );
153
154 $content = $this->newContent( $text );
155 $content = $content->preloadTransform( $this->context->getTitle(), $options );
156
157 $this->assertEquals( $expected, $content->getNativeData() );
158 }
159
160 public static function dataGetRedirectTarget() {
161 return [
162 [ '#REDIRECT [[Test]]',
163 null,
164 ],
165 ];
166 }
167
172 public function testGetRedirectTarget( $text, $expected ) {
173 $content = $this->newContent( $text );
174 $t = $content->getRedirectTarget();
175
176 if ( is_null( $expected ) ) {
177 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
178 } else {
179 $this->assertEquals( $expected, $t->getPrefixedText() );
180 }
181 }
182
187 public function testIsRedirect( $text, $expected ) {
188 $content = $this->newContent( $text );
189
190 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
191 }
192
193 public static function dataIsCountable() {
194 return [
195 [ '',
196 null,
197 'any',
198 true
199 ],
200 [ 'Foo',
201 null,
202 'any',
203 true
204 ],
205 ];
206 }
207
212 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
213 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
214
215 $content = $this->newContent( $text );
216
217 $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
218
219 $this->assertEquals(
220 $expected,
221 $v,
222 'isCountable() returned unexpected value ' . var_export( $v, true )
223 . ' instead of ' . var_export( $expected, true )
224 . " in mode `$mode` for text \"$text\""
225 );
226 }
227
228 public static function dataGetTextForSummary() {
229 return [
230 [ "hello\nworld.",
231 16,
232 'hello world.',
233 ],
234 [ 'hello world.',
235 8,
236 'hello...',
237 ],
238 [ '[[hello world]].',
239 8,
240 '[[hel...',
241 ],
242 ];
243 }
244
249 public function testGetTextForSummary( $text, $maxlength, $expected ) {
250 $content = $this->newContent( $text );
251
252 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
253 }
254
258 public function testGetTextForSearchIndex() {
259 $content = $this->newContent( 'hello world.' );
260
261 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
262 }
263
267 public function testCopy() {
268 $content = $this->newContent( 'hello world.' );
269 $copy = $content->copy();
270
271 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
272 $this->assertEquals( 'hello world.', $copy->getNativeData() );
273 }
274
278 public function testGetSize() {
279 $content = $this->newContent( 'hello world.' );
280
281 $this->assertEquals( 12, $content->getSize() );
282 }
283
287 public function testGetNativeData() {
288 $content = $this->newContent( 'hello world.' );
289
290 $this->assertEquals( 'hello world.', $content->getNativeData() );
291 }
292
297 $content = $this->newContent( 'hello world.' );
298
299 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
300 }
301
305 public function testGetModel() {
306 $content = $this->newContent( "hello world." );
307
308 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
309 }
310
314 public function testGetContentHandler() {
315 $content = $this->newContent( "hello world." );
316
317 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
318 }
319
320 public static function dataIsEmpty() {
321 return [
322 [ '', true ],
323 [ ' ', false ],
324 [ '0', false ],
325 [ 'hallo welt.', false ],
326 ];
327 }
328
333 public function testIsEmpty( $text, $empty ) {
334 $content = $this->newContent( $text );
335
336 $this->assertEquals( $empty, $content->isEmpty() );
337 }
338
339 public static function dataEquals() {
340 return [
341 [ new TextContent( "hallo" ), null, false ],
342 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
343 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
344 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
345 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
346 ];
347 }
348
353 public function testEquals( Content $a, Content $b = null, $equal = false ) {
354 $this->assertEquals( $equal, $a->equals( $b ) );
355 }
356
357 public static function dataGetDeletionUpdates() {
358 return [
359 [
360 CONTENT_MODEL_TEXT, "hello ''world''\n",
361 []
362 ],
363 [
364 CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
365 []
366 ],
367 // TODO: more...?
368 ];
369 }
370
375 public function testDeletionUpdates( $model, $text, $expectedStuff ) {
376 $page = $this->getNonexistingTestPage( get_class( $this ) . '-' . $this->getName() );
377 $title = $page->getTitle();
378
379 $content = ContentHandler::makeContent( $text, $title, $model );
380 $page->doEditContent( $content, '' );
381
382 $updates = $content->getDeletionUpdates( $page );
383
384 // make updates accessible by class name
385 foreach ( $updates as $update ) {
386 $class = get_class( $update );
387 $updates[$class] = $update;
388 }
389
390 foreach ( $expectedStuff as $class => $fieldValues ) {
391 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
392
393 $update = $updates[$class];
394
395 foreach ( $fieldValues as $field => $value ) {
396 $v = $update->$field; # if the field doesn't exist, just crash and burn
397 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
398 }
399 }
400
401 // make phpunit happy even if $expectedStuff was empty
402 $this->assertTrue( true );
403 }
404
405 public static function provideConvert() {
406 return [
407 [ // #0
408 'Hallo Welt',
409 CONTENT_MODEL_WIKITEXT,
410 'lossless',
411 'Hallo Welt'
412 ],
413 [ // #1
414 'Hallo Welt',
415 CONTENT_MODEL_WIKITEXT,
416 'lossless',
417 'Hallo Welt'
418 ],
419 [ // #1
420 'Hallo Welt',
421 CONTENT_MODEL_CSS,
422 'lossless',
423 'Hallo Welt'
424 ],
425 [ // #1
426 'Hallo Welt',
427 CONTENT_MODEL_JAVASCRIPT,
428 'lossless',
429 'Hallo Welt'
430 ],
431 ];
432 }
433
438 public function testConvert( $text, $model, $lossy, $expectedNative ) {
439 $content = $this->newContent( $text );
440
441 $converted = $content->convert( $model, $lossy );
442
443 if ( $expectedNative === false ) {
444 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
445 } else {
446 $this->assertInstanceOf( Content::class, $converted );
447 $this->assertEquals( $expectedNative, $converted->getNativeData() );
448 }
449 }
450
455 public function testNormalizeLineEndings( $input, $expected ) {
456 $this->assertEquals( $expected, TextContent::normalizeLineEndings( $input ) );
457 }
458
459 public static function provideNormalizeLineEndings() {
460 return [
461 [
462 "Foo\r\nbar",
463 "Foo\nbar"
464 ],
465 [
466 "Foo\rbar",
467 "Foo\nbar"
468 ],
469 [
470 "Foobar\n ",
471 "Foobar"
472 ]
473 ];
474 }
475
476}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
WebRequest clone which takes values from a provided array.
Content for JavaScript pages.
static destroySingleton()
Destroy the current singleton instance.
Definition MWTidy.php:139
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.
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:47
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:237
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:235
const CONTENT_MODEL_TEXT
Definition Defines.php:238
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:236
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:2050
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:994
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:2055
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:2062
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:247
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