MDL-57268 auth_db: Unit tests for deletion from a large user set
[moodle.git] / course / tests / search_test.php
blobe8ed2fa4c323439112b734664f037b3a09170d7d
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 /**
18 * Course global search unit tests.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
31 /**
32 * Provides the unit tests for course global search.
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
39 class course_search_testcase extends advanced_testcase {
41 /**
42 * @var string Area id
44 protected $mycoursesareaid = null;
46 public function setUp() {
47 $this->resetAfterTest(true);
48 set_config('enableglobalsearch', true);
50 $this->mycoursesareaid = \core_search\manager::generate_areaid('core_course', 'mycourse');
52 // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
53 $search = testable_core_search::instance();
56 /**
57 * Indexing my courses contents.
59 * @return void
61 public function test_mycourses_indexing() {
63 // Returns the instance as long as the area is supported.
64 $searcharea = \core_search\manager::get_search_area($this->mycoursesareaid);
65 $this->assertInstanceOf('\core_course\search\mycourse', $searcharea);
67 $user1 = self::getDataGenerator()->create_user();
68 $user2 = self::getDataGenerator()->create_user();
70 $course1 = self::getDataGenerator()->create_course();
71 $course2 = self::getDataGenerator()->create_course();
73 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
74 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student');
76 $record = new stdClass();
77 $record->course = $course1->id;
79 // All records.
80 $recordset = $searcharea->get_recordset_by_timestamp(0);
81 $this->assertTrue($recordset->valid());
82 $nrecords = 0;
83 foreach ($recordset as $record) {
84 $this->assertInstanceOf('stdClass', $record);
85 $doc = $searcharea->get_document($record);
86 $this->assertInstanceOf('\core_search\document', $doc);
87 $nrecords++;
89 // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
90 $recordset->close();
91 $this->assertEquals(3, $nrecords);
93 // The +2 is to prevent race conditions.
94 $recordset = $searcharea->get_recordset_by_timestamp(time() + 2);
96 // No new records.
97 $this->assertFalse($recordset->valid());
98 $recordset->close();
102 * Document contents.
104 * @return void
106 public function test_mycourses_document() {
108 // Returns the instance as long as the area is supported.
109 $searcharea = \core_search\manager::get_search_area($this->mycoursesareaid);
110 $this->assertInstanceOf('\core_course\search\mycourse', $searcharea);
112 $user = self::getDataGenerator()->create_user();
113 $course = self::getDataGenerator()->create_course();
114 $this->getDataGenerator()->enrol_user($user->id, $course->id, 'teacher');
116 $doc = $searcharea->get_document($course);
117 $this->assertInstanceOf('\core_search\document', $doc);
118 $this->assertEquals($course->id, $doc->get('itemid'));
119 $this->assertEquals($this->mycoursesareaid . '-' . $course->id, $doc->get('id'));
120 $this->assertEquals($course->id, $doc->get('courseid'));
121 $this->assertFalse($doc->is_set('userid'));
122 $this->assertEquals(\core_search\manager::NO_OWNER_ID, $doc->get('owneruserid'));
123 $this->assertEquals($course->fullname, $doc->get('title'));
125 // Not nice. Applying \core_search\document::set line breaks clean up.
126 $summary = preg_replace("/\s+/u", " ", content_to_text($course->summary, $course->summaryformat));
127 $this->assertEquals($summary, $doc->get('content'));
128 $this->assertEquals($course->shortname, $doc->get('description1'));
132 * Document accesses.
134 * @return void
136 public function test_mycourses_access() {
138 // Returns the instance as long as the area is supported.
139 $searcharea = \core_search\manager::get_search_area($this->mycoursesareaid);
141 $user1 = self::getDataGenerator()->create_user();
142 $user2 = self::getDataGenerator()->create_user();
144 $course1 = self::getDataGenerator()->create_course();
145 $course2 = self::getDataGenerator()->create_course(array('visible' => 0));
146 $course3 = self::getDataGenerator()->create_course();
148 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'teacher');
149 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student');
150 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, 'teacher');
151 $this->getDataGenerator()->enrol_user($user2->id, $course2->id, 'student');
153 $this->setUser($user1);
154 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course1->id));
155 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course2->id));
156 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($course3->id));
157 $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access(-123));
159 $this->setUser($user2);
160 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($course1->id));
161 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($course2->id));
162 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($course3->id));