Merge branch 'MDL-79693' of https://github.com/paulholden/moodle
[moodle.git] / search / tests / top_result_test.php
blob7a69a8f846d57015d0d2257eb9d5fdf3625b1ad6
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.
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/>.
17 namespace core_search;
19 use stdClass;
21 defined('MOODLE_INTERNAL') || die();
23 global $CFG;
24 require_once(__DIR__ . '/fixtures/testable_core_search.php');
25 require_once(__DIR__ . '/fixtures/mock_search_area.php');
27 /**
28 * Test for top results
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
35 class top_result_test extends \advanced_testcase {
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;
50 /**
51 * Prepare test and users.
53 private function prepare_test_courses_and_users(): void {
54 global $DB;
56 $this->setAdminUser();
58 // Search engine.
59 $this->search = \testable_core_search::instance(new \search_simpledb\engine());
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");
71 // Generate test data.
72 $generator = $this->getDataGenerator();
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']);
80 // User 1.
81 $urecord1 = new \stdClass();
82 $urecord1->firstname = "User 1";
83 $urecord1->lastname = "Test";
84 $this->user1 = $generator->create_user($urecord1);
86 // User 2.
87 $urecord2 = new \stdClass();
88 $urecord2->firstname = "User 2";
89 $urecord2->lastname = "Test";
90 $this->user2 = $generator->create_user($urecord2);
92 // User 3.
93 $urecord3 = new \stdClass();
94 $urecord3->firstname = "User 3";
95 $urecord3->lastname = "Test";
96 $this->user3 = $generator->create_user($urecord3);
99 /**
100 * Test course ranking
102 public function test_search_course_rank(): void {
103 $this->resetAfterTest();
104 $this->prepare_test_courses_and_users();
105 $this->setUser($this->user1);
107 // Search query.
108 $data = new \stdClass();
109 $data->q = 'Top course result';
110 $data->cat = 'core-all';
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'));
118 // Enrol user to course 2.
119 $this->getDataGenerator()->enrol_user($this->user1->id, $this->course2->id, 'student');
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'));
129 * Test without teacher indexing
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');
137 // Search query.
138 $data = new \stdClass();
139 $data->q = 'Top course result';
140 $data->cat = 'core-all';
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'));
151 * Test with teacher indexing
153 public function test_search_with_course_teacher_indexing(): void {
154 $this->resetAfterTest();
155 $this->prepare_test_courses_and_users();
157 $this->getDataGenerator()->enrol_user($this->user1->id, $this->course1->id, 'teacher');
158 $this->getDataGenerator()->enrol_user($this->user2->id, $this->course1->id, 'student');
160 // Search query.
161 $data = new \stdClass();
162 $data->q = 'Top course result 1';
163 $data->cat = 'core-all';
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'));
173 * Test with teacher indexing
175 public function test_search_with_course_teacher_content_indexing(): void {
176 $this->resetAfterTest();
177 $this->prepare_test_courses_and_users();
179 // Create forums as course content.
180 $generator = $this->getDataGenerator();
182 // Course Teacher.
183 $this->getDataGenerator()->enrol_user($this->user1->id, $this->course1->id, 'teacher');
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']);
191 $this->run_index();
193 // Search query.
194 $data = new \stdClass();
195 $data->q = 'Top course result 1';
196 $data->cat = 'core-all';
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'));
206 * Execute indexing
208 private function run_index(): void {
209 // Indexing.
210 $this->waitForSecond();
211 $this->search->index(false, 0);