MediaWiki  1.30.0
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  ) {
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 ) {
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(),
126  $options
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 ) {
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  [ 'Foo',
201  null,
202  'comma',
203  false
204  ],
205  [ 'Foo, bar',
206  null,
207  'comma',
208  false
209  ],
210  ];
211  }
212 
218  public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
219  $this->setMwGlobals( 'wgArticleCountMethod', $mode );
220 
221  $content = $this->newContent( $text );
222 
223  $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
224 
225  $this->assertEquals(
226  $expected,
227  $v,
228  'isCountable() returned unexpected value ' . var_export( $v, true )
229  . ' instead of ' . var_export( $expected, true )
230  . " in mode `$mode` for text \"$text\""
231  );
232  }
233 
234  public static function dataGetTextForSummary() {
235  return [
236  [ "hello\nworld.",
237  16,
238  'hello world.',
239  ],
240  [ 'hello world.',
241  8,
242  'hello...',
243  ],
244  [ '[[hello world]].',
245  8,
246  '[[hel...',
247  ],
248  ];
249  }
250 
255  public function testGetTextForSummary( $text, $maxlength, $expected ) {
256  $content = $this->newContent( $text );
257 
258  $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
259  }
260 
264  public function testGetTextForSearchIndex() {
265  $content = $this->newContent( 'hello world.' );
266 
267  $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
268  }
269 
273  public function testCopy() {
274  $content = $this->newContent( 'hello world.' );
275  $copy = $content->copy();
276 
277  $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
278  $this->assertEquals( 'hello world.', $copy->getNativeData() );
279  }
280 
284  public function testGetSize() {
285  $content = $this->newContent( 'hello world.' );
286 
287  $this->assertEquals( 12, $content->getSize() );
288  }
289 
293  public function testGetNativeData() {
294  $content = $this->newContent( 'hello world.' );
295 
296  $this->assertEquals( 'hello world.', $content->getNativeData() );
297  }
298 
302  public function testGetWikitextForTransclusion() {
303  $content = $this->newContent( 'hello world.' );
304 
305  $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
306  }
307 
311  public function testGetModel() {
312  $content = $this->newContent( "hello world." );
313 
314  $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
315  }
316 
320  public function testGetContentHandler() {
321  $content = $this->newContent( "hello world." );
322 
323  $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
324  }
325 
326  public static function dataIsEmpty() {
327  return [
328  [ '', true ],
329  [ ' ', false ],
330  [ '0', false ],
331  [ 'hallo welt.', false ],
332  ];
333  }
334 
339  public function testIsEmpty( $text, $empty ) {
340  $content = $this->newContent( $text );
341 
342  $this->assertEquals( $empty, $content->isEmpty() );
343  }
344 
345  public static function dataEquals() {
346  return [
347  [ new TextContent( "hallo" ), null, false ],
348  [ new TextContent( "hallo" ), new TextContent( "hallo" ), true ],
349  [ new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ],
350  [ new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ],
351  [ new TextContent( "hallo" ), new TextContent( "HALLO" ), false ],
352  ];
353  }
354 
359  public function testEquals( Content $a, Content $b = null, $equal = false ) {
360  $this->assertEquals( $equal, $a->equals( $b ) );
361  }
362 
363  public static function dataGetDeletionUpdates() {
364  return [
365  [ "TextContentTest_testGetSecondaryDataUpdates_1",
366  CONTENT_MODEL_TEXT, "hello ''world''\n",
367  []
368  ],
369  [ "TextContentTest_testGetSecondaryDataUpdates_2",
370  CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
371  []
372  ],
373  // TODO: more...?
374  ];
375  }
376 
381  public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
382  $ns = $this->getDefaultWikitextNS();
383  $title = Title::newFromText( $title, $ns );
384 
385  $content = ContentHandler::makeContent( $text, $title, $model );
386 
387  $page = WikiPage::factory( $title );
388  $page->doEditContent( $content, '' );
389 
390  $updates = $content->getDeletionUpdates( $page );
391 
392  // make updates accessible by class name
393  foreach ( $updates as $update ) {
394  $class = get_class( $update );
395  $updates[$class] = $update;
396  }
397 
398  if ( !$expectedStuff ) {
399  $this->assertTrue( true ); // make phpunit happy
400  return;
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  $page->doDeleteArticle( '' );
415  }
416 
417  public static function provideConvert() {
418  return [
419  [ // #0
420  'Hallo Welt',
421  CONTENT_MODEL_WIKITEXT,
422  'lossless',
423  'Hallo Welt'
424  ],
425  [ // #1
426  'Hallo Welt',
427  CONTENT_MODEL_WIKITEXT,
428  'lossless',
429  'Hallo Welt'
430  ],
431  [ // #1
432  'Hallo Welt',
433  CONTENT_MODEL_CSS,
434  'lossless',
435  'Hallo Welt'
436  ],
437  [ // #1
438  'Hallo Welt',
439  CONTENT_MODEL_JAVASCRIPT,
440  'lossless',
441  'Hallo Welt'
442  ],
443  ];
444  }
445 
450  public function testConvert( $text, $model, $lossy, $expectedNative ) {
451  $content = $this->newContent( $text );
452 
453  $converted = $content->convert( $model, $lossy );
454 
455  if ( $expectedNative === false ) {
456  $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
457  } else {
458  $this->assertInstanceOf( 'Content', $converted );
459  $this->assertEquals( $expectedNative, $converted->getNativeData() );
460  }
461  }
462 
467  public function testNormalizeLineEndings( $input, $expected ) {
468  $this->assertEquals( $expected, TextContent::normalizeLineEndings( $input ) );
469  }
470 
471  public static function provideNormalizeLineEndings() {
472  return [
473  [
474  "Foo\r\nbar",
475  "Foo\nbar"
476  ],
477  [
478  "Foo\rbar",
479  "Foo\nbar"
480  ],
481  [
482  "Foobar\n ",
483  "Foobar"
484  ]
485  ];
486  }
487 
488 }
TextContentTest\dataGetTextForSummary
static dataGetTextForSummary()
Definition: TextContentTest.php:234
$user
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 account $user
Definition: hooks.txt:244
TextContentTest\testGetTextForSummary
testGetTextForSummary( $text, $maxlength, $expected)
dataGetTextForSummary TextContent::getTextForSummary
Definition: TextContentTest.php:255
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:268
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:1552
TextContentTest\testPreSaveTransform
testPreSaveTransform( $text, $expected)
dataPreSaveTransform TextContent::preSaveTransform
Definition: TextContentTest.php:117
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:145
TextContentTest\dataGetRedirectTarget
static dataGetRedirectTarget()
Definition: TextContentTest.php:155
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:238
TextContentTest\setUp
setUp()
Definition: TextContentTest.php:11
TextContentTest
ContentHandler Database ^— needed, because we do need the database to test link updates.
Definition: TextContentTest.php:8
TextContentTest\testIsCountable
testIsCountable( $text, $hasLinks, $mode, $expected)
dataIsCountable Database TextContent::isCountable
Definition: TextContentTest.php:218
TextContentTest\testGetNativeData
testGetNativeData()
TextContent::getNativeData.
Definition: TextContentTest.php:293
User
User
Definition: All_system_messages.txt:425
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:236
TextContentTest\dataGetParserOutput
static dataGetParserOutput()
Definition: TextContentTest.php:46
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:359
TextContentTest\dataEquals
static dataEquals()
Definition: TextContentTest.php:345
$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:1965
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:121
ParserOptions\newFromUserAndLang
static newFromUserAndLang(User $user, Language $lang)
Get a ParserOptions object from a given user and language.
Definition: ParserOptions.php:992
TextContentTest\testGetParserOutput
testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields=null)
dataGetParserOutput TextContent::getParserOutput
Definition: TextContentTest.php:64
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:672
TextContentTest\$context
$context
Definition: TextContentTest.php:9
TextContentTest\testGetModel
testGetModel()
TextContent::getModel.
Definition: TextContentTest.php:311
TextContentTest\newContent
newContent( $text)
Definition: TextContentTest.php:42
TextContentTest\testGetTextForSearchIndex
testGetTextForSearchIndex()
TextContent::getTextForSearchIndex.
Definition: TextContentTest.php:264
MediaWikiTestCase\getDefaultWikitextNS
getDefaultWikitextNS()
Returns the ID of a namespace that defaults to Wikitext.
Definition: MediaWikiTestCase.php:1693
TextContentTest\dataPreloadTransform
static dataPreloadTransform()
Definition: TextContentTest.php:132
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
RequestContext
Group all the pieces relevant to the context of a request into one instance.
Definition: RequestContext.php:33
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:33
TextContentTest\dataIsEmpty
static dataIsEmpty()
Definition: TextContentTest.php:326
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:129
JavaScriptContent
Content for JavaScript pages.
Definition: JavaScriptContent.php:33
TextContentTest\testIsEmpty
testIsEmpty( $text, $empty)
dataIsEmpty TextContent::isEmpty
Definition: TextContentTest.php:339
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:167
$value
$value
Definition: styleTest.css.php:45
TextContentTest\dataPreSaveTransform
static dataPreSaveTransform()
Definition: TextContentTest.php:93
TextContentTest\dataIsCountable
static dataIsCountable()
Definition: TextContentTest.php:188
TextContentTest\tearDown
tearDown()
Definition: TextContentTest.php:37
TextContentTest\testGetSize
testGetSize()
TextContent::getSize.
Definition: TextContentTest.php:284
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
TextContentTest\dataGetDeletionUpdates
static dataGetDeletionUpdates()
Definition: TextContentTest.php:363
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:35
Content
Base interface for content objects.
Definition: Content.php:34
$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:1965
TextContentTest\testGetWikitextForTransclusion
testGetWikitextForTransclusion()
TextContent::getWikitextForTransclusion.
Definition: TextContentTest.php:302
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:1965
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:237
$t
$t
Definition: testCompression.php:67
TextContentTest\testDeletionUpdates
testDeletionUpdates( $title, $model, $text, $expectedStuff)
dataGetDeletionUpdates TextContent::getDeletionUpdates
Definition: TextContentTest.php:381
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:239
TextContentTest\testCopy
testCopy()
TextContent::copy.
Definition: TextContentTest.php:273
MWTidy\destroySingleton
static destroySingleton()
Destroy the current singleton instance.
Definition: MWTidy.php:163
TextContentTest\testGetContentHandler
testGetContentHandler()
TextContent::getContentHandler.
Definition: TextContentTest.php:320
TextContentTest\testIsRedirect
testIsRedirect( $text, $expected)
dataGetRedirectTarget TextContent::isRedirect
Definition: TextContentTest.php:182
$wgContLang
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 content language as $wgContLang
Definition: design.txt:56