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.
This commit is contained in:
sam marshall 2019-07-12 11:50:51 +01:00
parent f7e108438f
commit ed73ff2b0e
3 changed files with 36 additions and 25 deletions

View file

@ -154,6 +154,11 @@ class manager {
*/ */
protected static $phpunitfaketime = 0; 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. * Constructor, use \core_search\manager::instance instead to get a class instance.
* *
@ -887,6 +892,10 @@ class manager {
} else { } else {
// Get the possible count reported by engine, and limit to our max. // Get the possible count reported by engine, and limit to our max.
$out->totalcount = $this->engine->get_query_total_count(); $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); $out->totalcount = min($out->totalcount, static::MAX_RESULTS);
} }
@ -955,6 +964,12 @@ class manager {
$docs[] = $doc; $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; return $docs;
} }
} }

View file

@ -135,4 +135,14 @@ class behat_search extends behat_base {
set_config('behat_fakeresult', json_encode($outdata), 'core_search'); 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);
}
} }

View file

@ -7,21 +7,19 @@ Feature: Use global search interface
Background: Background:
Given the following config values are set as admin: Given the following config values are set as admin:
| enableglobalsearch | 1 | | enableglobalsearch | 1 |
| searchengine | solr | | searchengine | simpledb |
And the following "courses" exist: And the following "courses" exist:
| shortname | fullname | | shortname | fullname |
| F1 | Amphibians | | F1 | Amphibians |
And the following "activities" exist: And the following "activities" exist:
| activity | name | intro | course | idnumber | | activity | name | intro | course | idnumber |
| page | PageName1 | PageDesc1 | F1 | PAGE1 | | page | PageName1 frogs amphibians | PageDesc1 | F1 | PAGE1 |
| forum | ForumName1 | ForumDesc1 | F1 | FORUM1 | | forum | ForumName1 toads amphibians | ForumDesc1 | F1 | FORUM1 |
And I update the global search index
And I log in as "admin" And I log in as "admin"
@javascript @javascript
Scenario: Search from header search box with one result 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 When I search for "frogs" using the header global search box
Then I should see "PageName1" Then I should see "PageName1"
And I should see "PageDesc1" And I should see "PageDesc1"
@ -32,15 +30,9 @@ Feature: Use global search interface
@javascript @javascript
Scenario: Search from search page with two results 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 When I search for "zombies" using the header global search box
Then I should see "No results" Then I should see "No results"
And I set the field "id_q" to "Toads" And I set the field "id_q" to "amphibians"
And global search expects the query "Toads" and will return:
| type | idnumber |
| activity | FORUM1 |
| activity | PAGE1 |
# You cannot press "Search" because there's a fieldset with the same name that gets in the way. # 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 press "id_submitbutton"
And I should see "ForumName1" And I should see "ForumName1"
@ -54,9 +46,6 @@ Feature: Use global search interface
@javascript @javascript
Scenario: Search starting from site context (no within option) 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 When I search for "frogs" using the header global search box
And I expand all fieldsets And I expand all fieldsets
Then I should not see "Search within" Then I should not see "Search within"
@ -64,9 +53,6 @@ Feature: Use global search interface
@javascript @javascript
Scenario: Search starting from course context (within option lists course) 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 When I am on "Amphibians" course homepage
And I search for "frogs" using the header global search box And I search for "frogs" using the header global search box
And I expand all fieldsets And I expand all fieldsets
@ -78,9 +64,6 @@ Feature: Use global search interface
@javascript @javascript
Scenario: Search starting from forum context (within option lists course and forum) 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 When I am on "Amphibians" course homepage
And I follow "ForumName1" And I follow "ForumName1"
And I search for "frogs" using the header global search box And I search for "frogs" using the header global search box
@ -95,7 +78,10 @@ Feature: Use global search interface
@javascript @javascript
Scenario: Check that groups option in search form appears when intended 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 | | name | course | idnumber |
| A Group | F1 | G1 | | A Group | F1 | G1 |
| B Group | F1 | G2 | | B Group | F1 | G2 |