Merge "Convert various FormActions to OOUI"
[lhc/web/wiklou.git] / tests / phpunit / includes / specials / SpecialSearchTest.php
index 3fa8a9f..4e9d826 100644 (file)
@@ -73,7 +73,7 @@ class SpecialSearchTest extends MediaWikiTestCase {
                        [
                                $EMPTY_REQUEST, $NO_USER_PREF,
                                'default', $defaultNS,
-                               'Bug 33270: No request nor user preferences should give default profile'
+                               'T35270: No request nor user preferences should give default profile'
                        ],
                        [
                                [ 'ns5' => 1 ], $NO_USER_PREF,
@@ -88,7 +88,7 @@ class SpecialSearchTest extends MediaWikiTestCase {
                                return "searchNs$ns";
                        }, $defaultNS ), 0 ),
                                'advanced', [ 2, 14 ],
-                               'Bug 33583: search with no option should honor User search preferences'
+                               'T35583: search with no option should honor User search preferences'
                                        . ' and have all other namespace disabled'
                        ],
                ];
@@ -121,13 +121,15 @@ class SpecialSearchTest extends MediaWikiTestCase {
                ] );
 
                # Initialize [[Special::Search]]
+               $ctx = new RequestContext();
+               $term = '{{SITENAME}}';
+               $ctx->setRequest( new FauxRequest( [ 'search' => $term, 'fulltext' => 1 ] ) );
+               $ctx->setTitle( Title::newFromText( 'Special:Search' ) );
                $search = new SpecialSearch();
-               $search->getContext()->setTitle( Title::newFromText( 'Special:Search' ) );
-               $search->load();
+               $search->setContext( $ctx );
 
                # Simulate a user searching for a given term
-               $term = '{{SITENAME}}';
-               $search->showResults( $term );
+               $search->execute( '' );
 
                # Lookup the HTML page title set for that page
                $pageTitle = $search
@@ -148,23 +150,25 @@ class SpecialSearchTest extends MediaWikiTestCase {
                        [
                                'With suggestion and no rewritten query shows did you mean',
                                '/Did you mean: <a[^>]+>first suggestion/',
-                               new SpecialSearchTestMockResultSet( 'first suggestion', null, [
-                                       SearchResult::newFromTitle( Title::newMainPage() ),
-                               ] ),
+                               'first suggestion',
+                               null,
+                               [ Title::newMainPage() ]
                        ],
 
                        [
                                'With rewritten query informs user of change',
                                '/Showing results for <a[^>]+>first suggestion/',
-                               new SpecialSearchTestMockResultSet( 'asdf', 'first suggestion', [
-                                       SearchResult::newFromTitle( Title::newMainPage() ),
-                               ] ),
+                               'asdf',
+                               'first suggestion',
+                               [ Title::newMainPage() ]
                        ],
 
                        [
                                'When both queries have no results user gets no results',
                                '/There were no results matching the query/',
-                               new SpecialSearchTestMockResultSet( 'first suggestion', 'first suggestion', [] ),
+                               'first suggestion',
+                               'first suggestion',
+                               []
                        ],
                ];
        }
@@ -172,8 +176,24 @@ class SpecialSearchTest extends MediaWikiTestCase {
        /**
         * @dataProvider provideRewriteQueryWithSuggestion
         */
-       public function testRewriteQueryWithSuggestion( $message, $expectRegex, $results ) {
-               $mockSearchEngine = $this->mockSearchEngine( $results );
+       public function testRewriteQueryWithSuggestion(
+               $message,
+               $expectRegex,
+               $suggestion,
+               $rewrittenQuery,
+               array $resultTitles
+       ) {
+               $results = array_map( function( $title ) {
+                       return SearchResult::newFromTitle( $title );
+               }, $resultTitles );
+
+               $searchResults = new SpecialSearchTestMockResultSet(
+                       $suggestion,
+                       $rewrittenQuery,
+                       $results
+               );
+
+               $mockSearchEngine = $this->mockSearchEngine( $searchResults );
                $search = $this->getMockBuilder( 'SpecialSearch' )
                        ->setMethods( [ 'getSearchEngine' ] )
                        ->getMock();
@@ -203,6 +223,27 @@ class SpecialSearchTest extends MediaWikiTestCase {
 
                return $mock;
        }
+
+       public function testSubPageRedirect() {
+               $this->setMwGlobals( [
+                       'wgScript' => '/w/index.php',
+               ] );
+
+               $ctx = new RequestContext;
+               $sp = Title::newFromText( 'Special:Search/foo_bar' );
+               SpecialPageFactory::executePath( $sp, $ctx );
+               $url = $ctx->getOutput()->getRedirect();
+               // some older versions of hhvm have a bug that doesn't parse relative
+               // urls with a port, so help it out a little bit.
+               // https://github.com/facebook/hhvm/issues/7136
+               $url = wfExpandUrl( $url, PROTO_CURRENT );
+
+               $parts = parse_url( $url );
+               $this->assertEquals( '/w/index.php', $parts['path'] );
+               parse_str( $parts['query'], $query );
+               $this->assertEquals( 'Special:Search', $query['title'] );
+               $this->assertEquals( 'foo bar', $query['search'] );
+       }
 }
 
 class SpecialSearchTestMockResultSet extends SearchResultSet {