3 * Test class for SpecialSearch class
4 * Copyright © 2012, Antoine Musso
6 * @author Antoine Musso
10 class SpecialSearchTest
extends MediaWikiTestCase
{
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
23 public function testProfileAndNamespaceLoading( $requested, $userOptions,
24 $expectedProfile, $expectedNS, $message = 'Profile name and namespaces mismatches!'
26 $context = new RequestContext
;
28 $this->newUserWithSearchNS( $userOptions )
31 $context->setRequest( new FauxRequest( array(
36 $context->setRequest( new FauxRequest( $requested ) );
37 $search = new SpecialSearch();
38 $search->setContext( $context );
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.
47 array( /** Expected: */
48 'ProfileName' => $expectedProfile,
49 'Namespaces' => $expectedNS,
52 'ProfileName' => $search->getProfile(),
53 'Namespaces' => $search->getNamespaces(),
59 public static function provideSearchOptionsTests() {
60 $defaultNS = SearchEngine
::defaultNamespaces();
61 $EMPTY_REQUEST = array();
67 * <Web Request>, <User options>
68 * Followed by expected values:
69 * <ProfileName>, <NSList>
70 * Then an optional message.
73 $EMPTY_REQUEST, $NO_USER_PREF,
74 'default', $defaultNS,
75 'Bug 33270: No request nor user preferences should give default profile'
78 array( 'ns5' => 1 ), $NO_USER_PREF,
79 'advanced', array( 5 ),
80 'Web request with specific NS should override user preference'
83 $EMPTY_REQUEST, array(
86 ) +
array_fill_keys( array_map( function ( $ns ) {
89 'advanced', array( 2, 14 ),
90 'Bug 33583: search with no option should honor User search preferences'
91 . ' and have all other namespace disabled'
97 * Helper to create a new User object with given options
98 * User remains anonymous though
99 * @param array|null $opt
101 function newUserWithSearchNS( $opt = null ) {
102 $u = User
::newFromId( 0 );
103 if ( $opt === null ) {
106 foreach ( $opt as $name => $value ) {
107 $u->setOption( $name, $value );
114 * Verify we do not expand search term in <title> on search result page
115 * https://gerrit.wikimedia.org/r/4841
117 public function testSearchTermIsNotExpanded() {
118 $this->setMwGlobals( array(
119 'wgSearchType' => null,
122 # Initialize [[Special::Search]]
123 $search = new SpecialSearch();
124 $search->getContext()->setTitle( Title
::newFromText( 'Special:Search' ) );
127 # Simulate a user searching for a given term
128 $term = '{{SITENAME}}';
129 $search->showResults( $term );
131 # Lookup the HTML page title set for that page
139 '/' . preg_quote( $term ) . '/',
141 "Search term '{$term}' should not be expanded in Special:Search <title>"