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() {
30 array("TextContentTest_testGetParserOutput", CONTENT_MODEL_TEXT
, "hello ''world'' & stuff\n", "hello ''world'' & stuff"),
36 * @dataProvider dataGetParserOutput
38 public function testGetParserOutput( $title, $model, $text, $expectedHtml ) {
39 $title = Title
::newFromText( $title );
40 $content = ContentHandler
::makeContent( $text, $title, $model );
42 $po = $content->getParserOutput( $title );
44 $html = $po->getText();
45 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
47 $this->assertEquals( $expectedHtml, trim( $html ) );
48 // @todo: assert more properties
51 public function dataPreSaveTransform() {
53 array( 'hello this is ~~~',
60 * @dataProvider dataPreSaveTransform
62 public function testPreSaveTransform( $text, $expected ) {
65 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
67 $content = $this->newContent( $text );
68 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
70 $this->assertEquals( $expected, $content->getNativeData() );
73 public function dataPreloadTransform() {
75 array( 'hello this is ~~~',
82 * @dataProvider dataPreloadTransform
84 public function testPreloadTransform( $text, $expected ) {
86 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
88 $content = $this->newContent( $text );
89 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
91 $this->assertEquals( $expected, $content->getNativeData() );
94 public function dataGetRedirectTarget() {
96 array( '#REDIRECT [[Test]]',
103 * @dataProvider dataGetRedirectTarget
105 public function testGetRedirectTarget( $text, $expected ) {
106 $content = $this->newContent( $text );
107 $t = $content->getRedirectTarget( );
109 if ( is_null( $expected ) ) {
110 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
112 $this->assertEquals( $expected, $t->getPrefixedText() );
117 * @dataProvider dataGetRedirectTarget
119 public function isRedirect( $text, $expected ) {
120 $content = $this->newContent( $text );
122 $this->assertEquals( !is_null($expected), $content->isRedirect() );
127 * @todo: test needs database! Should be done by a test class in the Database group.
130 public function getRedirectChain() {
131 $text = $this->getNativeData();
132 return Title::newFromRedirectArray( $text );
137 * @todo: test needs database! Should be done by a test class in the Database group.
140 public function getUltimateRedirectTarget() {
141 $text = $this->getNativeData();
142 return Title::newFromRedirectRecurse( $text );
147 public function dataIsCountable() {
174 * @dataProvider dataIsCountable
177 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
178 global $wgArticleCountMethod;
180 $old = $wgArticleCountMethod;
181 $wgArticleCountMethod = $mode;
183 $content = $this->newContent( $text );
185 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
186 $wgArticleCountMethod = $old;
188 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
189 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
192 public function dataGetTextForSummary() {
194 array( "hello\nworld.",
198 array( 'hello world.',
202 array( '[[hello world]].',
210 * @dataProvider dataGetTextForSummary
212 public function testGetTextForSummary( $text, $maxlength, $expected ) {
213 $content = $this->newContent( $text );
215 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
219 public function testGetTextForSearchIndex( ) {
220 $content = $this->newContent( "hello world." );
222 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
225 public function testCopy() {
226 $content = $this->newContent( "hello world." );
227 $copy = $content->copy();
229 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
230 $this->assertEquals( "hello world.", $copy->getNativeData() );
233 public function testGetSize( ) {
234 $content = $this->newContent( "hello world." );
236 $this->assertEquals( 12, $content->getSize() );
239 public function testGetNativeData( ) {
240 $content = $this->newContent( "hello world." );
242 $this->assertEquals( "hello world.", $content->getNativeData() );
245 public function testGetWikitextForTransclusion( ) {
246 $content = $this->newContent( "hello world." );
248 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
251 # =================================================================================================================
253 public function testGetModel() {
254 $content = $this->newContent( "hello world." );
256 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getModel() );
259 public function testGetContentHandler() {
260 $content = $this->newContent( "hello world." );
262 $this->assertEquals( CONTENT_MODEL_TEXT
, $content->getContentHandler()->getModelID() );
265 public function dataIsEmpty( ) {
270 array( 'hallo welt.', false ),
275 * @dataProvider dataIsEmpty
277 public function testIsEmpty( $text, $empty ) {
278 $content = $this->newContent( $text );
280 $this->assertEquals( $empty, $content->isEmpty() );
283 public function dataEquals( ) {
285 array( new TextContent( "hallo" ), null, false ),
286 array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
287 array( new TextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
288 array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
289 array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
294 * @dataProvider dataEquals
296 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
297 $this->assertEquals( $equal, $a->equals( $b ) );
300 public function dataGetDeletionUpdates() {
302 array("TextContentTest_testGetSecondaryDataUpdates_1",
303 CONTENT_MODEL_TEXT
, "hello ''world''\n",
306 array("TextContentTest_testGetSecondaryDataUpdates_2",
307 CONTENT_MODEL_TEXT
, "hello [[world test 21344]]\n",
315 * @dataProvider dataGetDeletionUpdates
317 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
318 $title = Title
::newFromText( $title );
319 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
321 $content = ContentHandler
::makeContent( $text, $title, $model );
323 $updates = $content->getDeletionUpdates( WikiPage
::factory( $title ) );
325 // make updates accessible by class name
326 foreach ( $updates as $update ) {
327 $class = get_class( $update );
328 $updates[ $class ] = $update;
331 if ( !$expectedStuff ) {
332 $this->assertTrue( true ); // make phpunit happy
336 foreach ( $expectedStuff as $class => $fieldValues ) {
337 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
339 $update = $updates[ $class ];
341 foreach ( $fieldValues as $field => $value ) {
342 $v = $update->$field; #if the field doesn't exist, just crash and burn
343 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );