Merge "Add missing @param to function docs"
[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 private $search;
12
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
23 */
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 )
30 );
31 /*
32 $context->setRequest( new FauxRequest( array(
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
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.
46 */
47 $this->assertEquals(
48 array( /** Expected: */
49 'ProfileName' => $expectedProfile,
50 'Namespaces' => $expectedNS,
51 ),
52 array( /** Actual: */
53 'ProfileName' => $search->getProfile(),
54 'Namespaces' => $search->getNamespaces(),
55 ),
56 $message
57 );
58 }
59
60 public static function provideSearchOptionsTests() {
61 $defaultNS = SearchEngine::defaultNamespaces();
62 $EMPTY_REQUEST = array();
63 $NO_USER_PREF = null;
64
65 return array(
66 /**
67 * Parameters:
68 * <Web Request>, <User options>
69 * Followed by expected values:
70 * <ProfileName>, <NSList>
71 * Then an optional message.
72 */
73 array(
74 $EMPTY_REQUEST, $NO_USER_PREF,
75 'default', $defaultNS,
76 'Bug 33270: No request nor user preferences should give default profile'
77 ),
78 array(
79 array( 'ns5' => 1 ), $NO_USER_PREF,
80 'advanced', array( 5 ),
81 'Web request with specific NS should override user preference'
82 ),
83 array(
84 $EMPTY_REQUEST, array(
85 'searchNs2' => 1,
86 'searchNs14' => 1,
87 ) + array_fill_keys( array_map( function ( $ns ) {
88 return "searchNs$ns";
89 }, $defaultNS ), 0 ),
90 'advanced', array( 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
97 /**
98 * Helper to create a new User object with given options
99 * User remains anonymous though
100 * @param array|null $opt
101 */
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
114 /**
115 * Verify we do not expand search term in <title> on search result page
116 * https://gerrit.wikimedia.org/r/4841
117 */
118 public function testSearchTermIsNotExpanded() {
119 $this->setMwGlobals( array(
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 }