3 use MediaWiki\MediaWikiServices
;
6 * Test class for SpecialSearch class
7 * Copyright © 2012, Antoine Musso
9 * @author Antoine Musso
12 class SpecialSearchTest
extends MediaWikiTestCase
{
15 * @covers SpecialSearch::load
16 * @dataProvider provideSearchOptionsTests
17 * @param array $requested Request parameters. For example:
18 * [ 'ns5' => true, 'ns6' => true ]. Null to use default options.
19 * @param array $userOptions User options to test with. For example:
20 * [ 'searchNs5' => 1 ];. Null to use default options.
21 * @param string $expectedProfile An expected search profile name
22 * @param array $expectedNS Expected namespaces
23 * @param string $message
25 public function testProfileAndNamespaceLoading( $requested, $userOptions,
26 $expectedProfile, $expectedNS, $message = 'Profile name and namespaces mismatches!'
28 $context = new RequestContext
;
30 $this->newUserWithSearchNS( $userOptions )
33 $context->setRequest( new FauxRequest( [
38 $context->setRequest( new FauxRequest( $requested ) );
39 $search = new SpecialSearch();
40 $search->setContext( $context );
44 * Verify profile name and namespace in the same assertion to make
45 * sure we will be able to fully compare the above code. PHPUnit stop
46 * after an assertion fail.
50 'ProfileName' => $expectedProfile,
51 'Namespaces' => $expectedNS,
54 'ProfileName' => $search->getProfile(),
55 'Namespaces' => $search->getNamespaces(),
61 public static function provideSearchOptionsTests() {
62 $defaultNS = MediaWikiServices
::getInstance()->getSearchEngineConfig()->defaultNamespaces();
69 * <Web Request>, <User options>
70 * Followed by expected values:
71 * <ProfileName>, <NSList>
72 * Then an optional message.
75 $EMPTY_REQUEST, $NO_USER_PREF,
76 'default', $defaultNS,
77 'T35270: No request nor user preferences should give default profile'
80 [ 'ns5' => 1 ], $NO_USER_PREF,
82 'Web request with specific NS should override user preference'
88 ] +
array_fill_keys( array_map( function ( $ns ) {
91 'advanced', [ 2, 14 ],
92 'T35583: search with no option should honor User search preferences'
93 . ' and have all other namespace disabled'
99 * Helper to create a new User object with given options
100 * User remains anonymous though
101 * @param array|null $opt
103 function newUserWithSearchNS( $opt = null ) {
104 $u = User
::newFromId( 0 );
105 if ( $opt === null ) {
108 foreach ( $opt as $name => $value ) {
109 $u->setOption( $name, $value );
116 * Verify we do not expand search term in <title> on search result page
117 * https://gerrit.wikimedia.org/r/4841
118 * @covers SpecialSearch::setupPage
120 public function testSearchTermIsNotExpanded() {
121 $this->setMwGlobals( [
122 'wgSearchType' => null,
125 # Initialize [[Special::Search]]
126 $ctx = new RequestContext();
127 $term = '{{SITENAME}}';
128 $ctx->setRequest( new FauxRequest( [ 'search' => $term, 'fulltext' => 1 ] ) );
129 $ctx->setTitle( Title
::newFromText( 'Special:Search' ) );
130 $search = new SpecialSearch();
131 $search->setContext( $ctx );
133 # Simulate a user searching for a given term
134 $search->execute( '' );
136 # Lookup the HTML page title set for that page
144 '/' . preg_quote( $term, '/' ) . '/',
146 "Search term '{$term}' should not be expanded in Special:Search <title>"
150 public function provideRewriteQueryWithSuggestion() {
153 'With suggestion and no rewritten query shows did you mean',
154 '/Did you mean: <a[^>]+>first suggestion/',
157 [ Title
::newMainPage() ]
161 'With rewritten query informs user of change',
162 '/Showing results for <a[^>]+>first suggestion/',
165 [ Title
::newMainPage() ]
169 'When both queries have no results user gets no results',
170 '/There were no results matching the query/',
179 * @dataProvider provideRewriteQueryWithSuggestion
180 * @covers SpecialSearch::showResults
182 public function testRewriteQueryWithSuggestion(
189 $results = array_map( function ( $title ) {
190 return SearchResult
::newFromTitle( $title );
193 $searchResults = new SpecialSearchTestMockResultSet(
199 $mockSearchEngine = $this->mockSearchEngine( $searchResults );
200 $search = $this->getMockBuilder( SpecialSearch
::class )
201 ->setMethods( [ 'getSearchEngine' ] )
203 $search->expects( $this->any() )
204 ->method( 'getSearchEngine' )
205 ->will( $this->returnValue( $mockSearchEngine ) );
207 $search->getContext()->setTitle( Title
::makeTitle( NS_SPECIAL
, 'Search' ) );
208 $search->getContext()->setLanguage( Language
::factory( 'en' ) );
210 $search->showResults( 'this is a fake search' );
212 $html = $search->getContext()->getOutput()->getHTML();
213 foreach ( (array)$expectRegex as $regex ) {
214 $this->assertRegExp( $regex, $html, $message );
218 protected function mockSearchEngine( $results ) {
219 $mock = $this->getMockBuilder( SearchEngine
::class )
220 ->setMethods( [ 'searchText', 'searchTitle' ] )
223 $mock->expects( $this->any() )
224 ->method( 'searchText' )
225 ->will( $this->returnValue( $results ) );
231 * @covers SpecialSearch::execute
233 public function testSubPageRedirect() {
234 $this->setMwGlobals( [
235 'wgScript' => '/w/index.php',
238 $ctx = new RequestContext
;
239 $sp = Title
::newFromText( 'Special:Search/foo_bar' );
240 MediaWikiServices
::getInstance()->getSpecialPageFactory()->executePath( $sp, $ctx );
241 $url = $ctx->getOutput()->getRedirect();
242 // some older versions of hhvm have a bug that doesn't parse relative
243 // urls with a port, so help it out a little bit.
244 // https://github.com/facebook/hhvm/issues/7136
245 $url = wfExpandUrl( $url, PROTO_CURRENT
);
247 $parts = parse_url( $url );
248 $this->assertEquals( '/w/index.php', $parts['path'] );
249 parse_str( $parts['query'], $query );
250 $this->assertEquals( 'Special:Search', $query['title'] );
251 $this->assertEquals( 'foo bar', $query['search'] );
255 class SpecialSearchTestMockResultSet
extends SearchResultSet
{
257 protected $suggestion;
259 public function __construct(
261 $rewrittenQuery = null,
263 $containedSyntax = false
265 $this->suggestion
= $suggestion;
266 $this->rewrittenQuery
= $rewrittenQuery;
267 $this->results
= $results;
268 $this->containedSyntax
= $containedSyntax;
271 public function expandResults() {
272 return $this->results
;
275 public function getTotalHits() {
276 return $this->numRows();
279 public function hasSuggestion() {
280 return $this->suggestion
!== null;
283 public function getSuggestionQuery() {
284 return $this->suggestion
;
287 public function getSuggestionSnippet() {
288 return $this->suggestion
;
291 public function hasRewrittenQuery() {
292 return $this->rewrittenQuery
!== null;
295 public function getQueryAfterRewrite() {
296 return $this->rewrittenQuery
;
299 public function getQueryAfterRewriteSnippet() {
300 return htmlspecialchars( $this->rewrittenQuery
);