MediaWiki  1.23.1
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->setMwGlobals( array(
19  'wgUser' => $user,
20  'wgTextModelsToParse' => array(
24  ),
25  'wgUseTidy' => false,
26  'wgAlwaysUseTidy' => false,
27  ) );
28 
29  $this->context = new RequestContext( new FauxRequest() );
30  $this->context->setTitle( Title::newFromText( 'Test' ) );
31  $this->context->setUser( $user );
32  }
33 
34  public function newContent( $text ) {
35  return new TextContent( $text );
36  }
37 
38  public static function dataGetParserOutput() {
39  return array(
40  array(
41  'TextContentTest_testGetParserOutput',
43  "hello ''world'' & [[stuff]]\n", "hello ''world'' &amp; [[stuff]]",
44  array(
45  'Links' => array()
46  )
47  ),
48  // TODO: more...?
49  );
50  }
51 
56  public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
58  $content = ContentHandler::makeContent( $text, $title, $model );
59 
60  $po = $content->getParserOutput( $title );
61 
62  $html = $po->getText();
63  $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
64 
65  $this->assertEquals( $expectedHtml, trim( $html ) );
66 
67  if ( $expectedFields ) {
68  foreach ( $expectedFields as $field => $exp ) {
69  $f = 'get' . ucfirst( $field );
70  $v = call_user_func( array( $po, $f ) );
71 
72  if ( is_array( $exp ) ) {
73  $this->assertArrayEquals( $exp, $v );
74  } else {
75  $this->assertEquals( $exp, $v );
76  }
77  }
78  }
79 
80  // TODO: assert more properties
81  }
82 
83  public static function dataPreSaveTransform() {
84  return array(
85  array(
86  #0: no signature resolution
87  'hello this is ~~~',
88  'hello this is ~~~',
89  ),
90  array(
91  #1: rtrim
92  " Foo \n ",
93  ' Foo',
94  ),
95  );
96  }
97 
102  public function testPreSaveTransform( $text, $expected ) {
104 
105  $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
106 
107  $content = $this->newContent( $text );
108  $content = $content->preSaveTransform( $this->context->getTitle(), $this->context->getUser(), $options );
109 
110  $this->assertEquals( $expected, $content->getNativeData() );
111  }
112 
113  public static function dataPreloadTransform() {
114  return array(
115  array(
116  'hello this is ~~~',
117  'hello this is ~~~',
118  ),
119  );
120  }
121 
126  public function testPreloadTransform( $text, $expected ) {
128  $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
129 
130  $content = $this->newContent( $text );
131  $content = $content->preloadTransform( $this->context->getTitle(), $options );
132 
133  $this->assertEquals( $expected, $content->getNativeData() );
134  }
135 
136  public static function dataGetRedirectTarget() {
137  return array(
138  array( '#REDIRECT [[Test]]',
139  null,
140  ),
141  );
142  }
143 
148  public function testGetRedirectTarget( $text, $expected ) {
149  $content = $this->newContent( $text );
150  $t = $content->getRedirectTarget();
151 
152  if ( is_null( $expected ) ) {
153  $this->assertNull( $t, "text should not have generated a redirect target: $text" );
154  } else {
155  $this->assertEquals( $expected, $t->getPrefixedText() );
156  }
157  }
158 
163  public function testIsRedirect( $text, $expected ) {
164  $content = $this->newContent( $text );
165 
166  $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
167  }
168 
172  /*
173  public function getRedirectChain() {
174  $text = $this->getNativeData();
175  return Title::newFromRedirectArray( $text );
176  }
177  */
178 
182  /*
183  public function getUltimateRedirectTarget() {
184  $text = $this->getNativeData();
185  return Title::newFromRedirectRecurse( $text );
186  }
187  */
188 
189  public static function dataIsCountable() {
190  return array(
191  array( '',
192  null,
193  'any',
194  true
195  ),
196  array( 'Foo',
197  null,
198  'any',
199  true
200  ),
201  array( 'Foo',
202  null,
203  'comma',
204  false
205  ),
206  array( 'Foo, bar',
207  null,
208  'comma',
209  false
210  ),
211  );
212  }
213 
219  public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
220  $this->setMwGlobals( 'wgArticleCountMethod', $mode );
221 
222  $content = $this->newContent( $text );
223 
224  $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
225 
226  $this->assertEquals( $expected, $v, 'isCountable() returned unexpected value ' . var_export( $v, true )
227  . ' instead of ' . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
228  }
229 
230  public static function dataGetTextForSummary() {
231  return array(
232  array( "hello\nworld.",
233  16,
234  'hello world.',
235  ),
236  array( 'hello world.',
237  8,
238  'hello...',
239  ),
240  array( '[[hello world]].',
241  8,
242  '[[hel...',
243  ),
244  );
245  }
246 
251  public function testGetTextForSummary( $text, $maxlength, $expected ) {
252  $content = $this->newContent( $text );
253 
254  $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
255  }
256 
260  public function testGetTextForSearchIndex() {
261  $content = $this->newContent( 'hello world.' );
262 
263  $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
264  }
265 
269  public function testCopy() {
270  $content = $this->newContent( 'hello world.' );
271  $copy = $content->copy();
272 
273  $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
274  $this->assertEquals( 'hello world.', $copy->getNativeData() );
275  }
276 
280  public function testGetSize() {
281  $content = $this->newContent( 'hello world.' );
282 
283  $this->assertEquals( 12, $content->getSize() );
284  }
285 
289  public function testGetNativeData() {
290  $content = $this->newContent( 'hello world.' );
291 
292  $this->assertEquals( 'hello world.', $content->getNativeData() );
293  }
294 
298  public function testGetWikitextForTransclusion() {
299  $content = $this->newContent( 'hello world.' );
300 
301  $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
302  }
303 
307  public function testGetModel() {
308  $content = $this->newContent( "hello world." );
309 
310  $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
311  }
312 
316  public function testGetContentHandler() {
317  $content = $this->newContent( "hello world." );
318 
319  $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
320  }
321 
322  public static function dataIsEmpty() {
323  return array(
324  array( '', true ),
325  array( ' ', false ),
326  array( '0', false ),
327  array( 'hallo welt.', false ),
328  );
329  }
330 
335  public function testIsEmpty( $text, $empty ) {
336  $content = $this->newContent( $text );
337 
338  $this->assertEquals( $empty, $content->isEmpty() );
339  }
340 
341  public static function dataEquals() {
342  return array(
343  array( new TextContent( "hallo" ), null, false ),
344  array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
345  array( new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
346  array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
347  array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
348  );
349  }
350 
355  public function testEquals( Content $a, Content $b = null, $equal = false ) {
356  $this->assertEquals( $equal, $a->equals( $b ) );
357  }
358 
359  public static function dataGetDeletionUpdates() {
360  return array(
361  array( "TextContentTest_testGetSecondaryDataUpdates_1",
362  CONTENT_MODEL_TEXT, "hello ''world''\n",
363  array()
364  ),
365  array( "TextContentTest_testGetSecondaryDataUpdates_2",
366  CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
367  array()
368  ),
369  // TODO: more...?
370  );
371  }
372 
377  public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
378  $ns = $this->getDefaultWikitextNS();
379  $title = Title::newFromText( $title, $ns );
380 
381  $content = ContentHandler::makeContent( $text, $title, $model );
382 
383  $page = WikiPage::factory( $title );
384  $page->doEditContent( $content, '' );
385 
386  $updates = $content->getDeletionUpdates( $page );
387 
388  // make updates accessible by class name
389  foreach ( $updates as $update ) {
390  $class = get_class( $update );
391  $updates[$class] = $update;
392  }
393 
394  if ( !$expectedStuff ) {
395  $this->assertTrue( true ); // make phpunit happy
396  return;
397  }
398 
399  foreach ( $expectedStuff as $class => $fieldValues ) {
400  $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
401 
402  $update = $updates[$class];
403 
404  foreach ( $fieldValues as $field => $value ) {
405  $v = $update->$field; #if the field doesn't exist, just crash and burn
406  $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
407  }
408  }
409 
410  $page->doDeleteArticle( '' );
411  }
412 
413  public static function provideConvert() {
414  return array(
415  array( // #0
416  'Hallo Welt',
417  CONTENT_MODEL_WIKITEXT,
418  'lossless',
419  'Hallo Welt'
420  ),
421  array( // #1
422  'Hallo Welt',
423  CONTENT_MODEL_WIKITEXT,
424  'lossless',
425  'Hallo Welt'
426  ),
427  array( // #1
428  'Hallo Welt',
429  CONTENT_MODEL_CSS,
430  'lossless',
431  'Hallo Welt'
432  ),
433  array( // #1
434  'Hallo Welt',
435  CONTENT_MODEL_JAVASCRIPT,
436  'lossless',
437  'Hallo Welt'
438  ),
439  );
440  }
441 
446  public function testConvert( $text, $model, $lossy, $expectedNative ) {
447  $content = $this->newContent( $text );
448 
449  $converted = $content->convert( $model, $lossy );
450 
451  if ( $expectedNative === false ) {
452  $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
453  } else {
454  $this->assertInstanceOf( 'Content', $converted );
455  $this->assertEquals( $expectedNative, $converted->getNativeData() );
456  }
457  }
458 }
TextContentTest\dataGetTextForSummary
static dataGetTextForSummary()
Definition: TextContentTest.php:230
TextContentTest\testGetTextForSummary
testGetTextForSummary( $text, $maxlength, $expected)
@dataProvider dataGetTextForSummary @covers TextContent::getTextForSummary
Definition: TextContentTest.php:251
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: WebRequest.php:1275
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:189
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:764
TextContentTest\testPreSaveTransform
testPreSaveTransform( $text, $expected)
@dataProvider dataPreSaveTransform @covers TextContent::preSaveTransform
Definition: TextContentTest.php:102
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$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:1530
TextContentTest\testPreloadTransform
testPreloadTransform( $text, $expected)
@dataProvider dataPreloadTransform @covers TextContent::preloadTransform
Definition: TextContentTest.php:126
TextContentTest\dataGetRedirectTarget
static dataGetRedirectTarget()
Definition: TextContentTest.php:136
$f
$f
Definition: UtfNormalTest2.php:38
CONTENT_MODEL_CSS
const CONTENT_MODEL_CSS
Definition: Defines.php:285
TextContentTest\setUp
setUp()
Definition: TextContentTest.php:11
TextContentTest
@group ContentHandler @group Database ^— needed, because we do need the database to test link updates
Definition: TextContentTest.php:8
TextContentTest\testIsCountable
testIsCountable( $text, $hasLinks, $mode, $expected)
@dataProvider dataIsCountable @group Database @covers TextContent::isCountable
Definition: TextContentTest.php:219
TextContentTest\testGetNativeData
testGetNativeData()
@covers TextContent::getNativeData
Definition: TextContentTest.php:289
$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
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:283
TextContentTest\dataGetParserOutput
static dataGetParserOutput()
Definition: TextContentTest.php:38
TextContentTest\testEquals
testEquals(Content $a, Content $b=null, $equal=false)
@dataProvider dataEquals @covers TextContent::equals
Definition: TextContentTest.php:355
TextContentTest\dataEquals
static dataEquals()
Definition: TextContentTest.php:341
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
ParserOptions\newFromUserAndLang
static newFromUserAndLang(User $user, Language $lang)
Get a ParserOptions object from a given user and language.
Definition: ParserOptions.php:386
TextContentTest\testGetParserOutput
testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields=null)
@dataProvider dataGetParserOutput @covers TextContent::getParserOutput
Definition: TextContentTest.php:56
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:302
TextContentTest\$context
$context
Definition: TextContentTest.php:9
TextContentTest\testGetModel
testGetModel()
@covers TextContent::getModel
Definition: TextContentTest.php:307
TextContentTest\newContent
newContent( $text)
Definition: TextContentTest.php:34
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
TextContentTest\testGetTextForSearchIndex
testGetTextForSearchIndex()
@covers TextContent::getTextForSearchIndex
Definition: TextContentTest.php:260
MediaWikiTestCase\getDefaultWikitextNS
getDefaultWikitextNS()
Returns the ID of a namespace that defaults to Wikitext.
Definition: MediaWikiTestCase.php:903
TextContentTest\dataPreloadTransform
static dataPreloadTransform()
Definition: TextContentTest.php:113
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:30
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:33
TextContentTest\dataIsEmpty
static dataIsEmpty()
Definition: TextContentTest.php:322
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:144
JavaScriptContent
Content for JavaScript pages.
Definition: JavaScriptContent.php:33
$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:1530
TextContentTest\testIsEmpty
testIsEmpty( $text, $empty)
@dataProvider dataIsEmpty @covers TextContent::isEmpty
Definition: TextContentTest.php:335
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)
@dataProvider dataGetRedirectTarget @covers TextContent::getRedirectTarget
Definition: TextContentTest.php:148
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
$value
$value
Definition: styleTest.css.php:45
TextContentTest\dataPreSaveTransform
static dataPreSaveTransform()
Definition: TextContentTest.php:83
TextContentTest\dataIsCountable
static dataIsCountable()
Definition: TextContentTest.php:189
TextContentTest\testGetSize
testGetSize()
@covers TextContent::getSize
Definition: TextContentTest.php:280
MediaWikiLangTestCase
Base class that store and restore the Language objects.
Definition: MediaWikiLangTestCase.php:6
TextContentTest\dataGetDeletionUpdates
static dataGetDeletionUpdates()
Definition: TextContentTest.php:359
$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:237
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:35
Content
Base interface for content objects.
Definition: Content.php:34
TextContentTest\testGetWikitextForTransclusion
testGetWikitextForTransclusion()
@covers TextContent::getWikitextForTransclusion
Definition: TextContentTest.php:298
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
User
User
Definition: All_system_messages.txt:425
CONTENT_MODEL_JAVASCRIPT
const CONTENT_MODEL_JAVASCRIPT
Definition: Defines.php:284
$t
$t
Definition: testCompression.php:65
TextContentTest\testDeletionUpdates
testDeletionUpdates( $title, $model, $text, $expectedStuff)
@dataProvider dataGetDeletionUpdates @covers TextContent::getDeletionUpdates
Definition: TextContentTest.php:377
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:286
TextContentTest\testCopy
testCopy()
@covers TextContent::copy
Definition: TextContentTest.php:269
TextContentTest\testGetContentHandler
testGetContentHandler()
@covers TextContent::getContentHandler
Definition: TextContentTest.php:316
TextContentTest\testIsRedirect
testIsRedirect( $text, $expected)
@dataProvider dataGetRedirectTarget @covers TextContent::isRedirect
Definition: TextContentTest.php:163