Proyectos de Subversion Moodle

Rev

| 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",
56
            [get_string('search', 'core'), 'button', '#usernavigation', 'css_element']);
57
    }
58
 
59
    /**
60
     * Sets results which will be returned for the next search. It will only return links to
61
     * activities at present.
62
     *
63
     * @Given /^global search expects the query "(?P<query>[^"]*)" and will return:$/
64
     * @param string $query Expected query value (just used to check the query passed to the engine)
65
     * @param TableNode $data Data rows
66
     */
67
    public function global_search_expects_the_query_and_will_return($query, TableNode $data) {
68
        global $DB;
69
        $outdata = new stdClass();
70
        $outdata->query = $query;
71
        $outdata->results = [];
72
        foreach ($data->getHash() as $rowdata) {
73
            // Check and get the data from the user-entered row.
74
            $input = [
75
                'type' => '',
76
                'idnumber' => '',
77
                'title' => '',
78
                'content' => '',
79
                'modified' => ''
80
            ];
81
            foreach ($rowdata as $key => $value) {
82
                if (!array_key_exists($key, $input)) {
83
                    throw new Exception('Field ' . $key . '" does not exist');
84
                }
85
                $input[$key] = $value;
86
            }
87
            foreach (['idnumber', 'type'] as $requiredfield) {
88
                if (!$input[$requiredfield]) {
89
                    throw new Exception('Must specify required field: ' . $requiredfield);
90
                }
91
            }
92
 
93
            // Check type (we only support activity at present, this could be extended to allow
94
            // faking other types of search results such as a user, course, or forum post).
95
            if ($input['type'] !== 'activity') {
96
                throw new Exception('Unsupported type: ' . $input['type']);
97
            }
98
 
99
            // Find the specified activity.
100
            $idnumber = $input['idnumber'];
101
            $cmid = $DB->get_field('course_modules', 'id', ['idnumber' => $idnumber], IGNORE_MISSING);
102
            if (!$cmid) {
103
                throw new Exception('Cannot find activity with idnumber: ' . $idnumber);
104
            }
105
            list ($course, $cm) = get_course_and_cm_from_cmid($cmid);
106
            $rec = $DB->get_record($cm->modname, ['id' => $cm->instance], '*', MUST_EXIST);
107
            $context = \context_module::instance($cm->id);
108
 
109
            // Set up the internal fields used in creating the search document.
110
            $out = new stdClass();
111
            $out->itemid = $cm->instance;
112
            $out->componentname = 'mod_' . $cm->modname;
113
            $out->areaname = 'activity';
114
            $out->fields = new stdClass();
115
            $out->fields->contextid = $context->id;
116
            $out->fields->courseid = $course->id;
117
            if ($input['title']) {
118
                $out->fields->title = $input['title'];
119
            } else {
120
                $out->fields->title = $cm->name;
121
            }
122
            if ($input['content']) {
123
                $out->fields->content = $input['content'];
124
            } else {
125
                $out->fields->content = content_to_text($rec->intro, $rec->introformat);
126
            }
127
            if ($input['modified']) {
128
                $out->fields->modified = strtotime($input['modified']);
129
            } else {
130
                $out->fields->modified = $cm->added;
131
            }
132
            $out->extrafields = new stdClass();
133
            $out->extrafields->coursefullname = $course->fullname;
134
 
135
            $outdata->results[] = $out;
136
        }
137
 
138
        set_config('behat_fakeresult', json_encode($outdata), 'core_search');
139
    }
140
 
141
    /**
142
     * Updates the global search index to take account of any added activities.
143
     *
144
     * @Given /^I update the global search index$/
145
     * @throws moodle_exception
146
     */
147
    public function i_update_the_global_search_index() {
148
        \core_search\manager::instance()->index(false);
149
    }
150
 
151
    /**
152
     * This step looks to see if Solr is installed or skip the rest of the scenario otherwise
153
     *
154
     * @Given /^solr is installed/
155
     */
156
    public function solr_is_installed() {
157
        if (!function_exists('solr_get_version')) {
158
            throw new SkippedException('Skipping this scenario because Solr is not installed.');
159
        }
160
    }
161
}