MediaWiki  1.33.0
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  ) {
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(),
135  $options
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 
309  public function testGetWikitextForTransclusion() {
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 }
TextContentTest\dataGetTextForSummary
static dataGetTextForSummary()
Definition: TextContentTest.php:232
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
TextContentTest\testGetTextForSummary
testGetTextForSummary( $text, $maxlength, $expected)
dataGetTextForSummary TextContent::getTextForSummary
Definition: TextContentTest.php:253
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:306
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:2070
TextContentTest\testPreSaveTransform
testPreSaveTransform( $text, $expected)
dataPreSaveTransform TextContent::preSaveTransform
Definition: TextContentTest.php:127
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
TextContentTest\testPreloadTransform
testPreloadTransform( $text, $expected)
dataPreloadTransform TextContent::preloadTransform
Definition: TextContentTest.php:154
TextContentTest\dataGetRedirectTarget
static dataGetRedirectTarget()
Definition: TextContentTest.php:164
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:237
TextContentTest\setUp
setUp()
Definition: TextContentTest.php:13
TextContentTest
ContentHandler Database ^— needed, because we do need the database to test link updates.
Definition: TextContentTest.php:10
TextContentTest\testIsCountable
testIsCountable( $text, $hasLinks, $mode, $expected)
dataIsCountable TextContent::isCountable
Definition: TextContentTest.php:216
TextContentTest\testGetNativeData
testGetNativeData()
TextContent::getNativeData.
Definition: TextContentTest.php:300
User
User
Definition: All_system_messages.txt:425
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:235
TextContentTest\testGetText
testGetText()
TextContent::getText.
Definition: TextContentTest.php:291
TextContentTest\dataGetParserOutput
static dataGetParserOutput()
Definition: TextContentTest.php:56
php
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:35
TextContentTest\testEquals
testEquals(Content $a, Content $b=null, $equal=false)
dataEquals TextContent::equals
Definition: TextContentTest.php:366
TextContentTest\dataEquals
static dataEquals()
Definition: TextContentTest.php:352
MediaWikiTestCase\getNonexistingTestPage
getNonexistingTestPage( $title=null)
Returns a WikiPage representing a non-existing page.
Definition: MediaWikiTestCase.php:256
$html
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:1985
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
ParserOptions\newFromUserAndLang
static newFromUserAndLang(User $user, Language $lang)
Get a ParserOptions object from a given user and language.
Definition: ParserOptions.php:1031
TextContentTest\testGetParserOutput
testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields=null)
dataGetParserOutput TextContent::getParserOutput
Definition: TextContentTest.php:74
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
TextContentTest\$context
$context
Definition: TextContentTest.php:11
TextContentTest\testGetModel
testGetModel()
TextContent::getModel.
Definition: TextContentTest.php:318
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
TextContentTest\newContent
newContent( $text)
Definition: TextContentTest.php:52
TextContentTest\testGetTextForSearchIndex
testGetTextForSearchIndex()
TextContent::getTextForSearchIndex.
Definition: TextContentTest.php:262
TextContentTest\dataPreloadTransform
static dataPreloadTransform()
Definition: TextContentTest.php:141
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:32
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:36
TextContentTest\dataIsEmpty
static dataIsEmpty()
Definition: TextContentTest.php:333
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:133
JavaScriptContent
Content for JavaScript pages.
Definition: JavaScriptContent.php:33
TextContentTest\testIsEmpty
testIsEmpty( $text, $empty)
dataIsEmpty TextContent::isEmpty
Definition: TextContentTest.php:346
Content\equals
equals(Content $that=null)
Returns true if this Content objects is conceptually equivalent to the given Content object.
TextContentTest\testGetRedirectTarget
testGetRedirectTarget( $text, $expected)
dataGetRedirectTarget TextContent::getRedirectTarget
Definition: TextContentTest.php:176
$value
$value
Definition: styleTest.css.php:49
TextContentTest\dataPreSaveTransform
static dataPreSaveTransform()
Definition: TextContentTest.php:103
TextContentTest\dataIsCountable
static dataIsCountable()
Definition: TextContentTest.php:197
TextContentTest\tearDown
tearDown()
Definition: TextContentTest.php:43
TextContentTest\testGetSize
testGetSize()
TextContent::getSize.
Definition: TextContentTest.php:282
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:8
TextContentTest\testDeletionUpdates
testDeletionUpdates( $model, $text, $expectedStuff)
dataGetDeletionUpdates TextContent::getDeletionUpdates
Definition: TextContentTest.php:388
TextContentTest\dataGetDeletionUpdates
static dataGetDeletionUpdates()
Definition: TextContentTest.php:370
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:37
Content
Base interface for content objects.
Definition: Content.php:34
text
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
Definition: All_system_messages.txt:1267
$options
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:1985
TextContentTest\testGetWikitextForTransclusion
testGetWikitextForTransclusion()
TextContent::getWikitextForTransclusion.
Definition: TextContentTest.php:309
as
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
Definition: distributors.txt:9
true
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:1985
$content
$content
Definition: pageupdater.txt:72
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:236
$f
$f
Definition: router.php:79
$t
$t
Definition: testCompression.php:69
MediaWikiServices
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 MediaWikiServices
Definition: injection.txt:23
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:238
TextContentTest\testCopy
testCopy()
TextContent::copy.
Definition: TextContentTest.php:271
MWTidy\destroySingleton
static destroySingleton()
Destroy the current singleton instance.
Definition: MWTidy.php:93
TextContentTest\testGetContentHandler
testGetContentHandler()
TextContent::getContentHandler.
Definition: TextContentTest.php:327
TextContentTest\testIsRedirect
testIsRedirect( $text, $expected)
dataGetRedirectTarget TextContent::isRedirect
Definition: TextContentTest.php:191