Merge "Add logic for "tags" in ApiBase"
[lhc/web/wiklou.git] / tests / phpunit / includes / EditPageTest.php
index 15778e4..90ee1bb 100644 (file)
  */
 class EditPageTest extends MediaWikiLangTestCase {
 
+       protected function setUp() {
+               global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
+
+               parent::setUp();
+
+               $this->setMwGlobals( array(
+                       'wgExtraNamespaces' => $wgExtraNamespaces,
+                       'wgNamespaceContentModels' => $wgNamespaceContentModels,
+                       'wgContentHandlers' => $wgContentHandlers,
+                       'wgContLang' => $wgContLang,
+               ) );
+
+               $wgExtraNamespaces[12312] = 'Dummy';
+               $wgExtraNamespaces[12313] = 'Dummy_talk';
+
+               $wgNamespaceContentModels[12312] = "testing";
+               $wgContentHandlers["testing"] = 'DummyContentHandlerForTesting';
+
+               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
+               $wgContLang->resetNamespaces(); # reset namespace cache
+       }
+
        /**
         * @dataProvider provideExtractSectionTitle
         * @covers EditPage::extractSectionTitle
@@ -64,7 +86,7 @@ class EditPageTest extends MediaWikiLangTestCase {
         * @param string $msg
         */
        protected function assertEditedTextEquals( $expected, $actual, $msg = '' ) {
-               return $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg );
+               $this->assertEquals( rtrim( $expected ), rtrim( $actual ), $msg );
        }
 
        /**
@@ -116,7 +138,7 @@ class EditPageTest extends MediaWikiLangTestCase {
                        $page->doEditContent( $content, "base text for test" );
                        $this->forceRevisionDate( $page, '20120101000000' );
 
-                       //sanity check
+                       // sanity check
                        $page->clear();
                        $currentText = ContentHandler::getContentText( $page->getContent() );
 
@@ -250,6 +272,25 @@ class EditPageTest extends MediaWikiLangTestCase {
        public function testCreatePage(
                $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
        ) {
+               $checkId = null;
+
+               $this->setMwGlobals( 'wgHooks', array(
+                       'PageContentInsertComplete' => array( function (
+                               WikiPage &$page, User &$user, Content $content,
+                               $summary, $minor, $u1, $u2, &$flags, Revision $revision
+                       ) {
+                               // types/refs checked
+                       } ),
+                       'PageContentSaveComplete' => array( function (
+                               WikiPage &$page, User &$user, Content $content,
+                               $summary, $minor, $u1, $u2, &$flags, Revision $revision,
+                               Status &$status, $baseRevId
+                       ) use ( &$checkId ) {
+                               $checkId = $status->value['revision']->getId();
+                               // types/refs checked
+                       } ),
+               ) );
+
                $edit = array( 'wpTextbox1' => $editText );
                if ( $ignoreBlank ) {
                        $edit['wpIgnoreBlankArticle'] = 1;
@@ -258,7 +299,67 @@ class EditPageTest extends MediaWikiLangTestCase {
                $page = $this->assertEdit( $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
 
                if ( $expectedCode != EditPage::AS_BLANK_ARTICLE ) {
+                       $latest = $page->getLatest();
+                       $page->doDeleteArticleReal( $pageTitle );
+
+                       $this->assertGreaterThan( 0, $latest, "Page revision ID updated in object" );
+                       $this->assertEquals( $latest, $checkId, "Revision in Status for hook" );
+               }
+       }
+
+       /**
+        * @dataProvider provideCreatePages
+        * @covers EditPage
+        */
+       public function testCreatePageTrx(
+               $desc, $pageTitle, $user, $editText, $expectedCode, $expectedText, $ignoreBlank = false
+       ) {
+               $checkIds = array();
+               $this->setMwGlobals( 'wgHooks', array(
+                       'PageContentInsertComplete' => array( function (
+                               WikiPage &$page, User &$user, Content $content,
+                               $summary, $minor, $u1, $u2, &$flags, Revision $revision
+                       ) {
+                               // types/refs checked
+                       } ),
+                       'PageContentSaveComplete' => array( function (
+                               WikiPage &$page, User &$user, Content $content,
+                               $summary, $minor, $u1, $u2, &$flags, Revision $revision,
+                               Status &$status, $baseRevId
+                       ) use ( &$checkIds ) {
+                               $checkIds[] = $status->value['revision']->getId();
+                               // types/refs checked
+                       } ),
+               ) );
+
+               wfGetDB( DB_MASTER )->begin( __METHOD__ );
+
+               $edit = array( 'wpTextbox1' => $editText );
+               if ( $ignoreBlank ) {
+                       $edit['wpIgnoreBlankArticle'] = 1;
+               }
+
+               $page = $this->assertEdit(
+                       $pageTitle, null, $user, $edit, $expectedCode, $expectedText, $desc );
+
+               $pageTitle2 = (string)$pageTitle . '/x';
+               $page2 = $this->assertEdit(
+                       $pageTitle2, null, $user, $edit, $expectedCode, $expectedText, $desc );
+
+               wfGetDB( DB_MASTER )->commit( __METHOD__ );
+
+               if ( $expectedCode != EditPage::AS_BLANK_ARTICLE ) {
+                       $latest = $page->getLatest();
                        $page->doDeleteArticleReal( $pageTitle );
+
+                       $this->assertGreaterThan( 0, $latest, "Page #1 revision ID updated in object" );
+                       $this->assertEquals( $latest, $checkIds[0], "Revision #1 in Status for hook" );
+
+                       $latest2 = $page2->getLatest();
+                       $page2->doDeleteArticleReal( $pageTitle2 );
+
+                       $this->assertGreaterThan( 0, $latest2, "Page #2 revision ID updated in object" );
+                       $this->assertEquals( $latest2, $checkIds[1], "Revision #2 in Status for hook" );
                }
        }
 
@@ -313,7 +414,7 @@ hello
                $textWithNewSectionAdded = "$text\n$newSection";
 
                return array(
-                       array( #0
+                       array( # 0
                                $text,
                                '',
                                'hello',
@@ -321,7 +422,7 @@ hello
                                'hello'
                        ),
 
-                       array( #1
+                       array( # 1
                                $text,
                                '1',
                                $sectionOne,
@@ -329,7 +430,7 @@ hello
                                $textWithNewSectionOne,
                        ),
 
-                       array( #2
+                       array( # 2
                                $text,
                                'new',
                                'hello',
@@ -358,14 +459,14 @@ hello
        public static function provideAutoMerge() {
                $tests = array();
 
-               $tests[] = array( #0: plain conflict
+               $tests[] = array( # 0: plain conflict
                        "Elmo", # base edit user
                        "one\n\ntwo\n\nthree\n",
-                       array( #adam's edit
+                       array( # adam's edit
                                'wpStarttime' => 1,
                                'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
                        ),
-                       array( #berta's edit
+                       array( # berta's edit
                                'wpStarttime' => 2,
                                'wpTextbox1' => "(one)\n\ntwo\n\nthree\n",
                        ),
@@ -374,14 +475,14 @@ hello
                        'expected edit conflict', # message
                );
 
-               $tests[] = array( #1: successful merge
+               $tests[] = array( # 1: successful merge
                        "Elmo", # base edit user
                        "one\n\ntwo\n\nthree\n",
-                       array( #adam's edit
+                       array( # adam's edit
                                'wpStarttime' => 1,
                                'wpTextbox1' => "ONE\n\ntwo\n\nthree\n",
                        ),
-                       array( #berta's edit
+                       array( # berta's edit
                                'wpStarttime' => 2,
                                'wpTextbox1' => "one\n\ntwo\n\nTHREE\n",
                        ),
@@ -402,15 +503,15 @@ hello
                // generate expected text after merge
                $expected = str_replace( 'one', 'ONE', str_replace( 'three', 'THREE', $text ) );
 
-               $tests[] = array( #2: merge in section
+               $tests[] = array( # 2: merge in section
                        "Elmo", # base edit user
                        $text,
-                       array( #adam's edit
+                       array( # adam's edit
                                'wpStarttime' => 1,
                                'wpTextbox1' => str_replace( 'one', 'ONE', $section ),
                                'wpSection' => '1'
                        ),
-                       array( #berta's edit
+                       array( # berta's edit
                                'wpStarttime' => 2,
                                'wpTextbox1' => str_replace( 'three', 'THREE', $section ),
                                'wpSection' => '1'
@@ -441,9 +542,9 @@ hello
        public function testAutoMerge( $baseUser, $text, $adamsEdit, $bertasEdit,
                $expectedCode, $expectedText, $message = null
        ) {
-               $this->checkHasDiff3();
+               $this->markTestSkippedIfNoDiff3();
 
-               //create page
+               // create page
                $ns = $this->getDefaultWikitextNS();
                $title = Title::newFromText( 'EditPageTest_testAutoMerge', $ns );
                $page = WikiPage::factory( $title );
@@ -499,4 +600,37 @@ hello
                $this->assertEdit( 'EditPageTest_testAutoMerge', null, 'Berta', $bertasEdit,
                        $expectedCode, $expectedText, $message );
        }
+
+       /**
+        * @depends testAutoMerge
+        */
+       public function testCheckDirectEditingDisallowed_forNonTextContent() {
+               $title = Title::newFromText( 'Dummy:NonTextPageForEditPage' );
+               $page = WikiPage::factory( $title );
+
+               $article = new Article( $title );
+               $article->getContext()->setTitle( $title );
+               $ep = new EditPage( $article );
+               $ep->setContextTitle( $title );
+
+               $user = $GLOBALS['wgUser'];
+
+               $edit = array(
+                       'wpTextbox1' => serialize( 'non-text content' ),
+                       'wpEditToken' => $user->getEditToken(),
+                       'wpEdittime' => '',
+                       'wpStarttime' => wfTimestampNow()
+               );
+
+               $req = new FauxRequest( $edit, true );
+               $ep->importFormData( $req );
+
+               $this->setExpectedException(
+                       'MWException',
+                       'This content model is not supported: testing'
+               );
+
+               $ep->internalAttemptSave( $result, false );
+       }
+
 }