Merge "Add SearchResultTrait"
[lhc/web/wiklou.git] / tests / phpunit / includes / title / NamespaceInfoTest.php
index 7eb8fd5..7f97a16 100644 (file)
@@ -6,8 +6,11 @@
  */
 
 use MediaWiki\Config\ServiceOptions;
+use MediaWiki\Linker\LinkTarget;
 
 class NamespaceInfoTest extends MediaWikiTestCase {
+       use TestAllServiceOptionsUsed;
+
        /**********************************************************************************************
         * Shared code
         * %{
@@ -51,19 +54,20 @@ class NamespaceInfoTest extends MediaWikiTestCase {
                'ExtraNamespaces' => [],
                'ExtraSignatureNamespaces' => [],
                'NamespaceContentModels' => [],
-               'NamespaceProtection' => [],
                'NamespacesWithSubpages' => [
                        NS_TALK => true,
                        NS_USER => true,
                        NS_USER_TALK => true,
                ],
                'NonincludableNamespaces' => [],
-               'RestrictionLevels' => [ '', 'autoconfirmed', 'sysop' ],
        ];
 
        private function newObj( array $options = [] ) : NamespaceInfo {
-               return new NamespaceInfo( new ServiceOptions( NamespaceInfo::$constructorOptions,
-                       $options, self::$defaultOptions ) );
+               return new NamespaceInfo( new LoggedServiceOptions(
+                       self::$serviceOptionsAccessLog,
+                       NamespaceInfo::$constructorOptions,
+                       $options, self::$defaultOptions
+               ) );
        }
 
        // %} End shared code
@@ -688,73 +692,88 @@ class NamespaceInfoTest extends MediaWikiTestCase {
 
        /**
         * @dataProvider provideSpecialNamespaces
-        * @covers NamespaceInfo::getTalk
-        * @covers NamespaceInfo::getTalkPage
+        * @covers NamespaceInfo::getAssociated
         * @covers NamespaceInfo::isMethodValidFor
         *
         * @param int $ns
         */
-       public function testGetTalkPage_special( $ns ) {
-               $this->setExpectedException( MWException::class,
-                       "NamespaceInfo::getTalk does not make any sense for given namespace $ns" );
-               $this->newObj()->getTalkPage( new TitleValue( $ns, 'A' ) );
+       public function testGetAssociated_special( $ns ) {
+               $this->setExpectedException(
+                       MWException::class,
+                       "NamespaceInfo::getAssociated does not make any sense for given namespace $ns"
+               );
+               $this->newObj()->getAssociated( $ns );
+       }
+
+       public static function provideCanHaveTalkPage() {
+               return [
+                       [ new TitleValue( NS_MAIN, 'Test' ), true ],
+                       [ new TitleValue( NS_TALK, 'Test' ), true ],
+                       [ new TitleValue( NS_USER, 'Test' ), true ],
+                       [ new TitleValue( NS_SPECIAL, 'Test' ), false ],
+                       [ new TitleValue( NS_MEDIA, 'Test' ), false ],
+                       [ new TitleValue( NS_MAIN, '', 'Kittens' ), false ],
+                       [ new TitleValue( NS_MAIN, 'Kittens', '', 'acme' ), false ],
+               ];
        }
 
        /**
-        * @dataProvider provideSpecialNamespaces
-        * @covers NamespaceInfo::getTalk
-        * @covers NamespaceInfo::getTalkPage
-        * @covers NamespaceInfo::isMethodValidFor
-        * @covers Title::getTalkPage
-        *
-        * @param int $ns
+        * @dataProvider provideCanHaveTalkPage
+        * @covers NamespaceInfo::canHaveTalkPage
         */
-       public function testTitleGetTalkPage_special( $ns ) {
-               $this->setExpectedException( MWException::class,
-                       "NamespaceInfo::getTalk does not make any sense for given namespace $ns" );
-               Title::makeTitle( $ns, 'A' )->getTalkPage();
+       public function testCanHaveTalkPage( LinkTarget $t, $expected ) {
+               $actual = $this->newObj()->canHaveTalkPage( $t );
+               $this->assertEquals( $expected, $actual, $t->getDBkey() );
+       }
+
+       public static function provideGetTalkPage_good() {
+               return [
+                       [ new TitleValue( NS_MAIN, 'Test' ), new TitleValue( NS_TALK, 'Test' ) ],
+                       [ new TitleValue( NS_TALK, 'Test' ), new TitleValue( NS_TALK, 'Test' ) ],
+                       [ new TitleValue( NS_USER, 'Test' ), new TitleValue( NS_USER_TALK, 'Test' ) ],
+               ];
        }
 
        /**
-        * @dataProvider provideSpecialNamespaces
-        * @covers NamespaceInfo::getAssociated
+        * @dataProvider provideGetTalkPage_good
+        * @covers NamespaceInfo::getTalk
+        * @covers NamespaceInfo::getTalkPage
         * @covers NamespaceInfo::isMethodValidFor
-        *
-        * @param int $ns
         */
-       public function testGetAssociated_special( $ns ) {
-               $this->setExpectedException( MWException::class,
-                       "NamespaceInfo::getAssociated does not make any sense for given namespace $ns" );
-               $this->newObj()->getAssociated( $ns );
+       public function testGetTalkPage_good( LinkTarget $t, LinkTarget $expected ) {
+               $actual = $this->newObj()->getTalkPage( $t );
+               $this->assertEquals( $expected, $actual, $t->getDBkey() );
+       }
+
+       public static function provideGetTalkPage_bad() {
+               return [
+                       [ new TitleValue( NS_SPECIAL, 'Test' ) ],
+                       [ new TitleValue( NS_MEDIA, 'Test' ) ],
+                       [ new TitleValue( NS_MAIN, '', 'Kittens' ) ],
+                       [ new TitleValue( NS_MAIN, 'Kittens', '', 'acme' ) ],
+               ];
        }
 
        /**
-        * @dataProvider provideSpecialNamespaces
-        * @covers NamespaceInfo::getAssociated
-        * @covers NamespaceInfo::getAssociatedPage
+        * @dataProvider provideGetTalkPage_bad
+        * @covers NamespaceInfo::getTalk
+        * @covers NamespaceInfo::getTalkPage
         * @covers NamespaceInfo::isMethodValidFor
-        *
-        * @param int $ns
         */
-       public function testGetAssociatedPage_special( $ns ) {
-               $this->setExpectedException( MWException::class,
-                       "NamespaceInfo::getAssociated does not make any sense for given namespace $ns" );
-               $this->newObj()->getAssociatedPage( new TitleValue( $ns, 'A' ) );
+       public function testGetTalkPage_bad( LinkTarget $t ) {
+               $this->setExpectedException( MWException::class );
+               $this->newObj()->getTalkPage( $t );
        }
 
        /**
-        * @dataProvider provideSpecialNamespaces
+        * @dataProvider provideGetTalkPage_bad
         * @covers NamespaceInfo::getAssociated
         * @covers NamespaceInfo::getAssociatedPage
         * @covers NamespaceInfo::isMethodValidFor
-        * @covers Title::getOtherPage
-        *
-        * @param int $ns
         */
-       public function testTitleGetOtherPage_special( $ns ) {
-               $this->setExpectedException( MWException::class,
-                       "NamespaceInfo::getAssociated does not make any sense for given namespace $ns" );
-               Title::makeTitle( $ns, 'A' )->getOtherPage();
+       public function testGetAssociatedPage_bad( LinkTarget $t ) {
+               $this->setExpectedException( MWException::class );
+               $this->newObj()->getAssociatedPage( $t );
        }
 
        /**
@@ -1224,53 +1243,17 @@ class NamespaceInfoTest extends MediaWikiTestCase {
         */
 
        /**
-        * This mock user can only have isAllowed() called on it.
-        *
-        * @param array $groups Groups for the mock user to have
-        * @return User
-        */
-       private function getMockUser( array $groups = [] ) : User {
-               $groups[] = '*';
-
-               $mock = $this->createMock( User::class );
-               $mock->method( 'isAllowed' )->will( $this->returnCallback(
-                       function ( $action ) use ( $groups ) {
-                               global $wgGroupPermissions, $wgRevokePermissions;
-                               if ( $action == '' ) {
-                                       return true;
-                               }
-                               foreach ( $wgRevokePermissions as $group => $rights ) {
-                                       if ( !in_array( $group, $groups ) ) {
-                                               continue;
-                                       }
-                                       if ( isset( $rights[$action] ) && $rights[$action] ) {
-                                               return false;
-                                       }
-                               }
-                               foreach ( $wgGroupPermissions as $group => $rights ) {
-                                       if ( !in_array( $group, $groups ) ) {
-                                               continue;
-                                       }
-                                       if ( isset( $rights[$action] ) && $rights[$action] ) {
-                                               return true;
-                                       }
-                               }
-                               return false;
-                       }
-               ) );
-               $mock->expects( $this->never() )->method( $this->anythingBut( 'isAllowed' ) );
-               return $mock;
-       }
-
-       /**
+        * TODO: This is superceeded by PermissionManagerTest::testGetNamespaceRestrictionLevels
+        * Remove when deprecated method is removed.
         * @dataProvider provideGetRestrictionLevels
-        * @covers NamespaceInfo::getRestrictionLevels
+        * @covers       NamespaceInfo::getRestrictionLevels
         *
         * @param array $expected
         * @param int $ns
-        * @param User|null $user
+        * @param array|null $groups
+        * @throws MWException
         */
-       public function testGetRestrictionLevels( array $expected, $ns, User $user = null ) {
+       public function testGetRestrictionLevels( array $expected, $ns, array $groups = null ) {
                $this->setMwGlobals( [
                        'wgGroupPermissions' => [
                                '*' => [ 'edit' => true ],
@@ -1284,14 +1267,17 @@ class NamespaceInfoTest extends MediaWikiTestCase {
                        'wgRevokePermissions' => [
                                'noeditsemiprotected' => [ 'editsemiprotected' => true ],
                        ],
-               ] );
-               $obj = $this->newObj( [
-                       'NamespaceProtection' => [
+                       'wgNamespaceProtection' => [
                                NS_MAIN => 'autoconfirmed',
                                NS_USER => 'sysop',
                                101 => [ 'editsemiprotected', 'privileged' ],
                        ],
+                       'wgRestrictionLevels' => [ '', 'autoconfirmed', 'sysop' ],
+                       'wgAutopromote' => []
                ] );
+               $this->resetServices();
+               $obj = $this->newObj();
+               $user = is_null( $groups ) ? null : $this->getTestUser( $groups )->getUser();
                $this->assertSame( $expected, $obj->getRestrictionLevels( $ns, $user ) );
        }
 
@@ -1301,31 +1287,38 @@ class NamespaceInfoTest extends MediaWikiTestCase {
                        'Restricted to autoconfirmed' => [ [ '', 'sysop' ], NS_MAIN ],
                        'Restricted to sysop' => [ [ '' ], NS_USER ],
                        'Restricted to someone in two groups' => [ [ '', 'sysop' ], 101 ],
-                       'No special permissions' => [ [ '' ], NS_TALK, $this->getMockUser() ],
+                       'No special permissions' => [ [ '' ], NS_TALK, [] ],
                        'autoconfirmed' => [
                                [ '', 'autoconfirmed' ],
                                NS_TALK,
-                               $this->getMockUser( [ 'autoconfirmed' ] )
+                               [ 'autoconfirmed' ]
                        ],
                        'autoconfirmed revoked' => [
                                [ '' ],
                                NS_TALK,
-                               $this->getMockUser( [ 'autoconfirmed', 'noeditsemiprotected' ] )
+                               [ 'autoconfirmed', 'noeditsemiprotected' ]
                        ],
                        'sysop' => [
                                [ '', 'autoconfirmed', 'sysop' ],
                                NS_TALK,
-                               $this->getMockUser( [ 'sysop' ] )
+                               [ 'sysop' ]
                        ],
                        'sysop with autoconfirmed revoked (a bit silly)' => [
                                [ '', 'sysop' ],
                                NS_TALK,
-                               $this->getMockUser( [ 'sysop', 'noeditsemiprotected' ] )
+                               [ 'sysop', 'noeditsemiprotected' ]
                        ],
                ];
        }
 
        // %} End restriction levels
+
+       /**
+        * @coversNothing
+        */
+       public function testAllServiceOptionsUsed() {
+               $this->assertAllServiceOptionsUsed();
+       }
 }
 
 /**