Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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.
27
require_once(__DIR__ . '/../../../lib/behat/behat_base.php');
28
 
29
use Behat\Gherkin\Node\TableNode as TableNode;
30
use Moodle\BehatExtension\Exception\SkippedException;
31
 
32
/**
33
 * Behat search-related step definitions.
34
 *
35
 * @package core_search
36
 * @category test
37
 * @copyright 2017 The Open University
38
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
40
class behat_search extends behat_base {
41
    /**
42
     * Create event when starting on the front page.
43
     *
44
     * @Given /^I search for "(?P<query>[^"]*)" using the header global search box$/
45
     * @param string $query Query to search for
46
     */
47
    public function i_search_for_using_the_header_global_search_box($query) {
48
        // Click the search icon.
49
        $this->execute("behat_general::i_click_on", [get_string('togglesearch', 'core'), 'button']);
50
 
51
        // Set the field.
52
        $this->execute('behat_forms::i_set_the_field_to', ['q', $query]);
53
 
54
        // Submit the form.
55
        $this->execute("behat_general::i_click_on_in_the",
1441 ariadna 56
            [get_string('performsearch', 'search'), 'button', '#usernavigation', 'css_element']
57
        );
1 efrain 58
    }
59
 
60
    /**
61
     * Sets results which will be returned for the next search. It will only return links to
62
     * activities at present.
63
     *
64
     * @Given /^global search expects the query "(?P<query>[^"]*)" and will return:$/
65
     * @param string $query Expected query value (just used to check the query passed to the engine)
66
     * @param TableNode $data Data rows
67
     */
68
    public function global_search_expects_the_query_and_will_return($query, TableNode $data) {
69
        global $DB;
70
        $outdata = new stdClass();
71
        $outdata->query = $query;
72
        $outdata->results = [];
73
        foreach ($data->getHash() as $rowdata) {
74
            // Check and get the data from the user-entered row.
75
            $input = [
76
                'type' => '',
77
                'idnumber' => '',
78
                'title' => '',
79
                'content' => '',
80
                'modified' => ''
81
            ];
82
            foreach ($rowdata as $key => $value) {
83
                if (!array_key_exists($key, $input)) {
84
                    throw new Exception('Field ' . $key . '" does not exist');
85
                }
86
                $input[$key] = $value;
87
            }
88
            foreach (['idnumber', 'type'] as $requiredfield) {
89
                if (!$input[$requiredfield]) {
90
                    throw new Exception('Must specify required field: ' . $requiredfield);
91
                }
92
            }
93
 
94
            // Check type (we only support activity at present, this could be extended to allow
95
            // faking other types of search results such as a user, course, or forum post).
96
            if ($input['type'] !== 'activity') {
97
                throw new Exception('Unsupported type: ' . $input['type']);
98
            }
99
 
100
            // Find the specified activity.
101
            $idnumber = $input['idnumber'];
102
            $cmid = $DB->get_field('course_modules', 'id', ['idnumber' => $idnumber], IGNORE_MISSING);
103
            if (!$cmid) {
104
                throw new Exception('Cannot find activity with idnumber: ' . $idnumber);
105
            }
106
            list ($course, $cm) = get_course_and_cm_from_cmid($cmid);
107
            $rec = $DB->get_record($cm->modname, ['id' => $cm->instance], '*', MUST_EXIST);
108
            $context = \context_module::instance($cm->id);
109
 
110
            // Set up the internal fields used in creating the search document.
111
            $out = new stdClass();
112
            $out->itemid = $cm->instance;
113
            $out->componentname = 'mod_' . $cm->modname;
114
            $out->areaname = 'activity';
115
            $out->fields = new stdClass();
116
            $out->fields->contextid = $context->id;
117
            $out->fields->courseid = $course->id;
118
            if ($input['title']) {
119
                $out->fields->title = $input['title'];
120
            } else {
121
                $out->fields->title = $cm->name;
122
            }
123
            if ($input['content']) {
124
                $out->fields->content = $input['content'];
125
            } else {
126
                $out->fields->content = content_to_text($rec->intro, $rec->introformat);
127
            }
128
            if ($input['modified']) {
129
                $out->fields->modified = strtotime($input['modified']);
130
            } else {
131
                $out->fields->modified = $cm->added;
132
            }
133
            $out->extrafields = new stdClass();
134
            $out->extrafields->coursefullname = $course->fullname;
135
 
136
            $outdata->results[] = $out;
137
        }
138
 
139
        set_config('behat_fakeresult', json_encode($outdata), 'core_search');
140
    }
141
 
142
    /**
143
     * Updates the global search index to take account of any added activities.
144
     *
145
     * @Given /^I update the global search index$/
146
     * @throws moodle_exception
147
     */
148
    public function i_update_the_global_search_index() {
149
        \core_search\manager::instance()->index(false);
150
    }
151
 
152
    /**
153
     * This step looks to see if Solr is installed or skip the rest of the scenario otherwise
154
     *
155
     * @Given /^solr is installed/
156
     */
157
    public function solr_is_installed() {
158
        if (!function_exists('solr_get_version')) {
159
            throw new SkippedException('Skipping this scenario because Solr is not installed.');
160
        }
161
    }
162
}