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
,
25 'wgAlwaysUseTidy' => false,
28 $this->context
= new RequestContext( new FauxRequest() );
29 $this->context
->setTitle( Title
::newFromText( 'Test' ) );
30 $this->context
->setUser( $user );
33 public function newContent( $text ) {
34 return new TextContent( $text );
37 public static function dataGetParserOutput() {
40 'TextContentTest_testGetParserOutput',
42 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
52 * @dataProvider dataGetParserOutput
54 public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
55 $title = Title
::newFromText( $title );
56 $content = ContentHandler
::makeContent( $text, $title, $model );
58 $po = $content->getParserOutput( $title );
60 $html = $po->getText();
61 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
63 $this->assertEquals( $expectedHtml, trim( $html ) );
65 if ( $expectedFields ) {
66 foreach ( $expectedFields as $field => $exp ) {
67 $f = 'get' . ucfirst( $field );
68 $v = call_user_func( array( $po, $f ) );
70 if ( is_array( $exp ) ) {
71 $this->assertArrayEquals( $exp, $v );
73 $this->assertEquals( $exp, $v );
78 // TODO: assert more properties
81 public static function dataPreSaveTransform() {
84 #0: no signature resolution
97 * @dataProvider dataPreSaveTransform
99 public function testPreSaveTransform( $text, $expected ) {
102 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
104 $content = $this->newContent( $text );
105 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
107 $this->assertEquals( $expected, $content->getNativeData() );
110 public static function dataPreloadTransform() {
120 * @dataProvider dataPreloadTransform
122 public function testPreloadTransform( $text, $expected ) {
124 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
126 $content = $this->newContent( $text );
127 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
129 $this->assertEquals( $expected, $content->getNativeData() );
132 public static function dataGetRedirectTarget() {
134 array( '#REDIRECT [[Test]]',
141 * @dataProvider dataGetRedirectTarget
143 public function testGetRedirectTarget( $text, $expected ) {
144 $content = $this->newContent( $text );
145 $t = $content->getRedirectTarget( );
147 if ( is_null( $expected ) ) {
148 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
150 $this->assertEquals( $expected, $t->getPrefixedText() );
155 * @dataProvider dataGetRedirectTarget
157 public function testIsRedirect( $text, $expected ) {
158 $content = $this->newContent( $text );
160 $this->assertEquals( !is_null($expected), $content->isRedirect() );
164 * @todo: test needs database! Should be done by a test class in the Database group.
167 public function getRedirectChain() {
168 $text = $this->getNativeData();
169 return Title::newFromRedirectArray( $text );
174 * @todo: test needs database! Should be done by a test class in the Database group.
177 public function getUltimateRedirectTarget() {
178 $text = $this->getNativeData();
179 return Title::newFromRedirectRecurse( $text );
183 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 ) );
255 public function testGetTextForSearchIndex( ) {
256 $content = $this->newContent( 'hello world.' );
258 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
261 public function testCopy() {
262 $content = $this->newContent( 'hello world.' );
263 $copy = $content->copy();
265 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
266 $this->assertEquals( 'hello world.', $copy->getNativeData() );
269 public function testGetSize( ) {
270 $content = $this->newContent( 'hello world.' );
272 $this->assertEquals( 12, $content->getSize() );
275 public function testGetNativeData( ) {
276 $content = $this->newContent( 'hello world.' );
278 $this->assertEquals( 'hello world.', $content->getNativeData() );
281 public function testGetWikitextForTransclusion( ) {
282 $content = $this->newContent( 'hello world.' );
284 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
287 public function testGetModel() {
288 $content = $this->newContent( "hello world." );
290 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
293 public function testGetContentHandler() {
294 $content = $this->newContent( "hello world." );
296 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
299 public static function dataIsEmpty( ) {
304 array( 'hallo welt.', false ),
309 * @dataProvider dataIsEmpty
311 public function testIsEmpty( $text, $empty ) {
312 $content = $this->newContent( $text );
314 $this->assertEquals( $empty, $content->isEmpty() );
317 public static function dataEquals( ) {
319 array( new TextContent( "hallo" ), null, false ),
320 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
321 array( new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
322 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
323 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
328 * @dataProvider dataEquals
330 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
331 $this->assertEquals( $equal, $a->equals( $b ) );
334 public static function dataGetDeletionUpdates() {
336 array("TextContentTest_testGetSecondaryDataUpdates_1",
337 CONTENT_MODEL_TEXT
, "hello ''world''\n",
340 array("TextContentTest_testGetSecondaryDataUpdates_2",
341 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
349 * @dataProvider dataGetDeletionUpdates
351 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
352 $ns = $this->getDefaultWikitextNS();
353 $title = Title
::newFromText( $title, $ns );
355 $content = ContentHandler
::makeContent( $text, $title, $model );
357 $page = WikiPage
::factory( $title );
358 $page->doEditContent( $content, '' );
360 $updates = $content->getDeletionUpdates( $page );
362 // make updates accessible by class name
363 foreach ( $updates as $update ) {
364 $class = get_class( $update );
365 $updates[ $class ] = $update;
368 if ( !$expectedStuff ) {
369 $this->assertTrue( true ); // make phpunit happy
373 foreach ( $expectedStuff as $class => $fieldValues ) {
374 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
376 $update = $updates[ $class ];
378 foreach ( $fieldValues as $field => $value ) {
379 $v = $update->$field; #if the field doesn't exist, just crash and burn
380 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
384 $page->doDeleteArticle( '' );
387 public static function provideConvert() {
391 CONTENT_MODEL_WIKITEXT
,
397 CONTENT_MODEL_WIKITEXT
,
409 CONTENT_MODEL_JAVASCRIPT
,
417 * @dataProvider provideConvert
419 public function testConvert( $text, $model, $lossy, $expectedNative ) {
420 $content = $this->newContent( $text );
422 $converted = $content->convert( $model, $lossy );
424 if ( $expectedNative === false ) {
425 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
427 $this->assertInstanceOf( 'Content', $converted );
428 $this->assertEquals( $expectedNative, $converted->getNativeData() );