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( array(
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 'Bug 33270: 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 'Bug 33583: 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 $search = new SpecialSearch();
125 $search->getContext()->setTitle( Title
::newFromText( 'Special:Search' ) );
128 # Simulate a user searching for a given term
129 $term = '{{SITENAME}}';
130 $search->showResults( $term );
132 # Lookup the HTML page title set for that page
140 '/' . preg_quote( $term, '/' ) . '/',
142 "Search term '{$term}' should not be expanded in Special:Search <title>"
146 public function provideRewriteQueryWithSuggestion() {
149 'With suggestion and no rewritten query shows did you mean',
150 '/Did you mean: <a[^>]+>first suggestion/',
151 new SpecialSearchTestMockResultSet( 'first suggestion', null, [
152 SearchResult
::newFromTitle( Title
::newMainPage() ),
157 'With rewritten query informs user of change',
158 '/Showing results for <a[^>]+>first suggestion/',
159 new SpecialSearchTestMockResultSet( 'asdf', 'first suggestion', [
160 SearchResult
::newFromTitle( Title
::newMainPage() ),
165 'When both queries have no results user gets no results',
166 '/There were no results matching the query/',
167 new SpecialSearchTestMockResultSet( 'first suggestion', 'first suggestion', [] ),
173 * @dataProvider provideRewriteQueryWithSuggestion
175 public function testRewriteQueryWithSuggestion( $message, $expectRegex, $results ) {
176 $mockSearchEngine = $this->mockSearchEngine( $results );
177 $search = $this->getMockBuilder( 'SpecialSearch' )
178 ->setMethods( [ 'getSearchEngine' ] )
180 $search->expects( $this->any() )
181 ->method( 'getSearchEngine' )
182 ->will( $this->returnValue( $mockSearchEngine ) );
184 $search->getContext()->setTitle( Title
::makeTitle( NS_SPECIAL
, 'Search' ) );
185 $search->getContext()->setLanguage( Language
::factory( 'en' ) );
187 $search->showResults( 'this is a fake search' );
189 $html = $search->getContext()->getOutput()->getHTML();
190 foreach ( (array)$expectRegex as $regex ) {
191 $this->assertRegExp( $regex, $html, $message );
195 protected function mockSearchEngine( $results ) {
196 $mock = $this->getMockBuilder( 'SearchEngine' )
197 ->setMethods( [ 'searchText', 'searchTitle' ] )
200 $mock->expects( $this->any() )
201 ->method( 'searchText' )
202 ->will( $this->returnValue( $results ) );
208 class SpecialSearchTestMockResultSet
extends SearchResultSet
{
210 protected $suggestion;
212 public function __construct(
214 $rewrittenQuery = null,
216 $containedSyntax = false
218 $this->suggestion
= $suggestion;
219 $this->rewrittenQuery
= $rewrittenQuery;
220 $this->results
= $results;
221 $this->containedSyntax
= $containedSyntax;
224 public function numRows() {
225 return count( $this->results
);
228 public function getTotalHits() {
229 return $this->numRows();
232 public function hasSuggestion() {
233 return $this->suggestion
!== null;
236 public function getSuggestionQuery() {
237 return $this->suggestion
;
240 public function getSuggestionSnippet() {
241 return $this->suggestion
;
244 public function hasRewrittenQuery() {
245 return $this->rewrittenQuery
!== null;
248 public function getQueryAfterRewrite() {
249 return $this->rewrittenQuery
;
252 public function getQueryAfterRewriteSnippet() {
253 return htmlspecialchars( $this->rewrittenQuery
);