4 * @group ContentHandler
6 * ^--- needed, because we do need the database to test link updates
8 class TextContentTest
extends MediaWikiLangTestCase
{
10 protected $savedContentGetParserOutput;
12 protected function setUp() {
19 $user->setName( '127.0.0.1' );
21 $this->context
= new RequestContext( new FauxRequest() );
22 $this->context
->setTitle( Title
::newFromText( 'Test' ) );
23 $this->context
->setUser( $user );
25 $this->setMwGlobals( array(
27 'wgTextModelsToParse' => array(
28 CONTENT_MODEL_WIKITEXT
,
30 CONTENT_MODEL_JAVASCRIPT
,
33 'wgAlwaysUseTidy' => false,
36 // bypass hooks that force custom rendering
37 if ( isset( $wgHooks['ContentGetParserOutput'] ) ) {
38 $this->savedContentGetParserOutput
= $wgHooks['ContentGetParserOutput'];
39 unset( $wgHooks['ContentGetParserOutput'] );
43 public function teardown() {
46 // restore hooks that force custom rendering
47 if ( $this->savedContentGetParserOutput
!== null ) {
48 $wgHooks['ContentGetParserOutput'] = $this->savedContentGetParserOutput
;
54 public function newContent( $text ) {
55 return new TextContent( $text );
58 public static function dataGetParserOutput() {
61 'TextContentTest_testGetParserOutput',
63 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
73 * @dataProvider dataGetParserOutput
75 public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
76 $title = Title
::newFromText( $title );
77 $content = ContentHandler
::makeContent( $text, $title, $model );
79 $po = $content->getParserOutput( $title );
81 $html = $po->getText();
82 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
84 $this->assertEquals( $expectedHtml, trim( $html ) );
86 if ( $expectedFields ) {
87 foreach ( $expectedFields as $field => $exp ) {
88 $f = 'get' . ucfirst( $field );
89 $v = call_user_func( array( $po, $f ) );
91 if ( is_array( $exp ) ) {
92 $this->assertArrayEquals( $exp, $v );
94 $this->assertEquals( $exp, $v );
99 // TODO: assert more properties
102 public static function dataPreSaveTransform() {
105 #0: no signature resolution
118 * @dataProvider dataPreSaveTransform
120 public function testPreSaveTransform( $text, $expected ) {
123 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
125 $content = $this->newContent( $text );
126 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
128 $this->assertEquals( $expected, $content->getNativeData() );
131 public static function dataPreloadTransform() {
141 * @dataProvider dataPreloadTransform
143 public function testPreloadTransform( $text, $expected ) {
145 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
147 $content = $this->newContent( $text );
148 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
150 $this->assertEquals( $expected, $content->getNativeData() );
153 public static function dataGetRedirectTarget() {
155 array( '#REDIRECT [[Test]]',
162 * @dataProvider dataGetRedirectTarget
164 public function testGetRedirectTarget( $text, $expected ) {
165 $content = $this->newContent( $text );
166 $t = $content->getRedirectTarget();
168 if ( is_null( $expected ) ) {
169 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
171 $this->assertEquals( $expected, $t->getPrefixedText() );
176 * @dataProvider dataGetRedirectTarget
178 public function testIsRedirect( $text, $expected ) {
179 $content = $this->newContent( $text );
181 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
185 * @todo Test needs database! Should be done by a test class in the Database group.
188 public function getRedirectChain() {
189 $text = $this->getNativeData();
190 return Title::newFromRedirectArray( $text );
195 * @todo Test needs database! Should be done by a test class in the Database group.
198 public function getUltimateRedirectTarget() {
199 $text = $this->getNativeData();
200 return Title::newFromRedirectRecurse( $text );
204 public static function dataIsCountable() {
230 * @dataProvider dataIsCountable
233 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
234 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
236 $content = $this->newContent( $text );
238 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
240 $this->assertEquals( $expected, $v, 'isCountable() returned unexpected value ' . var_export( $v, true )
241 . ' instead of ' . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
244 public static function dataGetTextForSummary() {
246 array( "hello\nworld.",
250 array( 'hello world.',
254 array( '[[hello world]].',
262 * @dataProvider dataGetTextForSummary
264 public function testGetTextForSummary( $text, $maxlength, $expected ) {
265 $content = $this->newContent( $text );
267 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
270 public function testGetTextForSearchIndex() {
271 $content = $this->newContent( 'hello world.' );
273 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
276 public function testCopy() {
277 $content = $this->newContent( 'hello world.' );
278 $copy = $content->copy();
280 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
281 $this->assertEquals( 'hello world.', $copy->getNativeData() );
284 public function testGetSize() {
285 $content = $this->newContent( 'hello world.' );
287 $this->assertEquals( 12, $content->getSize() );
290 public function testGetNativeData() {
291 $content = $this->newContent( 'hello world.' );
293 $this->assertEquals( 'hello world.', $content->getNativeData() );
296 public function testGetWikitextForTransclusion() {
297 $content = $this->newContent( 'hello world.' );
299 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
302 public function testGetModel() {
303 $content = $this->newContent( "hello world." );
305 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
308 public function testGetContentHandler() {
309 $content = $this->newContent( "hello world." );
311 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
314 public static function dataIsEmpty() {
319 array( 'hallo welt.', false ),
324 * @dataProvider dataIsEmpty
326 public function testIsEmpty( $text, $empty ) {
327 $content = $this->newContent( $text );
329 $this->assertEquals( $empty, $content->isEmpty() );
332 public static function dataEquals() {
334 array( new TextContent( "hallo" ), null, false ),
335 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
336 array( new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
337 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
338 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
343 * @dataProvider dataEquals
345 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
346 $this->assertEquals( $equal, $a->equals( $b ) );
349 public static function dataGetDeletionUpdates() {
351 array( "TextContentTest_testGetSecondaryDataUpdates_1",
352 CONTENT_MODEL_TEXT
, "hello ''world''\n",
355 array( "TextContentTest_testGetSecondaryDataUpdates_2",
356 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
364 * @dataProvider dataGetDeletionUpdates
366 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
367 $ns = $this->getDefaultWikitextNS();
368 $title = Title
::newFromText( $title, $ns );
370 $content = ContentHandler
::makeContent( $text, $title, $model );
372 $page = WikiPage
::factory( $title );
373 $page->doEditContent( $content, '' );
375 $updates = $content->getDeletionUpdates( $page );
377 // make updates accessible by class name
378 foreach ( $updates as $update ) {
379 $class = get_class( $update );
380 $updates[$class] = $update;
383 if ( !$expectedStuff ) {
384 $this->assertTrue( true ); // make phpunit happy
388 foreach ( $expectedStuff as $class => $fieldValues ) {
389 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
391 $update = $updates[$class];
393 foreach ( $fieldValues as $field => $value ) {
394 $v = $update->$field; #if the field doesn't exist, just crash and burn
395 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
399 $page->doDeleteArticle( '' );
402 public static function provideConvert() {
406 CONTENT_MODEL_WIKITEXT
,
412 CONTENT_MODEL_WIKITEXT
,
424 CONTENT_MODEL_JAVASCRIPT
,
432 * @dataProvider provideConvert
434 public function testConvert( $text, $model, $lossy, $expectedNative ) {
435 $content = $this->newContent( $text );
437 $converted = $content->convert( $model, $lossy );
439 if ( $expectedNative === false ) {
440 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
442 $this->assertInstanceOf( 'Content', $converted );
443 $this->assertEquals( $expectedNative, $converted->getNativeData() );