Differences Between: [Versions 39 and 310]
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 * Course global search unit tests. 19 * 20 * @package core 21 * @category phpunit 22 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php'); 30 31 /** 32 * Provides the unit tests for course global search. 33 * 34 * @package core 35 * @category phpunit 36 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com} 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class course_search_testcase extends advanced_testcase { 40 41 /** 42 * @var string Area id 43 */ 44 protected $coursesareaid = null; 45 46 /** 47 * @var string Area id for sections 48 */ 49 protected $sectionareaid = null; 50 51 /** 52 * @var string Area id for custom fields. 53 */ 54 protected $customfieldareaid = null; 55 56 public function setUp(): void { 57 $this->resetAfterTest(true); 58 set_config('enableglobalsearch', true); 59 60 $this->coursesareaid = \core_search\manager::generate_areaid('core_course', 'course'); 61 $this->sectionareaid = \core_search\manager::generate_areaid('core_course', 'section'); 62 $this->customfieldareaid = \core_search\manager::generate_areaid('core_course', 'customfield'); 63 64 // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this. 65 $search = testable_core_search::instance(); 66 } 67 68 /** 69 * Indexing courses contents. 70 * 71 * @return void 72 */ 73 public function test_courses_indexing() { 74 75 // Returns the instance as long as the area is supported. 76 $searcharea = \core_search\manager::get_search_area($this->coursesareaid); 77 $this->assertInstanceOf('\core_course\search\course', $searcharea); 78 79 $user1 = self::getDataGenerator()->create_user(); 80 $user2 = self::getDataGenerator()->create_user(); 81 82 $course1 = self::getDataGenerator()->create_course(); 83 $course2 = self::getDataGenerator()->create_course(); 84 85 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student'); 86 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student'); 87 88 $record = new stdClass(); 89 $record->course = $course1->id; 90 91 // All records. 92 $recordset = $searcharea->get_recordset_by_timestamp(0); 93 $this->assertTrue($recordset->valid()); 94 $nrecords = 0; 95 foreach ($recordset as $record) { 96 $this->assertInstanceOf('stdClass', $record); 97 $doc = $searcharea->get_document($record); 98 $this->assertInstanceOf('\core_search\document', $doc); 99 $nrecords++; 100 } 101 // If there would be an error/failure in the foreach above the recordset would be closed on shutdown. 102 $recordset->close(); 103 $this->assertEquals(3, $nrecords); 104 105 // The +2 is to prevent race conditions. 106 $recordset = $searcharea->get_recordset_by_timestamp(time() + 2); 107 108 // No new records. 109 $this->assertFalse($recordset->valid()); 110 $recordset->close(); 111 } 112 113 /** 114 * Tests course indexing support for contexts. 115 */ 116 public function test_courses_indexing_contexts() { 117 global $DB, $USER, $SITE; 118 119 $searcharea = \core_search\manager::get_search_area($this->coursesareaid); 120 121 // Create some courses in categories, and a forum. 122 $generator = $this->getDataGenerator(); 123 $cat1 = $generator->create_category(); 124 $course1 = $generator->create_course(['category' => $cat1->id]); 125 $cat2 = $generator->create_category(['parent' => $cat1->id]); 126 $course2 = $generator->create_course(['category' => $cat2->id]); 127 $cat3 = $generator->create_category(); 128 $course3 = $generator->create_course(['category' => $cat3->id]); 129 $forum = $generator->create_module('forum', ['course' => $course1->id]); 130 $DB->set_field('course', 'timemodified', 0, ['id' => $SITE->id]); 131 $DB->set_field('course', 'timemodified', 1, ['id' => $course1->id]); 132 $DB->set_field('course', 'timemodified', 2, ['id' => $course2->id]); 133 $DB->set_field('course', 'timemodified', 3, ['id' => $course3->id]); 134 135 // Find the first block to use for a block context. 136 $blockid = array_values($DB->get_records('block_instances', null, 'id', 'id', 0, 1))[0]->id; 137 $blockcontext = context_block::instance($blockid); 138 139 // Check with block context - should be null. 140 $this->assertNull($searcharea->get_document_recordset(0, $blockcontext)); 141 142 // Check with user context - should be null. 143 $this->setAdminUser(); 144 $usercontext = context_user::instance($USER->id); 145 $this->assertNull($searcharea->get_document_recordset(0, $usercontext)); 146 147 // Check with module context - should be null. 148 $modcontext = context_module::instance($forum->cmid); 149 $this->assertNull($searcharea->get_document_recordset(0, $modcontext)); 150 151 // Check with course context - should return specified course if timestamp allows. 152 $coursecontext = context_course::instance($course3->id); 153 $results = self::recordset_to_ids($searcharea->get_document_recordset(3, $coursecontext)); 154 $this->assertEquals([$course3->id], $results); 155 $results = self::recordset_to_ids($searcharea->get_document_recordset(4, $coursecontext)); 156 $this->assertEquals([], $results); 157 158 // Check with category context - should return course in categories and subcategories. 159 $catcontext = context_coursecat::instance($cat1->id); 160 $results = self::recordset_to_ids($searcharea->get_document_recordset(0, $catcontext)); 161 $this->assertEquals([$course1->id, $course2->id], $results); 162 $results = self::recordset_to_ids($searcharea->get_document_recordset(2, $catcontext)); 163 $this->assertEquals([$course2->id], $results); 164 165 // Check with system context and null - should return all these courses + site course. 166 $systemcontext = context_system::instance(); 167 $results = self::recordset_to_ids($searcharea->get_document_recordset(0, $systemcontext)); 168 $this->assertEquals([$SITE->id, $course1->id, $course2->id, $course3->id], $results); 169 $results = self::recordset_to_ids($searcharea->get_document_recordset(0, null)); 170 $this->assertEquals([$SITE->id, $course1->id, $course2->id, $course3->id], $results); 171 $results = self::recordset_to_ids($searcharea->get_document_recordset(3, $systemcontext)); 172 $this->assertEquals([$course3->id], $results); 173 $results = self::recordset_to_ids($searcharea->get_document_recordset(3, null)); 174 $this->assertEquals([$course3->id], $results); 175 } 176 177 /** 178 * Utility function to convert recordset to array of IDs for testing. 179 * 180 * @param moodle_recordset $rs Recordset to convert (and close) 181 * @return array Array of IDs from records indexed by number (0, 1, 2, ...) 182 */ 183 protected static function recordset_to_ids(moodle_recordset $rs) { 184 $results = []; 185 foreach ($rs as $rec) { 186 $results[] = $rec->id; 187 } 188 $rs->close(); 189 return $results; 190 } 191 192 /** 193 * Document contents. 194 * 195 * @return void 196 */ 197 public function test_courses_document() { 198 199 // Returns the instance as long as the area is supported. 200 $searcharea = \core_search\manager::get_search_area($this->coursesareaid); 201 $this->assertInstanceOf('\core_course\search\course', $searcharea); 202 203 $user = self::getDataGenerator()->create_user(); 204 $course = self::getDataGenerator()->create_course(); 205 $this->getDataGenerator()->enrol_user($user->id, $course->id, 'teacher'); 206 207 $doc = $searcharea->get_document($course); 208 $this->assertInstanceOf('\core_search\document', $doc); 209 $this->assertEquals($course->id, $doc->get('itemid')); 210 $this->assertEquals($this->coursesareaid . '-' . $course->id, $doc->get('id')); 211 $this->assertEquals($course->id, $doc->get('courseid')); 212 $this->assertFalse($doc->is_set('userid')); 213 $this->assertEquals(\core_search\manager::NO_OWNER_ID, $doc->get('owneruserid')); 214 $this->assertEquals($course->fullname, $doc->get('title')); 215 216 // Not nice. Applying \core_search\document::set line breaks clean up. 217 $summary = preg_replace("/\s+/u", " ", content_to_text($course->summary, $course->summaryformat)); 218 $this->assertEquals($summary, $doc->get('content')); 219 $this->assertEquals($course->shortname, $doc->get('description1')); 220 } 221 222 /** 223 * Document accesses. 224 * 225 * @return void 226 */ 227 public function test_courses_access() { 228 $this->resetAfterTest(); 229 230 // Returns the instance as long as the area is supported. 231 $searcharea = \core_search\manager::get_search_area($this->coursesareaid); 232 233 $user1 = self::getDataGenerator()->create_user(); 234 $user2 = self::getDataGenerator()->create_user(); 235 236 $course1 = self::getDataGenerator()->create_course(); 237 $course2 = self::getDataGenerator()->create_course(array('visible' => 0)); 238 $course3 = self::getDataGenerator()->create_course(); 239 240 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'teacher'); 241 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student'); 242 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, 'teacher'); 243 $this->getDataGenerator()->enrol_user($user2->id, $course2->id, 'student'); 244 245 $this->setUser($user1); 246 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course1->id)); 247 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course2->id)); 248 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course3->id)); 249 $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-123)); 250 251 $this->setUser($user2); 252 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course1->id)); 253 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($course2->id)); 254 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course3->id)); 255 } 256 257 /** 258 * Indexing section contents. 259 */ 260 public function test_section_indexing() { 261 global $DB, $USER; 262 263 // Returns the instance as long as the area is supported. 264 $searcharea = \core_search\manager::get_search_area($this->sectionareaid); 265 $this->assertInstanceOf('\core_course\search\section', $searcharea); 266 267 // Create some courses in categories, and a forum. 268 $generator = $this->getDataGenerator(); 269 $cat1 = $generator->create_category(); 270 $cat2 = $generator->create_category(['parent' => $cat1->id]); 271 $course1 = $generator->create_course(['category' => $cat1->id]); 272 $course2 = $generator->create_course(['category' => $cat2->id]); 273 $forum = $generator->create_module('forum', ['course' => $course1->id]); 274 275 // Edit 2 sections on course 1 and one on course 2. 276 $existing = $DB->get_record('course_sections', ['course' => $course1->id, 'section' => 2]); 277 $course1section2id = $existing->id; 278 $new = clone($existing); 279 $new->name = 'Frogs'; 280 course_update_section($course1->id, $existing, $new); 281 282 $existing = $DB->get_record('course_sections', ['course' => $course1->id, 'section' => 3]); 283 $course1section3id = $existing->id; 284 $new = clone($existing); 285 $new->summary = 'Frogs'; 286 $new->summaryformat = FORMAT_HTML; 287 course_update_section($course1->id, $existing, $new); 288 289 $existing = $DB->get_record('course_sections', ['course' => $course2->id, 'section' => 1]); 290 $course2section1id = $existing->id; 291 $new = clone($existing); 292 $new->summary = 'Frogs'; 293 $new->summaryformat = FORMAT_HTML; 294 course_update_section($course2->id, $existing, $new); 295 296 // Bodge timemodified into a particular order. 297 $DB->set_field('course_sections', 'timemodified', 1, ['id' => $course1section3id]); 298 $DB->set_field('course_sections', 'timemodified', 2, ['id' => $course1section2id]); 299 $DB->set_field('course_sections', 'timemodified', 3, ['id' => $course2section1id]); 300 301 // All records. 302 $results = self::recordset_to_ids($searcharea->get_document_recordset(0)); 303 $this->assertEquals([$course1section3id, $course1section2id, $course2section1id], $results); 304 305 // Records after time 2. 306 $results = self::recordset_to_ids($searcharea->get_document_recordset(2)); 307 $this->assertEquals([$course1section2id, $course2section1id], $results); 308 309 // Records after time 10 (there aren't any). 310 $results = self::recordset_to_ids($searcharea->get_document_recordset(10)); 311 $this->assertEquals([], $results); 312 313 // Find the first block to use for a block context. 314 $blockid = array_values($DB->get_records('block_instances', null, 'id', 'id', 0, 1))[0]->id; 315 $blockcontext = context_block::instance($blockid); 316 317 // Check with block context - should be null. 318 $this->assertNull($searcharea->get_document_recordset(0, $blockcontext)); 319 320 // Check with user context - should be null. 321 $this->setAdminUser(); 322 $usercontext = context_user::instance($USER->id); 323 $this->assertNull($searcharea->get_document_recordset(0, $usercontext)); 324 325 // Check with module context - should be null. 326 $modcontext = context_module::instance($forum->cmid); 327 $this->assertNull($searcharea->get_document_recordset(0, $modcontext)); 328 329 // Check with course context - should return specific course entries. 330 $coursecontext = context_course::instance($course1->id); 331 $results = self::recordset_to_ids($searcharea->get_document_recordset(0, $coursecontext)); 332 $this->assertEquals([$course1section3id, $course1section2id], $results); 333 $results = self::recordset_to_ids($searcharea->get_document_recordset(2, $coursecontext)); 334 $this->assertEquals([$course1section2id], $results); 335 336 // Check with category context - should return course in categories and subcategories. 337 $catcontext = context_coursecat::instance($cat1->id); 338 $results = self::recordset_to_ids($searcharea->get_document_recordset(0, $catcontext)); 339 $this->assertEquals([$course1section3id, $course1section2id, $course2section1id], $results); 340 $catcontext = context_coursecat::instance($cat2->id); 341 $results = self::recordset_to_ids($searcharea->get_document_recordset(0, $catcontext)); 342 $this->assertEquals([$course2section1id], $results); 343 344 // Check with system context - should return everything (same as null, tested first). 345 $systemcontext = context_system::instance(); 346 $results = self::recordset_to_ids($searcharea->get_document_recordset(0, $systemcontext)); 347 $this->assertEquals([$course1section3id, $course1section2id, $course2section1id], $results); 348 } 349 350 /** 351 * Document contents for sections. 352 */ 353 public function test_section_document() { 354 global $DB; 355 356 $searcharea = \core_search\manager::get_search_area($this->sectionareaid); 357 358 // Create a course. 359 $generator = $this->getDataGenerator(); 360 $course = $generator->create_course(); 361 362 // Test with default title. 363 $sectionrec = (object)['id' => 123, 'course' => $course->id, 364 'section' => 3, 'timemodified' => 456, 365 'summary' => 'Kermit', 'summaryformat' => FORMAT_HTML]; 366 $doc = $searcharea->get_document($sectionrec); 367 $this->assertInstanceOf('\core_search\document', $doc); 368 $this->assertEquals(123, $doc->get('itemid')); 369 $this->assertEquals($this->sectionareaid . '-123', $doc->get('id')); 370 $this->assertEquals($course->id, $doc->get('courseid')); 371 $this->assertFalse($doc->is_set('userid')); 372 $this->assertEquals(\core_search\manager::NO_OWNER_ID, $doc->get('owneruserid')); 373 $this->assertEquals('Topic 3', $doc->get('title')); 374 $this->assertEquals('Kermit', $doc->get('content')); 375 376 // Test with user-set title. 377 $DB->set_field('course_sections', 'name', 'Frogs', 378 ['course' => $course->id, 'section' => 3]); 379 rebuild_course_cache($course->id, true); 380 $doc = $searcharea->get_document($sectionrec); 381 $this->assertEquals('Frogs', $doc->get('title')); 382 } 383 384 /** 385 * Document access for sections. 386 */ 387 public function test_section_access() { 388 global $DB; 389 390 $searcharea = \core_search\manager::get_search_area($this->sectionareaid); 391 392 // Create a course. 393 $generator = $this->getDataGenerator(); 394 $course = $generator->create_course(); 395 396 // Create 2 users - student and manager. Initially, student is not even enrolled. 397 $student = $generator->create_user(); 398 $manager = $generator->create_user(); 399 $generator->enrol_user($manager->id, $course->id, 'manager'); 400 401 // Two sections have content - one is hidden. 402 $DB->set_field('course_sections', 'name', 'Frogs', 403 ['course' => $course->id, 'section' => 1]); 404 $DB->set_field('course_sections', 'name', 'Toads', 405 ['course' => $course->id, 'section' => 2]); 406 $DB->set_field('course_sections', 'visible', '0', 407 ['course' => $course->id, 'section' => 2]); 408 409 // Make the modified time be in order of sections. 410 $DB->execute('UPDATE {course_sections} SET timemodified = section'); 411 412 // Get the two document objects. 413 $rs = $searcharea->get_document_recordset(); 414 $documents = []; 415 $index = 0; 416 foreach ($rs as $rec) { 417 $documents[$index++] = $searcharea->get_document($rec); 418 } 419 $this->assertCount(2, $documents); 420 421 // Log in as admin and check access. 422 $this->setAdminUser(); 423 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, 424 $searcharea->check_access($documents[0]->get('itemid'))); 425 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, 426 $searcharea->check_access($documents[1]->get('itemid'))); 427 428 // Log in as manager and check access. 429 $this->setUser($manager); 430 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, 431 $searcharea->check_access($documents[0]->get('itemid'))); 432 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, 433 $searcharea->check_access($documents[1]->get('itemid'))); 434 435 // Log in as student and check access - none yet. 436 $this->setUser($student); 437 $this->assertEquals(\core_search\manager::ACCESS_DENIED, 438 $searcharea->check_access($documents[0]->get('itemid'))); 439 $this->assertEquals(\core_search\manager::ACCESS_DENIED, 440 $searcharea->check_access($documents[1]->get('itemid'))); 441 442 // Enrol student - now they should get access but not to the hidden one. 443 $generator->enrol_user($student->id, $course->id, 'student'); 444 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, 445 $searcharea->check_access($documents[0]->get('itemid'))); 446 $this->assertEquals(\core_search\manager::ACCESS_DENIED, 447 $searcharea->check_access($documents[1]->get('itemid'))); 448 449 // Delete the course and check it returns deleted. 450 delete_course($course, false); 451 $this->assertEquals(\core_search\manager::ACCESS_DELETED, 452 $searcharea->check_access($documents[0]->get('itemid'))); 453 $this->assertEquals(\core_search\manager::ACCESS_DELETED, 454 $searcharea->check_access($documents[1]->get('itemid'))); 455 } 456 457 /** 458 * Indexing custom fields contents. 459 * 460 * @return void 461 */ 462 public function test_customfield_indexing() { 463 // Returns the instance as long as the area is supported. 464 $searcharea = \core_search\manager::get_search_area($this->customfieldareaid); 465 $this->assertInstanceOf('\core_course\search\customfield', $searcharea); 466 467 // We need to be admin for custom fields creation. 468 $this->setAdminUser(); 469 470 // Custom fields. 471 $fieldcategory = self::getDataGenerator()->create_custom_field_category(['name' => 'Other fields']); 472 $customfield = ['shortname' => 'test', 'name' => 'Customfield', 'type' => 'text', 473 'categoryid' => $fieldcategory->get('id')]; 474 $field = self::getDataGenerator()->create_custom_field($customfield); 475 476 $course1data = ['customfields' => [['shortname' => $customfield['shortname'], 'value' => 'Customvalue1']]]; 477 $course1 = self::getDataGenerator()->create_course($course1data); 478 479 $course2data = ['customfields' => [['shortname' => $customfield['shortname'], 'value' => 'Customvalue2']]]; 480 $course2 = self::getDataGenerator()->create_course($course2data); 481 482 // All records. 483 $recordset = $searcharea->get_recordset_by_timestamp(0); 484 $this->assertTrue($recordset->valid()); 485 $nrecords = 0; 486 foreach ($recordset as $record) { 487 $this->assertInstanceOf('stdClass', $record); 488 $doc = $searcharea->get_document($record); 489 $this->assertInstanceOf('\core_search\document', $doc); 490 $nrecords++; 491 } 492 // If there would be an error/failure in the foreach above the recordset would be closed on shutdown. 493 $recordset->close(); 494 $this->assertEquals(2, $nrecords); 495 496 // The +2 is to prevent race conditions. 497 $recordset = $searcharea->get_recordset_by_timestamp(time() + 2); 498 499 // No new records. 500 $this->assertFalse($recordset->valid()); 501 $recordset->close(); 502 } 503 504 /** 505 * Document contents for custom fields. 506 * 507 * @return void 508 */ 509 public function test_customfield_document() { 510 global $DB; 511 // Returns the instance as long as the area is supported. 512 $searcharea = \core_search\manager::get_search_area($this->customfieldareaid); 513 514 // We need to be admin for custom fields creation. 515 $this->setAdminUser(); 516 517 // Custom fields. 518 $fieldcategory = self::getDataGenerator()->create_custom_field_category(['name' => 'Other fields']); 519 $customfield = ['shortname' => 'test', 'name' => 'Customfield', 'type' => 'text', 520 'categoryid' => $fieldcategory->get('id')]; 521 $field = self::getDataGenerator()->create_custom_field($customfield); 522 523 $coursedata = ['customfields' => [['shortname' => $customfield['shortname'], 'value' => 'Customvalue1']]]; 524 $course = self::getDataGenerator()->create_course($coursedata); 525 526 // Retrieve data we need to compare with document instance. 527 $record = $DB->get_record('customfield_data', ['instanceid' => $course->id]); 528 $field = \core_customfield\field_controller::create($record->fieldid); 529 $data = \core_customfield\data_controller::create(0, $record, $field); 530 531 $doc = $searcharea->get_document($record); 532 $this->assertInstanceOf('\core_search\document', $doc); 533 $this->assertEquals('Customfield', $doc->get('title')); 534 $this->assertEquals('Customvalue1', $doc->get('content')); 535 $this->assertEquals($course->id, $doc->get('courseid')); 536 $this->assertEquals(\core_search\manager::NO_OWNER_ID, $doc->get('owneruserid')); 537 $this->assertEquals($course->id, $doc->get('courseid')); 538 $this->assertFalse($doc->is_set('userid')); 539 } 540 541 /** 542 * Document accesses for customfield area. 543 */ 544 public function test_customfield_access() { 545 $this->resetAfterTest(); 546 547 // Returns the instance as long as the area is supported. 548 $searcharea = \core_search\manager::get_search_area($this->customfieldareaid); 549 550 $user1 = self::getDataGenerator()->create_user(); 551 $user2 = self::getDataGenerator()->create_user(); 552 553 $course1 = self::getDataGenerator()->create_course(); 554 $course2 = self::getDataGenerator()->create_course(array('visible' => 0)); 555 $course3 = self::getDataGenerator()->create_course(); 556 557 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'teacher'); 558 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student'); 559 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, 'teacher'); 560 $this->getDataGenerator()->enrol_user($user2->id, $course2->id, 'student'); 561 562 $this->setUser($user1); 563 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course1->id)); 564 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course2->id)); 565 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course3->id)); 566 $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-123)); 567 568 $this->setUser($user2); 569 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course1->id)); 570 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($course2->id)); 571 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course3->id)); 572 } 573 574 /** 575 * Test document icon for course area. 576 */ 577 public function test_get_doc_icon_for_course_area() { 578 $searcharea = \core_search\manager::get_search_area($this->coursesareaid); 579 580 $document = $this->getMockBuilder('\core_search\document') 581 ->disableOriginalConstructor() 582 ->getMock(); 583 584 $result = $searcharea->get_doc_icon($document); 585 586 $this->assertEquals('i/course', $result->get_name()); 587 $this->assertEquals('moodle', $result->get_component()); 588 } 589 590 /** 591 * Test document icon for section area. 592 */ 593 public function test_get_doc_icon_for_section_area() { 594 $searcharea = \core_search\manager::get_search_area($this->sectionareaid); 595 596 $document = $this->getMockBuilder('\core_search\document') 597 ->disableOriginalConstructor() 598 ->getMock(); 599 600 $result = $searcharea->get_doc_icon($document); 601 602 $this->assertEquals('i/section', $result->get_name()); 603 $this->assertEquals('moodle', $result->get_component()); 604 } 605 606 /** 607 * Test assigned search categories. 608 */ 609 public function test_get_category_names() { 610 $coursessearcharea = \core_search\manager::get_search_area($this->coursesareaid); 611 $sectionsearcharea = \core_search\manager::get_search_area($this->sectionareaid); 612 $customfieldssearcharea = \core_search\manager::get_search_area($this->customfieldareaid); 613 614 $this->assertEquals(['core-courses'], $coursessearcharea->get_category_names()); 615 $this->assertEquals(['core-course-content'], $sectionsearcharea->get_category_names()); 616 $this->assertEquals(['core-course-content', 'core-courses'], $customfieldssearcharea->get_category_names()); 617 } 618 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body