4 * @group ContentHandler
6 * ^--- needed, because we do need the database to test link updates
8 class TextContentTest
extends MediaWikiLangTestCase
{
11 protected function setUp() {
16 $user->setName( '127.0.0.1' );
18 $this->setMwGlobals( array(
20 'wgTextModelsToParse' => array(
21 CONTENT_MODEL_WIKITEXT
,
23 CONTENT_MODEL_JAVASCRIPT
,
26 'wgAlwaysUseTidy' => false,
29 $this->context
= new RequestContext( new FauxRequest() );
30 $this->context
->setTitle( Title
::newFromText( 'Test' ) );
31 $this->context
->setUser( $user );
34 public function newContent( $text ) {
35 return new TextContent( $text );
38 public static function dataGetParserOutput() {
41 'TextContentTest_testGetParserOutput',
43 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
53 * @dataProvider dataGetParserOutput
54 * @covers TextContent::getParserOutput
56 public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
57 $title = Title
::newFromText( $title );
58 $content = ContentHandler
::makeContent( $text, $title, $model );
60 $po = $content->getParserOutput( $title );
62 $html = $po->getText();
63 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
65 $this->assertEquals( $expectedHtml, trim( $html ) );
67 if ( $expectedFields ) {
68 foreach ( $expectedFields as $field => $exp ) {
69 $f = 'get' . ucfirst( $field );
70 $v = call_user_func( array( $po, $f ) );
72 if ( is_array( $exp ) ) {
73 $this->assertArrayEquals( $exp, $v );
75 $this->assertEquals( $exp, $v );
80 // TODO: assert more properties
83 public static function dataPreSaveTransform() {
86 #0: no signature resolution
99 * @dataProvider dataPreSaveTransform
100 * @covers TextContent::preSaveTransform
102 public function testPreSaveTransform( $text, $expected ) {
105 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
107 $content = $this->newContent( $text );
108 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
110 $this->assertEquals( $expected, $content->getNativeData() );
113 public static function dataPreloadTransform() {
123 * @dataProvider dataPreloadTransform
124 * @covers TextContent::preloadTransform
126 public function testPreloadTransform( $text, $expected ) {
128 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
130 $content = $this->newContent( $text );
131 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
133 $this->assertEquals( $expected, $content->getNativeData() );
136 public static function dataGetRedirectTarget() {
138 array( '#REDIRECT [[Test]]',
145 * @dataProvider dataGetRedirectTarget
146 * @covers TextContent::getRedirectTarget
148 public function testGetRedirectTarget( $text, $expected ) {
149 $content = $this->newContent( $text );
150 $t = $content->getRedirectTarget();
152 if ( is_null( $expected ) ) {
153 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
155 $this->assertEquals( $expected, $t->getPrefixedText() );
160 * @dataProvider dataGetRedirectTarget
161 * @covers TextContent::isRedirect
163 public function testIsRedirect( $text, $expected ) {
164 $content = $this->newContent( $text );
166 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
170 * @todo Test needs database! Should be done by a test class in the Database group.
173 public function getRedirectChain() {
174 $text = $this->getNativeData();
175 return Title::newFromRedirectArray( $text );
180 * @todo Test needs database! Should be done by a test class in the Database group.
183 public function getUltimateRedirectTarget() {
184 $text = $this->getNativeData();
185 return Title::newFromRedirectRecurse( $text );
189 public static function dataIsCountable() {
215 * @dataProvider dataIsCountable
217 * @covers TextContent::isCountable
219 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
220 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
222 $content = $this->newContent( $text );
224 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
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\"" );
230 public static function dataGetTextForSummary() {
232 array( "hello\nworld.",
236 array( 'hello world.',
240 array( '[[hello world]].',
248 * @dataProvider dataGetTextForSummary
249 * @covers TextContent::getTextForSummary
251 public function testGetTextForSummary( $text, $maxlength, $expected ) {
252 $content = $this->newContent( $text );
254 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
258 * @covers TextContent::getTextForSearchIndex
260 public function testGetTextForSearchIndex() {
261 $content = $this->newContent( 'hello world.' );
263 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
267 * @covers TextContent::copy
269 public function testCopy() {
270 $content = $this->newContent( 'hello world.' );
271 $copy = $content->copy();
273 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
274 $this->assertEquals( 'hello world.', $copy->getNativeData() );
278 * @covers TextContent::getSize
280 public function testGetSize() {
281 $content = $this->newContent( 'hello world.' );
283 $this->assertEquals( 12, $content->getSize() );
287 * @covers TextContent::getNativeData
289 public function testGetNativeData() {
290 $content = $this->newContent( 'hello world.' );
292 $this->assertEquals( 'hello world.', $content->getNativeData() );
296 * @covers TextContent::getWikitextForTransclusion
298 public function testGetWikitextForTransclusion() {
299 $content = $this->newContent( 'hello world.' );
301 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
305 * @covers TextContent::getModel
307 public function testGetModel() {
308 $content = $this->newContent( "hello world." );
310 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
314 * @covers TextContent::getContentHandler
316 public function testGetContentHandler() {
317 $content = $this->newContent( "hello world." );
319 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
322 public static function dataIsEmpty() {
327 array( 'hallo welt.', false ),
332 * @dataProvider dataIsEmpty
333 * @covers TextContent::isEmpty
335 public function testIsEmpty( $text, $empty ) {
336 $content = $this->newContent( $text );
338 $this->assertEquals( $empty, $content->isEmpty() );
341 public static function dataEquals() {
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 ),
352 * @dataProvider dataEquals
353 * @covers TextContent::equals
355 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
356 $this->assertEquals( $equal, $a->equals( $b ) );
359 public static function dataGetDeletionUpdates() {
361 array( "TextContentTest_testGetSecondaryDataUpdates_1",
362 CONTENT_MODEL_TEXT
, "hello ''world''\n",
365 array( "TextContentTest_testGetSecondaryDataUpdates_2",
366 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
374 * @dataProvider dataGetDeletionUpdates
375 * @covers TextContent::getDeletionUpdates
377 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
378 $ns = $this->getDefaultWikitextNS();
379 $title = Title
::newFromText( $title, $ns );
381 $content = ContentHandler
::makeContent( $text, $title, $model );
383 $page = WikiPage
::factory( $title );
384 $page->doEditContent( $content, '' );
386 $updates = $content->getDeletionUpdates( $page );
388 // make updates accessible by class name
389 foreach ( $updates as $update ) {
390 $class = get_class( $update );
391 $updates[$class] = $update;
394 if ( !$expectedStuff ) {
395 $this->assertTrue( true ); // make phpunit happy
399 foreach ( $expectedStuff as $class => $fieldValues ) {
400 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
402 $update = $updates[$class];
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" );
410 $page->doDeleteArticle( '' );
413 public static function provideConvert() {
417 CONTENT_MODEL_WIKITEXT
,
423 CONTENT_MODEL_WIKITEXT
,
435 CONTENT_MODEL_JAVASCRIPT
,
443 * @dataProvider provideConvert
444 * @covers TextContent::convert
446 public function testConvert( $text, $model, $lossy, $expectedNative ) {
447 $content = $this->newContent( $text );
449 $converted = $content->convert( $model, $lossy );
451 if ( $expectedNative === false ) {
452 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
454 $this->assertInstanceOf( 'Content', $converted );
455 $this->assertEquals( $expectedNative, $converted->getNativeData() );