MediaWiki  1.28.0
SpecialSearchTest.php
Go to the documentation of this file.
1 <?php
3 
12 
24  public function testProfileAndNamespaceLoading( $requested, $userOptions,
25  $expectedProfile, $expectedNS, $message = 'Profile name and namespaces mismatches!'
26  ) {
28  $context->setUser(
29  $this->newUserWithSearchNS( $userOptions )
30  );
31  /*
32  $context->setRequest( new FauxRequest( [
33  'ns5'=>true,
34  'ns6'=>true,
35  ] ));
36  */
37  $context->setRequest( new FauxRequest( $requested ) );
38  $search = new SpecialSearch();
39  $search->setContext( $context );
40  $search->load();
41 
47  $this->assertEquals(
48  [
49  'ProfileName' => $expectedProfile,
50  'Namespaces' => $expectedNS,
51  ],
52  [
53  'ProfileName' => $search->getProfile(),
54  'Namespaces' => $search->getNamespaces(),
55  ],
56  $message
57  );
58  }
59 
60  public static function provideSearchOptionsTests() {
61  $defaultNS = MediaWikiServices::getInstance()->getSearchEngineConfig()->defaultNamespaces();
62  $EMPTY_REQUEST = [];
63  $NO_USER_PREF = null;
64 
65  return [
73  [
74  $EMPTY_REQUEST, $NO_USER_PREF,
75  'default', $defaultNS,
76  'Bug 33270: No request nor user preferences should give default profile'
77  ],
78  [
79  [ 'ns5' => 1 ], $NO_USER_PREF,
80  'advanced', [ 5 ],
81  'Web request with specific NS should override user preference'
82  ],
83  [
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'
93  ],
94  ];
95  }
96 
102  function newUserWithSearchNS( $opt = null ) {
103  $u = User::newFromId( 0 );
104  if ( $opt === null ) {
105  return $u;
106  }
107  foreach ( $opt as $name => $value ) {
108  $u->setOption( $name, $value );
109  }
110 
111  return $u;
112  }
113 
118  public function testSearchTermIsNotExpanded() {
119  $this->setMwGlobals( [
120  'wgSearchType' => null,
121  ] );
122 
123  # Initialize [[Special::Search]]
124  $search = new SpecialSearch();
125  $search->getContext()->setTitle( Title::newFromText( 'Special:Search' ) );
126  $search->load();
127 
128  # Simulate a user searching for a given term
129  $term = '{{SITENAME}}';
130  $search->showResults( $term );
131 
132  # Lookup the HTML page title set for that page
133  $pageTitle = $search
134  ->getContext()
135  ->getOutput()
136  ->getHTMLTitle();
137 
138  # Compare :-]
139  $this->assertRegExp(
140  '/' . preg_quote( $term, '/' ) . '/',
141  $pageTitle,
142  "Search term '{$term}' should not be expanded in Special:Search <title>"
143  );
144  }
145 
147  return [
148  [
149  'With suggestion and no rewritten query shows did you mean',
150  '/Did you mean: <a[^>]+>first suggestion/',
151  new SpecialSearchTestMockResultSet( 'first suggestion', null, [
153  ] ),
154  ],
155 
156  [
157  'With rewritten query informs user of change',
158  '/Showing results for <a[^>]+>first suggestion/',
159  new SpecialSearchTestMockResultSet( 'asdf', 'first suggestion', [
161  ] ),
162  ],
163 
164  [
165  'When both queries have no results user gets no results',
166  '/There were no results matching the query/',
167  new SpecialSearchTestMockResultSet( 'first suggestion', 'first suggestion', [] ),
168  ],
169  ];
170  }
171 
175  public function testRewriteQueryWithSuggestion( $message, $expectRegex, $results ) {
176  $mockSearchEngine = $this->mockSearchEngine( $results );
177  $search = $this->getMockBuilder( 'SpecialSearch' )
178  ->setMethods( [ 'getSearchEngine' ] )
179  ->getMock();
180  $search->expects( $this->any() )
181  ->method( 'getSearchEngine' )
182  ->will( $this->returnValue( $mockSearchEngine ) );
183 
184  $search->getContext()->setTitle( Title::makeTitle( NS_SPECIAL, 'Search' ) );
185  $search->getContext()->setLanguage( Language::factory( 'en' ) );
186  $search->load();
187  $search->showResults( 'this is a fake search' );
188 
189  $html = $search->getContext()->getOutput()->getHTML();
190  foreach ( (array)$expectRegex as $regex ) {
191  $this->assertRegExp( $regex, $html, $message );
192  }
193  }
194 
195  protected function mockSearchEngine( $results ) {
196  $mock = $this->getMockBuilder( 'SearchEngine' )
197  ->setMethods( [ 'searchText', 'searchTitle' ] )
198  ->getMock();
199 
200  $mock->expects( $this->any() )
201  ->method( 'searchText' )
202  ->will( $this->returnValue( $results ) );
203 
204  return $mock;
205  }
206 }
207 
209  protected $results;
210  protected $suggestion;
211 
212  public function __construct(
213  $suggestion = null,
214  $rewrittenQuery = null,
215  array $results = [],
216  $containedSyntax = false
217  ) {
218  $this->suggestion = $suggestion;
219  $this->rewrittenQuery = $rewrittenQuery;
220  $this->results = $results;
221  $this->containedSyntax = $containedSyntax;
222  }
223 
224  public function numRows() {
225  return count( $this->results );
226  }
227 
228  public function getTotalHits() {
229  return $this->numRows();
230  }
231 
232  public function hasSuggestion() {
233  return $this->suggestion !== null;
234  }
235 
236  public function getSuggestionQuery() {
237  return $this->suggestion;
238  }
239 
240  public function getSuggestionSnippet() {
241  return $this->suggestion;
242  }
243 
244  public function hasRewrittenQuery() {
245  return $this->rewrittenQuery !== null;
246  }
247 
248  public function getQueryAfterRewrite() {
249  return $this->rewrittenQuery;
250  }
251 
252  public function getQueryAfterRewriteSnippet() {
253  return htmlspecialchars( $this->rewrittenQuery );
254  }
255 }
testRewriteQueryWithSuggestion($message, $expectRegex, $results)
provideRewriteQueryWithSuggestion
static newFromTitle($title, SearchResultSet $parentSet=null)
Return a new SearchResult and initializes it with a title.
external whereas SearchGetNearMatch runs after $term
Definition: hooks.txt:2713
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1936
the array() calling protocol came about after MediaWiki 1.4rc1.
Group all the pieces relevant to the context of a request into one instance.
$context
Definition: load.php:50
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:556
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
implements Special:Search - Run text & title search and display the output
newUserWithSearchNS($opt=null)
Helper to create a new User object with given options User remains anonymous though.
__construct($suggestion=null, $rewrittenQuery=null, array $results=[], $containedSyntax=false)
$value
static newFromId($id)
Static factory method for creation from a given user ID.
Definition: User.php:548
const NS_SPECIAL
Definition: Defines.php:45
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
testProfileAndNamespaceLoading($requested, $userOptions, $expectedProfile, $expectedNS, $message= 'Profile name and namespaces mismatches!')
SpecialSearch::load provideSearchOptionsTests.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$containedSyntax
Types of interwiki results.
Test class for SpecialSearch class Copyright © 2012, Antoine Musso.
static factory($code)
Get a cached or new language object for a given language code.
Definition: Language.php:181
setMwGlobals($pairs, $value=null)
static provideSearchOptionsTests()
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
testSearchTermIsNotExpanded()
Verify we do not expand search term in on search result page https://gerrit.wikimedia.org/r/4841. </div><div class="ttdef"><b>Definition:</b> <a href="includes_2specials_2SpecialSearchTest_8php_source.html#l00118">SpecialSearchTest.php:118</a></div></div> <div class="ttc" id="hooks_8txt_html_ae2d36f45856c4960c998f6c76c83b7a8"><div class="ttname"><a href="hooks_8txt.html#ae2d36f45856c4960c998f6c76c83b7a8">$name</a></div><div class="ttdeci">Allows to change the fields on the form that will be generated $name</div><div class="ttdef"><b>Definition:</b> <a href="hooks_8txt_source.html#l00300">hooks.txt:300</a></div></div> </div><!-- fragment --></div><!-- contents --> </div><!-- doc-content --> <!-- start footer part --> <div id="nav-path" class="navpath"><!-- id is needed for treeview function! --> <ul> <li class="navelem"><a class="el" href="dir_197e323488a662e0d55ab082ec5335e9.html">tests</a></li><li class="navelem"><a class="el" href="dir_b62fa320a9e07b0b1638c032971bd4c7.html">phpunit</a></li><li class="navelem"><a class="el" href="dir_be0ed11a9185eaaafa41a0406ed53d8e.html">includes</a></li><li class="navelem"><a class="el" href="dir_54e08873f2c93511c4dadc81b8518f8c.html">specials</a></li><li class="navelem"><a class="el" href="includes_2specials_2SpecialSearchTest_8php.html">SpecialSearchTest.php</a></li> <li class="footer">Generated on Mon Feb 20 2017 09:19:02 for MediaWiki by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.8 </li> </ul> </div> </body> </html>