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
117 * @covers SpecialSearch::setupPage
119 public function testSearchTermIsNotExpanded() {
120 $this->setMwGlobals( [
121 'wgSearchType' => null,
124 # Initialize [[Special::Search]]
125 $ctx = new RequestContext();
126 $term = '{{SITENAME}}';
127 $ctx->setRequest( new FauxRequest( [ 'search' => $term, 'fulltext' => 1 ] ) );
128 $ctx->setTitle( Title
::newFromText( 'Special:Search' ) );
129 $search = new SpecialSearch();
130 $search->setContext( $ctx );
132 # Simulate a user searching for a given term
133 $search->execute( '' );
135 # Lookup the HTML page title set for that page
143 '/' . preg_quote( $term, '/' ) . '/',
145 "Search term '{$term}' should not be expanded in Special:Search <title>"
149 public function provideRewriteQueryWithSuggestion() {
152 'With suggestion and no rewritten query shows did you mean',
153 '/Did you mean: <a[^>]+>first suggestion/',
156 [ Title
::newMainPage() ]
160 'With rewritten query informs user of change',
161 '/Showing results for <a[^>]+>first suggestion/',
164 [ Title
::newMainPage() ]
168 'When both queries have no results user gets no results',
169 '/There were no results matching the query/',
178 * @dataProvider provideRewriteQueryWithSuggestion
179 * @covers SpecialSearch::showResults
181 public function testRewriteQueryWithSuggestion(
188 $results = array_map( function ( $title ) {
189 return SearchResult
::newFromTitle( $title );
192 $searchResults = new SpecialSearchTestMockResultSet(
198 $mockSearchEngine = $this->mockSearchEngine( $searchResults );
199 $search = $this->getMockBuilder( SpecialSearch
::class )
200 ->setMethods( [ 'getSearchEngine' ] )
202 $search->expects( $this->any() )
203 ->method( 'getSearchEngine' )
204 ->will( $this->returnValue( $mockSearchEngine ) );
206 $search->getContext()->setTitle( Title
::makeTitle( NS_SPECIAL
, 'Search' ) );
207 $search->getContext()->setLanguage( Language
::factory( 'en' ) );
209 $search->showResults( 'this is a fake search' );
211 $html = $search->getContext()->getOutput()->getHTML();
212 foreach ( (array)$expectRegex as $regex ) {
213 $this->assertRegExp( $regex, $html, $message );
217 protected function mockSearchEngine( $results ) {
218 $mock = $this->getMockBuilder( SearchEngine
::class )
219 ->setMethods( [ 'searchText', 'searchTitle' ] )
222 $mock->expects( $this->any() )
223 ->method( 'searchText' )
224 ->will( $this->returnValue( $results ) );
230 * @covers SpecialSearch::execute
232 public function testSubPageRedirect() {
233 $this->setMwGlobals( [
234 'wgScript' => '/w/index.php',
237 $ctx = new RequestContext
;
238 $sp = Title
::newFromText( 'Special:Search/foo_bar' );
239 MediaWikiServices
::getInstance()->getSpecialPageFactory()->executePath( $sp, $ctx );
240 $url = $ctx->getOutput()->getRedirect();
241 // some older versions of hhvm have a bug that doesn't parse relative
242 // urls with a port, so help it out a little bit.
243 // https://github.com/facebook/hhvm/issues/7136
244 $url = wfExpandUrl( $url, PROTO_CURRENT
);
246 $parts = parse_url( $url );
247 $this->assertEquals( '/w/index.php', $parts['path'] );
248 parse_str( $parts['query'], $query );
249 $this->assertEquals( 'Special:Search', $query['title'] );
250 $this->assertEquals( 'foo bar', $query['search'] );
254 class SpecialSearchTestMockResultSet
extends SearchResultSet
{
256 protected $suggestion;
258 public function __construct(
260 $rewrittenQuery = null,
262 $containedSyntax = false
264 $this->suggestion
= $suggestion;
265 $this->rewrittenQuery
= $rewrittenQuery;
266 $this->results
= $results;
267 $this->containedSyntax
= $containedSyntax;
270 public function expandResults() {
271 return $this->results
;
274 public function getTotalHits() {
275 return $this->numRows();
278 public function hasSuggestion() {
279 return $this->suggestion
!== null;
282 public function getSuggestionQuery() {
283 return $this->suggestion
;
286 public function getSuggestionSnippet() {
287 return $this->suggestion
;
290 public function hasRewrittenQuery() {
291 return $this->rewrittenQuery
!== null;
294 public function getQueryAfterRewrite() {
295 return $this->rewrittenQuery
;
298 public function getQueryAfterRewriteSnippet() {
299 return htmlspecialchars( $this->rewrittenQuery
);