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 * @covers SpecialSearch::showResults
18 public function testValidateSortOrder() {
19 $ctx = new RequestContext();
20 $ctx->setRequest( new FauxRequest( [
25 $sp = Title
::makeTitle( NS_SPECIAL
, 'Search' );
26 MediaWikiServices
::getInstance()
27 ->getSpecialPageFactory()
28 ->executePath( $sp, $ctx );
29 $html = $ctx->getOutput()->getHTML();
30 $this->assertRegExp( '/class="warningbox"/', $html, 'must contain warnings' );
31 $this->assertRegExp( '/Sort order of invalid is unrecognized/',
32 $html, 'must tell user sort order is invalid' );
36 * @covers SpecialSearch::load
37 * @dataProvider provideSearchOptionsTests
38 * @param array $requested Request parameters. For example:
39 * [ 'ns5' => true, 'ns6' => true ]. Null to use default options.
40 * @param array $userOptions User options to test with. For example:
41 * [ 'searchNs5' => 1 ];. Null to use default options.
42 * @param string $expectedProfile An expected search profile name
43 * @param array $expectedNS Expected namespaces
44 * @param string $message
46 public function testProfileAndNamespaceLoading( $requested, $userOptions,
47 $expectedProfile, $expectedNS, $message = 'Profile name and namespaces mismatches!'
49 $context = new RequestContext
;
51 $this->newUserWithSearchNS( $userOptions )
54 $context->setRequest( new FauxRequest( [
59 $context->setRequest( new FauxRequest( $requested ) );
60 $search = new SpecialSearch();
61 $search->setContext( $context );
65 * Verify profile name and namespace in the same assertion to make
66 * sure we will be able to fully compare the above code. PHPUnit stop
67 * after an assertion fail.
71 'ProfileName' => $expectedProfile,
72 'Namespaces' => $expectedNS,
75 'ProfileName' => $search->getProfile(),
76 'Namespaces' => $search->getNamespaces(),
82 public static function provideSearchOptionsTests() {
83 $defaultNS = MediaWikiServices
::getInstance()->getSearchEngineConfig()->defaultNamespaces();
90 * <Web Request>, <User options>
91 * Followed by expected values:
92 * <ProfileName>, <NSList>
93 * Then an optional message.
96 $EMPTY_REQUEST, $NO_USER_PREF,
97 'default', $defaultNS,
98 'T35270: No request nor user preferences should give default profile'
101 [ 'ns5' => 1 ], $NO_USER_PREF,
103 'Web request with specific NS should override user preference'
109 ] +
array_fill_keys( array_map( function ( $ns ) {
110 return "searchNs$ns";
111 }, $defaultNS ), 0 ),
112 'advanced', [ 2, 14 ],
113 'T35583: search with no option should honor User search preferences'
114 . ' and have all other namespace disabled'
120 * Helper to create a new User object with given options
121 * User remains anonymous though
122 * @param array|null $opt
124 function newUserWithSearchNS( $opt = null ) {
125 $u = User
::newFromId( 0 );
126 if ( $opt === null ) {
129 foreach ( $opt as $name => $value ) {
130 $u->setOption( $name, $value );
137 * Verify we do not expand search term in <title> on search result page
138 * https://gerrit.wikimedia.org/r/4841
139 * @covers SpecialSearch::setupPage
141 public function testSearchTermIsNotExpanded() {
142 $this->setMwGlobals( [
143 'wgSearchType' => null,
146 # Initialize [[Special::Search]]
147 $ctx = new RequestContext();
148 $term = '{{SITENAME}}';
149 $ctx->setRequest( new FauxRequest( [ 'search' => $term, 'fulltext' => 1 ] ) );
150 $ctx->setTitle( Title
::newFromText( 'Special:Search' ) );
151 $search = new SpecialSearch();
152 $search->setContext( $ctx );
154 # Simulate a user searching for a given term
155 $search->execute( '' );
157 # Lookup the HTML page title set for that page
165 '/' . preg_quote( $term, '/' ) . '/',
167 "Search term '{$term}' should not be expanded in Special:Search <title>"
171 public function provideRewriteQueryWithSuggestion() {
174 'With suggestion and no rewritten query shows did you mean',
175 '/Did you mean: <a[^>]+>first suggestion/',
178 [ Title
::newMainPage() ]
182 'With rewritten query informs user of change',
183 '/Showing results for <a[^>]+>first suggestion/',
186 [ Title
::newMainPage() ]
190 'When both queries have no results user gets no results',
191 '/There were no results matching the query/',
200 * @dataProvider provideRewriteQueryWithSuggestion
201 * @covers SpecialSearch::showResults
203 public function testRewriteQueryWithSuggestion(
210 $results = array_map( function ( $title ) {
211 return SearchResult
::newFromTitle( $title );
214 $searchResults = new SpecialSearchTestMockResultSet(
220 $mockSearchEngine = $this->mockSearchEngine( $searchResults );
221 $search = $this->getMockBuilder( SpecialSearch
::class )
222 ->setMethods( [ 'getSearchEngine' ] )
224 $search->expects( $this->any() )
225 ->method( 'getSearchEngine' )
226 ->will( $this->returnValue( $mockSearchEngine ) );
228 $search->getContext()->setTitle( Title
::makeTitle( NS_SPECIAL
, 'Search' ) );
229 $search->getContext()->setLanguage( Language
::factory( 'en' ) );
231 $search->showResults( 'this is a fake search' );
233 $html = $search->getContext()->getOutput()->getHTML();
234 foreach ( (array)$expectRegex as $regex ) {
235 $this->assertRegExp( $regex, $html, $message );
239 protected function mockSearchEngine( $results ) {
240 $mock = $this->getMockBuilder( SearchEngine
::class )
241 ->setMethods( [ 'searchText', 'searchTitle' ] )
244 $mock->expects( $this->any() )
245 ->method( 'searchText' )
246 ->will( $this->returnValue( $results ) );
252 * @covers SpecialSearch::execute
254 public function testSubPageRedirect() {
255 $this->setMwGlobals( [
256 'wgScript' => '/w/index.php',
259 $ctx = new RequestContext
;
260 $sp = Title
::newFromText( 'Special:Search/foo_bar' );
261 MediaWikiServices
::getInstance()->getSpecialPageFactory()->executePath( $sp, $ctx );
262 $url = $ctx->getOutput()->getRedirect();
263 // some older versions of hhvm have a bug that doesn't parse relative
264 // urls with a port, so help it out a little bit.
265 // https://github.com/facebook/hhvm/issues/7136
266 $url = wfExpandUrl( $url, PROTO_CURRENT
);
268 $parts = parse_url( $url );
269 $this->assertEquals( '/w/index.php', $parts['path'] );
270 parse_str( $parts['query'], $query );
271 $this->assertEquals( 'Special:Search', $query['title'] );
272 $this->assertEquals( 'foo bar', $query['search'] );
276 class SpecialSearchTestMockResultSet
extends SearchResultSet
{
278 protected $suggestion;
280 public function __construct(
282 $rewrittenQuery = null,
284 $containedSyntax = false
286 $this->suggestion
= $suggestion;
287 $this->rewrittenQuery
= $rewrittenQuery;
288 $this->results
= $results;
289 $this->containedSyntax
= $containedSyntax;
292 public function expandResults() {
293 return $this->results
;
296 public function getTotalHits() {
297 return $this->numRows();
300 public function hasSuggestion() {
301 return $this->suggestion
!== null;
304 public function getSuggestionQuery() {
305 return $this->suggestion
;
308 public function getSuggestionSnippet() {
309 return $this->suggestion
;
312 public function hasRewrittenQuery() {
313 return $this->rewrittenQuery
!== null;
316 public function getQueryAfterRewrite() {
317 return $this->rewrittenQuery
;
320 public function getQueryAfterRewriteSnippet() {
321 return htmlspecialchars( $this->rewrittenQuery
);