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
namespace core_search;
18
 
19
use stdClass;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once(__DIR__ . '/fixtures/testable_core_search.php');
25
require_once(__DIR__ . '/fixtures/mock_search_area.php');
26
 
27
/**
28
 * Test for top results
29
 *
30
 * @package core_search
31
 * @author  Nathan Nguyen <nathannguyen@catalyst-au.net>
32
 * @copyright  Catalyst IT
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class top_result_test extends \advanced_testcase {
36
 
37
    /** @var stdClass course 1 */
38
    protected $course1;
39
    /** @var stdClass course 2 */
40
    protected $course2;
41
    /** @var stdClass user 1 */
42
    protected $user1;
43
    /** @var stdClass user 2 */
44
    protected $user2;
45
    /** @var stdClass user 3 */
46
    protected $user3;
47
    /** @var stdClass search engine */
48
    protected $search;
49
 
50
    /**
51
     * Prepare test and users.
52
     */
53
    private function prepare_test_courses_and_users(): void {
54
        global $DB;
55
 
56
        $this->setAdminUser();
57
 
58
        // Search engine.
59
        $this->search = \testable_core_search::instance(new \search_simpledb\engine());
60
 
61
        // Set default configurations.
62
        set_config('searchallavailablecourses', 1);
63
        set_config('searchincludeallcourses', 1);
64
        set_config('searchenablecategories', true);
65
        set_config('enableglobalsearch', true);
66
        set_config('searchmaxtopresults', 3);
67
        $teacher = $DB->get_record('role', ['shortname' => 'teacher']);
68
        $editingteacher = $DB->get_record('role', ['shortname' => 'editingteacher']);
69
        set_config('searchteacherroles', "$teacher->id, $editingteacher->id");
70
 
71
        // Generate test data.
72
        $generator = $this->getDataGenerator();
73
 
74
        // Courses.
75
        $this->course1 = $generator->create_course(['fullname' => 'Top course result 1']);
76
        // Ensure course 1 is indexed before course 2.
77
        $this->run_index();
78
        $this->course2 = $generator->create_course(['fullname' => 'Top course result 2']);
79
 
80
        // User 1.
81
        $urecord1 = new \stdClass();
82
        $urecord1->firstname = "User 1";
83
        $urecord1->lastname = "Test";
84
        $this->user1 = $generator->create_user($urecord1);
85
 
86
        // User 2.
87
        $urecord2 = new \stdClass();
88
        $urecord2->firstname = "User 2";
89
        $urecord2->lastname = "Test";
90
        $this->user2 = $generator->create_user($urecord2);
91
 
92
        // User 3.
93
        $urecord3 = new \stdClass();
94
        $urecord3->firstname = "User 3";
95
        $urecord3->lastname = "Test";
96
        $this->user3 = $generator->create_user($urecord3);
97
    }
98
 
99
    /**
100
     * Test course ranking
101
     */
102
    public function test_search_course_rank(): void {
103
        $this->resetAfterTest();
104
        $this->prepare_test_courses_and_users();
105
        $this->setUser($this->user1);
106
 
107
        // Search query.
108
        $data = new \stdClass();
109
        $data->q = 'Top course result';
110
        $data->cat = 'core-all';
111
 
112
        // Course 1 at the first index.
113
        $this->run_index();
114
        $docs = $this->search->search_top($data);
115
        $this->assertEquals('Top course result 1', $docs[0]->get('title'));
116
        $this->assertEquals('Top course result 2', $docs[1]->get('title'));
117
 
118
        // Enrol user to course 2.
119
        $this->getDataGenerator()->enrol_user($this->user1->id, $this->course2->id, 'student');
120
 
121
        // Course 2 at the first index.
122
        $this->run_index();
123
        $docs = $this->search->search_top($data);
124
        $this->assertEquals('Top course result 2', $docs[0]->get('title'));
125
        $this->assertEquals('Top course result 1', $docs[1]->get('title'));
126
    }
127
 
128
    /**
129
     * Test without teacher indexing
130
     */
131
    public function test_search_with_no_course_teacher_indexing(): void {
132
        $this->resetAfterTest();
133
        $this->prepare_test_courses_and_users();
134
        set_config('searchteacherroles', "");
135
        $this->getDataGenerator()->enrol_user($this->user1->id, $this->course1->id, 'teacher');
136
 
137
        // Search query.
138
        $data = new \stdClass();
139
        $data->q = 'Top course result';
140
        $data->cat = 'core-all';
141
 
142
        // Only return the course.
143
        $this->run_index();
144
        $docs = $this->search->search_top($data);
145
        $this->assertCount(2, $docs);
146
        $this->assertEquals('Top course result 1', $docs[0]->get('title'));
147
        $this->assertEquals('Top course result 2', $docs[1]->get('title'));
148
    }
149
 
150
    /**
151
     * Test with teacher indexing
152
     */
153
    public function test_search_with_course_teacher_indexing(): void {
154
        $this->resetAfterTest();
155
        $this->prepare_test_courses_and_users();
156
 
157
        $this->getDataGenerator()->enrol_user($this->user1->id, $this->course1->id, 'teacher');
158
        $this->getDataGenerator()->enrol_user($this->user2->id, $this->course1->id, 'student');
159
 
160
        // Search query.
161
        $data = new \stdClass();
162
        $data->q = 'Top course result 1';
163
        $data->cat = 'core-all';
164
 
165
        // Return the course and the teachers.
166
        $this->run_index();
167
        $docs = $this->search->search_top($data);
168
        $this->assertEquals('Top course result 1', $docs[0]->get('title'));
169
        $this->assertEquals('User 1 Test', $docs[1]->get('title'));
170
    }
171
 
172
    /**
173
     * Test with teacher indexing
174
     */
175
    public function test_search_with_course_teacher_content_indexing(): void {
176
        $this->resetAfterTest();
177
        $this->prepare_test_courses_and_users();
178
 
179
        // Create forums as course content.
180
        $generator = $this->getDataGenerator();
181
 
182
        // Course Teacher.
183
        $this->getDataGenerator()->enrol_user($this->user1->id, $this->course1->id, 'teacher');
184
 
185
        // Forums.
186
        $generator->create_module('forum',
187
            ['course' => $this->course1->id, 'name' => 'Forum 1,  does not contain the keyword']);
188
        $generator->create_module('forum',
189
            ['course' => $this->course2->id, 'name' => 'Forum 2, contains keyword Top course result 1']);
190
 
191
        $this->run_index();
192
 
193
        // Search query.
194
        $data = new \stdClass();
195
        $data->q = 'Top course result 1';
196
        $data->cat = 'core-all';
197
 
198
        // Return the course and the teacher and the forum.
199
        $docs = $this->search->search_top($data);
200
        $this->assertEquals('Top course result 1', $docs[0]->get('title'));
201
        $this->assertEquals('User 1 Test', $docs[1]->get('title'));
202
        $this->assertEquals('Forum 2, contains keyword Top course result 1', $docs[2]->get('title'));
203
    }
204
 
205
    /**
206
     * Execute indexing
207
     */
208
    private function run_index(): void {
209
        // Indexing.
210
        $this->waitForSecond();
211
        $this->search->index(false, 0);
212
    }
213
}