Merge branch 'master' into Wikidata
[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 function setUp() { }
14 function tearDown() { }
15
16 /**
17 * @covers SpecialSearch::load
18 * @dataProvider provideSearchOptionsTests
19 * @param $requested Array Request parameters. For example array( 'ns5' => true, 'ns6' => true). NULL to use default options.
20 * @param $userOptions Array User options to test with. For example array('searchNs5' => 1 );. NULL to use default options.
21 * @param $expectedProfile An expected search profile name
22 * @param $expectedNs Array Expected namespaces
23 */
24 function testProfileAndNamespaceLoading(
25 $requested, $userOptions, $expectedProfile, $expectedNS,
26 $message = 'Profile name and namespaces mismatches!'
27 ) {
28 $context = new RequestContext;
29 $context->setUser(
30 $this->newUserWithSearchNS( $userOptions )
31 );
32 /*
33 $context->setRequest( new FauxRequest( array(
34 'ns5'=>true,
35 'ns6'=>true,
36 ) ));
37 */
38 $context->setRequest( new FauxRequest( $requested ));
39 $search = new SpecialSearch();
40 $search->setContext( $context );
41 $search->load();
42
43 /**
44 * Verify profile name and namespace in the same assertion to make
45 * sure we will be able to fully compare the above code. PHPUnit stop
46 * after an assertion fail.
47 */
48 $this->assertEquals(
49 array( /** Expected: */
50 'ProfileName' => $expectedProfile,
51 'Namespaces' => $expectedNS,
52 )
53 , array( /** Actual: */
54 'ProfileName' => $search->getProfile(),
55 'Namespaces' => $search->getNamespaces(),
56 )
57 , $message
58 );
59
60 }
61
62 function provideSearchOptionsTests() {
63 $defaultNS = SearchEngine::defaultNamespaces();
64 $EMPTY_REQUEST = array();
65 $NO_USER_PREF = null;
66
67 return array(
68 /**
69 * Parameters:
70 * <Web Request>, <User options>
71 * Followed by expected values:
72 * <ProfileName>, <NSList>
73 * Then an optional message.
74 */
75 array(
76 $EMPTY_REQUEST, $NO_USER_PREF,
77 'default', $defaultNS,
78 'Bug 33270: No request nor user preferences should give default profile'
79 ),
80 array(
81 array( 'ns5' => 1 ), $NO_USER_PREF,
82 'advanced', array( 5),
83 'Web request with specific NS should override user preference'
84 ),
85 array(
86 $EMPTY_REQUEST, array( 'searchNs2' => 1, 'searchNs14' => 1 ),
87 'advanced', array( 2, 14 ),
88 'Bug 33583: search with no option should honor User search preferences'
89 ),
90 );
91 }
92
93 /**
94 * Helper to create a new User object with given options
95 * User remains anonymous though
96 */
97 function newUserWithSearchNS( $opt = null ) {
98 $u = User::newFromId(0);
99 if( $opt === null ) {
100 return $u;
101 }
102 foreach($opt as $name => $value) {
103 $u->setOption( $name, $value );
104 }
105 return $u;
106 }
107 }
108