2 use MediaWiki\MediaWikiServices
;
5 * Test class for SpecialSearch class
6 * Copyright © 2012, Antoine Musso
8 * @author Antoine Musso
11 class SpecialSearchTest
extends MediaWikiTestCase
{
14 * @covers SpecialSearch::load
15 * @dataProvider provideSearchOptionsTests
16 * @param array $requested Request parameters. For example:
17 * array( 'ns5' => true, 'ns6' => true). Null to use default options.
18 * @param array $userOptions User options to test with. For example:
19 * array('searchNs5' => 1 );. Null to use default options.
20 * @param string $expectedProfile An expected search profile name
21 * @param array $expectedNS Expected namespaces
22 * @param string $message
24 public function testProfileAndNamespaceLoading( $requested, $userOptions,
25 $expectedProfile, $expectedNS, $message = 'Profile name and namespaces mismatches!'
27 $context = new RequestContext
;
29 $this->newUserWithSearchNS( $userOptions )
32 $context->setRequest( new FauxRequest( [
37 $context->setRequest( new FauxRequest( $requested ) );
38 $search = new SpecialSearch();
39 $search->setContext( $context );
43 * Verify profile name and namespace in the same assertion to make
44 * sure we will be able to fully compare the above code. PHPUnit stop
45 * after an assertion fail.
49 'ProfileName' => $expectedProfile,
50 'Namespaces' => $expectedNS,
53 'ProfileName' => $search->getProfile(),
54 'Namespaces' => $search->getNamespaces(),
60 public static function provideSearchOptionsTests() {
61 $defaultNS = MediaWikiServices
::getInstance()->getSearchEngineConfig()->defaultNamespaces();
68 * <Web Request>, <User options>
69 * Followed by expected values:
70 * <ProfileName>, <NSList>
71 * Then an optional message.
74 $EMPTY_REQUEST, $NO_USER_PREF,
75 'default', $defaultNS,
76 'T35270: No request nor user preferences should give default profile'
79 [ 'ns5' => 1 ], $NO_USER_PREF,
81 'Web request with specific NS should override user preference'
87 ] +
array_fill_keys( array_map( function ( $ns ) {
90 'advanced', [ 2, 14 ],
91 'T35583: search with no option should honor User search preferences'
92 . ' and have all other namespace disabled'
98 * Helper to create a new User object with given options
99 * User remains anonymous though
100 * @param array|null $opt
102 function newUserWithSearchNS( $opt = null ) {
103 $u = User
::newFromId( 0 );
104 if ( $opt === null ) {
107 foreach ( $opt as $name => $value ) {
108 $u->setOption( $name, $value );
115 * Verify we do not expand search term in <title> on search result page
116 * https://gerrit.wikimedia.org/r/4841
118 public function testSearchTermIsNotExpanded() {
119 $this->setMwGlobals( [
120 'wgSearchType' => null,
123 # Initialize [[Special::Search]]
124 $ctx = new RequestContext();
125 $term = '{{SITENAME}}';
126 $ctx->setRequest( new FauxRequest( [ 'search' => $term, 'fulltext' => 1 ] ) );
127 $ctx->setTitle( Title
::newFromText( 'Special:Search' ) );
128 $search = new SpecialSearch();
129 $search->setContext( $ctx );
131 # Simulate a user searching for a given term
132 $search->execute( '' );
134 # Lookup the HTML page title set for that page
142 '/' . preg_quote( $term, '/' ) . '/',
144 "Search term '{$term}' should not be expanded in Special:Search <title>"
148 public function provideRewriteQueryWithSuggestion() {
151 'With suggestion and no rewritten query shows did you mean',
152 '/Did you mean: <a[^>]+>first suggestion/',
155 [ Title
::newMainPage() ]
159 'With rewritten query informs user of change',
160 '/Showing results for <a[^>]+>first suggestion/',
163 [ Title
::newMainPage() ]
167 'When both queries have no results user gets no results',
168 '/There were no results matching the query/',
177 * @dataProvider provideRewriteQueryWithSuggestion
179 public function testRewriteQueryWithSuggestion(
186 $results = array_map( function ( $title ) {
187 return SearchResult
::newFromTitle( $title );
190 $searchResults = new SpecialSearchTestMockResultSet(
196 $mockSearchEngine = $this->mockSearchEngine( $searchResults );
197 $search = $this->getMockBuilder( 'SpecialSearch' )
198 ->setMethods( [ 'getSearchEngine' ] )
200 $search->expects( $this->any() )
201 ->method( 'getSearchEngine' )
202 ->will( $this->returnValue( $mockSearchEngine ) );
204 $search->getContext()->setTitle( Title
::makeTitle( NS_SPECIAL
, 'Search' ) );
205 $search->getContext()->setLanguage( Language
::factory( 'en' ) );
207 $search->showResults( 'this is a fake search' );
209 $html = $search->getContext()->getOutput()->getHTML();
210 foreach ( (array)$expectRegex as $regex ) {
211 $this->assertRegExp( $regex, $html, $message );
215 protected function mockSearchEngine( $results ) {
216 $mock = $this->getMockBuilder( 'SearchEngine' )
217 ->setMethods( [ 'searchText', 'searchTitle' ] )
220 $mock->expects( $this->any() )
221 ->method( 'searchText' )
222 ->will( $this->returnValue( $results ) );
227 public function testSubPageRedirect() {
228 $this->setMwGlobals( [
229 'wgScript' => '/w/index.php',
232 $ctx = new RequestContext
;
233 $sp = Title
::newFromText( 'Special:Search/foo_bar' );
234 SpecialPageFactory
::executePath( $sp, $ctx );
235 $url = $ctx->getOutput()->getRedirect();
236 // some older versions of hhvm have a bug that doesn't parse relative
237 // urls with a port, so help it out a little bit.
238 // https://github.com/facebook/hhvm/issues/7136
239 $url = wfExpandUrl( $url, PROTO_CURRENT
);
241 $parts = parse_url( $url );
242 $this->assertEquals( '/w/index.php', $parts['path'] );
243 parse_str( $parts['query'], $query );
244 $this->assertEquals( 'Special:Search', $query['title'] );
245 $this->assertEquals( 'foo bar', $query['search'] );
249 class SpecialSearchTestMockResultSet
extends SearchResultSet
{
251 protected $suggestion;
253 public function __construct(
255 $rewrittenQuery = null,
257 $containedSyntax = false
259 $this->suggestion
= $suggestion;
260 $this->rewrittenQuery
= $rewrittenQuery;
261 $this->results
= $results;
262 $this->containedSyntax
= $containedSyntax;
265 public function numRows() {
266 return count( $this->results
);
269 public function getTotalHits() {
270 return $this->numRows();
273 public function hasSuggestion() {
274 return $this->suggestion
!== null;
277 public function getSuggestionQuery() {
278 return $this->suggestion
;
281 public function getSuggestionSnippet() {
282 return $this->suggestion
;
285 public function hasRewrittenQuery() {
286 return $this->rewrittenQuery
!== null;
289 public function getQueryAfterRewrite() {
290 return $this->rewrittenQuery
;
293 public function getQueryAfterRewriteSnippet() {
294 return htmlspecialchars( $this->rewrittenQuery
);