test skeleton for Special:Search
authorAntoine Musso <hashar@users.mediawiki.org>
Mon, 9 Jan 2012 11:41:13 +0000 (11:41 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Mon, 9 Jan 2012 11:41:13 +0000 (11:41 +0000)
Only two tests for now. A third one was written for r106780 / bug 33583
but is disabled to avoid breaking Jenkins.

includes/specials/SpecialSearch.php
tests/phpunit/includes/specials/SpecialSearchTest.php [new file with mode: 0644]

index f884401..927464f 100644 (file)
@@ -37,6 +37,7 @@ class SpecialSearch extends SpecialPage {
         * null|string
         */
        protected $profile;
+       function getProfile() { return $this->profile; }
 
        /// Search engine
        protected $searchEngine;
@@ -56,6 +57,7 @@ class SpecialSearch extends SpecialPage {
         * @var array
         */
        protected $namespaces;
+       function getNamespaces() { return $this->namespaces; }
 
        /**
         * @var bool
diff --git a/tests/phpunit/includes/specials/SpecialSearchTest.php b/tests/phpunit/includes/specials/SpecialSearchTest.php
new file mode 100644 (file)
index 0000000..f444179
--- /dev/null
@@ -0,0 +1,108 @@
+<?php
+/**
+ * Test class for SpecialSearch class
+ * Copyright © 2012, Antoine Musso
+ *
+ * @author Antoine Musso
+ * @group Database
+ */
+
+class SpecialSearchTest extends MediaWikiTestCase {
+       private $search;
+
+       function setUp() { }
+       function tearDown() { }
+
+       /**
+        * @dataProvider provideSearchOptionsTests
+        * @param $requested Array Request parameters. For example array( 'ns5' => true, 'ns6' => true). NULL to use default options.
+        * @param $userOptions Array User options to test with. For example array('searchNs5' => 1 );. NULL to use default options.
+        */
+       function testFoobar(
+               $requested, $userOptions, $expectedProfile, $expectedNS,
+               $message = 'Profile name andnamespaces mismatches!'
+       ) {
+               $context = new RequestContext;
+               $context->setUser(
+                       $this->newUserWithSearchNS( $userOptions )
+               );
+               /*
+               $context->setRequest( new FauxRequest( array(
+                       'ns5'=>true,
+                       'ns6'=>true,
+               ) ));
+                */
+               $context->setRequest( new FauxRequest( $requested ));
+               $search = new SpecialSearch();
+               $search->setContext( $context );
+               $search->load();
+
+               /**
+                * Verify profile name and namespace in the same assertion to make
+                * sure we will be able to fully compare the above code. PHPUnit stop
+                * after an assertion fail.
+                */
+               $this->assertEquals(
+                       array( /** Expected: */
+                               'ProfileName' => $expectedProfile,
+                               'Namespaces'  => $expectedNS,
+                       )
+                       , array( /** Actual: */
+                               'ProfileName' => $search->getProfile(),
+                               'Namespaces'  => $search->getNamespaces(),
+                       )
+                       , $message
+               );
+
+       }
+
+       function provideSearchOptionsTests() {
+       $defaultNS = SearchEngine::defaultNamespaces();
+       $EMPTY_REQUEST = array();
+       $NO_USER_PREF  = null;
+
+       return array(
+               /**
+                * Parameters:
+                *      <Web Request>, <User options>
+                * Followed by expected values:
+                *      <ProfileName>, <NSList>
+                * Then an optional message.
+                */
+               array(
+                       $EMPTY_REQUEST, $NO_USER_PREF,
+                       'default', $defaultNS,
+                       'Bug 33270: No request nor user preferences should give default profile'
+               ),
+               array(
+                       array( 'ns5' => 1 ), $NO_USER_PREF,
+                       'advanced', array(  5),
+                       'Web request with specific NS should override user preference'
+               ),
+               /* FIXME this test is for bug 33583
+               array(
+                       $EMPTY_REQUEST, array( 'searchNs2' ),
+                       'advanced', array( 2 ),
+                       'Bug 33583: search with no option should honor User search preferences'
+               ),
+                */
+
+       );
+       }
+
+       /**
+        * Helper to create a new User object with given options
+        * User remains anonymous though
+        */
+       function newUserWithSearchNS( $opt = null ) {
+               $u = User::newFromId(0);
+               if( $opt === null ) {
+                       return $u;
+               }
+               foreach($opt as $name => $value) {
+                       $u->setOption( $name, $value );
+               }
+               return $u;
+       }
+}
+