MediaWiki REL1_27
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 ) {
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 }
107
112 public function testPreSaveTransform( $text, $expected ) {
114
115 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
116
117 $content = $this->newContent( $text );
118 $content = $content->preSaveTransform(
119 $this->context->getTitle(),
120 $this->context->getUser(),
122 );
123
124 $this->assertEquals( $expected, $content->getNativeData() );
125 }
126
127 public static function dataPreloadTransform() {
128 return [
129 [
130 'hello this is ~~~',
131 'hello this is ~~~',
132 ],
133 ];
134 }
135
140 public function testPreloadTransform( $text, $expected ) {
142 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
143
144 $content = $this->newContent( $text );
145 $content = $content->preloadTransform( $this->context->getTitle(), $options );
146
147 $this->assertEquals( $expected, $content->getNativeData() );
148 }
149
150 public static function dataGetRedirectTarget() {
151 return [
152 [ '#REDIRECT [[Test]]',
153 null,
154 ],
155 ];
156 }
157
162 public function testGetRedirectTarget( $text, $expected ) {
163 $content = $this->newContent( $text );
164 $t = $content->getRedirectTarget();
165
166 if ( is_null( $expected ) ) {
167 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
168 } else {
169 $this->assertEquals( $expected, $t->getPrefixedText() );
170 }
171 }
172
177 public function testIsRedirect( $text, $expected ) {
178 $content = $this->newContent( $text );
179
180 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
181 }
182
183 public static function dataIsCountable() {
184 return [
185 [ '',
186 null,
187 'any',
188 true
189 ],
190 [ 'Foo',
191 null,
192 'any',
193 true
194 ],
195 [ 'Foo',
196 null,
197 'comma',
198 false
199 ],
200 [ 'Foo, bar',
201 null,
202 'comma',
203 false
204 ],
205 ];
206 }
207
213 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
214 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
215
216 $content = $this->newContent( $text );
217
218 $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
219
220 $this->assertEquals(
221 $expected,
222 $v,
223 'isCountable() returned unexpected value ' . var_export( $v, true )
224 . ' instead of ' . var_export( $expected, true )
225 . " in mode `$mode` for text \"$text\""
226 );
227 }
228
229 public static function dataGetTextForSummary() {
230 return [
231 [ "hello\nworld.",
232 16,
233 'hello world.',
234 ],
235 [ 'hello world.',
236 8,
237 'hello...',
238 ],
239 [ '[[hello world]].',
240 8,
241 '[[hel...',
242 ],
243 ];
244 }
245
250 public function testGetTextForSummary( $text, $maxlength, $expected ) {
251 $content = $this->newContent( $text );
252
253 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
254 }
255
259 public function testGetTextForSearchIndex() {
260 $content = $this->newContent( 'hello world.' );
261
262 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
263 }
264
268 public function testCopy() {
269 $content = $this->newContent( 'hello world.' );
270 $copy = $content->copy();
271
272 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
273 $this->assertEquals( 'hello world.', $copy->getNativeData() );
274 }
275
279 public function testGetSize() {
280 $content = $this->newContent( 'hello world.' );
281
282 $this->assertEquals( 12, $content->getSize() );
283 }
284
288 public function testGetNativeData() {
289 $content = $this->newContent( 'hello world.' );
290
291 $this->assertEquals( 'hello world.', $content->getNativeData() );
292 }
293
298 $content = $this->newContent( 'hello world.' );
299
300 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
301 }
302
306 public function testGetModel() {
307 $content = $this->newContent( "hello world." );
308
309 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
310 }
311
315 public function testGetContentHandler() {
316 $content = $this->newContent( "hello world." );
317
318 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
319 }
320
321 public static function dataIsEmpty() {
322 return [
323 [ '', true ],
324 [ ' ', false ],
325 [ '0', false ],
326 [ 'hallo welt.', false ],
327 ];
328 }
329
334 public function testIsEmpty( $text, $empty ) {
335 $content = $this->newContent( $text );
336
337 $this->assertEquals( $empty, $content->isEmpty() );
338 }
339
340 public static function dataEquals() {
341 return [
342 [ new TextContent( "hallo" ), null, false ],
343 [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
344 [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
345 [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
346 [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
347 ];
348 }
349
354 public function testEquals( Content $a, Content $b = null, $equal = false ) {
355 $this->assertEquals( $equal, $a->equals( $b ) );
356 }
357
358 public static function dataGetDeletionUpdates() {
359 return [
360 [ "TextContentTest_testGetSecondaryDataUpdates_1",
361 CONTENT_MODEL_TEXT, "hello ''world''\n",
362 []
363 ],
364 [ "TextContentTest_testGetSecondaryDataUpdates_2",
365 CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
366 []
367 ],
368 // TODO: more...?
369 ];
370 }
371
376 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
377 $ns = $this->getDefaultWikitextNS();
379
380 $content = ContentHandler::makeContent( $text, $title, $model );
381
383 $page->doEditContent( $content, '' );
384
385 $updates = $content->getDeletionUpdates( $page );
386
387 // make updates accessible by class name
388 foreach ( $updates as $update ) {
389 $class = get_class( $update );
390 $updates[$class] = $update;
391 }
392
393 if ( !$expectedStuff ) {
394 $this->assertTrue( true ); // make phpunit happy
395 return;
396 }
397
398 foreach ( $expectedStuff as $class => $fieldValues ) {
399 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
400
401 $update = $updates[$class];
402
403 foreach ( $fieldValues as $field => $value ) {
404 $v = $update->$field; # if the field doesn't exist, just crash and burn
405 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
406 }
407 }
408
409 $page->doDeleteArticle( '' );
410 }
411
412 public static function provideConvert() {
413 return [
414 [ // #0
415 'Hallo Welt',
416 CONTENT_MODEL_WIKITEXT,
417 'lossless',
418 'Hallo Welt'
419 ],
420 [ // #1
421 'Hallo Welt',
422 CONTENT_MODEL_WIKITEXT,
423 'lossless',
424 'Hallo Welt'
425 ],
426 [ // #1
427 'Hallo Welt',
428 CONTENT_MODEL_CSS,
429 'lossless',
430 'Hallo Welt'
431 ],
432 [ // #1
433 'Hallo Welt',
434 CONTENT_MODEL_JAVASCRIPT,
435 'lossless',
436 'Hallo Welt'
437 ],
438 ];
439 }
440
445 public function testConvert( $text, $model, $lossy, $expectedNative ) {
446 $content = $this->newContent( $text );
447
448 $converted = $content->convert( $model, $lossy );
449
450 if ( $expectedNative === false ) {
451 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
452 } else {
453 $this->assertInstanceOf( 'Content', $converted );
454 $this->assertEquals( $expectedNative, $converted->getNativeData() );
455 }
456 }
457}
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
WebRequest clone which takes values from a provided array.
Content for JavaScript pages.
static destroySingleton()
Destroy the current singleton instance.
Definition MWTidy.php:153
Base class that store and restore the Language objects.
getDefaultWikitextNS()
Returns the ID of a namespace that defaults to Wikitext.
setMwGlobals( $pairs, $value=null)
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
static newFromUserAndLang(User $user, Language $lang)
Get a ParserOptions object from a given user and language.
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 Database 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.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition Title.php:277
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:99
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
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
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:280
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:278
const CONTENT_MODEL_TEXT
Definition Defines.php:281
const CONTENT_MODEL_JAVASCRIPT
Definition Defines.php:279
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:249
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition hooks.txt:2379
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition hooks.txt:1042
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition hooks.txt:1040
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:944
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:1811
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:1818
processing should stop and the error should be shown to the user * false
Definition hooks.txt:189
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.
#define the
table suitable for use with IDatabase::select()