Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / specials / SpecialSearchTest.php
blobe9cf6a3516ad151848e97724c82134635257e170
1 <?php
2 use MediaWiki\MediaWikiServices;
4 /**
5 * Test class for SpecialSearch class
6 * Copyright © 2012, Antoine Musso
8 * @author Antoine Musso
9 * @group Database
11 class SpecialSearchTest extends MediaWikiTestCase {
13 /**
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!'
26 ) {
27 $context = new RequestContext;
28 $context->setUser(
29 $this->newUserWithSearchNS( $userOptions )
32 $context->setRequest( new FauxRequest( [
33 'ns5'=>true,
34 'ns6'=>true,
35 ] ));
37 $context->setRequest( new FauxRequest( $requested ) );
38 $search = new SpecialSearch();
39 $search->setContext( $context );
40 $search->load();
42 /**
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.
47 $this->assertEquals(
48 [ /** Expected: */
49 'ProfileName' => $expectedProfile,
50 'Namespaces' => $expectedNS,
52 [ /** Actual: */
53 'ProfileName' => $search->getProfile(),
54 'Namespaces' => $search->getNamespaces(),
56 $message
60 public static function provideSearchOptionsTests() {
61 $defaultNS = MediaWikiServices::getInstance()->getSearchEngineConfig()->defaultNamespaces();
62 $EMPTY_REQUEST = [];
63 $NO_USER_PREF = null;
65 return [
66 /**
67 * Parameters:
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,
80 'advanced', [ 5 ],
81 'Web request with specific NS should override user preference'
84 $EMPTY_REQUEST, [
85 'searchNs2' => 1,
86 'searchNs14' => 1,
87 ] + array_fill_keys( array_map( function ( $ns ) {
88 return "searchNs$ns";
89 }, $defaultNS ), 0 ),
90 'advanced', [ 2, 14 ],
91 'Bug 33583: search with no option should honor User search preferences'
92 . ' and have all other namespace disabled'
97 /**
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 ) {
105 return $u;
107 foreach ( $opt as $name => $value ) {
108 $u->setOption( $name, $value );
111 return $u;
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,
121 ] );
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
135 $pageTitle = $search
136 ->getContext()
137 ->getOutput()
138 ->getHTMLTitle();
140 # Compare :-]
141 $this->assertRegExp(
142 '/' . preg_quote( $term, '/' ) . '/',
143 $pageTitle,
144 "Search term '{$term}' should not be expanded in Special:Search <title>"
148 public function provideRewriteQueryWithSuggestion() {
149 return [
151 'With suggestion and no rewritten query shows did you mean',
152 '/Did you mean: <a[^>]+>first suggestion/',
153 new SpecialSearchTestMockResultSet( 'first suggestion', null, [
154 SearchResult::newFromTitle( Title::newMainPage() ),
155 ] ),
159 'With rewritten query informs user of change',
160 '/Showing results for <a[^>]+>first suggestion/',
161 new SpecialSearchTestMockResultSet( 'asdf', 'first suggestion', [
162 SearchResult::newFromTitle( Title::newMainPage() ),
163 ] ),
167 'When both queries have no results user gets no results',
168 '/There were no results matching the query/',
169 new SpecialSearchTestMockResultSet( 'first suggestion', 'first suggestion', [] ),
175 * @dataProvider provideRewriteQueryWithSuggestion
177 public function testRewriteQueryWithSuggestion( $message, $expectRegex, $results ) {
178 $mockSearchEngine = $this->mockSearchEngine( $results );
179 $search = $this->getMockBuilder( 'SpecialSearch' )
180 ->setMethods( [ 'getSearchEngine' ] )
181 ->getMock();
182 $search->expects( $this->any() )
183 ->method( 'getSearchEngine' )
184 ->will( $this->returnValue( $mockSearchEngine ) );
186 $search->getContext()->setTitle( Title::makeTitle( NS_SPECIAL, 'Search' ) );
187 $search->getContext()->setLanguage( Language::factory( 'en' ) );
188 $search->load();
189 $search->showResults( 'this is a fake search' );
191 $html = $search->getContext()->getOutput()->getHTML();
192 foreach ( (array)$expectRegex as $regex ) {
193 $this->assertRegExp( $regex, $html, $message );
197 protected function mockSearchEngine( $results ) {
198 $mock = $this->getMockBuilder( 'SearchEngine' )
199 ->setMethods( [ 'searchText', 'searchTitle' ] )
200 ->getMock();
202 $mock->expects( $this->any() )
203 ->method( 'searchText' )
204 ->will( $this->returnValue( $results ) );
206 return $mock;
210 class SpecialSearchTestMockResultSet extends SearchResultSet {
211 protected $results;
212 protected $suggestion;
214 public function __construct(
215 $suggestion = null,
216 $rewrittenQuery = null,
217 array $results = [],
218 $containedSyntax = false
220 $this->suggestion = $suggestion;
221 $this->rewrittenQuery = $rewrittenQuery;
222 $this->results = $results;
223 $this->containedSyntax = $containedSyntax;
226 public function numRows() {
227 return count( $this->results );
230 public function getTotalHits() {
231 return $this->numRows();
234 public function hasSuggestion() {
235 return $this->suggestion !== null;
238 public function getSuggestionQuery() {
239 return $this->suggestion;
242 public function getSuggestionSnippet() {
243 return $this->suggestion;
246 public function hasRewrittenQuery() {
247 return $this->rewrittenQuery !== null;
250 public function getQueryAfterRewrite() {
251 return $this->rewrittenQuery;
254 public function getQueryAfterRewriteSnippet() {
255 return htmlspecialchars( $this->rewrittenQuery );