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
55 public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
56 $title = Title
::newFromText( $title );
57 $content = ContentHandler
::makeContent( $text, $title, $model );
59 $po = $content->getParserOutput( $title );
61 $html = $po->getText();
62 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
64 $this->assertEquals( $expectedHtml, trim( $html ) );
66 if ( $expectedFields ) {
67 foreach ( $expectedFields as $field => $exp ) {
68 $f = 'get' . ucfirst( $field );
69 $v = call_user_func( array( $po, $f ) );
71 if ( is_array( $exp ) ) {
72 $this->assertArrayEquals( $exp, $v );
74 $this->assertEquals( $exp, $v );
79 // TODO: assert more properties
82 public static function dataPreSaveTransform() {
85 #0: no signature resolution
98 * @dataProvider dataPreSaveTransform
100 public function testPreSaveTransform( $text, $expected ) {
103 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
105 $content = $this->newContent( $text );
106 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
108 $this->assertEquals( $expected, $content->getNativeData() );
111 public static function dataPreloadTransform() {
121 * @dataProvider dataPreloadTransform
123 public function testPreloadTransform( $text, $expected ) {
125 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
127 $content = $this->newContent( $text );
128 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
130 $this->assertEquals( $expected, $content->getNativeData() );
133 public static function dataGetRedirectTarget() {
135 array( '#REDIRECT [[Test]]',
142 * @dataProvider dataGetRedirectTarget
144 public function testGetRedirectTarget( $text, $expected ) {
145 $content = $this->newContent( $text );
146 $t = $content->getRedirectTarget();
148 if ( is_null( $expected ) ) {
149 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
151 $this->assertEquals( $expected, $t->getPrefixedText() );
156 * @dataProvider dataGetRedirectTarget
158 public function testIsRedirect( $text, $expected ) {
159 $content = $this->newContent( $text );
161 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
165 * @todo: test needs database! Should be done by a test class in the Database group.
168 public function getRedirectChain() {
169 $text = $this->getNativeData();
170 return Title::newFromRedirectArray( $text );
175 * @todo: test needs database! Should be done by a test class in the Database group.
178 public function getUltimateRedirectTarget() {
179 $text = $this->getNativeData();
180 return Title::newFromRedirectRecurse( $text );
184 public static function dataIsCountable() {
210 * @dataProvider dataIsCountable
213 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
214 global $wgArticleCountMethod;
216 $old = $wgArticleCountMethod;
217 $wgArticleCountMethod = $mode;
219 $content = $this->newContent( $text );
221 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
222 $wgArticleCountMethod = $old;
224 $this->assertEquals( $expected, $v, 'isCountable() returned unexpected value ' . var_export( $v, true )
225 . ' instead of ' . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
228 public static function dataGetTextForSummary() {
230 array( "hello\nworld.",
234 array( 'hello world.',
238 array( '[[hello world]].',
246 * @dataProvider dataGetTextForSummary
248 public function testGetTextForSummary( $text, $maxlength, $expected ) {
249 $content = $this->newContent( $text );
251 $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 $ns = $this->getDefaultWikitextNS();
352 $title = Title
::newFromText( $title, $ns );
354 $content = ContentHandler
::makeContent( $text, $title, $model );
356 $page = WikiPage
::factory( $title );
357 $page->doEditContent( $content, '' );
359 $updates = $content->getDeletionUpdates( $page );
361 // make updates accessible by class name
362 foreach ( $updates as $update ) {
363 $class = get_class( $update );
364 $updates[$class] = $update;
367 if ( !$expectedStuff ) {
368 $this->assertTrue( true ); // make phpunit happy
372 foreach ( $expectedStuff as $class => $fieldValues ) {
373 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
375 $update = $updates[$class];
377 foreach ( $fieldValues as $field => $value ) {
378 $v = $update->$field; #if the field doesn't exist, just crash and burn
379 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
383 $page->doDeleteArticle( '' );
386 public static function provideConvert() {
390 CONTENT_MODEL_WIKITEXT
,
396 CONTENT_MODEL_WIKITEXT
,
408 CONTENT_MODEL_JAVASCRIPT
,
416 * @dataProvider provideConvert
418 public function testConvert( $text, $model, $lossy, $expectedNative ) {
419 $content = $this->newContent( $text );
421 $converted = $content->convert( $model, $lossy );
423 if ( $expectedNative === false ) {
424 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
426 $this->assertInstanceOf( 'Content', $converted );
427 $this->assertEquals( $expectedNative, $converted->getNativeData() );