Add tests for DiffEngine tooltips
[lhc/web/wiklou.git] / tests / phpunit / includes / PrefixSearchTest.php
index 0ec200c..2f3e569 100644 (file)
@@ -1,9 +1,15 @@
 <?php
+
+use Wikimedia\TestingAccessWrapper;
+
 /**
  * @group Search
  * @group Database
+ * @covers PrefixSearch
  */
 class PrefixSearchTest extends MediaWikiLangTestCase {
+       const NS_NONCAP = 12346;
+
        private $originalHandlers;
 
        public function addDBDataOnce() {
@@ -31,6 +37,10 @@ class PrefixSearchTest extends MediaWikiLangTestCase {
                $this->insertPage( 'Talk:Example' );
 
                $this->insertPage( 'User:Example' );
+
+               $this->insertPage( Title::makeTitle( self::NS_NONCAP, 'Bar' ) );
+               $this->insertPage( Title::makeTitle( self::NS_NONCAP, 'Upper' ) );
+               $this->insertPage( Title::makeTitle( self::NS_NONCAP, 'sandbox' ) );
        }
 
        protected function setUp() {
@@ -44,15 +54,24 @@ class PrefixSearchTest extends MediaWikiLangTestCase {
                $this->setMwGlobals( [
                        'wgSpecialPages' => [],
                        'wgHooks' => [],
+                       'wgExtraNamespaces' => [ self::NS_NONCAP => 'NonCap' ],
+                       'wgCapitalLinkOverrides' => [ self::NS_NONCAP => false ],
                ] );
 
                $this->originalHandlers = TestingAccessWrapper::newFromClass( 'Hooks' )->handlers;
                TestingAccessWrapper::newFromClass( 'Hooks' )->handlers = [];
 
+               // Clear caches so that our new namespace appears
+               MWNamespace::clearCaches();
+               Language::factory( 'en' )->resetNamespaces();
+
                SpecialPageFactory::resetList();
        }
 
        public function tearDown() {
+               MWNamespace::clearCaches();
+               Language::factory( 'en' )->resetNamespaces();
+
                parent::tearDown();
 
                TestingAccessWrapper::newFromClass( 'Hooks' )->handlers = $this->originalHandlers;
@@ -116,11 +135,11 @@ class PrefixSearchTest extends MediaWikiLangTestCase {
                                'results' => [
                                        'Special:ActiveUsers',
                                        'Special:AllMessages',
-                                       'Special:AllMyFiles',
+                                       'Special:AllMyUploads',
                                ],
                                // Third result when testing offset
                                'offsetresult' => [
-                                       'Special:AllMyUploads',
+                                       'Special:AllPages',
                                ],
                        ] ],
                        [ [
@@ -133,7 +152,7 @@ class PrefixSearchTest extends MediaWikiLangTestCase {
                                ],
                                // Third result when testing offset
                                'offsetresult' => [
-                                       'Special:UncategorizedImages',
+                                       'Special:UncategorizedPages',
                                ],
                        ] ],
                        [ [
@@ -158,6 +177,29 @@ class PrefixSearchTest extends MediaWikiLangTestCase {
                                        'Special:EditWatchlist/clear',
                                ],
                        ] ],
+                       [ [
+                               'Namespace with case sensitive first letter',
+                               'query' => 'NonCap:upper',
+                               'results' => []
+                       ] ],
+                       [ [
+                               'Multinamespace search',
+                               'query' => 'B',
+                               'results' => [
+                                       'Bar',
+                                       'NonCap:Bar',
+                               ],
+                               'namespaces' => [ NS_MAIN, self::NS_NONCAP ],
+                       ] ],
+                       [ [
+                               'Multinamespace search with lowercase first letter',
+                               'query' => 'sand',
+                               'results' => [
+                                       'Sandbox',
+                                       'NonCap:sandbox',
+                               ],
+                               'namespaces' => [ NS_MAIN, self::NS_NONCAP ],
+                       ] ],
                ];
        }
 
@@ -168,8 +210,16 @@ class PrefixSearchTest extends MediaWikiLangTestCase {
         */
        public function testSearch( array $case ) {
                $this->searchProvision( null );
+
+               $namespaces = isset( $case['namespaces'] ) ? $case['namespaces'] : [];
+
+               if ( wfGetDB( DB_REPLICA )->getType() === 'postgres' ) {
+                       // Postgres will sort lexicographically on utf8 code units (" " before "/")
+                       sort( $case['results'], SORT_STRING );
+               }
+
                $searcher = new StringPrefixSearch;
-               $results = $searcher->search( $case['query'], 3 );
+               $results = $searcher->search( $case['query'], 3, $namespaces );
                $this->assertEquals(
                        $case['results'],
                        $results,
@@ -184,8 +234,16 @@ class PrefixSearchTest extends MediaWikiLangTestCase {
         */
        public function testSearchWithOffset( array $case ) {
                $this->searchProvision( null );
+
+               $namespaces = isset( $case['namespaces'] ) ? $case['namespaces'] : [];
+
                $searcher = new StringPrefixSearch;
-               $results = $searcher->search( $case['query'], 3, [], 1 );
+               $results = $searcher->search( $case['query'], 3, $namespaces, 1 );
+
+               if ( wfGetDB( DB_REPLICA )->getType() === 'postgres' ) {
+                       // Postgres will sort lexicographically on utf8 code units (" " before "/")
+                       sort( $case['results'], SORT_STRING );
+               }
 
                // We don't expect the first result when offsetting
                array_shift( $case['results'] );
@@ -218,7 +276,7 @@ class PrefixSearchTest extends MediaWikiLangTestCase {
                                ],
                        ] ],
                        [ [
-                               'Exact match not on top (bug 70958)',
+                               'Exact match not on top (T72958)',
                                'provision' => [
                                        'Barcelona',
                                        'Bar',
@@ -232,7 +290,7 @@ class PrefixSearchTest extends MediaWikiLangTestCase {
                                ],
                        ] ],
                        [ [
-                               'Exact match missing (bug 70958)',
+                               'Exact match missing (T72958)',
                                'provision' => [
                                        'Barcelona',
                                        'Barbara',