MDL-69454 core_search: consistent navbar search
[moodle.git] / search / tests / behat / behat_search.php
CommitLineData
e36eefae 1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Behat search-related step definitions.
19 *
20 * @package core_search
21 * @category test
22 * @copyright 2017 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26// NOTE: no MOODLE_INTERNAL used, this file may be required by behat before including /config.php.
27require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
28
29use Behat\Gherkin\Node\TableNode as TableNode;
30
31/**
32 * Behat search-related step definitions.
33 *
34 * @package core_search
35 * @category test
36 * @copyright 2017 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class behat_search extends behat_base {
40 /**
41 * Create event when starting on the front page.
42 *
43 * @Given /^I search for "(?P<query>[^"]*)" using the header global search box$/
44 * @param string $query Query to search for
45 */
46 public function i_search_for_using_the_header_global_search_box($query) {
1fdb4fda
BB
47 // Click the search icon.
48 $this->execute("behat_general::i_click_on", [get_string('togglesearch', 'core'), 'button']);
e36eefae 49
50 // Set the field.
51 $this->execute('behat_forms::i_set_the_field_to', ['q', $query]);
52
53 // Submit the form.
1fdb4fda 54 $this->execute_script('return document.querySelector(".searchform-navbar").submit();');
e36eefae 55 }
56
57 /**
58 * Sets results which will be returned for the next search. It will only return links to
59 * activities at present.
60 *
61 * @Given /^global search expects the query "(?P<query>[^"]*)" and will return:$/
62 * @param string $query Expected query value (just used to check the query passed to the engine)
63 * @param TableNode $data Data rows
64 */
65 public function global_search_expects_the_query_and_will_return($query, TableNode $data) {
66 global $DB;
67 $outdata = new stdClass();
68 $outdata->query = $query;
69 $outdata->results = [];
70 foreach ($data->getHash() as $rowdata) {
71 // Check and get the data from the user-entered row.
72 $input = [
73 'type' => '',
74 'idnumber' => '',
75 'title' => '',
76 'content' => '',
77 'modified' => ''
78 ];
79 foreach ($rowdata as $key => $value) {
80 if (!array_key_exists($key, $input)) {
81 throw new Exception('Field ' . $key . '" does not exist');
82 }
83 $input[$key] = $value;
84 }
85 foreach (['idnumber', 'type'] as $requiredfield) {
86 if (!$input[$requiredfield]) {
87 throw new Exception('Must specify required field: ' . $requiredfield);
88 }
89 }
90
91 // Check type (we only support activity at present, this could be extended to allow
92 // faking other types of search results such as a user, course, or forum post).
93 if ($input['type'] !== 'activity') {
94 throw new Exception('Unsupported type: ' . $input['type']);
95 }
96
97 // Find the specified activity.
98 $idnumber = $input['idnumber'];
3112c220 99 $cmid = $DB->get_field('course_modules', 'id', ['idnumber' => $idnumber], IGNORE_MISSING);
e36eefae 100 if (!$cmid) {
101 throw new Exception('Cannot find activity with idnumber: ' . $idnumber);
102 }
103 list ($course, $cm) = get_course_and_cm_from_cmid($cmid);
104 $rec = $DB->get_record($cm->modname, ['id' => $cm->instance], '*', MUST_EXIST);
105 $context = \context_module::instance($cm->id);
106
107 // Set up the internal fields used in creating the search document.
108 $out = new stdClass();
109 $out->itemid = $cm->instance;
110 $out->componentname = 'mod_' . $cm->modname;
111 $out->areaname = 'activity';
112 $out->fields = new stdClass();
113 $out->fields->contextid = $context->id;
114 $out->fields->courseid = $course->id;
115 if ($input['title']) {
116 $out->fields->title = $input['title'];
117 } else {
118 $out->fields->title = $cm->name;
119 }
120 if ($input['content']) {
121 $out->fields->content = $input['content'];
122 } else {
123 $out->fields->content = content_to_text($rec->intro, $rec->introformat);
124 }
125 if ($input['modified']) {
126 $out->fields->modified = strtotime($input['modified']);
127 } else {
128 $out->fields->modified = $cm->added;
129 }
130 $out->extrafields = new stdClass();
131 $out->extrafields->coursefullname = $course->fullname;
132
133 $outdata->results[] = $out;
134 }
135
136 set_config('behat_fakeresult', json_encode($outdata), 'core_search');
137 }
ed73ff2b 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 }
e36eefae 148}