10934b4ec840a39387b6cde636813a939eb88143
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( #0: no signature resolution
83 * @dataProvider dataPreSaveTransform
85 public function testPreSaveTransform( $text, $expected ) {
88 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
90 $content = $this->newContent( $text );
91 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
93 $this->assertEquals( $expected, $content->getNativeData() );
96 public function dataPreloadTransform() {
98 array( 'hello this is ~~~',
105 * @dataProvider dataPreloadTransform
107 public function testPreloadTransform( $text, $expected ) {
109 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
111 $content = $this->newContent( $text );
112 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
114 $this->assertEquals( $expected, $content->getNativeData() );
117 public function dataGetRedirectTarget() {
119 array( '#REDIRECT [[Test]]',
126 * @dataProvider dataGetRedirectTarget
128 public function testGetRedirectTarget( $text, $expected ) {
129 $content = $this->newContent( $text );
130 $t = $content->getRedirectTarget( );
132 if ( is_null( $expected ) ) {
133 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
135 $this->assertEquals( $expected, $t->getPrefixedText() );
140 * @dataProvider dataGetRedirectTarget
142 public function isRedirect( $text, $expected ) {
143 $content = $this->newContent( $text );
145 $this->assertEquals( !is_null($expected), $content->isRedirect() );
150 * @todo: test needs database! Should be done by a test class in the Database group.
153 public function getRedirectChain() {
154 $text = $this->getNativeData();
155 return Title::newFromRedirectArray( $text );
160 * @todo: test needs database! Should be done by a test class in the Database group.
163 public function getUltimateRedirectTarget() {
164 $text = $this->getNativeData();
165 return Title::newFromRedirectRecurse( $text );
170 public function dataIsCountable() {
197 * @dataProvider dataIsCountable
200 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
201 global $wgArticleCountMethod;
203 $old = $wgArticleCountMethod;
204 $wgArticleCountMethod = $mode;
206 $content = $this->newContent( $text );
208 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
209 $wgArticleCountMethod = $old;
211 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
212 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
215 public function dataGetTextForSummary() {
217 array( "hello\nworld.",
221 array( 'hello world.',
225 array( '[[hello world]].',
233 * @dataProvider dataGetTextForSummary
235 public function testGetTextForSummary( $text, $maxlength, $expected ) {
236 $content = $this->newContent( $text );
238 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
242 public function testGetTextForSearchIndex( ) {
243 $content = $this->newContent( "hello world." );
245 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
248 public function testCopy() {
249 $content = $this->newContent( "hello world." );
250 $copy = $content->copy();
252 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
253 $this->assertEquals( "hello world.", $copy->getNativeData() );
256 public function testGetSize( ) {
257 $content = $this->newContent( "hello world." );
259 $this->assertEquals( 12, $content->getSize() );
262 public function testGetNativeData( ) {
263 $content = $this->newContent( "hello world." );
265 $this->assertEquals( "hello world.", $content->getNativeData() );
268 public function testGetWikitextForTransclusion( ) {
269 $content = $this->newContent( "hello world." );
271 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
274 # =================================================================================================================
276 public function testGetModel() {
277 $content = $this->newContent( "hello world." );
279 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
282 public function testGetContentHandler() {
283 $content = $this->newContent( "hello world." );
285 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
288 public function dataIsEmpty( ) {
293 array( 'hallo welt.', false ),
298 * @dataProvider dataIsEmpty
300 public function testIsEmpty( $text, $empty ) {
301 $content = $this->newContent( $text );
303 $this->assertEquals( $empty, $content->isEmpty() );
306 public function dataEquals( ) {
308 array( new TextContent( "hallo" ), null, false ),
309 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
310 array( new TextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
311 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
312 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
317 * @dataProvider dataEquals
319 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
320 $this->assertEquals( $equal, $a->equals( $b ) );
323 public function dataGetDeletionUpdates() {
325 array("TextContentTest_testGetSecondaryDataUpdates_1",
326 CONTENT_MODEL_TEXT
, "hello ''world''\n",
329 array("TextContentTest_testGetSecondaryDataUpdates_2",
330 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
338 * @dataProvider dataGetDeletionUpdates
340 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
341 $title = Title
::newFromText( $title );
342 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
344 $content = ContentHandler
::makeContent( $text, $title, $model );
346 $updates = $content->getDeletionUpdates( WikiPage
::factory( $title ) );
348 // make updates accessible by class name
349 foreach ( $updates as $update ) {
350 $class = get_class( $update );
351 $updates[ $class ] = $update;
354 if ( !$expectedStuff ) {
355 $this->assertTrue( true ); // make phpunit happy
359 foreach ( $expectedStuff as $class => $fieldValues ) {
360 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
362 $update = $updates[ $class ];
364 foreach ( $fieldValues as $field => $value ) {
365 $v = $update->$field; #if the field doesn't exist, just crash and burn
366 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );