2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Behat search-related step definitions.
20 * @package core_search
22 * @copyright 2017 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 // NOTE: no MOODLE_INTERNAL used, this file may be required by behat before including /config.php.
27 require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
29 use Behat\Gherkin\Node\TableNode as TableNode;
32 * Behat search-related step definitions.
34 * @package core_search
36 * @copyright 2017 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class behat_search extends behat_base {
41 * Create event when starting on the front page.
43 * @Given /^I search for "(?P<query>[^"]*)" using the header global search box$/
44 * @param string $query Query to search for
46 public function i_search_for_using_the_header_global_search_box($query) {
47 // Click the search icon.
48 $this->execute("behat_general::i_click_on", [get_string('togglesearch', 'core'), 'button']);
51 $this->execute('behat_forms::i_set_the_field_to', ['q', $query]);
54 $this->execute_script('return document.querySelector(".searchform-navbar").submit();');
58 * Sets results which will be returned for the next search. It will only return links to
59 * activities at present.
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
65 public function global_search_expects_the_query_and_will_return($query, TableNode $data) {
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.
79 foreach ($rowdata as $key => $value) {
80 if (!array_key_exists($key, $input)) {
81 throw new Exception('Field ' . $key . '" does not exist');
83 $input[$key] = $value;
85 foreach (['idnumber', 'type'] as $requiredfield) {
86 if (!$input[$requiredfield]) {
87 throw new Exception('Must specify required field: ' . $requiredfield);
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']);
97 // Find the specified activity.
98 $idnumber = $input['idnumber'];
99 $cmid = $DB->get_field('course_modules', 'id', ['idnumber' => $idnumber], IGNORE_MISSING);
101 throw new Exception('Cannot find activity with idnumber: ' . $idnumber);
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);
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'];
118 $out->fields->title = $cm->name;
120 if ($input['content']) {
121 $out->fields->content = $input['content'];
123 $out->fields->content = content_to_text($rec->intro, $rec->introformat);
125 if ($input['modified']) {
126 $out->fields->modified = strtotime($input['modified']);
128 $out->fields->modified = $cm->added;
130 $out->extrafields = new stdClass();
131 $out->extrafields->coursefullname = $course->fullname;
133 $outdata->results[] = $out;
136 set_config('behat_fakeresult', json_encode($outdata), 'core_search');
140 * Updates the global search index to take account of any added activities.
142 * @Given /^I update the global search index$/
143 * @throws moodle_exception
145 public function i_update_the_global_search_index() {
146 \core_search\manager::instance()->index(false);