Go search to consider fragment only title invalid
authorErik Bernhardson <ebernhardson@wikimedia.org>
Mon, 10 Sep 2018 20:29:08 +0000 (13:29 -0700)
committerErik Bernhardson <ebernhardson@wikimedia.org>
Tue, 11 Sep 2018 20:03:53 +0000 (13:03 -0700)
When submiting using the 'go' feature of Special:Search (standard
usecase for autocomplete) a nearby title can be selected.
Unfortunately a fragment only title is considered valid, which
sends the user to Main_Page#searchterm.

Instead don't allow a search starting with # to trigger the go
feature.

Bug: T182452
Change-Id: I8ce3ce8633e23db41ae6eafb9996ca7294f8445a

includes/search/SearchNearMatcher.php
tests/phpunit/includes/search/SearchNearMatcherTest.php [new file with mode: 0644]

index 27046f3..0a64493 100644 (file)
@@ -53,7 +53,6 @@ class SearchNearMatcher {
         */
        protected function getNearMatchInternal( $searchterm ) {
                $lang = $this->language;
-
                $allSearchTerms = [ $searchterm ];
 
                if ( $lang->hasVariants() ) {
@@ -68,6 +67,13 @@ class SearchNearMatcher {
                        return $titleResult;
                }
 
+               // Most of our handling here deals with finding a valid title for the search term,
+               // but almost anything starting with '#' is "valid" and points to Main_Page#searchterm.
+               // Rather than doing something completely wrong, do nothing.
+               if ( $searchterm === '' || $searchterm[0] === '#' ) {
+                       return null;
+               }
+
                foreach ( $allSearchTerms as $term ) {
                        # Exact match? No need to look further.
                        $title = Title::newFromText( $term );
diff --git a/tests/phpunit/includes/search/SearchNearMatcherTest.php b/tests/phpunit/includes/search/SearchNearMatcherTest.php
new file mode 100644 (file)
index 0000000..132011a
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+/**
+ * @covers SearchNearMatcher
+ */
+class SearchNearMatcherTest extends \PHPUnit\Framework\TestCase {
+       public function nearMatchProvider() {
+               return [
+                       'empty request returns nothing' => [ null, 'en', '' ],
+                       'default behaviour' => [ 'Near Match Test', 'en', 'near match test' ],
+                       'with a hash returns nothing' => [ null, 'en', '#near match test' ],
+               ];
+       }
+
+       /**
+        * @dataProvider nearMatchProvider
+        */
+       public function testNearMatch( $expected, $langCode, $searchterm ) {
+               $linkCache = MediaWiki\MediaWikiServices::getInstance()->getLinkCache();
+               $linkCache->addGoodLinkObj( 42, Title::newFromText( 'Near Match Test' ) );
+               $config = new HashConfig( [
+                       'EnableSearchContributorsByIP' => false,
+               ] );
+               $lang = Language::factory( $langCode );
+               $matcher = new SearchNearMatcher( $config, $lang );
+
+               $title = $matcher->getNearMatch( $searchterm );
+               $this->assertEquals( $expected, $title === null ? null : (string)$title );
+       }
+}