3 * Test class for SpecialSearch class
4 * Copyright © 2012, Antoine Musso
6 * @author Antoine Musso
10 class SpecialSearchTest
extends MediaWikiTestCase
{
14 * @covers SpecialSearch::load
15 * @dataProvider provideSearchOptionsTests
16 * @param $requested Array Request parameters. For example array( 'ns5' => true, 'ns6' => true). NULL to use default options.
17 * @param $userOptions Array User options to test with. For example array('searchNs5' => 1 );. NULL to use default options.
18 * @param $expectedProfile An expected search profile name
19 * @param $expectedNs Array Expected namespaces
21 function testProfileAndNamespaceLoading(
22 $requested, $userOptions, $expectedProfile, $expectedNS,
23 $message = 'Profile name and namespaces mismatches!'
25 $context = new RequestContext
;
27 $this->newUserWithSearchNS( $userOptions )
30 $context->setRequest( new FauxRequest( array(
35 $context->setRequest( new FauxRequest( $requested ) );
36 $search = new SpecialSearch();
37 $search->setContext( $context );
41 * Verify profile name and namespace in the same assertion to make
42 * sure we will be able to fully compare the above code. PHPUnit stop
43 * after an assertion fail.
46 array( /** Expected: */
47 'ProfileName' => $expectedProfile,
48 'Namespaces' => $expectedNS,
50 , array( /** Actual: */
51 'ProfileName' => $search->getProfile(),
52 'Namespaces' => $search->getNamespaces(),
58 public static function provideSearchOptionsTests() {
59 $defaultNS = SearchEngine
::defaultNamespaces();
60 $EMPTY_REQUEST = array();
66 * <Web Request>, <User options>
67 * Followed by expected values:
68 * <ProfileName>, <NSList>
69 * Then an optional message.
72 $EMPTY_REQUEST, $NO_USER_PREF,
73 'default', $defaultNS,
74 'Bug 33270: No request nor user preferences should give default profile'
77 array( 'ns5' => 1 ), $NO_USER_PREF,
78 'advanced', array( 5 ),
79 'Web request with specific NS should override user preference'
82 $EMPTY_REQUEST, array(
85 ) +
array_fill_keys( array_map( function ( $ns ) {
88 'advanced', array( 2, 14 ),
89 'Bug 33583: search with no option should honor User search preferences'
90 . ' and have all other namespace disabled'
96 * Helper to create a new User object with given options
97 * User remains anonymous though
99 function newUserWithSearchNS( $opt = null ) {
100 $u = User
::newFromId( 0 );
101 if ( $opt === null ) {
104 foreach ( $opt as $name => $value ) {
105 $u->setOption( $name, $value );
112 * Verify we do not expand search term in <title> on search result page
113 * https://gerrit.wikimedia.org/r/4841
115 function testSearchTermIsNotExpanded() {
117 # Initialize [[Special::Search]]
118 $search = new SpecialSearch();
119 $search->getContext()->setTitle( Title
::newFromText( 'Special:Search' ) );
122 # Simulate a user searching for a given term
123 $term = '{{SITENAME}}';
124 $search->showResults( $term );
126 # Lookup the HTML page title set for that page
134 '/' . preg_quote( $term ) . '/',
136 "Search term '{$term}' should not be expanded in Special:Search <title>"