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 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
216 $content = $this->newContent( $text );
218 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
220 $this->assertEquals( $expected, $v, 'isCountable() returned unexpected value ' . var_export( $v, true )
221 . ' instead of ' . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
224 public static function dataGetTextForSummary() {
226 array( "hello\nworld.",
230 array( 'hello world.',
234 array( '[[hello world]].',
242 * @dataProvider dataGetTextForSummary
244 public function testGetTextForSummary( $text, $maxlength, $expected ) {
245 $content = $this->newContent( $text );
247 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
250 public function testGetTextForSearchIndex() {
251 $content = $this->newContent( 'hello world.' );
253 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
256 public function testCopy() {
257 $content = $this->newContent( 'hello world.' );
258 $copy = $content->copy();
260 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
261 $this->assertEquals( 'hello world.', $copy->getNativeData() );
264 public function testGetSize() {
265 $content = $this->newContent( 'hello world.' );
267 $this->assertEquals( 12, $content->getSize() );
270 public function testGetNativeData() {
271 $content = $this->newContent( 'hello world.' );
273 $this->assertEquals( 'hello world.', $content->getNativeData() );
276 public function testGetWikitextForTransclusion() {
277 $content = $this->newContent( 'hello world.' );
279 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
282 public function testGetModel() {
283 $content = $this->newContent( "hello world." );
285 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
288 public function testGetContentHandler() {
289 $content = $this->newContent( "hello world." );
291 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
294 public static function dataIsEmpty() {
299 array( 'hallo welt.', false ),
304 * @dataProvider dataIsEmpty
306 public function testIsEmpty( $text, $empty ) {
307 $content = $this->newContent( $text );
309 $this->assertEquals( $empty, $content->isEmpty() );
312 public static function dataEquals() {
314 array( new TextContent( "hallo" ), null, false ),
315 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
316 array( new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
317 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
318 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
323 * @dataProvider dataEquals
325 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
326 $this->assertEquals( $equal, $a->equals( $b ) );
329 public static function dataGetDeletionUpdates() {
331 array( "TextContentTest_testGetSecondaryDataUpdates_1",
332 CONTENT_MODEL_TEXT
, "hello ''world''\n",
335 array( "TextContentTest_testGetSecondaryDataUpdates_2",
336 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
344 * @dataProvider dataGetDeletionUpdates
346 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
347 $ns = $this->getDefaultWikitextNS();
348 $title = Title
::newFromText( $title, $ns );
350 $content = ContentHandler
::makeContent( $text, $title, $model );
352 $page = WikiPage
::factory( $title );
353 $page->doEditContent( $content, '' );
355 $updates = $content->getDeletionUpdates( $page );
357 // make updates accessible by class name
358 foreach ( $updates as $update ) {
359 $class = get_class( $update );
360 $updates[$class] = $update;
363 if ( !$expectedStuff ) {
364 $this->assertTrue( true ); // make phpunit happy
368 foreach ( $expectedStuff as $class => $fieldValues ) {
369 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
371 $update = $updates[$class];
373 foreach ( $fieldValues as $field => $value ) {
374 $v = $update->$field; #if the field doesn't exist, just crash and burn
375 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
379 $page->doDeleteArticle( '' );
382 public static function provideConvert() {
386 CONTENT_MODEL_WIKITEXT
,
392 CONTENT_MODEL_WIKITEXT
,
404 CONTENT_MODEL_JAVASCRIPT
,
412 * @dataProvider provideConvert
414 public function testConvert( $text, $model, $lossy, $expectedNative ) {
415 $content = $this->newContent( $text );
417 $converted = $content->convert( $model, $lossy );
419 if ( $expectedNative === false ) {
420 $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
422 $this->assertInstanceOf( 'Content', $converted );
423 $this->assertEquals( $expectedNative, $converted->getNativeData() );