| Index: trunk/phase3/tests/phpunit/includes/specials/SpecialSearchTest.php |
| — | — | @@ -0,0 +1,108 @@ |
| | 2 | +<?php |
| | 3 | +/** |
| | 4 | + * Test class for SpecialSearch class |
| | 5 | + * Copyright © 2012, Antoine Musso |
| | 6 | + * |
| | 7 | + * @author Antoine Musso |
| | 8 | + * @group Database |
| | 9 | + */ |
| | 10 | + |
| | 11 | +class SpecialSearchTest extends MediaWikiTestCase { |
| | 12 | + private $search; |
| | 13 | + |
| | 14 | + function setUp() { } |
| | 15 | + function tearDown() { } |
| | 16 | + |
| | 17 | + /** |
| | 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 | + */ |
| | 22 | + function testFoobar( |
| | 23 | + $requested, $userOptions, $expectedProfile, $expectedNS, |
| | 24 | + $message = 'Profile name andnamespaces 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 | + |
| | 60 | + 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 | + /* FIXME this test is for bug 33583 |
| | 84 | + array( |
| | 85 | + $EMPTY_REQUEST, array( 'searchNs2' ), |
| | 86 | + 'advanced', array( 2 ), |
| | 87 | + 'Bug 33583: search with no option should honor User search preferences' |
| | 88 | + ), |
| | 89 | + */ |
| | 90 | + |
| | 91 | + ); |
| | 92 | + } |
| | 93 | + |
| | 94 | + /** |
| | 95 | + * Helper to create a new User object with given options |
| | 96 | + * User remains anonymous though |
| | 97 | + */ |
| | 98 | + function newUserWithSearchNS( $opt = null ) { |
| | 99 | + $u = User::newFromId(0); |
| | 100 | + if( $opt === null ) { |
| | 101 | + return $u; |
| | 102 | + } |
| | 103 | + foreach($opt as $name => $value) { |
| | 104 | + $u->setOption( $name, $value ); |
| | 105 | + } |
| | 106 | + return $u; |
| | 107 | + } |
| | 108 | +} |
| | 109 | + |
| Property changes on: trunk/phase3/tests/phpunit/includes/specials/SpecialSearchTest.php |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| 1 | 110 | + native |
| Index: trunk/phase3/includes/specials/SpecialSearch.php |
| — | — | @@ -37,6 +37,7 @@ |
| 38 | 38 | * null|string |
| 39 | 39 | */ |
| 40 | 40 | protected $profile; |
| | 41 | + function getProfile() { return $this->profile; } |
| 41 | 42 | |
| 42 | 43 | /// Search engine |
| 43 | 44 | protected $searchEngine; |
| — | — | @@ -56,6 +57,7 @@ |
| 57 | 58 | * @var array |
| 58 | 59 | */ |
| 59 | 60 | protected $namespaces; |
| | 61 | + function getNamespaces() { return $this->namespaces; } |
| 60 | 62 | |
| 61 | 63 | /** |
| 62 | 64 | * @var bool |