Convert all array() syntax to []
[lhc/web/wiklou.git] / tests / phpunit / includes / specials / SpecialSearchTest.php
1 <?php
2 /**
3 * Test class for SpecialSearch class
4 * Copyright © 2012, Antoine Musso
5 *
6 * @author Antoine Musso
7 * @group Database
8 */
9
10 class SpecialSearchTest extends MediaWikiTestCase {
11
12 /**
13 * @covers SpecialSearch::load
14 * @dataProvider provideSearchOptionsTests
15 * @param array $requested Request parameters. For example:
16 * array( 'ns5' => true, 'ns6' => true). Null to use default options.
17 * @param array $userOptions User options to test with. For example:
18 * array('searchNs5' => 1 );. Null to use default options.
19 * @param string $expectedProfile An expected search profile name
20 * @param array $expectedNS Expected namespaces
21 * @param string $message
22 */
23 public function testProfileAndNamespaceLoading( $requested, $userOptions,
24 $expectedProfile, $expectedNS, $message = 'Profile name and namespaces mismatches!'
25 ) {
26 $context = new RequestContext;
27 $context->setUser(
28 $this->newUserWithSearchNS( $userOptions )
29 );
30 /*
31 $context->setRequest( new FauxRequest( array(
32 'ns5'=>true,
33 'ns6'=>true,
34 ) ));
35 */
36 $context->setRequest( new FauxRequest( $requested ) );
37 $search = new SpecialSearch();
38 $search->setContext( $context );
39 $search->load();
40
41 /**
42 * Verify profile name and namespace in the same assertion to make
43 * sure we will be able to fully compare the above code. PHPUnit stop
44 * after an assertion fail.
45 */
46 $this->assertEquals(
47 [ /** Expected: */
48 'ProfileName' => $expectedProfile,
49 'Namespaces' => $expectedNS,
50 ],
51 [ /** Actual: */
52 'ProfileName' => $search->getProfile(),
53 'Namespaces' => $search->getNamespaces(),
54 ],
55 $message
56 );
57 }
58
59 public static function provideSearchOptionsTests() {
60 $defaultNS = SearchEngine::defaultNamespaces();
61 $EMPTY_REQUEST = [];
62 $NO_USER_PREF = null;
63
64 return [
65 /**
66 * Parameters:
67 * <Web Request>, <User options>
68 * Followed by expected values:
69 * <ProfileName>, <NSList>
70 * Then an optional message.
71 */
72 [
73 $EMPTY_REQUEST, $NO_USER_PREF,
74 'default', $defaultNS,
75 'Bug 33270: No request nor user preferences should give default profile'
76 ],
77 [
78 [ 'ns5' => 1 ], $NO_USER_PREF,
79 'advanced', [ 5 ],
80 'Web request with specific NS should override user preference'
81 ],
82 [
83 $EMPTY_REQUEST, [
84 'searchNs2' => 1,
85 'searchNs14' => 1,
86 ] + array_fill_keys( array_map( function ( $ns ) {
87 return "searchNs$ns";
88 }, $defaultNS ), 0 ),
89 'advanced', [ 2, 14 ],
90 'Bug 33583: search with no option should honor User search preferences'
91 . ' and have all other namespace disabled'
92 ],
93 ];
94 }
95
96 /**
97 * Helper to create a new User object with given options
98 * User remains anonymous though
99 * @param array|null $opt
100 */
101 function newUserWithSearchNS( $opt = null ) {
102 $u = User::newFromId( 0 );
103 if ( $opt === null ) {
104 return $u;
105 }
106 foreach ( $opt as $name => $value ) {
107 $u->setOption( $name, $value );
108 }
109
110 return $u;
111 }
112
113 /**
114 * Verify we do not expand search term in <title> on search result page
115 * https://gerrit.wikimedia.org/r/4841
116 */
117 public function testSearchTermIsNotExpanded() {
118 $this->setMwGlobals( [
119 'wgSearchType' => null,
120 ] );
121
122 # Initialize [[Special::Search]]
123 $search = new SpecialSearch();
124 $search->getContext()->setTitle( Title::newFromText( 'Special:Search' ) );
125 $search->load();
126
127 # Simulate a user searching for a given term
128 $term = '{{SITENAME}}';
129 $search->showResults( $term );
130
131 # Lookup the HTML page title set for that page
132 $pageTitle = $search
133 ->getContext()
134 ->getOutput()
135 ->getHTMLTitle();
136
137 # Compare :-]
138 $this->assertRegExp(
139 '/' . preg_quote( $term, '/' ) . '/',
140 $pageTitle,
141 "Search term '{$term}' should not be expanded in Special:Search <title>"
142 );
143 }
144
145 public function provideRewriteQueryWithSuggestion() {
146 return [
147 [
148 'With suggestion and no rewritten query shows did you mean',
149 '/Did you mean: <a[^>]+>first suggestion/',
150 new SpecialSearchTestMockResultSet( 'first suggestion', null, [
151 SearchResult::newFromTitle( Title::newMainPage() ),
152 ] ),
153 ],
154
155 [
156 'With rewritten query informs user of change',
157 '/Showing results for <a[^>]+>first suggestion/',
158 new SpecialSearchTestMockResultSet( 'asdf', 'first suggestion', [
159 SearchResult::newFromTitle( Title::newMainPage() ),
160 ] ),
161 ],
162
163 [
164 'When both queries have no results user gets no results',
165 '/There were no results matching the query/',
166 new SpecialSearchTestMockResultSet( 'first suggestion', 'first suggestion', [] ),
167 ],
168 ];
169 }
170
171 /**
172 * @dataProvider provideRewriteQueryWithSuggestion
173 */
174 public function testRewriteQueryWithSuggestion( $message, $expectRegex, $results ) {
175 $mockSearchEngine = $this->mockSearchEngine( $results );
176 $search = $this->getMockBuilder( 'SpecialSearch' )
177 ->setMethods( [ 'getSearchEngine' ] )
178 ->getMock();
179 $search->expects( $this->any() )
180 ->method( 'getSearchEngine' )
181 ->will( $this->returnValue( $mockSearchEngine ) );
182
183 $search->getContext()->setTitle( Title::makeTitle( NS_SPECIAL, 'Search' ) );
184 $search->getContext()->setLanguage( Language::factory( 'en' ) );
185 $search->load();
186 $search->showResults( 'this is a fake search' );
187
188 $html = $search->getContext()->getOutput()->getHTML();
189 foreach ( (array)$expectRegex as $regex ) {
190 $this->assertRegExp( $regex, $html, $message );
191 }
192 }
193
194 protected function mockSearchEngine( $results ) {
195 $mock = $this->getMockBuilder( 'SearchEngine' )
196 ->setMethods( [ 'searchText', 'searchTitle' ] )
197 ->getMock();
198
199 $mock->expects( $this->any() )
200 ->method( 'searchText' )
201 ->will( $this->returnValue( $results ) );
202
203 return $mock;
204 }
205 }
206
207 class SpecialSearchTestMockResultSet extends SearchResultSet {
208 protected $results;
209 protected $suggestion;
210
211 public function __construct(
212 $suggestion = null,
213 $rewrittenQuery = null,
214 array $results = [],
215 $containedSyntax = false
216 ) {
217 $this->suggestion = $suggestion;
218 $this->rewrittenQuery = $rewrittenQuery;
219 $this->results = $results;
220 $this->containedSyntax = $containedSyntax;
221 }
222
223 public function numRows() {
224 return count( $this->results );
225 }
226
227 public function getTotalHits() {
228 return $this->numRows();
229 }
230
231 public function hasSuggestion() {
232 return $this->suggestion !== null;
233 }
234
235 public function getSuggestionQuery() {
236 return $this->suggestion;
237 }
238
239 public function getSuggestionSnippet() {
240 return $this->suggestion;
241 }
242
243 public function hasRewrittenQuery() {
244 return $this->rewrittenQuery !== null;
245 }
246
247 public function getQueryAfterRewrite() {
248 return $this->rewrittenQuery;
249 }
250
251 public function getQueryAfterRewriteSnippet() {
252 return htmlspecialchars( $this->rewrittenQuery );
253 }
254 }