4 * @group ContentHandler
7 * ^--- needed, because we do need the database to test link updates
9 class TextContentTest
extends MediaWikiTestCase
{
11 public function setup() {
16 $wgUser->setName( '127.0.0.1' );
18 $this->context
= new RequestContext( new FauxRequest() );
19 $this->context
->setTitle( Title
::newFromText( "Test" ) );
20 $this->context
->setUser( $wgUser );
23 public function newContent( $text ) {
24 return new TextContent( $text );
28 public function dataGetParserOutput() {
31 "TextContentTest_testGetParserOutput",
33 "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[stuff]]",
34 array( 'Links' => array() ) ),
40 * @dataProvider dataGetParserOutput
42 public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
43 $title = Title
::newFromText( $title );
44 $content = ContentHandler
::makeContent( $text, $title, $model );
46 $po = $content->getParserOutput( $title );
48 $html = $po->getText();
49 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
51 $this->assertEquals( $expectedHtml, trim( $html ) );
53 if ( $expectedFields ) {
54 foreach ( $expectedFields as $field => $exp ) {
55 $f = 'get' . ucfirst( $field );
56 $v = call_user_func( array( $po, $f ) );
58 if ( is_array( $exp ) ) {
59 $this->assertArrayEquals( $exp, $v );
61 $this->assertEquals( $exp, $v );
66 // @todo: assert more properties
69 public function dataPreSaveTransform() {
71 array( 'hello this is ~~~',
78 * @dataProvider dataPreSaveTransform
80 public function testPreSaveTransform( $text, $expected ) {
83 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
85 $content = $this->newContent( $text );
86 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
88 $this->assertEquals( $expected, $content->getNativeData() );
91 public function dataPreloadTransform() {
93 array( 'hello this is ~~~',
100 * @dataProvider dataPreloadTransform
102 public function testPreloadTransform( $text, $expected ) {
104 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
106 $content = $this->newContent( $text );
107 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
109 $this->assertEquals( $expected, $content->getNativeData() );
112 public function dataGetRedirectTarget() {
114 array( '#REDIRECT [[Test]]',
121 * @dataProvider dataGetRedirectTarget
123 public function testGetRedirectTarget( $text, $expected ) {
124 $content = $this->newContent( $text );
125 $t = $content->getRedirectTarget( );
127 if ( is_null( $expected ) ) {
128 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
130 $this->assertEquals( $expected, $t->getPrefixedText() );
135 * @dataProvider dataGetRedirectTarget
137 public function isRedirect( $text, $expected ) {
138 $content = $this->newContent( $text );
140 $this->assertEquals( !is_null($expected), $content->isRedirect() );
145 * @todo: test needs database! Should be done by a test class in the Database group.
148 public function getRedirectChain() {
149 $text = $this->getNativeData();
150 return Title::newFromRedirectArray( $text );
155 * @todo: test needs database! Should be done by a test class in the Database group.
158 public function getUltimateRedirectTarget() {
159 $text = $this->getNativeData();
160 return Title::newFromRedirectRecurse( $text );
165 public function dataIsCountable() {
192 * @dataProvider dataIsCountable
195 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
196 global $wgArticleCountMethod;
198 $old = $wgArticleCountMethod;
199 $wgArticleCountMethod = $mode;
201 $content = $this->newContent( $text );
203 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
204 $wgArticleCountMethod = $old;
206 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
207 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
210 public function dataGetTextForSummary() {
212 array( "hello\nworld.",
216 array( 'hello world.',
220 array( '[[hello world]].',
228 * @dataProvider dataGetTextForSummary
230 public function testGetTextForSummary( $text, $maxlength, $expected ) {
231 $content = $this->newContent( $text );
233 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
237 public function testGetTextForSearchIndex( ) {
238 $content = $this->newContent( "hello world." );
240 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
243 public function testCopy() {
244 $content = $this->newContent( "hello world." );
245 $copy = $content->copy();
247 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
248 $this->assertEquals( "hello world.", $copy->getNativeData() );
251 public function testGetSize( ) {
252 $content = $this->newContent( "hello world." );
254 $this->assertEquals( 12, $content->getSize() );
257 public function testGetNativeData( ) {
258 $content = $this->newContent( "hello world." );
260 $this->assertEquals( "hello world.", $content->getNativeData() );
263 public function testGetWikitextForTransclusion( ) {
264 $content = $this->newContent( "hello world." );
266 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
269 # =================================================================================================================
271 public function testGetModel() {
272 $content = $this->newContent( "hello world." );
274 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
277 public function testGetContentHandler() {
278 $content = $this->newContent( "hello world." );
280 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
283 public function dataIsEmpty( ) {
288 array( 'hallo welt.', false ),
293 * @dataProvider dataIsEmpty
295 public function testIsEmpty( $text, $empty ) {
296 $content = $this->newContent( $text );
298 $this->assertEquals( $empty, $content->isEmpty() );
301 public function dataEquals( ) {
303 array( new TextContent( "hallo" ), null, false ),
304 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
305 array( new TextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
306 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
307 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
312 * @dataProvider dataEquals
314 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
315 $this->assertEquals( $equal, $a->equals( $b ) );
318 public function dataGetDeletionUpdates() {
320 array("TextContentTest_testGetSecondaryDataUpdates_1",
321 CONTENT_MODEL_TEXT
, "hello ''world''\n",
324 array("TextContentTest_testGetSecondaryDataUpdates_2",
325 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
333 * @dataProvider dataGetDeletionUpdates
335 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
336 $title = Title
::newFromText( $title );
337 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
339 $content = ContentHandler
::makeContent( $text, $title, $model );
341 $updates = $content->getDeletionUpdates( WikiPage
::factory( $title ) );
343 // make updates accessible by class name
344 foreach ( $updates as $update ) {
345 $class = get_class( $update );
346 $updates[ $class ] = $update;
349 if ( !$expectedStuff ) {
350 $this->assertTrue( true ); // make phpunit happy
354 foreach ( $expectedStuff as $class => $fieldValues ) {
355 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
357 $update = $updates[ $class ];
359 foreach ( $fieldValues as $field => $value ) {
360 $v = $update->$field; #if the field doesn't exist, just crash and burn
361 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );