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