RevisionStorageTest: use MediaWikiTestCase methods for setting globals
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionStorageTest.php
index 642ada2..a15b9b4 100644 (file)
@@ -16,12 +16,13 @@ class RevisionStorageTest extends MediaWikiTestCase {
         */
        private $the_page;
 
-       function __construct( $name = null, array $data = [], $dataName = '' ) {
+       public function __construct( $name = null, array $data = [], $dataName = '' ) {
                parent::__construct( $name, $data, $dataName );
 
                $this->tablesUsed = array_merge( $this->tablesUsed,
                        [ 'page',
                                'revision',
+                               'ip_changes',
                                'text',
 
                                'recentchanges',
@@ -38,18 +39,35 @@ class RevisionStorageTest extends MediaWikiTestCase {
        }
 
        protected function setUp() {
-               global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
+               global $wgContLang;
 
                parent::setUp();
 
-               $wgExtraNamespaces[12312] = 'Dummy';
-               $wgExtraNamespaces[12313] = 'Dummy_talk';
+               $this->mergeMwGlobalArrayValue(
+                       'wgExtraNamespaces',
+                       [
+                               12312 => 'Dummy',
+                               12313 => 'Dummy_talk',
+                       ]
+               );
+
+               $this->mergeMwGlobalArrayValue(
+                       'wgNamespaceContentModels',
+                       [
+                               12312 => 'DUMMY',
+                       ]
+               );
 
-               $wgNamespaceContentModels[12312] = 'DUMMY';
-               $wgContentHandlers['DUMMY'] = 'DummyContentHandlerForTesting';
+               $this->mergeMwGlobalArrayValue(
+                       'wgContentHandlers',
+                       [
+                               'DUMMY' => 'DummyContentHandlerForTesting',
+                       ]
+               );
 
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
-               $wgContLang->resetNamespaces(); # reset namespace cache
+               MWNamespace::clearCaches();
+               // Reset namespace cache
+               $wgContLang->resetNamespaces();
                if ( !$this->the_page ) {
                        $this->the_page = $this->createPage(
                                'RevisionStorageTest_the_page',
@@ -62,18 +80,13 @@ class RevisionStorageTest extends MediaWikiTestCase {
        }
 
        protected function tearDown() {
-               global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers, $wgContLang;
+               global $wgContLang;
 
                parent::tearDown();
 
-               unset( $wgExtraNamespaces[12312] );
-               unset( $wgExtraNamespaces[12313] );
-
-               unset( $wgNamespaceContentModels[12312] );
-               unset( $wgContentHandlers['DUMMY'] );
-
-               MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
-               $wgContLang->resetNamespaces(); # reset namespace cache
+               MWNamespace::clearCaches();
+               // Reset namespace cache
+               $wgContLang->resetNamespaces();
        }
 
        protected function makeRevision( $props = null ) {
@@ -101,30 +114,33 @@ class RevisionStorageTest extends MediaWikiTestCase {
                return $rev;
        }
 
-       protected function createPage( $page, $text, $model = null ) {
-               if ( is_string( $page ) ) {
-                       if ( !preg_match( '/:/', $page ) &&
-                               ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
-                       ) {
-                               $ns = $this->getDefaultWikitextNS();
-                               $page = MWNamespace::getCanonicalName( $ns ) . ':' . $page;
-                       }
-
-                       $page = Title::newFromText( $page );
+       /**
+        * @param string $titleString
+        * @param string $text
+        * @param string|null $model
+        *
+        * @return WikiPage
+        */
+       protected function createPage( $titleString, $text, $model = null ) {
+               if ( !preg_match( '/:/', $titleString ) &&
+                       ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
+               ) {
+                       $ns = $this->getDefaultWikitextNS();
+                       $titleString = MWNamespace::getCanonicalName( $ns ) . ':' . $titleString;
                }
 
-               if ( $page instanceof Title ) {
-                       $page = new WikiPage( $page );
-               }
+               $title = Title::newFromText( $titleString );
+               $wikipage = new WikiPage( $title );
 
-               if ( $page->exists() ) {
-                       $page->doDeleteArticle( "done" );
+               // Delete the article if it already exists
+               if ( $wikipage->exists() ) {
+                       $wikipage->doDeleteArticle( "done" );
                }
 
-               $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
-               $page->doEditContent( $content, "testing", EDIT_NEW );
+               $content = ContentHandler::makeContent( $text, $title, $model );
+               $wikipage->doEditContent( $content, __METHOD__, EDIT_NEW );
 
-               return $page;
+               return $wikipage;
        }
 
        protected function assertRevEquals( Revision $orig, Revision $rev = null ) {
@@ -145,8 +161,8 @@ class RevisionStorageTest extends MediaWikiTestCase {
        public function testConstructFromRow() {
                $orig = $this->makeRevision();
 
-               $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( 'revision', '*', [ 'rev_id' => $orig->getId() ] );
+               $dbr = wfGetDB( DB_REPLICA );
+               $res = $dbr->select( 'revision', Revision::selectFields(), [ 'rev_id' => $orig->getId() ] );
                $this->assertTrue( is_object( $res ), 'query failed' );
 
                $row = $res->fetchObject();
@@ -157,14 +173,64 @@ class RevisionStorageTest extends MediaWikiTestCase {
                $this->assertRevEquals( $orig, $rev );
        }
 
+       /**
+        * @covers Revision::newFromTitle
+        */
+       public function testNewFromTitle_withoutId() {
+               $page = $this->createPage(
+                       __METHOD__,
+                       'GOAT',
+                       CONTENT_MODEL_WIKITEXT
+               );
+               $latestRevId = $page->getLatest();
+
+               $rev = Revision::newFromTitle( $page->getTitle() );
+
+               $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
+               $this->assertEquals( $latestRevId, $rev->getId() );
+       }
+
+       /**
+        * @covers Revision::newFromTitle
+        */
+       public function testNewFromTitle_withId() {
+               $page = $this->createPage(
+                       __METHOD__,
+                       'GOAT',
+                       CONTENT_MODEL_WIKITEXT
+               );
+               $latestRevId = $page->getLatest();
+
+               $rev = Revision::newFromTitle( $page->getTitle(), $latestRevId );
+
+               $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
+               $this->assertEquals( $latestRevId, $rev->getId() );
+       }
+
+       /**
+        * @covers Revision::newFromTitle
+        */
+       public function testNewFromTitle_withBadId() {
+               $page = $this->createPage(
+                       __METHOD__,
+                       'GOAT',
+                       CONTENT_MODEL_WIKITEXT
+               );
+               $latestRevId = $page->getLatest();
+
+               $rev = Revision::newFromTitle( $page->getTitle(), $latestRevId + 1 );
+
+               $this->assertNull( $rev );
+       }
+
        /**
         * @covers Revision::newFromRow
         */
        public function testNewFromRow() {
                $orig = $this->makeRevision();
 
-               $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( 'revision', '*', [ 'rev_id' => $orig->getId() ] );
+               $dbr = wfGetDB( DB_REPLICA );
+               $res = $dbr->select( 'revision', Revision::selectFields(), [ 'rev_id' => $orig->getId() ] );
                $this->assertTrue( is_object( $res ), 'query failed' );
 
                $row = $res->fetchObject();
@@ -187,8 +253,10 @@ class RevisionStorageTest extends MediaWikiTestCase {
                $orig = $page->getRevision();
                $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
 
-               $dbr = wfGetDB( DB_SLAVE );
-               $res = $dbr->select( 'archive', '*', [ 'ar_rev_id' => $orig->getId() ] );
+               $dbr = wfGetDB( DB_REPLICA );
+               $res = $dbr->select(
+                       'archive', Revision::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
+               );
                $this->assertTrue( is_object( $res ), 'query failed' );
 
                $row = $res->fetchObject();
@@ -438,21 +506,30 @@ class RevisionStorageTest extends MediaWikiTestCase {
                $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
        }
 
+       /**
+        * @covers Revision::insertOn
+        */
+       public function testInsertOn() {
+               $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
+
+               $orig = $this->makeRevision( [
+                       'user_text' => $ip
+               ] );
+
+               // Make sure the revision was copied to ip_changes
+               $dbr = wfGetDB( DB_REPLICA );
+               $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
+               $row = $res->fetchObject();
+
+               $this->assertEquals( IP::toHex( $ip ), $row->ipc_hex );
+               $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp );
+       }
+
        public static function provideUserWasLastToEdit() {
-               return [
-                       [ # 0
-                               3, true, # actually the last edit
-                       ],
-                       [ # 1
-                               2, true, # not the current edit, but still by this user
-                       ],
-                       [ # 2
-                               1, false, # edit by another user
-                       ],
-                       [ # 3
-                               0, false, # first edit, by this user, but another user edited in the mean time
-                       ],
-               ];
+               yield 'actually the last edit' => [ 3, true ];
+               yield 'not the current edit, but still by this user' => [ 2, true ];
+               yield 'edit by another user' => [ 1, false ];
+               yield 'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
        }
 
        /**
@@ -480,7 +557,6 @@ class RevisionStorageTest extends MediaWikiTestCase {
                        'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
                $page->insertOn( $dbw );
 
-               # zero
                $revisions[0] = new Revision( [
                        'page' => $page->getId(),
                        // we need the title to determine the page's default content model
@@ -493,7 +569,6 @@ class RevisionStorageTest extends MediaWikiTestCase {
                ] );
                $revisions[0]->insertOn( $dbw );
 
-               # one
                $revisions[1] = new Revision( [
                        'page' => $page->getId(),
                        // still need the title, because $page->getId() is 0 (there's no entry in the page table)
@@ -506,7 +581,6 @@ class RevisionStorageTest extends MediaWikiTestCase {
                ] );
                $revisions[1]->insertOn( $dbw );
 
-               # two
                $revisions[2] = new Revision( [
                        'page' => $page->getId(),
                        'title' => $page->getTitle(),
@@ -518,7 +592,6 @@ class RevisionStorageTest extends MediaWikiTestCase {
                ] );
                $revisions[2]->insertOn( $dbw );
 
-               # three
                $revisions[3] = new Revision( [
                        'page' => $page->getId(),
                        'title' => $page->getTitle(),
@@ -530,7 +603,6 @@ class RevisionStorageTest extends MediaWikiTestCase {
                ] );
                $revisions[3]->insertOn( $dbw );
 
-               # four
                $revisions[4] = new Revision( [
                        'page' => $page->getId(),
                        'title' => $page->getTitle(),