Skip to content

Commit aa01e0e

Browse files
committed
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.
1 parent 39ef515 commit aa01e0e

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

search/classes/manager.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ class manager {
154154
*/
155155
protected static $phpunitfaketime = 0;
156156

157+
/**
158+
* @var int Result count when used with mock results for Behat tests.
159+
*/
160+
protected $behatresultcount = 0;
161+
157162
/**
158163
* Constructor, use \core_search\manager::instance instead to get a class instance.
159164
*
@@ -887,6 +892,10 @@ public function paged_search(\stdClass $formdata, $pagenum) {
887892
} else {
888893
// Get the possible count reported by engine, and limit to our max.
889894
$out->totalcount = $this->engine->get_query_total_count();
895+
if (defined('BEHAT_SITE_RUNNING') && $this->behatresultcount) {
896+
// Override results when using Behat mock results.
897+
$out->totalcount = $this->behatresultcount;
898+
}
890899
$out->totalcount = min($out->totalcount, static::MAX_RESULTS);
891900
}
892901

@@ -955,6 +964,12 @@ public function search(\stdClass $formdata, $limit = 0) {
955964
$docs[] = $doc;
956965
}
957966

967+
// Store the mock count, and apply the limit to the returned results.
968+
$this->behatresultcount = count($docs);
969+
if ($this->behatresultcount > $limit) {
970+
$docs = array_slice($docs, 0, $limit);
971+
}
972+
958973
return $docs;
959974
}
960975
}

search/tests/behat/behat_search.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,14 @@ public function global_search_expects_the_query_and_will_return($query, TableNod
135135

136136
set_config('behat_fakeresult', json_encode($outdata), 'core_search');
137137
}
138+
139+
/**
140+
* Updates the global search index to take account of any added activities.
141+
*
142+
* @Given /^I update the global search index$/
143+
* @throws moodle_exception
144+
*/
145+
public function i_update_the_global_search_index() {
146+
\core_search\manager::instance()->index(false);
147+
}
138148
}

search/tests/behat/search_query.feature

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,20 @@ Feature: Use global search interface
66

77
Background:
88
Given the following config values are set as admin:
9-
| enableglobalsearch | 1 |
10-
| searchengine | solr |
9+
| enableglobalsearch | 1 |
10+
| searchengine | simpledb |
1111
And the following "courses" exist:
1212
| shortname | fullname |
1313
| F1 | Amphibians |
1414
And the following "activities" exist:
15-
| activity | name | intro | course | idnumber |
16-
| page | PageName1 | PageDesc1 | F1 | PAGE1 |
17-
| forum | ForumName1 | ForumDesc1 | F1 | FORUM1 |
15+
| activity | name | intro | course | idnumber |
16+
| page | PageName1 frogs amphibians | PageDesc1 | F1 | PAGE1 |
17+
| forum | ForumName1 toads amphibians | ForumDesc1 | F1 | FORUM1 |
18+
And I update the global search index
1819
And I log in as "admin"
1920

2021
@javascript
2122
Scenario: Search from header search box with one result
22-
Given global search expects the query "frogs" and will return:
23-
| type | idnumber |
24-
| activity | PAGE1 |
2523
When I search for "frogs" using the header global search box
2624
Then I should see "PageName1"
2725
And I should see "PageDesc1"
@@ -32,15 +30,9 @@ Feature: Use global search interface
3230

3331
@javascript
3432
Scenario: Search from search page with two results
35-
Given global search expects the query "zombies" and will return:
36-
| nothing |
3733
When I search for "zombies" using the header global search box
3834
Then I should see "No results"
39-
And I set the field "id_q" to "Toads"
40-
And global search expects the query "Toads" and will return:
41-
| type | idnumber |
42-
| activity | FORUM1 |
43-
| activity | PAGE1 |
35+
And I set the field "id_q" to "amphibians"
4436
# You cannot press "Search" because there's a fieldset with the same name that gets in the way.
4537
And I press "id_submitbutton"
4638
And I should see "ForumName1"
@@ -54,19 +46,13 @@ Feature: Use global search interface
5446

5547
@javascript
5648
Scenario: Search starting from site context (no within option)
57-
Given global search expects the query "frogs" and will return:
58-
| type | idnumber |
59-
| activity | PAGE1 |
6049
When I search for "frogs" using the header global search box
6150
And I expand all fieldsets
6251
Then I should not see "Search within"
6352
And I should see "Courses" in the "region-main" "region"
6453

6554
@javascript
6655
Scenario: Search starting from course context (within option lists course)
67-
Given global search expects the query "frogs" and will return:
68-
| type | idnumber |
69-
| activity | PAGE1 |
7056
When I am on "Amphibians" course homepage
7157
And I search for "frogs" using the header global search box
7258
And I expand all fieldsets
@@ -78,9 +64,6 @@ Feature: Use global search interface
7864

7965
@javascript
8066
Scenario: Search starting from forum context (within option lists course and forum)
81-
Given global search expects the query "frogs" and will return:
82-
| type | idnumber |
83-
| activity | PAGE1 |
8467
When I am on "Amphibians" course homepage
8568
And I follow "ForumName1"
8669
And I search for "frogs" using the header global search box
@@ -95,7 +78,10 @@ Feature: Use global search interface
9578

9679
@javascript
9780
Scenario: Check that groups option in search form appears when intended
98-
Given the following "groups" exist:
81+
# Switch to mocked Solr search because simpledb doesn't support groups.
82+
Given the following config values are set as admin:
83+
| searchengine | solr |
84+
And the following "groups" exist:
9985
| name | course | idnumber |
10086
| A Group | F1 | G1 |
10187
| B Group | F1 | G2 |

0 commit comments

Comments
 (0)