b1c06cdf6df71ea8cc5ba5e5691bc869b1bf101e
[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 * @dataProvider provideSearchOptionsTests
18 * @param $requested Array Request parameters. For example array( 'ns5' => true, 'ns6' => true). NULL to use default options.
19 * @param $userOptions Array User options to test with. For example array('searchNs5' => 1 );. NULL to use default options.
20 */
21 function testFoobar(
22 $requested, $userOptions, $expectedProfile, $expectedNS,
23 $message = 'Profile name andnamespaces mismatches!'
24 ) {
25 $context = new RequestContext;
26 $context->setUser(
27 $this->newUserWithSearchNS( $userOptions )
28 );
29 /*
30 $context->setRequest( new FauxRequest( array(
31 'ns5'=>true,
32 'ns6'=>true,
33 ) ));
34 */
35 $context->setRequest( new FauxRequest( $requested ));
36 $search = new SpecialSearch();
37 $search->setContext( $context );
38 $search->load();
39
40 /**
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.
44 */
45 $this->assertEquals(
46 array( /** Expected: */
47 'ProfileName' => $expectedProfile,
48 'Namespaces' => $expectedNS,
49 )
50 , array( /** Actual: */
51 'ProfileName' => $search->getProfile(),
52 'Namespaces' => $search->getNamespaces(),
53 )
54 , $message
55 );
56
57 }
58
59 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 /* FIXME this test is for bug 33583
83 array(
84 $EMPTY_REQUEST, array( 'searchNs2' ),
85 'advanced', array( 2 ),
86 'Bug 33583: search with no option should honor User search preferences'
87 ),
88 */
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