MDL-59913 Global search: Allow search of non-enrolled courses
[moodle.git] / search / tests / manager_test.php
CommitLineData
db48207e
DM
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 * Search manager unit tests.
19 *
20 * @package core_search
21 * @category phpunit
22 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28require_once(__DIR__ . '/fixtures/testable_core_search.php');
25ba053f 29require_once(__DIR__ . '/fixtures/mock_search_area.php');
db48207e
DM
30
31/**
32 * Unit tests for search manager.
33 *
34 * @package core_search
35 * @category phpunit
36 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class search_manager_testcase extends advanced_testcase {
40
41 protected $forumpostareaid = null;
724703d3 42 protected $mycoursesareaid = null;
db48207e
DM
43
44 public function setUp() {
45 $this->forumpostareaid = \core_search\manager::generate_areaid('mod_forum', 'post');
724703d3 46 $this->mycoursesareaid = \core_search\manager::generate_areaid('core_course', 'mycourse');
db48207e
DM
47 }
48
49 public function test_search_enabled() {
50
51 $this->resetAfterTest();
52
53 // Disabled by default.
54 $this->assertFalse(\core_search\manager::is_global_search_enabled());
55
56 set_config('enableglobalsearch', true);
57 $this->assertTrue(\core_search\manager::is_global_search_enabled());
58
59 set_config('enableglobalsearch', false);
60 $this->assertFalse(\core_search\manager::is_global_search_enabled());
61 }
62
63 public function test_search_areas() {
64 global $CFG;
65
66 $this->resetAfterTest();
67
68 set_config('enableglobalsearch', true);
69
70 $fakeareaid = \core_search\manager::generate_areaid('mod_unexisting', 'chihuaquita');
71
72 $searcharea = \core_search\manager::get_search_area($this->forumpostareaid);
0bd8383a 73 $this->assertInstanceOf('\core_search\base', $searcharea);
db48207e
DM
74
75 $this->assertFalse(\core_search\manager::get_search_area($fakeareaid));
76
77 $this->assertArrayHasKey($this->forumpostareaid, \core_search\manager::get_search_areas_list());
78 $this->assertArrayNotHasKey($fakeareaid, \core_search\manager::get_search_areas_list());
79
80 // Enabled by default once global search is enabled.
81 $this->assertArrayHasKey($this->forumpostareaid, \core_search\manager::get_search_areas_list(true));
82
83 list($componentname, $varname) = $searcharea->get_config_var_name();
6a4c2146 84 set_config($varname . '_enabled', 0, $componentname);
db48207e
DM
85 \core_search\manager::clear_static();
86
87 $this->assertArrayNotHasKey('mod_forum', \core_search\manager::get_search_areas_list(true));
88
6a4c2146 89 set_config($varname . '_enabled', 1, $componentname);
db48207e
DM
90
91 // Although the result is wrong, we want to check that \core_search\manager::get_search_areas_list returns cached results.
92 $this->assertArrayNotHasKey($this->forumpostareaid, \core_search\manager::get_search_areas_list(true));
93
94 // Now we check the real result.
95 \core_search\manager::clear_static();
96 $this->assertArrayHasKey($this->forumpostareaid, \core_search\manager::get_search_areas_list(true));
97 }
98
99 public function test_search_config() {
100
101 $this->resetAfterTest();
102
103 $search = testable_core_search::instance();
104
105 // We should test both plugin types and core subsystems. No core subsystems available yet.
106 $searcharea = $search->get_search_area($this->forumpostareaid);
107
108 list($componentname, $varname) = $searcharea->get_config_var_name();
109
110 // Just with a couple of vars should be enough.
111 $start = time() - 100;
112 $end = time();
113 set_config($varname . '_indexingstart', $start, $componentname);
114 set_config($varname . '_indexingend', $end, $componentname);
115
116 $configs = $search->get_areas_config(array($this->forumpostareaid => $searcharea));
117 $this->assertEquals($start, $configs[$this->forumpostareaid]->indexingstart);
118 $this->assertEquals($end, $configs[$this->forumpostareaid]->indexingend);
67d64795 119 $this->assertEquals(false, $configs[$this->forumpostareaid]->partial);
db48207e
DM
120
121 try {
122 $fakeareaid = \core_search\manager::generate_areaid('mod_unexisting', 'chihuaquita');
123 $search->reset_config($fakeareaid);
124 $this->fail('An exception should be triggered if the provided search area does not exist.');
125 } catch (moodle_exception $ex) {
126 $this->assertContains($fakeareaid . ' search area is not available.', $ex->getMessage());
127 }
128
69d66020 129 // We clean it all but enabled components.
db48207e 130 $search->reset_config($this->forumpostareaid);
6a4c2146
DM
131 $config = $searcharea->get_config();
132 $this->assertEquals(1, $config[$varname . '_enabled']);
133 $this->assertEquals(0, $config[$varname . '_indexingstart']);
134 $this->assertEquals(0, $config[$varname . '_indexingend']);
135 $this->assertEquals(0, $config[$varname . '_lastindexrun']);
67d64795 136 $this->assertEquals(0, $config[$varname . '_partial']);
db48207e
DM
137 // No caching.
138 $configs = $search->get_areas_config(array($this->forumpostareaid => $searcharea));
139 $this->assertEquals(0, $configs[$this->forumpostareaid]->indexingstart);
140 $this->assertEquals(0, $configs[$this->forumpostareaid]->indexingend);
141
142 set_config($varname . '_indexingstart', $start, $componentname);
143 set_config($varname . '_indexingend', $end, $componentname);
144
145 // All components config should be reset.
146 $search->reset_config();
147 $this->assertEquals(0, get_config($componentname, $varname . '_indexingstart'));
148 $this->assertEquals(0, get_config($componentname, $varname . '_indexingend'));
149 $this->assertEquals(0, get_config($componentname, $varname . '_lastindexrun'));
db48207e
DM
150 // No caching.
151 $configs = $search->get_areas_config(array($this->forumpostareaid => $searcharea));
152 $this->assertEquals(0, $configs[$this->forumpostareaid]->indexingstart);
153 $this->assertEquals(0, $configs[$this->forumpostareaid]->indexingend);
154 }
155
67d64795 156 /**
157 * Tests the get_last_indexing_duration method in the base area class.
158 */
159 public function test_get_last_indexing_duration() {
160 $this->resetAfterTest();
161
162 $search = testable_core_search::instance();
163
164 $searcharea = $search->get_search_area($this->forumpostareaid);
165
166 // When never indexed, the duration is false.
167 $this->assertSame(false, $searcharea->get_last_indexing_duration());
168
169 // Set the start/end times.
170 list($componentname, $varname) = $searcharea->get_config_var_name();
171 $start = time() - 100;
172 $end = time();
173 set_config($varname . '_indexingstart', $start, $componentname);
174 set_config($varname . '_indexingend', $end, $componentname);
175
176 // The duration should now be 100.
177 $this->assertSame(100, $searcharea->get_last_indexing_duration());
178 }
179
180 /**
181 * Tests that partial indexing works correctly.
182 */
183 public function test_partial_indexing() {
184 global $USER;
185
186 $this->resetAfterTest();
187 $this->setAdminUser();
188
189 // Create a course and a forum.
190 $generator = $this->getDataGenerator();
191 $course = $generator->create_course();
192 $forum = $generator->create_module('forum', ['course' => $course->id]);
193
194 // Index everything up to current. Ensure the course is older than current second so it
195 // definitely doesn't get indexed again next time.
196 $this->waitForSecond();
197 $search = testable_core_search::instance();
198 $search->index(false, 0);
199
200 $searcharea = $search->get_search_area($this->forumpostareaid);
201 list($componentname, $varname) = $searcharea->get_config_var_name();
202 $this->assertFalse(get_config($componentname, $varname . '_partial'));
203
204 // Add 3 discussions to the forum.
205 $now = time();
206 $generator->get_plugin_generator('mod_forum')->create_discussion(['course' => $course->id,
207 'forum' => $forum->id, 'userid' => $USER->id, 'timemodified' => $now,
208 'name' => 'Frog']);
209 $generator->get_plugin_generator('mod_forum')->create_discussion(['course' => $course->id,
210 'forum' => $forum->id, 'userid' => $USER->id, 'timemodified' => $now + 1,
211 'name' => 'Toad']);
212 $generator->get_plugin_generator('mod_forum')->create_discussion(['course' => $course->id,
213 'forum' => $forum->id, 'userid' => $USER->id, 'timemodified' => $now + 2,
214 'name' => 'Zombie']);
215 time_sleep_until($now + 3);
216
217 // Clear the count of added documents.
218 $search->get_engine()->get_and_clear_added_documents();
219
220 // Make the search engine delay while indexing each document.
221 $search->get_engine()->set_add_delay(1.2);
222
223 // Index with a limit of 2 seconds - it should index 2 of the documents (after the second
224 // one, it will have taken 2.4 seconds so it will stop).
225 $search->index(false, 2);
226 $added = $search->get_engine()->get_and_clear_added_documents();
227 $this->assertCount(2, $added);
228 $this->assertEquals('Frog', $added[0]->get('title'));
229 $this->assertEquals('Toad', $added[1]->get('title'));
230 $this->assertEquals(1, get_config($componentname, $varname . '_partial'));
231
232 // Add a label.
233 $generator->create_module('label', ['course' => $course->id, 'intro' => 'Vampire']);
234
235 // Wait to next second (so as to not reindex the label more than once, as it will now
236 // be timed before the indexing run).
237 $this->waitForSecond();
238
239 // Next index with 1 second limit should do the label and not the forum - the logic is,
240 // if it spent ages indexing an area last time, do that one last on next run.
241 $search->index(false, 1);
242 $added = $search->get_engine()->get_and_clear_added_documents();
243 $this->assertCount(1, $added);
244 $this->assertEquals('Vampire', $added[0]->get('title'));
245
246 // Index again with a 2 second limit - it will redo last post for safety (because of other
247 // things possibly having the same time second), and then do the remaining one. (Note:
248 // because it always does more than one second worth of items, it would actually index 2
249 // posts even if the limit were less than 2.)
250 $search->index(false, 2);
251 $added = $search->get_engine()->get_and_clear_added_documents();
252 $this->assertCount(2, $added);
253 $this->assertEquals('Toad', $added[0]->get('title'));
254 $this->assertEquals('Zombie', $added[1]->get('title'));
255 $this->assertFalse(get_config($componentname, $varname . '_partial'));
256
257 // Index again - there should be nothing to index this time.
258 $search->index(false, 2);
259 $added = $search->get_engine()->get_and_clear_added_documents();
260 $this->assertCount(0, $added);
261 $this->assertFalse(get_config($componentname, $varname . '_partial'));
262 }
263
db48207e
DM
264 /**
265 * Adding this test here as get_areas_user_accesses process is the same, results just depend on the context level.
266 *
267 * @return void
268 */
269 public function test_search_user_accesses() {
270 global $DB;
271
272 $this->resetAfterTest();
273
274 $frontpage = $DB->get_record('course', array('id' => SITEID));
275 $course1 = $this->getDataGenerator()->create_course();
724703d3 276 $course1ctx = context_course::instance($course1->id);
db48207e 277 $course2 = $this->getDataGenerator()->create_course();
724703d3 278 $course2ctx = context_course::instance($course2->id);
db48207e 279 $teacher = $this->getDataGenerator()->create_user();
25ba053f 280 $teacherctx = context_user::instance($teacher->id);
db48207e 281 $student = $this->getDataGenerator()->create_user();
25ba053f 282 $studentctx = context_user::instance($student->id);
db48207e 283 $noaccess = $this->getDataGenerator()->create_user();
25ba053f 284 $noaccessctx = context_user::instance($noaccess->id);
db48207e
DM
285 $this->getDataGenerator()->enrol_user($teacher->id, $course1->id, 'teacher');
286 $this->getDataGenerator()->enrol_user($student->id, $course1->id, 'student');
287
288 $frontpageforum = $this->getDataGenerator()->create_module('forum', array('course' => $frontpage->id));
289 $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id));
290 $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id));
291 $forum3 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id));
292 $frontpageforumcontext = context_module::instance($frontpageforum->cmid);
293 $context1 = context_module::instance($forum1->cmid);
294 $context2 = context_module::instance($forum2->cmid);
295 $context3 = context_module::instance($forum3->cmid);
296
297 $search = testable_core_search::instance();
25ba053f
DM
298 $mockareaid = \core_search\manager::generate_areaid('core_mocksearch', 'mock_search_area');
299 $search->add_core_search_areas();
300 $search->add_search_area($mockareaid, new core_mocksearch\search\mock_search_area());
db48207e
DM
301
302 $this->setAdminUser();
303 $this->assertTrue($search->get_areas_user_accesses());
304
724703d3 305 $sitectx = \context_course::instance(SITEID);
25ba053f 306 $systemctxid = \context_system::instance()->id;
724703d3 307
db48207e
DM
308 // Can access the frontpage ones.
309 $this->setUser($noaccess);
310 $contexts = $search->get_areas_user_accesses();
311 $this->assertEquals(array($frontpageforumcontext->id => $frontpageforumcontext->id), $contexts[$this->forumpostareaid]);
724703d3 312 $this->assertEquals(array($sitectx->id => $sitectx->id), $contexts[$this->mycoursesareaid]);
25ba053f
DM
313 $mockctxs = array($noaccessctx->id => $noaccessctx->id, $systemctxid => $systemctxid);
314 $this->assertEquals($mockctxs, $contexts[$mockareaid]);
db48207e
DM
315
316 $this->setUser($teacher);
317 $contexts = $search->get_areas_user_accesses();
318 $frontpageandcourse1 = array($frontpageforumcontext->id => $frontpageforumcontext->id, $context1->id => $context1->id,
319 $context2->id => $context2->id);
320 $this->assertEquals($frontpageandcourse1, $contexts[$this->forumpostareaid]);
724703d3
DM
321 $this->assertEquals(array($sitectx->id => $sitectx->id, $course1ctx->id => $course1ctx->id),
322 $contexts[$this->mycoursesareaid]);
25ba053f
DM
323 $mockctxs = array($teacherctx->id => $teacherctx->id, $systemctxid => $systemctxid);
324 $this->assertEquals($mockctxs, $contexts[$mockareaid]);
db48207e
DM
325
326 $this->setUser($student);
327 $contexts = $search->get_areas_user_accesses();
328 $this->assertEquals($frontpageandcourse1, $contexts[$this->forumpostareaid]);
724703d3
DM
329 $this->assertEquals(array($sitectx->id => $sitectx->id, $course1ctx->id => $course1ctx->id),
330 $contexts[$this->mycoursesareaid]);
25ba053f
DM
331 $mockctxs = array($studentctx->id => $studentctx->id, $systemctxid => $systemctxid);
332 $this->assertEquals($mockctxs, $contexts[$mockareaid]);
db48207e
DM
333
334 // Hide the activity.
335 set_coursemodule_visible($forum2->cmid, 0);
336 $contexts = $search->get_areas_user_accesses();
337 $this->assertEquals(array($frontpageforumcontext->id => $frontpageforumcontext->id, $context1->id => $context1->id),
338 $contexts[$this->forumpostareaid]);
427e3cbc
EM
339
340 // Now test course limited searches.
341 set_coursemodule_visible($forum2->cmid, 1);
342 $this->getDataGenerator()->enrol_user($student->id, $course2->id, 'student');
343 $contexts = $search->get_areas_user_accesses();
344 $allcontexts = array($frontpageforumcontext->id => $frontpageforumcontext->id, $context1->id => $context1->id,
345 $context2->id => $context2->id, $context3->id => $context3->id);
346 $this->assertEquals($allcontexts, $contexts[$this->forumpostareaid]);
724703d3
DM
347 $this->assertEquals(array($sitectx->id => $sitectx->id, $course1ctx->id => $course1ctx->id,
348 $course2ctx->id => $course2ctx->id), $contexts[$this->mycoursesareaid]);
427e3cbc
EM
349
350 $contexts = $search->get_areas_user_accesses(array($course1->id, $course2->id));
351 $allcontexts = array($context1->id => $context1->id, $context2->id => $context2->id, $context3->id => $context3->id);
352 $this->assertEquals($allcontexts, $contexts[$this->forumpostareaid]);
724703d3
DM
353 $this->assertEquals(array($course1ctx->id => $course1ctx->id,
354 $course2ctx->id => $course2ctx->id), $contexts[$this->mycoursesareaid]);
427e3cbc
EM
355
356 $contexts = $search->get_areas_user_accesses(array($course2->id));
357 $allcontexts = array($context3->id => $context3->id);
358 $this->assertEquals($allcontexts, $contexts[$this->forumpostareaid]);
724703d3 359 $this->assertEquals(array($course2ctx->id => $course2ctx->id), $contexts[$this->mycoursesareaid]);
427e3cbc
EM
360
361 $contexts = $search->get_areas_user_accesses(array($course1->id));
362 $allcontexts = array($context1->id => $context1->id, $context2->id => $context2->id);
363 $this->assertEquals($allcontexts, $contexts[$this->forumpostareaid]);
724703d3 364 $this->assertEquals(array($course1ctx->id => $course1ctx->id), $contexts[$this->mycoursesareaid]);
db48207e 365 }
f3d38863 366
902be008 367 /**
368 * Tests the block support in get_search_user_accesses.
369 *
370 * @return void
371 */
372 public function test_search_user_accesses_blocks() {
373 $this->resetAfterTest();
374 $this->setAdminUser();
375
376 // Create course and add HTML block.
377 $generator = $this->getDataGenerator();
378 $course1 = $generator->create_course();
379 $context1 = \context_course::instance($course1->id);
380 $page = new \moodle_page();
381 $page->set_context($context1);
382 $page->set_course($course1);
383 $page->set_pagelayout('standard');
384 $page->set_pagetype('course-view');
385 $page->blocks->load_blocks();
386 $page->blocks->add_block_at_end_of_default_region('html');
387
388 // Create another course with HTML blocks only in some weird page or a module page (not
389 // yet supported, so both these blocks will be ignored).
390 $course2 = $generator->create_course();
391 $context2 = \context_course::instance($course2->id);
392 $page = new \moodle_page();
393 $page->set_context($context2);
394 $page->set_course($course2);
395 $page->set_pagelayout('standard');
396 $page->set_pagetype('bogus-page');
397 $page->blocks->load_blocks();
398 $page->blocks->add_block_at_end_of_default_region('html');
399
400 $forum = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id));
401 $forumcontext = context_module::instance($forum->cmid);
402 $page = new \moodle_page();
403 $page->set_context($forumcontext);
404 $page->set_course($course2);
405 $page->set_pagelayout('standard');
406 $page->set_pagetype('mod-forum-view');
407 $page->blocks->load_blocks();
408 $page->blocks->add_block_at_end_of_default_region('html');
409
410 // The third course has 2 HTML blocks.
411 $course3 = $generator->create_course();
412 $context3 = \context_course::instance($course3->id);
413 $page = new \moodle_page();
414 $page->set_context($context3);
415 $page->set_course($course3);
416 $page->set_pagelayout('standard');
417 $page->set_pagetype('course-view');
418 $page->blocks->load_blocks();
419 $page->blocks->add_block_at_end_of_default_region('html');
420 $page->blocks->add_block_at_end_of_default_region('html');
421
422 // Student 1 belongs to all 3 courses.
423 $student1 = $generator->create_user();
424 $generator->enrol_user($student1->id, $course1->id, 'student');
425 $generator->enrol_user($student1->id, $course2->id, 'student');
426 $generator->enrol_user($student1->id, $course3->id, 'student');
427
428 // Student 2 belongs only to course 2.
429 $student2 = $generator->create_user();
430 $generator->enrol_user($student2->id, $course2->id, 'student');
431
432 // And the third student is only in course 3.
433 $student3 = $generator->create_user();
434 $generator->enrol_user($student3->id, $course3->id, 'student');
435
436 $search = testable_core_search::instance();
437 $search->add_core_search_areas();
438
439 // Admin gets 'true' result to function regardless of blocks.
440 $this->setAdminUser();
441 $this->assertTrue($search->get_areas_user_accesses());
442
443 // Student 1 gets all 3 block contexts.
444 $this->setUser($student1);
445 $contexts = $search->get_areas_user_accesses();
446 $this->assertArrayHasKey('block_html-content', $contexts);
447 $this->assertCount(3, $contexts['block_html-content']);
448
449 // Student 2 does not get any blocks.
450 $this->setUser($student2);
451 $contexts = $search->get_areas_user_accesses();
452 $this->assertArrayNotHasKey('block_html-content', $contexts);
453
454 // Student 3 gets only two of them.
455 $this->setUser($student3);
456 $contexts = $search->get_areas_user_accesses();
457 $this->assertArrayHasKey('block_html-content', $contexts);
458 $this->assertCount(2, $contexts['block_html-content']);
459
460 // A course limited search for student 1 is the same as the student 3 search.
461 $this->setUser($student1);
462 $limitedcontexts = $search->get_areas_user_accesses([$course3->id]);
463 $this->assertEquals($contexts['block_html-content'], $limitedcontexts['block_html-content']);
464 }
465
2d94d4ea 466 /**
467 * Test get_areas_user_accesses with regard to the 'all available courses' config option.
468 *
469 * @return void
470 */
471 public function test_search_user_accesses_allavailable() {
472 global $DB, $CFG;
473
474 $this->resetAfterTest();
475
476 // Front page, including a forum.
477 $frontpage = $DB->get_record('course', array('id' => SITEID));
478 $forumfront = $this->getDataGenerator()->create_module('forum', array('course' => $frontpage->id));
479 $forumfrontctx = context_module::instance($forumfront->cmid);
480
481 // Course 1 does not allow guest access.
482 $course1 = $this->getDataGenerator()->create_course((object)array(
483 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
484 'enrol_guest_password_0' => ''));
485 $forum1 = $this->getDataGenerator()->create_module('forum', array('course' => $course1->id));
486 $forum1ctx = context_module::instance($forum1->cmid);
487
488 // Course 2 does not allow guest but is accessible by all users.
489 $course2 = $this->getDataGenerator()->create_course((object)array(
490 'enrol_guest_status_0' => ENROL_INSTANCE_DISABLED,
491 'enrol_guest_password_0' => ''));
492 $course2ctx = context_course::instance($course2->id);
493 $forum2 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id));
494 $forum2ctx = context_module::instance($forum2->cmid);
495 assign_capability('moodle/course:view', CAP_ALLOW, $CFG->defaultuserroleid, $course2ctx->id);
496
497 // Course 3 allows guest access without password.
498 $course3 = $this->getDataGenerator()->create_course((object)array(
499 'enrol_guest_status_0' => ENROL_INSTANCE_ENABLED,
500 'enrol_guest_password_0' => ''));
501 $forum3 = $this->getDataGenerator()->create_module('forum', array('course' => $course2->id));
502 $forum3ctx = context_module::instance($forum3->cmid);
503
504 // Student user is enrolled in course 1.
505 $student = $this->getDataGenerator()->create_user();
506 $this->getDataGenerator()->enrol_user($student->id, $course1->id, 'student');
507
508 // No access user is just a user with no permissions.
509 $noaccess = $this->getDataGenerator()->create_user();
510
511 // First test without the all available option.
512 $search = testable_core_search::instance();
513
514 // Admin user can access everything.
515 $this->setAdminUser();
516 $this->assertTrue($search->get_areas_user_accesses());
517
518 // No-access user can access only the front page forum.
519 $this->setUser($noaccess);
520 $contexts = $search->get_areas_user_accesses();
521 $this->assertEquals([$forumfrontctx->id], array_keys($contexts[$this->forumpostareaid]));
522
523 // Student can access the front page forum plus the enrolled one.
524 $this->setUser($student);
525 $contexts = $search->get_areas_user_accesses();
526 $this->assertEquals([$forum1ctx->id, $forumfrontctx->id],
527 array_keys($contexts[$this->forumpostareaid]));
528
529 // Now turn on the all available option.
530 set_config('searchallavailablecourses', 1);
531
532 // Admin user can access everything.
533 $this->setAdminUser();
534 $this->assertTrue($search->get_areas_user_accesses());
535
536 // No-access user can access the front page forum and course 2, 3.
537 $this->setUser($noaccess);
538 $contexts = $search->get_areas_user_accesses();
539 $this->assertEquals([$forum2ctx->id, $forum3ctx->id, $forumfrontctx->id],
540 array_keys($contexts[$this->forumpostareaid]));
541
542 // Student can access the front page forum plus the enrolled one plus courses 2, 3.
543 $this->setUser($student);
544 $contexts = $search->get_areas_user_accesses();
545 $this->assertEquals([$forum1ctx->id, $forum2ctx->id, $forum3ctx->id, $forumfrontctx->id],
546 array_keys($contexts[$this->forumpostareaid]));
547 }
548
f3d38863
DM
549 /**
550 * test_is_search_area
551 *
552 * @return void
553 */
554 public function test_is_search_area() {
555
556 $this->assertFalse(testable_core_search::is_search_area('\asd\asd'));
557 $this->assertFalse(testable_core_search::is_search_area('\mod_forum\search\posta'));
558 $this->assertFalse(testable_core_search::is_search_area('\core_search\base_mod'));
559 $this->assertTrue(testable_core_search::is_search_area('\mod_forum\search\post'));
560 $this->assertTrue(testable_core_search::is_search_area('\\mod_forum\\search\\post'));
561 $this->assertTrue(testable_core_search::is_search_area('mod_forum\\search\\post'));
562 }
db48207e 563}