2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Course global search unit tests.
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();
29 require_once($CFG->dirroot
. '/search/tests/fixtures/testable_core_search.php');
32 * Provides the unit tests for course global search.
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
{
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();
57 * Indexing my courses contents.
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
;
80 $recordset = $searcharea->get_recordset_by_timestamp(0);
81 $this->assertTrue($recordset->valid());
83 foreach ($recordset as $record) {
84 $this->assertInstanceOf('stdClass', $record);
85 $doc = $searcharea->get_document($record);
86 $this->assertInstanceOf('\core_search\document', $doc);
89 // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
91 $this->assertEquals(3, $nrecords);
93 // The +2 is to prevent race conditions.
94 $recordset = $searcharea->get_recordset_by_timestamp(time() +
2);
97 $this->assertFalse($recordset->valid());
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'));
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
));