Do not insert page titles into querycache.qc_value
[lhc/web/wiklou.git] / tests / phpunit / includes / specials / SpecialSearchTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * Test class for SpecialSearch class
7 * Copyright © 2012, Antoine Musso
8 *
9 * @author Antoine Musso
10 * @group Database
11 */
12 class SpecialSearchTest extends MediaWikiTestCase {
13
14 /**
15 * @covers SpecialSearch::load
16 * @covers SpecialSearch::showResults
17 */
18 public function testValidateSortOrder() {
19 $ctx = new RequestContext();
20 $ctx->setRequest( new FauxRequest( [
21 'search' => 'foo',
22 'fulltext' => 1,
23 'sort' => 'invalid',
24 ] ) );
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' );
33 }
34
35 /**
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
45 */
46 public function testProfileAndNamespaceLoading( $requested, $userOptions,
47 $expectedProfile, $expectedNS, $message = 'Profile name and namespaces mismatches!'
48 ) {
49 $context = new RequestContext;
50 $context->setUser(
51 $this->newUserWithSearchNS( $userOptions )
52 );
53 /*
54 $context->setRequest( new FauxRequest( [
55 'ns5'=>true,
56 'ns6'=>true,
57 ] ));
58 */
59 $context->setRequest( new FauxRequest( $requested ) );
60 $search = new SpecialSearch();
61 $search->setContext( $context );
62 $search->load();
63
64 /**
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.
68 */
69 $this->assertEquals(
70 [ /** Expected: */
71 'ProfileName' => $expectedProfile,
72 'Namespaces' => $expectedNS,
73 ],
74 [ /** Actual: */
75 'ProfileName' => $search->getProfile(),
76 'Namespaces' => $search->getNamespaces(),
77 ],
78 $message
79 );
80 }
81
82 public static function provideSearchOptionsTests() {
83 $defaultNS = MediaWikiServices::getInstance()->getSearchEngineConfig()->defaultNamespaces();
84 $EMPTY_REQUEST = [];
85 $NO_USER_PREF = null;
86
87 return [
88 /**
89 * Parameters:
90 * <Web Request>, <User options>
91 * Followed by expected values:
92 * <ProfileName>, <NSList>
93 * Then an optional message.
94 */
95 [
96 $EMPTY_REQUEST, $NO_USER_PREF,
97 'default', $defaultNS,
98 'T35270: No request nor user preferences should give default profile'
99 ],
100 [
101 [ 'ns5' => 1 ], $NO_USER_PREF,
102 'advanced', [ 5 ],
103 'Web request with specific NS should override user preference'
104 ],
105 [
106 $EMPTY_REQUEST, [
107 'searchNs2' => 1,
108 'searchNs14' => 1,
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'
115 ],
116 ];
117 }
118
119 /**
120 * Helper to create a new User object with given options
121 * User remains anonymous though
122 * @param array|null $opt
123 */
124 function newUserWithSearchNS( $opt = null ) {
125 $u = User::newFromId( 0 );
126 if ( $opt === null ) {
127 return $u;
128 }
129 foreach ( $opt as $name => $value ) {
130 $u->setOption( $name, $value );
131 }
132
133 return $u;
134 }
135
136 /**
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
140 */
141 public function testSearchTermIsNotExpanded() {
142 $this->setMwGlobals( [
143 'wgSearchType' => null,
144 ] );
145
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 );
153
154 # Simulate a user searching for a given term
155 $search->execute( '' );
156
157 # Lookup the HTML page title set for that page
158 $pageTitle = $search
159 ->getContext()
160 ->getOutput()
161 ->getHTMLTitle();
162
163 # Compare :-]
164 $this->assertRegExp(
165 '/' . preg_quote( $term, '/' ) . '/',
166 $pageTitle,
167 "Search term '{$term}' should not be expanded in Special:Search <title>"
168 );
169 }
170
171 public function provideRewriteQueryWithSuggestion() {
172 return [
173 [
174 'With suggestion and no rewritten query shows did you mean',
175 '/Did you mean: <a[^>]+>first suggestion/',
176 'first suggestion',
177 null,
178 [ Title::newMainPage() ]
179 ],
180
181 [
182 'With rewritten query informs user of change',
183 '/Showing results for <a[^>]+>first suggestion/',
184 'asdf',
185 'first suggestion',
186 [ Title::newMainPage() ]
187 ],
188
189 [
190 'When both queries have no results user gets no results',
191 '/There were no results matching the query/',
192 'first suggestion',
193 'first suggestion',
194 []
195 ],
196 ];
197 }
198
199 /**
200 * @dataProvider provideRewriteQueryWithSuggestion
201 * @covers SpecialSearch::showResults
202 */
203 public function testRewriteQueryWithSuggestion(
204 $message,
205 $expectRegex,
206 $suggestion,
207 $rewrittenQuery,
208 array $resultTitles
209 ) {
210 $results = array_map( function ( $title ) {
211 return SearchResult::newFromTitle( $title );
212 }, $resultTitles );
213
214 $searchResults = new SpecialSearchTestMockResultSet(
215 $suggestion,
216 $rewrittenQuery,
217 $results
218 );
219
220 $mockSearchEngine = $this->mockSearchEngine( $searchResults );
221 $search = $this->getMockBuilder( SpecialSearch::class )
222 ->setMethods( [ 'getSearchEngine' ] )
223 ->getMock();
224 $search->expects( $this->any() )
225 ->method( 'getSearchEngine' )
226 ->will( $this->returnValue( $mockSearchEngine ) );
227
228 $search->getContext()->setTitle( Title::makeTitle( NS_SPECIAL, 'Search' ) );
229 $search->getContext()->setLanguage( Language::factory( 'en' ) );
230 $search->load();
231 $search->showResults( 'this is a fake search' );
232
233 $html = $search->getContext()->getOutput()->getHTML();
234 foreach ( (array)$expectRegex as $regex ) {
235 $this->assertRegExp( $regex, $html, $message );
236 }
237 }
238
239 protected function mockSearchEngine( $results ) {
240 $mock = $this->getMockBuilder( SearchEngine::class )
241 ->setMethods( [ 'searchText', 'searchTitle' ] )
242 ->getMock();
243
244 $mock->expects( $this->any() )
245 ->method( 'searchText' )
246 ->will( $this->returnValue( $results ) );
247
248 return $mock;
249 }
250
251 /**
252 * @covers SpecialSearch::execute
253 */
254 public function testSubPageRedirect() {
255 $this->setMwGlobals( [
256 'wgScript' => '/w/index.php',
257 ] );
258
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 );
267
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'] );
273 }
274 }
275
276 class SpecialSearchTestMockResultSet extends SearchResultSet {
277 protected $results;
278 protected $suggestion;
279
280 public function __construct(
281 $suggestion = null,
282 $rewrittenQuery = null,
283 array $results = [],
284 $containedSyntax = false
285 ) {
286 $this->suggestion = $suggestion;
287 $this->rewrittenQuery = $rewrittenQuery;
288 $this->results = $results;
289 $this->containedSyntax = $containedSyntax;
290 }
291
292 public function expandResults() {
293 return $this->results;
294 }
295
296 public function getTotalHits() {
297 return $this->numRows();
298 }
299
300 public function hasSuggestion() {
301 return $this->suggestion !== null;
302 }
303
304 public function getSuggestionQuery() {
305 return $this->suggestion;
306 }
307
308 public function getSuggestionSnippet() {
309 return $this->suggestion;
310 }
311
312 public function hasRewrittenQuery() {
313 return $this->rewrittenQuery !== null;
314 }
315
316 public function getQueryAfterRewrite() {
317 return $this->rewrittenQuery;
318 }
319
320 public function getQueryAfterRewriteSnippet() {
321 return htmlspecialchars( $this->rewrittenQuery );
322 }
323 }