Merge "ParserTest: clear Language namespaces cache"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Mon, 26 Mar 2018 17:21:48 +0000 (17:21 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Mon, 26 Mar 2018 17:21:48 +0000 (17:21 +0000)
includes/api/ApiBlock.php
includes/api/ApiDelete.php
includes/installer/PostgresUpdater.php
tests/phpunit/includes/api/ApiBlockTest.php
tests/phpunit/includes/api/ApiDeleteTest.php

index f4aea98..8f40283 100644 (file)
@@ -124,8 +124,8 @@ class ApiBlock extends ApiBase {
                        $res['id'] = $block->getId();
                } else {
                        # should be unreachable
-                       $res['expiry'] = '';
-                       $res['id'] = '';
+                       $res['expiry'] = ''; // @codeCoverageIgnore
+                       $res['id'] = ''; // @codeCoverageIgnore
                }
 
                $res['reason'] = $params['reason'];
index e19f1f2..a63dee6 100644 (file)
@@ -116,7 +116,8 @@ class ApiDelete extends ApiBase {
                        $hasHistory = false;
                        $reason = $page->getAutoDeleteReason( $hasHistory );
                        if ( $reason === false ) {
-                               return Status::newFatal( 'cannotdelete', $title->getPrefixedText() );
+                               // Should be reachable only if the page has no revisions
+                               return Status::newFatal( 'cannotdelete', $title->getPrefixedText() ); // @codeCoverageIgnore
                        }
                }
 
index 48f47f5..ba6e968 100644 (file)
@@ -294,7 +294,7 @@ class PostgresUpdater extends DatabaseUpdater {
                                [ 'log_timestamp', 'timestamptz_ops', 'btree', 0 ],
                        ],
                        'CREATE INDEX "logging_times" ON "logging" USING "btree" ("log_timestamp")' ],
-                       [ 'dropIndex', 'oldimage', 'oi_name' ],
+                       [ 'dropPgIndex', 'oldimage', 'oi_name' ],
                        [ 'checkIndex', 'oi_name_archive_name', [
                                [ 'oi_name', 'text_ops', 'btree', 0 ],
                                [ 'oi_archive_name', 'text_ops', 'btree', 0 ],
@@ -353,7 +353,7 @@ class PostgresUpdater extends DatabaseUpdater {
                        [ 'checkOiNameConstraint' ],
                        [ 'checkPageDeletedTrigger' ],
                        [ 'checkRevUserFkey' ],
-                       [ 'dropIndex', 'ipblocks', 'ipb_address' ],
+                       [ 'dropPgIndex', 'ipblocks', 'ipb_address' ],
                        [ 'checkIndex', 'ipb_address_unique', [
                                [ 'ipb_address', 'text_ops', 'btree', 0 ],
                                [ 'ipb_user', 'int4_ops', 'btree', 0 ],
@@ -1060,7 +1060,7 @@ END;
                }
        }
 
-       protected function dropIndex( $table, $index, $patch = '', $fullpath = false ) {
+       protected function dropPgIndex( $table, $index ) {
                if ( $this->db->indexExists( $table, $index ) ) {
                        $this->output( "Dropping obsolete index '$index'\n" );
                        $this->db->query( "DROP INDEX \"" . $index . "\"" );
index 832a113..c0cf87e 100644 (file)
@@ -8,13 +8,17 @@
  * @covers ApiBlock
  */
 class ApiBlockTest extends ApiTestCase {
+       protected $mUser = null;
+
        protected function setUp() {
                parent::setUp();
                $this->doLogin();
+
+               $this->mUser = $this->getMutableTestUser()->getUser();
        }
 
        protected function tearDown() {
-               $block = Block::newFromTarget( 'UTApiBlockee' );
+               $block = Block::newFromTarget( $this->mUser->getName() );
                if ( !is_null( $block ) ) {
                        $block->delete();
                }
@@ -25,80 +29,192 @@ class ApiBlockTest extends ApiTestCase {
                return $this->getTokenList( self::$users['sysop'] );
        }
 
-       function addDBDataOnce() {
-               $user = User::newFromName( 'UTApiBlockee' );
-
-               if ( $user->getId() == 0 ) {
-                       $user->addToDatabase();
-                       TestUser::setPasswordForUser( $user, 'UTApiBlockeePassword' );
-
-                       $user->saveSettings();
-               }
-       }
-
        /**
-        * This test has probably always been broken and use an invalid token
-        * Bug tracking brokenness is https://phabricator.wikimedia.org/T37646
-        *
-        * Root cause is https://gerrit.wikimedia.org/r/3434
-        * Which made the Block/Unblock API to actually verify the token
-        * previously always considered valid (T36212).
+        * @param array $extraParams Extra API parameters to pass to doApiRequest
+        * @param User  $blocker     User to do the blocking, null to pick
+        *                           arbitrarily
         */
-       public function testMakeNormalBlock() {
-               $tokens = $this->getTokens();
+       private function doBlock( array $extraParams = [], User $blocker = null ) {
+               if ( $blocker === null ) {
+                       $blocker = self::$users['sysop']->getUser();
+               }
 
-               $user = User::newFromName( 'UTApiBlockee' );
+               $tokens = $this->getTokens();
 
-               if ( !$user->getId() ) {
-                       $this->markTestIncomplete( "The user UTApiBlockee does not exist" );
-               }
+               $this->assertNotNull( $this->mUser, 'Sanity check' );
+               $this->assertNotSame( 0, $this->mUser->getId(), 'Sanity check' );
 
-               if ( !array_key_exists( 'blocktoken', $tokens ) ) {
-                       $this->markTestIncomplete( "No block token found" );
-               }
+               $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
 
-               $this->doApiRequest( [
+               $params = [
                        'action' => 'block',
-                       'user' => 'UTApiBlockee',
+                       'user' => $this->mUser->getName(),
                        'reason' => 'Some reason',
-                       'token' => $tokens['blocktoken'] ], null, false, self::$users['sysop']->getUser() );
+                       'token' => $tokens['blocktoken'],
+               ];
+               if ( array_key_exists( 'userid', $extraParams ) ) {
+                       // Make sure we don't have both user and userid
+                       unset( $params['user'] );
+               }
+               $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
+                       false, $blocker );
 
-               $block = Block::newFromTarget( 'UTApiBlockee' );
+               $block = Block::newFromTarget( $this->mUser->getName() );
 
                $this->assertTrue( !is_null( $block ), 'Block is valid' );
 
-               $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
-               $this->assertEquals( 'Some reason', $block->mReason );
-               $this->assertEquals( 'infinity', $block->mExpiry );
+               $this->assertSame( $this->mUser->getName(), (string)$block->getTarget() );
+               $this->assertSame( 'Some reason', $block->mReason );
+
+               return $ret;
+       }
+
+       /**
+        * Block by username
+        */
+       public function testNormalBlock() {
+               $this->doBlock();
        }
 
        /**
         * Block by user ID
         */
-       public function testMakeNormalBlockId() {
-               $tokens = $this->getTokens();
-               $user = User::newFromName( 'UTApiBlockee' );
+       public function testBlockById() {
+               $this->doBlock( [ 'userid' => $this->mUser->getId() ] );
+       }
 
-               if ( !$user->getId() ) {
-                       $this->markTestIncomplete( "The user UTApiBlockee does not exist." );
-               }
+       /**
+        * A blocked user can't block
+        */
+       public function testBlockByBlockedUser() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'You cannot block or unblock other users because you are yourself blocked.' );
+
+               $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
+               $block = new Block( [
+                       'address' => $blocked->getName(),
+                       'by' => self::$users['sysop']->getUser()->getId(),
+                       'reason' => 'Capriciousness',
+                       'timestamp' => '19370101000000',
+                       'expiry' => 'infinity',
+               ] );
+               $block->insert();
+
+               $this->doBlock( [], $blocked );
+       }
 
-               if ( !array_key_exists( 'blocktoken', $tokens ) ) {
-                       $this->markTestIncomplete( "No block token found" );
-               }
+       public function testBlockOfNonexistentUser() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'There is no user by the name "Nonexistent". Check your spelling.' );
 
-               $data = $this->doApiRequest( [
-                       'action' => 'block',
-                       'userid' => $user->getId(),
-                       'reason' => 'Some reason',
-                       'token' => $tokens['blocktoken'] ], null, false, self::$users['sysop']->getUser() );
+               $this->doBlock( [ 'user' => 'Nonexistent' ] );
+       }
+
+       public function testBlockOfNonexistentUserId() {
+               $id = 948206325;
+               $this->setExpectedException( ApiUsageException::class,
+                       "There is no user with ID $id." );
+
+               $this->assertFalse( User::whoIs( $id ), 'Sanity check' );
+
+               $this->doBlock( [ 'userid' => $id ] );
+       }
+
+       public function testBlockWithTag() {
+               ChangeTags::defineTag( 'custom tag' );
+
+               $this->doBlock( [ 'tags' => 'custom tag' ] );
+
+               $dbw = wfGetDB( DB_MASTER );
+               $this->assertSame( 'custom tag', $dbw->selectField(
+                       [ 'change_tag', 'logging' ],
+                       'ct_tag',
+                       [ 'log_type' => 'block' ],
+                       __METHOD__,
+                       [],
+                       [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
+               ) );
+       }
+
+       public function testBlockWithProhibitedTag() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'You do not have permission to apply change tags along with your changes.' );
+
+               ChangeTags::defineTag( 'custom tag' );
+
+               $this->setMwGlobals( 'wgRevokePermissions',
+                       [ 'user' => [ 'applychangetags' => true ] ] );
+
+               $this->doBlock( [ 'tags' => 'custom tag' ] );
+       }
+
+       public function testBlockWithHide() {
+               global $wgGroupPermissions;
+               $newPermissions = $wgGroupPermissions['sysop'];
+               $newPermissions['hideuser'] = true;
+               $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
+                       [ 'sysop' => $newPermissions ] );
+
+               $res = $this->doBlock( [ 'hidename' => '' ] );
+
+               $dbw = wfGetDB( DB_MASTER );
+               $this->assertSame( '1', $dbw->selectField(
+                       'ipblocks',
+                       'ipb_deleted',
+                       [ 'ipb_id' => $res[0]['block']['id'] ],
+                       __METHOD__
+               ) );
+       }
+
+       public function testBlockWithProhibitedHide() {
+               $this->setExpectedException( ApiUsageException::class,
+                       "You don't have permission to hide user names from the block log." );
+
+               $this->doBlock( [ 'hidename' => '' ] );
+       }
+
+       public function testBlockWithEmailBlock() {
+               $res = $this->doBlock( [ 'noemail' => '' ] );
+
+               $dbw = wfGetDB( DB_MASTER );
+               $this->assertSame( '1', $dbw->selectField(
+                       'ipblocks',
+                       'ipb_block_email',
+                       [ 'ipb_id' => $res[0]['block']['id'] ],
+                       __METHOD__
+               ) );
+       }
+
+       public function testBlockWithProhibitedEmailBlock() {
+               $this->setExpectedException( ApiUsageException::class,
+                       "You don't have permission to block users from sending email through the wiki." );
+
+               $this->setMwGlobals( 'wgRevokePermissions',
+                       [ 'sysop' => [ 'blockemail' => true ] ] );
+
+               $this->doBlock( [ 'noemail' => '' ] );
+       }
+
+       public function testBlockWithExpiry() {
+               $res = $this->doBlock( [ 'expiry' => '1 day' ] );
+
+               $dbw = wfGetDB( DB_MASTER );
+               $expiry = $dbw->selectField(
+                       'ipblocks',
+                       'ipb_expiry',
+                       [ 'ipb_id' => $res[0]['block']['id'] ],
+                       __METHOD__
+               );
+
+               // Allow flakiness up to one second
+               $this->assertLessThan( 1,
+                       abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
+       }
 
-               $block = Block::newFromTarget( 'UTApiBlockee' );
+       public function testBlockWithInvalidExpiry() {
+               $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
 
-               $this->assertTrue( !is_null( $block ), 'Block is valid.' );
-               $this->assertEquals( 'UTApiBlockee', (string)$block->getTarget() );
-               $this->assertEquals( 'Some reason', $block->mReason );
-               $this->assertEquals( 'infinity', $block->mExpiry );
+               $this->doBlock( [ 'expiry' => '' ] );
        }
 
        /**
@@ -109,7 +225,7 @@ class ApiBlockTest extends ApiTestCase {
                $this->doApiRequest(
                        [
                                'action' => 'block',
-                               'user' => 'UTApiBlockee',
+                               'user' => $this->mUser->getName(),
                                'reason' => 'Some reason',
                        ],
                        null,
index 87167f0..c9ce28e 100644 (file)
@@ -20,18 +20,7 @@ class ApiDeleteTest extends ApiTestCase {
        }
 
        public function testDelete() {
-               $name = 'Help:ApiDeleteTest_testDelete';
-
-               // test non-existing page
-               try {
-                       $this->doApiRequestWithToken( [
-                               'action' => 'delete',
-                               'title' => $name,
-                       ] );
-                       $this->fail( "Should have raised an ApiUsageException" );
-               } catch ( ApiUsageException $e ) {
-                       $this->assertTrue( self::apiExceptionHasCode( $e, 'missingtitle' ) );
-               }
+               $name = 'Help:' . ucfirst( __FUNCTION__ );
 
                // create new page
                $this->editPage( $name, 'Some text' );
@@ -40,23 +29,31 @@ class ApiDeleteTest extends ApiTestCase {
                $apiResult = $this->doApiRequestWithToken( [
                        'action' => 'delete',
                        'title' => $name,
-               ] );
-               $apiResult = $apiResult[0];
+               ] )[0];
 
                $this->assertArrayHasKey( 'delete', $apiResult );
                $this->assertArrayHasKey( 'title', $apiResult['delete'] );
-               // Normalized $name is used
-               $this->assertSame(
-                       'Help:ApiDeleteTest testDelete',
-                       $apiResult['delete']['title']
-               );
+               $this->assertSame( $name, $apiResult['delete']['title'] );
                $this->assertArrayHasKey( 'logid', $apiResult['delete'] );
 
                $this->assertFalse( Title::newFromText( $name )->exists() );
        }
 
+       public function testDeleteNonexistent() {
+               $this->setExpectedException( ApiUsageException::class,
+                       "The page you specified doesn't exist." );
+
+               $this->doApiRequestWithToken( [
+                       'action' => 'delete',
+                       'title' => 'This page deliberately left nonexistent',
+               ] );
+       }
+
        public function testDeletionWithoutPermission() {
-               $name = 'Help:ApiDeleteTest_testDeleteWithoutPermission';
+               $this->setExpectedException( ApiUsageException::class,
+                       'The action you have requested is limited to users in the group:' );
+
+               $name = 'Help:' . ucfirst( __FUNCTION__ );
 
                // create new page
                $this->editPage( $name, 'Some text' );
@@ -69,11 +66,110 @@ class ApiDeleteTest extends ApiTestCase {
                                'title' => $name,
                                'token' => $user->getEditToken(),
                        ], null, null, $user );
-                       $this->fail( "Should have raised an ApiUsageException" );
-               } catch ( ApiUsageException $e ) {
-                       $this->assertTrue( self::apiExceptionHasCode( $e, 'permissiondenied' ) );
+               } finally {
+                       $this->assertTrue( Title::newFromText( $name )->exists() );
+               }
+       }
+
+       public function testDeleteWithTag() {
+               $name = 'Help:' . ucfirst( __FUNCTION__ );
+
+               ChangeTags::defineTag( 'custom tag' );
+
+               $this->editPage( $name, 'Some text' );
+
+               $this->doApiRequestWithToken( [
+                       'action' => 'delete',
+                       'title' => $name,
+                       'tags' => 'custom tag',
+               ] );
+
+               $this->assertFalse( Title::newFromText( $name )->exists() );
+
+               $dbw = wfGetDB( DB_MASTER );
+               $this->assertSame( 'custom tag', $dbw->selectField(
+                       [ 'change_tag', 'logging' ],
+                       'ct_tag',
+                       [
+                               'log_namespace' => NS_HELP,
+                               'log_title' => ucfirst( __FUNCTION__ ),
+                       ],
+                       __METHOD__,
+                       [],
+                       [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
+               ) );
+       }
+
+       public function testDeleteWithoutTagPermission() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'You do not have permission to apply change tags along with your changes.' );
+
+               $name = 'Help:' . ucfirst( __FUNCTION__ );
+
+               ChangeTags::defineTag( 'custom tag' );
+               $this->setMwGlobals( 'wgRevokePermissions',
+                       [ 'user' => [ 'applychangetags' => true ] ] );
+
+               $this->editPage( $name, 'Some text' );
+
+               try {
+                       $this->doApiRequestWithToken( [
+                               'action' => 'delete',
+                               'title' => $name,
+                               'tags' => 'custom tag',
+                       ] );
+               } finally {
+                       $this->assertTrue( Title::newFromText( $name )->exists() );
+               }
+       }
+
+       public function testDeleteAbortedByHook() {
+               $this->setExpectedException( ApiUsageException::class,
+                       'Deletion aborted by hook. It gave no explanation.' );
+
+               $name = 'Help:' . ucfirst( __FUNCTION__ );
+
+               $this->editPage( $name, 'Some text' );
+
+               $this->setTemporaryHook( 'ArticleDelete',
+                       function () {
+                               return false;
+                       }
+               );
+
+               try {
+                       $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name ] );
+               } finally {
+                       $this->assertTrue( Title::newFromText( $name )->exists() );
                }
+       }
+
+       public function testDeleteWatch() {
+               $name = 'Help:' . ucfirst( __FUNCTION__ );
+               $user = self::$users['sysop']->getUser();
+
+               $this->editPage( $name, 'Some text' );
+               $this->assertTrue( Title::newFromText( $name )->exists() );
+               $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
+
+               $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'watch' => '' ] );
 
+               $this->assertFalse( Title::newFromText( $name )->exists() );
+               $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
+       }
+
+       public function testDeleteUnwatch() {
+               $name = 'Help:' . ucfirst( __FUNCTION__ );
+               $user = self::$users['sysop']->getUser();
+
+               $this->editPage( $name, 'Some text' );
                $this->assertTrue( Title::newFromText( $name )->exists() );
+               $user->addWatch( Title::newFromText( $name ) );
+               $this->assertTrue( $user->isWatched( Title::newFromText( $name ) ) );
+
+               $this->doApiRequestWithToken( [ 'action' => 'delete', 'title' => $name, 'unwatch' => '' ] );
+
+               $this->assertFalse( Title::newFromText( $name )->exists() );
+               $this->assertFalse( $user->isWatched( Title::newFromText( $name ) ) );
        }
 }