Florianschmidtwelzow has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/230474

Change subject: Add unit tests and fix some failures
......................................................................

Add unit tests and fix some failures

Add basic phpunit tests for basic functions.

Fix setFirstResult, that would try to create a new title, if the given title
object does not exist (which would result in an exception, titles can be crafted
from strings only).

Fix deprecation warning for ApiBase::getResultData and use
ApiBase::getResult()->getResultData() instead. Also, add metadata only,
if the result data contains a page id.

Change-Id: I7aca81842e627af0bc03f9594956ca085df32be5
---
M extension.json
M includes/QuickSearchLookup.hooks.php
M includes/QuickSearchLookup.php
A tests/phpunit/QuickSearchLookupTest.php
4 files changed, 106 insertions(+), 4 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/QuickSearchLookup 
refs/changes/74/230474/1

diff --git a/extension.json b/extension.json
index c31af1a..a44d5cf 100644
--- a/extension.json
+++ b/extension.json
@@ -21,6 +21,9 @@
                ],
                "SpecialSearchResultsAppend": [
                        "QuickSearchLookupHooks::onSpecialSearchResultsAppend"
+               ],
+               "UnitTestsList": [
+                       "QuickSearchLookupHooks::onUnitTestsList"
                ]
        },
        "ResourceModules": {
diff --git a/includes/QuickSearchLookup.hooks.php 
b/includes/QuickSearchLookup.hooks.php
index e311aca..9f2a53a 100644
--- a/includes/QuickSearchLookup.hooks.php
+++ b/includes/QuickSearchLookup.hooks.php
@@ -38,4 +38,16 @@
        public static function onSpecialSearchResultsAppend( SpecialSearch 
$specialSearch, OutputPage $output ) {
                QuickSearchLookup::getMain()->outputLookup( $output );
        }
+
+       /**
+        * UnitTestsList hook handler
+        * @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList
+        *
+        * @param array $files
+        * @return bool
+        */
+       public static function onUnitTestsList( &$files ) {
+               $files[] = __DIR__ . '/../tests/phpunit';
+               return true;
+       }
 }
\ No newline at end of file
diff --git a/includes/QuickSearchLookup.php b/includes/QuickSearchLookup.php
index 0bca366..43481d8 100644
--- a/includes/QuickSearchLookup.php
+++ b/includes/QuickSearchLookup.php
@@ -17,7 +17,7 @@
         *
         * @param RequestContext $context
         */
-       public function __construct( RequestContext $context ) {
+       public function __construct( IContextSource $context ) {
                $this->context = $context;
        }
 
@@ -31,6 +31,14 @@
                        self::$instance = new self( RequestContext::getMain() );
                }
                return self::$instance;
+       }
+
+       /**
+        * Set a main instance.
+        * @param QuickSearchLookup|null $instance
+        */
+       public static function setInstance( $instance ) {
+               self::$instance = $instance;
        }
 
        /**
@@ -60,13 +68,16 @@
        public function setFirstResult( $titleTerm ) {
                if ( $titleTerm instanceof Title && $titleTerm->exists() ) {
                        $this->setTitle( $titleTerm );
-               } else {
+                       return true;
+               } elseif ( is_string( $titleTerm ) ) {
                        // check, if the term is the exact name of a title in 
this wiki
                        $title = Title::newFromText( $titleTerm );
                        if ( $title && $title->exists() ) {
                                $this->setTitle( $title );
+                               return true;
                        }
                }
+               return false;
        }
 
        /**
@@ -275,9 +286,11 @@
                        );
                        $api = new ApiMain( $params );
                        $api->execute();
-                       $data = $api->getResultData();
+                       $data = $api->getResult()->getResultData();
                        foreach ( $data['query']['pages'] as $id => $d ) {
-                               $this->metadata = $d;
+                               if ( isset( $d['pageid'] ) ) {
+                                       $this->metadata = $d;
+                               }
                        }
                }
                return $this->metadata;
diff --git a/tests/phpunit/QuickSearchLookupTest.php 
b/tests/phpunit/QuickSearchLookupTest.php
new file mode 100644
index 0000000..9fc7480
--- /dev/null
+++ b/tests/phpunit/QuickSearchLookupTest.php
@@ -0,0 +1,74 @@
+<?php
+
+/**
+ * @group Extensions
+ */
+class QuickSearchLooupTest extends MediaWikiTestCase {
+       protected function setUp() {
+               parent::setUp();
+               QuickSearchLookup::setInstance( new MockQuickLookupTest() );
+       }
+
+       protected function tearDown() {
+               QuickSearchLookup::setInstance( null );
+               parent::tearDown();
+       }
+
+       private function makeQSL( $url = '/' ) {
+               $params = wfParseUrl( wfExpandUrl( $url ) );
+               $q = array();
+               if ( isset( $params['query'] ) ) {
+                       $q = wfCgiToArray( $params['query'] );
+               }
+               $request = new FauxRequest( $q );
+               $request->setRequestURL( $url );
+               $context = new DerivativeContext( RequestContext::getMain() );
+               $context->setRequest( $request );
+               $context->setOutput( new OutputPage( $context ) );
+               return new QuickSearchLookup( $context );
+       }
+
+       /**
+        * @dataProvider getTitleResults
+        */
+       public function testSetFirstResult( $title, $result ) {
+               $qsl = $this->makeQSL();
+               $this->assertEquals( $qsl->setFirstResult( $title ), $result );
+       }
+
+       /**
+        * @dataProvider getTitleResults
+        */
+       public function testNeedsFirstResult( $title, $result ) {
+               $qsl = $this->makeQSL();
+               $qsl->setFirstResult( $title );
+               $this->assertEquals( $qsl->needsFirstResult(), !$result );
+       }
+
+       /**
+        * @dataProvider getTitleResults
+        */
+       public function testOutputLookup( $title, $result ) {
+               $qsl = $this->makeQSL();
+               $request = new RequestContext();
+               $out = $request->getOutput();
+               $qsl->setFirstResult( $title );
+               $qsl->outputLookup( $out );
+               $this->assertEquals( $result, (bool)$out->getHTML() );
+       }
+
+       public function getTitleResults() {
+               return array(
+                       array( 'BogusTest', false ),
+                       array( 'Main_Page', true ),
+                       array( Title::newFromText( 'BogusTest' ), false ),
+                       array( Title::newMainPage(), true ),
+               );
+       }
+}
+
+class MockQuickLookupTest {
+       public function __call( $name, $args ) {
+               throw new Exception( 'Functions shouldn\'t call the singleton 
itself.' );
+       }
+}
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/230474
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7aca81842e627af0bc03f9594956ca085df32be5
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/QuickSearchLookup
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <florian.schmidt.stargatewis...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to