From ed73ff2b0e3d5a6a45eeece43e14b25bfa8efa8d Mon Sep 17 00:00:00 2001 From: sam marshall Date: Fri, 12 Jul 2019 11:50:51 +0100 Subject: [PATCH] MDL-66132 Search: Behat tests should use simpledb, fix mock count When searching using mock results (the 'global search expects the query' step), the result count is not correctly set. As a result, the page incorrectly reports that there are no results and doesn't correctly show the first page of multi-page results. Additionally, some of the core Behat tests can now be moved to use real searching with the simpledb engine, rather than using mock results at all. This gives better tests. Unfortunately it was not possible to move all of the core Behat tests and deprecate the mock step, because some of the tests are related to the UI for 'special' features searching by user or group, neither of which are supported by the simpledb engine. --- search/classes/manager.php | 15 +++++++++++ search/tests/behat/behat_search.php | 10 +++++++ search/tests/behat/search_query.feature | 36 ++++++++----------------- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/search/classes/manager.php b/search/classes/manager.php index fea54021d25..a68c6e93d73 100644 --- a/search/classes/manager.php +++ b/search/classes/manager.php @@ -154,6 +154,11 @@ class manager { */ protected static $phpunitfaketime = 0; + /** + * @var int Result count when used with mock results for Behat tests. + */ + protected $behatresultcount = 0; + /** * Constructor, use \core_search\manager::instance instead to get a class instance. * @@ -887,6 +892,10 @@ class manager { } else { // Get the possible count reported by engine, and limit to our max. $out->totalcount = $this->engine->get_query_total_count(); + if (defined('BEHAT_SITE_RUNNING') && $this->behatresultcount) { + // Override results when using Behat mock results. + $out->totalcount = $this->behatresultcount; + } $out->totalcount = min($out->totalcount, static::MAX_RESULTS); } @@ -955,6 +964,12 @@ class manager { $docs[] = $doc; } + // Store the mock count, and apply the limit to the returned results. + $this->behatresultcount = count($docs); + if ($this->behatresultcount > $limit) { + $docs = array_slice($docs, 0, $limit); + } + return $docs; } } diff --git a/search/tests/behat/behat_search.php b/search/tests/behat/behat_search.php index 206819de969..c7ca9478f31 100644 --- a/search/tests/behat/behat_search.php +++ b/search/tests/behat/behat_search.php @@ -135,4 +135,14 @@ class behat_search extends behat_base { set_config('behat_fakeresult', json_encode($outdata), 'core_search'); } + + /** + * Updates the global search index to take account of any added activities. + * + * @Given /^I update the global search index$/ + * @throws moodle_exception + */ + public function i_update_the_global_search_index() { + \core_search\manager::instance()->index(false); + } } diff --git a/search/tests/behat/search_query.feature b/search/tests/behat/search_query.feature index be0938449fe..1d7c87af489 100644 --- a/search/tests/behat/search_query.feature +++ b/search/tests/behat/search_query.feature @@ -6,22 +6,20 @@ Feature: Use global search interface Background: Given the following config values are set as admin: - | enableglobalsearch | 1 | - | searchengine | solr | + | enableglobalsearch | 1 | + | searchengine | simpledb | And the following "courses" exist: | shortname | fullname | | F1 | Amphibians | And the following "activities" exist: - | activity | name | intro | course | idnumber | - | page | PageName1 | PageDesc1 | F1 | PAGE1 | - | forum | ForumName1 | ForumDesc1 | F1 | FORUM1 | + | activity | name | intro | course | idnumber | + | page | PageName1 frogs amphibians | PageDesc1 | F1 | PAGE1 | + | forum | ForumName1 toads amphibians | ForumDesc1 | F1 | FORUM1 | + And I update the global search index And I log in as "admin" @javascript Scenario: Search from header search box with one result - Given global search expects the query "frogs" and will return: - | type | idnumber | - | activity | PAGE1 | When I search for "frogs" using the header global search box Then I should see "PageName1" And I should see "PageDesc1" @@ -32,15 +30,9 @@ Feature: Use global search interface @javascript Scenario: Search from search page with two results - Given global search expects the query "zombies" and will return: - | nothing | When I search for "zombies" using the header global search box Then I should see "No results" - And I set the field "id_q" to "Toads" - And global search expects the query "Toads" and will return: - | type | idnumber | - | activity | FORUM1 | - | activity | PAGE1 | + And I set the field "id_q" to "amphibians" # You cannot press "Search" because there's a fieldset with the same name that gets in the way. And I press "id_submitbutton" And I should see "ForumName1" @@ -54,9 +46,6 @@ Feature: Use global search interface @javascript Scenario: Search starting from site context (no within option) - Given global search expects the query "frogs" and will return: - | type | idnumber | - | activity | PAGE1 | When I search for "frogs" using the header global search box And I expand all fieldsets Then I should not see "Search within" @@ -64,9 +53,6 @@ Feature: Use global search interface @javascript Scenario: Search starting from course context (within option lists course) - Given global search expects the query "frogs" and will return: - | type | idnumber | - | activity | PAGE1 | When I am on "Amphibians" course homepage And I search for "frogs" using the header global search box And I expand all fieldsets @@ -78,9 +64,6 @@ Feature: Use global search interface @javascript Scenario: Search starting from forum context (within option lists course and forum) - Given global search expects the query "frogs" and will return: - | type | idnumber | - | activity | PAGE1 | When I am on "Amphibians" course homepage And I follow "ForumName1" And I search for "frogs" using the header global search box @@ -95,7 +78,10 @@ Feature: Use global search interface @javascript Scenario: Check that groups option in search form appears when intended - Given the following "groups" exist: + # Switch to mocked Solr search because simpledb doesn't support groups. + Given the following config values are set as admin: + | searchengine | solr | + And the following "groups" exist: | name | course | idnumber | | A Group | F1 | G1 | | B Group | F1 | G2 |