Merge "Prevent fatal PHP errors when PageRestriction::getTitle() returns null."
[lhc/web/wiklou.git] / tests / phpunit / includes / user / UserTest.php
index b9289db..863ff50 100644 (file)
@@ -4,6 +4,7 @@ define( 'NS_UNITTEST', 5600 );
 define( 'NS_UNITTEST_TALK', 5601 );
 
 use MediaWiki\Block\Restriction\PageRestriction;
+use MediaWiki\Block\Restriction\NamespaceRestriction;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\User\UserIdentityValue;
 use Wikimedia\TestingAccessWrapper;
@@ -392,6 +393,7 @@ class UserTest extends MediaWikiTestCase {
                                ],
                        ],
                ] );
+               $this->hideDeprecated( 'User::getPasswordValidity' );
 
                $user = static::getTestUser()->getUser();
 
@@ -581,6 +583,7 @@ class UserTest extends MediaWikiTestCase {
         * When a user is autoblocked a cookie is set with which to track them
         * in case they log out and change IP addresses.
         * @link https://phabricator.wikimedia.org/T5233
+        * @covers User::trackBlockWithCookie
         */
        public function testAutoblockCookies() {
                // Set up the bits of global configuration that we use.
@@ -663,6 +666,7 @@ class UserTest extends MediaWikiTestCase {
        /**
         * Make sure that no cookie is set to track autoblocked users
         * when $wgCookieSetOnAutoblock is false.
+        * @covers User::trackBlockWithCookie
         */
        public function testAutoblockCookiesDisabled() {
                // Set up the bits of global configuration that we use.
@@ -710,6 +714,7 @@ class UserTest extends MediaWikiTestCase {
         * When a user is autoblocked and a cookie is set to track them, the expiry time of the cookie
         * should match the block's expiry, to a maximum of 24 hours. If the expiry time is changed,
         * the cookie's should change with it.
+        * @covers User::trackBlockWithCookie
         */
        public function testAutoblockCookieInfiniteExpiry() {
                $this->setMwGlobals( [
@@ -774,6 +779,9 @@ class UserTest extends MediaWikiTestCase {
                $block->delete();
        }
 
+       /**
+        * @covers User::getBlockedStatus
+        */
        public function testSoftBlockRanges() {
                global $wgUser;
 
@@ -805,6 +813,7 @@ class UserTest extends MediaWikiTestCase {
 
        /**
         * Test that a modified BlockID cookie doesn't actually load the relevant block (T152951).
+        * @covers User::trackBlockWithCookie
         */
        public function testAutoblockCookieInauthentic() {
                // Set up the bits of global configuration that we use.
@@ -851,6 +860,7 @@ class UserTest extends MediaWikiTestCase {
        /**
         * The BlockID cookie is normally verified with a HMAC, but not if wgSecretKey is not set.
         * This checks that a non-authenticated cookie still works.
+        * @covers User::trackBlockWithCookie
         */
        public function testAutoblockCookieNoSecretKey() {
                // Set up the bits of global configuration that we use.
@@ -1020,6 +1030,9 @@ class UserTest extends MediaWikiTestCase {
                $this->assertTrue( User::isLocallyBlockedProxy( $ip ) );
        }
 
+       /**
+        * @covers User::newFromActorId
+        */
        public function testActorId() {
                $domain = MediaWikiServices::getInstance()->getDBLoadBalancer()->getLocalDomainID();
                $this->hideDeprecated( 'User::selectFields' );
@@ -1083,6 +1096,9 @@ class UserTest extends MediaWikiTestCase {
                        'User::newFromActorId works for an anonymous user' );
        }
 
+       /**
+        * @covers User::newFromAnyId
+        */
        public function testNewFromAnyId() {
                // Registered user
                $user = $this->getTestUser()->getUser();
@@ -1244,6 +1260,9 @@ class UserTest extends MediaWikiTestCase {
                        );
                        $restrictions[] = new PageRestriction( 0, $page->getId() );
                }
+               foreach ( $options['namespaceRestrictions'] ?? [] as $ns ) {
+                       $restrictions[] = new NamespaceRestriction( 0, $ns );
+               }
 
                $block = new Block( [
                        'expiry' => wfTimestamp( TS_MW, wfTimestamp() + ( 40 * 60 * 60 ) ),
@@ -1266,42 +1285,85 @@ class UserTest extends MediaWikiTestCase {
 
        public static function provideIsBlockedFrom() {
                return [
-                       'Basic operation' => [ 'Test page', true ],
-                       'User talk page, not allowed' => [ self::USER_TALK_PAGE, true, [
-                               'allowUsertalk' => false,
-                       ] ],
-                       'User talk page, allowed' => [ self::USER_TALK_PAGE, false, [
-                               'allowUsertalk' => true,
-                       ] ],
-                       'User talk page, allowed but $wgBlockAllowsUTEdit is false' => [ self::USER_TALK_PAGE, true, [
-                               'allowUsertalk' => true,
-                               'blockAllowsUTEdit' => false,
-                       ] ],
-
-                       'Partial block, blocking the page' => [ 'Test page', true, [
-                               'pageRestrictions' => [ 'Test page' ],
-                       ] ],
-                       'Partial block, not blocking the page' => [ 'Test page 2', false, [
-                               'pageRestrictions' => [ 'Test page' ],
-                       ] ],
-                       'Partial block, overriding allowUsertalk' => [ self::USER_TALK_PAGE, true, [
-                               'allowUsertalk' => false,
-                               'pageRestrictions' => [ self::USER_TALK_PAGE ],
-                       ] ],
-                       'Partial block, allowing user talk' => [ self::USER_TALK_PAGE, false, [
-                               'allowUsertalk' => true,
-                               'pageRestrictions' => [ 'Test page' ],
-                       ] ],
-                       'Partial block, not allowing user talk' => [ self::USER_TALK_PAGE, true, [
-                               'allowUsertalk' => false,
-                               'pageRestrictions' => [ 'Test page' ],
-                       ] ],
+                       'Sitewide block, basic operation' => [ 'Test page', true ],
+                       'Sitewide block, not allowing user talk' => [
+                               self::USER_TALK_PAGE, true, [
+                                       'allowUsertalk' => false,
+                               ]
+                       ],
+                       'Sitewide block, allowing user talk' => [
+                               self::USER_TALK_PAGE, false, [
+                                       'allowUsertalk' => true,
+                               ]
+                       ],
+                       'Sitewide block, allowing user talk but $wgBlockAllowsUTEdit is false' => [
+                               self::USER_TALK_PAGE, true, [
+                                       'allowUsertalk' => true,
+                                       'blockAllowsUTEdit' => false,
+                               ]
+                       ],
+                       'Partial block, blocking the page' => [
+                               'Test page', true, [
+                                       'pageRestrictions' => [ 'Test page' ],
+                               ]
+                       ],
+                       'Partial block, not blocking the page' => [
+                               'Test page 2', false, [
+                                       'pageRestrictions' => [ 'Test page' ],
+                               ]
+                       ],
+                       'Partial block, not allowing user talk but user talk page is not blocked' => [
+                               self::USER_TALK_PAGE, false, [
+                                       'allowUsertalk' => false,
+                                       'pageRestrictions' => [ 'Test page' ],
+                               ]
+                       ],
+                       'Partial block, allowing user talk but user talk page is blocked' => [
+                               self::USER_TALK_PAGE, true, [
+                                       'allowUsertalk' => true,
+                                       'pageRestrictions' => [ self::USER_TALK_PAGE ],
+                               ]
+                       ],
+                       'Partial block, user talk page is not blocked but $wgBlockAllowsUTEdit is false' => [
+                               self::USER_TALK_PAGE, false, [
+                                       'allowUsertalk' => false,
+                                       'pageRestrictions' => [ 'Test page' ],
+                                       'blockAllowsUTEdit' => false,
+                               ]
+                       ],
+                       'Partial block, user talk page is blocked and $wgBlockAllowsUTEdit is false' => [
+                               self::USER_TALK_PAGE, true, [
+                                       'allowUsertalk' => true,
+                                       'pageRestrictions' => [ self::USER_TALK_PAGE ],
+                                       'blockAllowsUTEdit' => false,
+                               ]
+                       ],
+                       'Partial user talk namespace block, not allowing user talk' => [
+                               self::USER_TALK_PAGE, true, [
+                                       'allowUsertalk' => false,
+                                       'namespaceRestrictions' => [ NS_USER_TALK ],
+                               ]
+                       ],
+                       'Partial user talk namespace block, allowing user talk' => [
+                               self::USER_TALK_PAGE, false, [
+                                       'allowUsertalk' => true,
+                                       'namespaceRestrictions' => [ NS_USER_TALK ],
+                               ]
+                       ],
+                       'Partial user talk namespace block, where $wgBlockAllowsUTEdit is false' => [
+                               self::USER_TALK_PAGE, true, [
+                                       'allowUsertalk' => true,
+                                       'namespaceRestrictions' => [ NS_USER_TALK ],
+                                       'blockAllowsUTEdit' => false,
+                               ]
+                       ],
                ];
        }
 
        /**
         * Block cookie should be set for IP Blocks if
         * wgCookieSetOnIpBlock is set to true
+        * @covers User::trackBlockWithCookie
         */
        public function testIpBlockCookieSet() {
                $this->setMwGlobals( [
@@ -1337,6 +1399,7 @@ class UserTest extends MediaWikiTestCase {
        /**
         * Block cookie should NOT be set when wgCookieSetOnIpBlock
         * is disabled
+        * @covers User::trackBlockWithCookie
         */
        public function testIpBlockCookieNotSet() {
                $this->setMwGlobals( [
@@ -1372,6 +1435,7 @@ class UserTest extends MediaWikiTestCase {
        /**
         * When an ip user is blocked and then they log in, cookie block
         * should be invalid and the cookie removed.
+        * @covers User::trackBlockWithCookie
         */
        public function testIpBlockCookieIgnoredWhenUserLoggedIn() {
                $this->setMwGlobals( [