4 * @group ContentHandler
7 * ^--- needed, because we do need the database to test link updates
9 class TextContentTest
extends MediaWikiTestCase
{
11 public function setup() {
18 $wgUser->setName( '127.0.0.1' );
20 $this->context
= new RequestContext( new FauxRequest() );
21 $this->context
->setTitle( Title
::newFromText( "Test" ) );
22 $this->context
->setUser( $wgUser );
25 public function newContent( $text ) {
26 return new TextContent( $text );
30 public function dataGetParserOutput() {
33 "TextContentTest_testGetParserOutput",
35 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
36 array( 'Links' => array() ) ),
42 * @dataProvider dataGetParserOutput
44 public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
45 $title = Title
::newFromText( $title );
46 $content = ContentHandler
::makeContent( $text, $title, $model );
48 $po = $content->getParserOutput( $title );
50 $html = $po->getText();
51 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
53 $this->assertEquals( $expectedHtml, trim( $html ) );
55 if ( $expectedFields ) {
56 foreach ( $expectedFields as $field => $exp ) {
57 $f = 'get' . ucfirst( $field );
58 $v = call_user_func( array( $po, $f ) );
60 if ( is_array( $exp ) ) {
61 $this->assertArrayEquals( $exp, $v );
63 $this->assertEquals( $exp, $v );
68 // @todo: assert more properties
71 public function dataPreSaveTransform() {
73 array( #0: no signature resolution
85 * @dataProvider dataPreSaveTransform
87 public function testPreSaveTransform( $text, $expected ) {
90 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
92 $content = $this->newContent( $text );
93 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
95 $this->assertEquals( $expected, $content->getNativeData() );
98 public function dataPreloadTransform() {
100 array( 'hello this is ~~~',
107 * @dataProvider dataPreloadTransform
109 public function testPreloadTransform( $text, $expected ) {
111 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
113 $content = $this->newContent( $text );
114 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
116 $this->assertEquals( $expected, $content->getNativeData() );
119 public function dataGetRedirectTarget() {
121 array( '#REDIRECT [[Test]]',
128 * @dataProvider dataGetRedirectTarget
130 public function testGetRedirectTarget( $text, $expected ) {
131 $content = $this->newContent( $text );
132 $t = $content->getRedirectTarget( );
134 if ( is_null( $expected ) ) {
135 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
137 $this->assertEquals( $expected, $t->getPrefixedText() );
142 * @dataProvider dataGetRedirectTarget
144 public function isRedirect( $text, $expected ) {
145 $content = $this->newContent( $text );
147 $this->assertEquals( !is_null($expected), $content->isRedirect() );
152 * @todo: test needs database! Should be done by a test class in the Database group.
155 public function getRedirectChain() {
156 $text = $this->getNativeData();
157 return Title::newFromRedirectArray( $text );
162 * @todo: test needs database! Should be done by a test class in the Database group.
165 public function getUltimateRedirectTarget() {
166 $text = $this->getNativeData();
167 return Title::newFromRedirectRecurse( $text );
172 public function dataIsCountable() {
199 * @dataProvider dataIsCountable
202 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
203 global $wgArticleCountMethod;
205 $old = $wgArticleCountMethod;
206 $wgArticleCountMethod = $mode;
208 $content = $this->newContent( $text );
210 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
211 $wgArticleCountMethod = $old;
213 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
214 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
217 public function dataGetTextForSummary() {
219 array( "hello\nworld.",
223 array( 'hello world.',
227 array( '[[hello world]].',
235 * @dataProvider dataGetTextForSummary
237 public function testGetTextForSummary( $text, $maxlength, $expected ) {
238 $content = $this->newContent( $text );
240 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
244 public function testGetTextForSearchIndex( ) {
245 $content = $this->newContent( "hello world." );
247 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
250 public function testCopy() {
251 $content = $this->newContent( "hello world." );
252 $copy = $content->copy();
254 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
255 $this->assertEquals( "hello world.", $copy->getNativeData() );
258 public function testGetSize( ) {
259 $content = $this->newContent( "hello world." );
261 $this->assertEquals( 12, $content->getSize() );
264 public function testGetNativeData( ) {
265 $content = $this->newContent( "hello world." );
267 $this->assertEquals( "hello world.", $content->getNativeData() );
270 public function testGetWikitextForTransclusion( ) {
271 $content = $this->newContent( "hello world." );
273 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
276 # =================================================================================================================
278 public function testGetModel() {
279 $content = $this->newContent( "hello world." );
281 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
284 public function testGetContentHandler() {
285 $content = $this->newContent( "hello world." );
287 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
290 public function dataIsEmpty( ) {
295 array( 'hallo welt.', false ),
300 * @dataProvider dataIsEmpty
302 public function testIsEmpty( $text, $empty ) {
303 $content = $this->newContent( $text );
305 $this->assertEquals( $empty, $content->isEmpty() );
308 public function dataEquals( ) {
310 array( new TextContent( "hallo" ), null, false ),
311 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
312 array( new TextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
313 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
314 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
319 * @dataProvider dataEquals
321 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
322 $this->assertEquals( $equal, $a->equals( $b ) );
325 public function dataGetDeletionUpdates() {
327 array("TextContentTest_testGetSecondaryDataUpdates_1",
328 CONTENT_MODEL_TEXT
, "hello ''world''\n",
331 array("TextContentTest_testGetSecondaryDataUpdates_2",
332 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
340 * @dataProvider dataGetDeletionUpdates
342 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
343 $title = Title
::newFromText( $title );
344 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
346 $content = ContentHandler
::makeContent( $text, $title, $model );
348 $updates = $content->getDeletionUpdates( WikiPage
::factory( $title ) );
350 // make updates accessible by class name
351 foreach ( $updates as $update ) {
352 $class = get_class( $update );
353 $updates[ $class ] = $update;
356 if ( !$expectedStuff ) {
357 $this->assertTrue( true ); // make phpunit happy
361 foreach ( $expectedStuff as $class => $fieldValues ) {
362 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
364 $update = $updates[ $class ];
366 foreach ( $fieldValues as $field => $value ) {
367 $v = $update->$field; #if the field doesn't exist, just crash and burn
368 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );