MediaWiki  REL1_21
SpecialSearchTest.php
Go to the documentation of this file.
00001 <?php
00010 class SpecialSearchTest extends MediaWikiTestCase {
00011         private $search;
00012 
00021         function testProfileAndNamespaceLoading(
00022                 $requested, $userOptions, $expectedProfile, $expectedNS,
00023                 $message = 'Profile name and namespaces mismatches!'
00024         ) {
00025                 $context = new RequestContext;
00026                 $context->setUser(
00027                         $this->newUserWithSearchNS( $userOptions )
00028                 );
00029                 /*
00030                 $context->setRequest( new FauxRequest( array(
00031                         'ns5'=>true,
00032                         'ns6'=>true,
00033                 ) ));
00034                  */
00035                 $context->setRequest( new FauxRequest( $requested ) );
00036                 $search = new SpecialSearch();
00037                 $search->setContext( $context );
00038                 $search->load();
00039 
00045                 $this->assertEquals(
00046                         array( 
00047                                 'ProfileName' => $expectedProfile,
00048                                 'Namespaces' => $expectedNS,
00049                         )
00050                         , array( 
00051                                 'ProfileName' => $search->getProfile(),
00052                                 'Namespaces' => $search->getNamespaces(),
00053                         )
00054                         , $message
00055                 );
00056 
00057         }
00058 
00059         function provideSearchOptionsTests() {
00060                 $defaultNS = SearchEngine::defaultNamespaces();
00061                 $EMPTY_REQUEST = array();
00062                 $NO_USER_PREF = null;
00063 
00064                 return array(
00072                         array(
00073                                 $EMPTY_REQUEST, $NO_USER_PREF,
00074                                 'default', $defaultNS,
00075                                 'Bug 33270: No request nor user preferences should give default profile'
00076                         ),
00077                         array(
00078                                 array( 'ns5' => 1 ), $NO_USER_PREF,
00079                                 'advanced', array( 5 ),
00080                                 'Web request with specific NS should override user preference'
00081                         ),
00082                         array(
00083                                 $EMPTY_REQUEST, array(
00084                                 'searchNs2' => 1,
00085                                 'searchNs14' => 1,
00086                         ) + array_fill_keys( array_map( function ( $ns ) {
00087                                 return "searchNs$ns";
00088                         }, $defaultNS ), 0 ),
00089                                 'advanced', array( 2, 14 ),
00090                                 'Bug 33583: search with no option should honor User search preferences'
00091                                         . ' and have all other namespace disabled'
00092                         ),
00093                 );
00094         }
00095 
00100         function newUserWithSearchNS( $opt = null ) {
00101                 $u = User::newFromId( 0 );
00102                 if ( $opt === null ) {
00103                         return $u;
00104                 }
00105                 foreach ( $opt as $name => $value ) {
00106                         $u->setOption( $name, $value );
00107                 }
00108                 return $u;
00109         }
00110 
00115         function testSearchTermIsNotExpanded() {
00116 
00117                 # Initialize [[Special::Search]]
00118                 $search = new SpecialSearch();
00119                 $search->getContext()->setTitle( Title::newFromText( 'Special:Search' ) );
00120                 $search->load();
00121 
00122                 # Simulate a user searching for a given term
00123                 $term = '{{SITENAME}}';
00124                 $search->showResults( $term );
00125 
00126                 # Lookup the HTML page title set for that page
00127                 $pageTitle = $search
00128                         ->getContext()
00129                         ->getOutput()
00130                         ->getHTMLTitle();
00131 
00132                 # Compare :-]
00133                 $this->assertRegExp(
00134                         '/' . preg_quote( $term ) . '/',
00135                         $pageTitle,
00136                         "Search term '{$term}' should not be expanded in Special:Search <title>"
00137                 );
00138 
00139         }
00140 }