Merge "Fix RevDel_RevisionItem::getAuthorNameField to work for ips"
[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 */
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 array( /** Expected: */
48 'ProfileName' => $expectedProfile,
49 'Namespaces' => $expectedNS,
50 ),
51 array( /** 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 = array();
62 $NO_USER_PREF = null;
63
64 return array(
65 /**
66 * Parameters:
67 * <Web Request>, <User options>
68 * Followed by expected values:
69 * <ProfileName>, <NSList>
70 * Then an optional message.
71 */
72 array(
73 $EMPTY_REQUEST, $NO_USER_PREF,
74 'default', $defaultNS,
75 'Bug 33270: No request nor user preferences should give default profile'
76 ),
77 array(
78 array( 'ns5' => 1 ), $NO_USER_PREF,
79 'advanced', array( 5 ),
80 'Web request with specific NS should override user preference'
81 ),
82 array(
83 $EMPTY_REQUEST, array(
84 'searchNs2' => 1,
85 'searchNs14' => 1,
86 ) + array_fill_keys( array_map( function ( $ns ) {
87 return "searchNs$ns";
88 }, $defaultNS ), 0 ),
89 'advanced', array( 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 */
100 function newUserWithSearchNS( $opt = null ) {
101 $u = User::newFromId( 0 );
102 if ( $opt === null ) {
103 return $u;
104 }
105 foreach ( $opt as $name => $value ) {
106 $u->setOption( $name, $value );
107 }
108
109 return $u;
110 }
111
112 /**
113 * Verify we do not expand search term in <title> on search result page
114 * https://gerrit.wikimedia.org/r/4841
115 */
116 public function testSearchTermIsNotExpanded() {
117
118 # Initialize [[Special::Search]]
119 $search = new SpecialSearch();
120 $search->getContext()->setTitle( Title::newFromText( 'Special:Search' ) );
121 $search->load();
122
123 # Simulate a user searching for a given term
124 $term = '{{SITENAME}}';
125 $search->showResults( $term );
126
127 # Lookup the HTML page title set for that page
128 $pageTitle = $search
129 ->getContext()
130 ->getOutput()
131 ->getHTMLTitle();
132
133 # Compare :-]
134 $this->assertRegExp(
135 '/' . preg_quote( $term ) . '/',
136 $pageTitle,
137 "Search term '{$term}' should not be expanded in Special:Search <title>"
138 );
139 }
140 }