Merge "Convert -{}- markups in title="" and alt=""."
[lhc/web/wiklou.git] / tests / phpunit / includes / WikitextContentTest.php
index 57f992a..dac8041 100644 (file)
@@ -2,21 +2,32 @@
 
 /**
  * @group ContentHandler
+ *
+ * @group Database
+ *        ^--- needed, because we do need the database to test link updates
  */
 class WikitextContentTest extends MediaWikiTestCase {
 
        public function setup() {
+               global $wgUser;
+
+               // anon user
+               $wgUser = new User();
+               $wgUser->setName( '127.0.0.1' );
+
                $this->context = new RequestContext( new FauxRequest() );
                $this->context->setTitle( Title::newFromText( "Test" ) );
+               $this->context->setUser( $wgUser );
        }
 
        public function newContent( $text ) {
                return new WikitextContent( $text );
        }
 
+
        public function dataGetParserOutput() {
                return array(
-                       array("hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
+                       array("WikitextContentTest_testGetParserOutput", CONTENT_MODEL_WIKITEXT, "hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
                        // @todo: more...?
                );
        }
@@ -24,15 +35,63 @@ class WikitextContentTest extends MediaWikiTestCase {
        /**
         * @dataProvider dataGetParserOutput
         */
-       public function testGetParserOutput( $text, $expectedHtml ) {
-               $content = $this->newContent( $text );
+       public function testGetParserOutput( $title, $model, $text, $expectedHtml ) {
+               $title = Title::newFromText( $title );
+               $content = ContentHandler::makeContent( $text, $title, $model );
 
-               $po = $content->getParserOutput( $this->context );
+               $po = $content->getParserOutput( $title );
 
                $this->assertEquals( $expectedHtml, $po->getText() );
-               return $po;
+               // @todo: assert more properties
+       }
+
+       public function dataGetSecondaryDataUpdates() {
+               return array(
+                       array("WikitextContentTest_testGetSecondaryDataUpdates_1",
+                               CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
+                               array( 'LinksUpdate' => array(  'mRecursive' => true,
+                                                               'mLinks' => array() ) )
+                       ),
+                       array("WikitextContentTest_testGetSecondaryDataUpdates_2",
+                               CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
+                               array( 'LinksUpdate' => array(  'mRecursive' => true,
+                                                               'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) )
+                       ),
+                       // @todo: more...?
+               );
+       }
+
+       /**
+        * @dataProvider dataGetSecondaryDataUpdates
+        * @group Database
+        */
+       public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
+               $title = Title::newFromText( $title );
+               $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
+
+               $content = ContentHandler::makeContent( $text, $title, $model );
+
+               $updates = $content->getSecondaryDataUpdates( $title );
+
+               // make updates accessible by class name
+               foreach ( $updates as $update ) {
+                       $class = get_class( $update );
+                       $updates[$class] = $update;
+               }
+
+               foreach ( $expectedStuff as $class => $fieldValues ) {
+                       $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
+
+                       $update = $updates[$class];
+
+                       foreach ( $fieldValues as $field => $value ) {
+                               $v = $update->$field; #if the field doesn't exist, just crash and burn
+                               $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
+                       }
+               }
        }
 
+
        static $sections =
 
 "Intro
@@ -143,8 +202,9 @@ just a test"
         * @dataProvider dataPreSaveTransform
         */
        public function testPreSaveTransform( $text, $expected ) {
-               global $wgUser, $wgContLang;
-               $options = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
+               global $wgContLang;
+
+               $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
 
                $content = $this->newContent( $text );
                $content = $content->preSaveTransform( $this->context->getTitle(), $this->context->getUser(), $options );
@@ -167,8 +227,8 @@ just a test"
         * @dataProvider dataPreloadTransform
         */
        public function testPreloadTransform( $text, $expected ) {
-               global $wgUser, $wgContLang;
-               $options = ParserOptions::newFromUserAndLang( $wgUser, $wgContLang );
+               global $wgContLang;
+               $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
 
                $content = $this->newContent( $text );
                $content = $content->preloadTransform( $this->context->getTitle(), $options );
@@ -197,8 +257,11 @@ just a test"
                $content = $this->newContent( $text );
                $t = $content->getRedirectTarget( );
 
-               if ( is_null( $expected ) ) $this->assertNull( $t, "text should not have generated a redirect target: $text" );
-               else $this->assertEquals( $expected, $t->getPrefixedText() );
+               if ( is_null( $expected ) ) {
+                       $this->assertNull( $t, "text should not have generated a redirect target: $text" );
+               } else {
+                       $this->assertEquals( $expected, $t->getPrefixedText() );
+               }
        }
 
        /**
@@ -212,7 +275,7 @@ just a test"
 
 
        /**
-        * @todo: test needs database!
+        * @todo: test needs database! Should be done by a test class in the Database group.
         */
        /*
        public function getRedirectChain() {
@@ -222,7 +285,7 @@ just a test"
        */
 
        /**
-        * @todo: test needs database!
+        * @todo: test needs database! Should be done by a test class in the Database group.
         */
        /*
        public function getUltimateRedirectTarget() {
@@ -295,6 +358,7 @@ just a test"
 
        /**
         * @dataProvider dataIsCountable
+        * @group Database
         */
        public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
                global $wgArticleCountMethod;
@@ -304,7 +368,7 @@ just a test"
 
                $content = $this->newContent( $text );
 
-               $v = $content->isCountable( $hasLinks, $this->context );
+               $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
                $wgArticleCountMethod = $old;
 
                $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
@@ -370,6 +434,35 @@ just a test"
                $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
        }
 
+       public function testMatchMagicWord( ) {
+               $mw = MagicWord::get( "staticredirect" );
+
+               $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
+               $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
+
+               $content = $this->newContent( "#REDIRECT [[FOO]]" );
+               $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
+       }
+
+       public function testUpdateRedirect( ) {
+               $target = Title::newFromText( "testUpdateRedirect_target" );
+
+               // test with non-redirect page
+               $content = $this->newContent( "hello world." );
+               $newContent = $content->updateRedirect( $target );
+
+               $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
+
+               // test with actual redirect
+               $content = $this->newContent( "#REDIRECT [[Someplace]]" );
+               $newContent = $content->updateRedirect( $target );
+
+               $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
+               $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
+
+               $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
+       }
+
        # =================================================================================================================
 
        public function testGetModel() {
@@ -418,4 +511,47 @@ just a test"
                $this->assertEquals( $equal, $a->equals( $b ) );
        }
 
+       public function dataGetDeletionUpdates() {
+               return array(
+                       array("WikitextContentTest_testGetSecondaryDataUpdates_1",
+                               CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
+                               array( 'LinksDeletionUpdate' => array( ) )
+                       ),
+                       array("WikitextContentTest_testGetSecondaryDataUpdates_2",
+                               CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
+                               array( 'LinksDeletionUpdate' => array( ) )
+                       ),
+                       // @todo: more...?
+               );
+       }
+
+       /**
+        * @dataProvider dataGetDeletionUpdates
+        */
+       public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
+               $title = Title::newFromText( $title );
+               $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
+
+               $content = ContentHandler::makeContent( $text, $title, $model );
+
+               $updates = $content->getDeletionUpdates( WikiPage::factory( $title ) );
+
+               // make updates accessible by class name
+               foreach ( $updates as $update ) {
+                       $class = get_class( $update );
+                       $updates[ $class ] = $update;
+               }
+
+               foreach ( $expectedStuff as $class => $fieldValues ) {
+                       $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
+
+                       $update = $updates[ $class ];
+
+                       foreach ( $fieldValues as $field => $value ) {
+                               $v = $update->$field; #if the field doesn't exist, just crash and burn
+                               $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
+                       }
+               }
+       }
+
 }