4 * @group ContentHandler
6 * ^--- needed, because we do need the database to test link updates
8 class TextContentTest
extends MediaWikiTestCase
{
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
,
27 $this->context
= new RequestContext( new FauxRequest() );
28 $this->context
->setTitle( Title
::newFromText( 'Test' ) );
29 $this->context
->setUser( $user );
32 public function newContent( $text ) {
33 return new TextContent( $text );
36 public static function dataGetParserOutput() {
39 'TextContentTest_testGetParserOutput',
41 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
51 * @dataProvider dataGetParserOutput
53 public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
54 $title = Title
::newFromText( $title );
55 $content = ContentHandler
::makeContent( $text, $title, $model );
57 $po = $content->getParserOutput( $title );
59 $html = $po->getText();
60 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
62 $this->assertEquals( $expectedHtml, trim( $html ) );
64 if ( $expectedFields ) {
65 foreach ( $expectedFields as $field => $exp ) {
66 $f = 'get' . ucfirst( $field );
67 $v = call_user_func( array( $po, $f ) );
69 if ( is_array( $exp ) ) {
70 $this->assertArrayEquals( $exp, $v );
72 $this->assertEquals( $exp, $v );
77 // TODO: assert more properties
80 public static function dataPreSaveTransform() {
83 #0: no signature resolution
96 * @dataProvider dataPreSaveTransform
98 public function testPreSaveTransform( $text, $expected ) {
101 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
103 $content = $this->newContent( $text );
104 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
106 $this->assertEquals( $expected, $content->getNativeData() );
109 public static function dataPreloadTransform() {
119 * @dataProvider dataPreloadTransform
121 public function testPreloadTransform( $text, $expected ) {
123 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
125 $content = $this->newContent( $text );
126 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
128 $this->assertEquals( $expected, $content->getNativeData() );
131 public static function dataGetRedirectTarget() {
133 array( '#REDIRECT [[Test]]',
140 * @dataProvider dataGetRedirectTarget
142 public function testGetRedirectTarget( $text, $expected ) {
143 $content = $this->newContent( $text );
144 $t = $content->getRedirectTarget( );
146 if ( is_null( $expected ) ) {
147 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
149 $this->assertEquals( $expected, $t->getPrefixedText() );
154 * @dataProvider dataGetRedirectTarget
156 public function testIsRedirect( $text, $expected ) {
157 $content = $this->newContent( $text );
159 $this->assertEquals( !is_null($expected), $content->isRedirect() );
163 * @todo: test needs database! Should be done by a test class in the Database group.
166 public function getRedirectChain() {
167 $text = $this->getNativeData();
168 return Title::newFromRedirectArray( $text );
173 * @todo: test needs database! Should be done by a test class in the Database group.
176 public function getUltimateRedirectTarget() {
177 $text = $this->getNativeData();
178 return Title::newFromRedirectRecurse( $text );
182 public static function dataIsCountable() {
209 * @dataProvider dataIsCountable
212 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
213 global $wgArticleCountMethod;
215 $old = $wgArticleCountMethod;
216 $wgArticleCountMethod = $mode;
218 $content = $this->newContent( $text );
220 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
221 $wgArticleCountMethod = $old;
223 $this->assertEquals( $expected, $v, 'isCountable() returned unexpected value ' . var_export( $v, true )
224 . ' instead of ' . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
227 public static function dataGetTextForSummary() {
229 array( "hello\nworld.",
233 array( 'hello world.',
237 array( '[[hello world]].',
245 * @dataProvider dataGetTextForSummary
247 public function testGetTextForSummary( $text, $maxlength, $expected ) {
248 $content = $this->newContent( $text );
250 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
254 public function testGetTextForSearchIndex( ) {
255 $content = $this->newContent( 'hello world.' );
257 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
260 public function testCopy() {
261 $content = $this->newContent( 'hello world.' );
262 $copy = $content->copy();
264 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
265 $this->assertEquals( 'hello world.', $copy->getNativeData() );
268 public function testGetSize( ) {
269 $content = $this->newContent( 'hello world.' );
271 $this->assertEquals( 12, $content->getSize() );
274 public function testGetNativeData( ) {
275 $content = $this->newContent( 'hello world.' );
277 $this->assertEquals( 'hello world.', $content->getNativeData() );
280 public function testGetWikitextForTransclusion( ) {
281 $content = $this->newContent( 'hello world.' );
283 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
286 public function testGetModel() {
287 $content = $this->newContent( "hello world." );
289 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
292 public function testGetContentHandler() {
293 $content = $this->newContent( "hello world." );
295 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
298 public static function dataIsEmpty( ) {
303 array( 'hallo welt.', false ),
308 * @dataProvider dataIsEmpty
310 public function testIsEmpty( $text, $empty ) {
311 $content = $this->newContent( $text );
313 $this->assertEquals( $empty, $content->isEmpty() );
316 public static function dataEquals( ) {
318 array( new TextContent( "hallo" ), null, false ),
319 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
320 array( new TextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
321 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
322 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
327 * @dataProvider dataEquals
329 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
330 $this->assertEquals( $equal, $a->equals( $b ) );
333 public static function dataGetDeletionUpdates() {
335 array("TextContentTest_testGetSecondaryDataUpdates_1",
336 CONTENT_MODEL_TEXT
, "hello ''world''\n",
339 array("TextContentTest_testGetSecondaryDataUpdates_2",
340 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
348 * @dataProvider dataGetDeletionUpdates
350 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
351 $title = Title
::newFromText( $title );
352 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
354 $content = ContentHandler
::makeContent( $text, $title, $model );
356 $updates = $content->getDeletionUpdates( WikiPage
::factory( $title ) );
358 // make updates accessible by class name
359 foreach ( $updates as $update ) {
360 $class = get_class( $update );
361 $updates[ $class ] = $update;
364 if ( !$expectedStuff ) {
365 $this->assertTrue( true ); // make phpunit happy
369 foreach ( $expectedStuff as $class => $fieldValues ) {
370 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
372 $update = $updates[ $class ];
374 foreach ( $fieldValues as $field => $value ) {
375 $v = $update->$field; #if the field doesn't exist, just crash and burn
376 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );