MDL-41191 enrol: unit tests for enrol_get_shared_courses
[moodle.git] / enrol / tests / enrollib_test.php
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/>.
17 /**
18  * Test non-plugin enrollib parts.
19  *
20  * @package    core_enrol
21  * @category   phpunit
22  * @copyright  2012 Petr Skoda {@link http://skodak.org}
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
29 /**
30  * Test non-plugin enrollib parts.
31  *
32  * @package    core
33  * @category   phpunit
34  * @copyright  2012 Petr Skoda {@link http://skodak.org}
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class core_enrollib_testcase extends advanced_testcase {
39     public function test_enrol_get_all_users_courses() {
40         global $DB, $CFG;
42         $this->resetAfterTest();
44         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
45         $this->assertNotEmpty($studentrole);
46         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
47         $this->assertNotEmpty($teacherrole);
49         $admin = get_admin();
50         $user1 = $this->getDataGenerator()->create_user();
51         $user2 = $this->getDataGenerator()->create_user();
52         $user3 = $this->getDataGenerator()->create_user();
53         $user4 = $this->getDataGenerator()->create_user();
54         $user5 = $this->getDataGenerator()->create_user();
56         $category1 = $this->getDataGenerator()->create_category(array('visible'=>0));
57         $category2 = $this->getDataGenerator()->create_category();
58         $course1 = $this->getDataGenerator()->create_course(array('category'=>$category1->id));
59         $course2 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
60         $course3 = $this->getDataGenerator()->create_course(array('category'=>$category2->id, 'visible'=>0));
61         $course4 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
63         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
64         $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id));
65         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
66         $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
67         $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
68         $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
70         $manual = enrol_get_plugin('manual');
71         $this->assertNotEmpty($manual);
73         $manual->enrol_user($maninstance1, $user1->id, $teacherrole->id);
74         $manual->enrol_user($maninstance1, $user2->id, $studentrole->id);
75         $manual->enrol_user($maninstance1, $user4->id, $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
76         $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
78         $manual->enrol_user($maninstance2, $user1->id);
79         $manual->enrol_user($maninstance2, $user2->id);
80         $manual->enrol_user($maninstance2, $user3->id, 0, 1, time()+(60*60));
82         $manual->enrol_user($maninstance3, $user1->id);
83         $manual->enrol_user($maninstance3, $user2->id);
84         $manual->enrol_user($maninstance3, $user3->id, 0, 1, time()-(60*60));
85         $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED);
88         $courses = enrol_get_all_users_courses($CFG->siteguest);
89         $this->assertSame(array(), $courses);
91         $courses = enrol_get_all_users_courses(0);
92         $this->assertSame(array(), $courses);
94         // Results are sorted by visibility, sortorder by default (in our case order of creation)
96         $courses = enrol_get_all_users_courses($admin->id);
97         $this->assertCount(1, $courses);
98         $this->assertEquals(array($course1->id), array_keys($courses));
100         $courses = enrol_get_all_users_courses($admin->id, true);
101         $this->assertCount(0, $courses);
102         $this->assertEquals(array(), array_keys($courses));
104         $courses = enrol_get_all_users_courses($user1->id);
105         $this->assertCount(3, $courses);
106         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
108         $courses = enrol_get_all_users_courses($user1->id, true);
109         $this->assertCount(2, $courses);
110         $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
112         $courses = enrol_get_all_users_courses($user2->id);
113         $this->assertCount(3, $courses);
114         $this->assertEquals(array($course2->id, $course1->id, $course3->id), array_keys($courses));
116         $courses = enrol_get_all_users_courses($user2->id, true);
117         $this->assertCount(2, $courses);
118         $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
120         $courses = enrol_get_all_users_courses($user3->id);
121         $this->assertCount(2, $courses);
122         $this->assertEquals(array($course2->id, $course3->id), array_keys($courses));
124         $courses = enrol_get_all_users_courses($user3->id, true);
125         $this->assertCount(1, $courses);
126         $this->assertEquals(array($course2->id), array_keys($courses));
128         $courses = enrol_get_all_users_courses($user4->id);
129         $this->assertCount(2, $courses);
130         $this->assertEquals(array($course1->id, $course3->id), array_keys($courses));
132         $courses = enrol_get_all_users_courses($user4->id, true);
133         $this->assertCount(0, $courses);
134         $this->assertEquals(array(), array_keys($courses));
136         // Make sure sorting and columns work.
138         $basefields = array('id', 'category', 'sortorder', 'shortname', 'fullname', 'idnumber',
139             'startdate', 'visible', 'groupmode', 'groupmodeforce');
141         $courses = enrol_get_all_users_courses($user2->id, true);
142         $course = reset($courses);
143         context_helper::preload_from_record($course);
144         $course = (array)$course;
145         $this->assertEquals($basefields, array_keys($course), '', 0, 10, true);
147         $courses = enrol_get_all_users_courses($user2->id, false, 'modinfo');
148         $course = reset($courses);
149         $this->assertTrue(property_exists($course, 'modinfo'));
151         $courses = enrol_get_all_users_courses($user2->id, false, null, 'id DESC');
152         $this->assertEquals(array($course3->id, $course2->id, $course1->id), array_keys($courses));
153     }
155     public function test_enrol_user_sees_own_courses() {
156         global $DB, $CFG;
158         $this->resetAfterTest();
160         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
161         $this->assertNotEmpty($studentrole);
162         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
163         $this->assertNotEmpty($teacherrole);
165         $admin = get_admin();
166         $user1 = $this->getDataGenerator()->create_user();
167         $user2 = $this->getDataGenerator()->create_user();
168         $user3 = $this->getDataGenerator()->create_user();
169         $user4 = $this->getDataGenerator()->create_user();
170         $user5 = $this->getDataGenerator()->create_user();
171         $user6 = $this->getDataGenerator()->create_user();
173         $category1 = $this->getDataGenerator()->create_category(array('visible'=>0));
174         $category2 = $this->getDataGenerator()->create_category();
175         $course1 = $this->getDataGenerator()->create_course(array('category'=>$category1->id));
176         $course2 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
177         $course3 = $this->getDataGenerator()->create_course(array('category'=>$category2->id, 'visible'=>0));
178         $course4 = $this->getDataGenerator()->create_course(array('category'=>$category2->id));
180         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
181         $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id));
182         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
183         $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
184         $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
185         $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
187         $manual = enrol_get_plugin('manual');
188         $this->assertNotEmpty($manual);
190         $manual->enrol_user($maninstance1, $admin->id, $studentrole->id);
192         $manual->enrol_user($maninstance3, $user1->id, $teacherrole->id);
194         $manual->enrol_user($maninstance2, $user2->id, $studentrole->id);
196         $manual->enrol_user($maninstance1, $user3->id, $studentrole->id, 1, time()+(60*60));
197         $manual->enrol_user($maninstance2, $user3->id, 0, 1, time()-(60*60));
198         $manual->enrol_user($maninstance3, $user2->id, $studentrole->id);
199         $manual->enrol_user($maninstance4, $user2->id, 0, 0, 0, ENROL_USER_SUSPENDED);
201         $manual->enrol_user($maninstance1, $user4->id, $teacherrole->id, 0, 0, ENROL_USER_SUSPENDED);
202         $manual->enrol_user($maninstance3, $user4->id, 0, 0, 0, ENROL_USER_SUSPENDED);
205         $this->assertFalse(enrol_user_sees_own_courses($CFG->siteguest));
206         $this->assertFalse(enrol_user_sees_own_courses(0));
207         $this->assertFalse(enrol_user_sees_own_courses($admin));
208         $this->assertFalse(enrol_user_sees_own_courses(-222)); // Nonexistent user.
210         $this->assertTrue(enrol_user_sees_own_courses($user1));
211         $this->assertTrue(enrol_user_sees_own_courses($user2->id));
212         $this->assertFalse(enrol_user_sees_own_courses($user3->id));
213         $this->assertFalse(enrol_user_sees_own_courses($user4));
214         $this->assertFalse(enrol_user_sees_own_courses($user5));
216         $this->setAdminUser();
217         $this->assertFalse(enrol_user_sees_own_courses());
219         $this->setGuestUser();
220         $this->assertFalse(enrol_user_sees_own_courses());
222         $this->setUser(0);
223         $this->assertFalse(enrol_user_sees_own_courses());
225         $this->setUser($user1);
226         $this->assertTrue(enrol_user_sees_own_courses());
228         $this->setUser($user2);
229         $this->assertTrue(enrol_user_sees_own_courses());
231         $this->setUser($user3);
232         $this->assertFalse(enrol_user_sees_own_courses());
234         $this->setUser($user4);
235         $this->assertFalse(enrol_user_sees_own_courses());
237         $this->setUser($user5);
238         $this->assertFalse(enrol_user_sees_own_courses());
240         $user1 = $DB->get_record('user', array('id'=>$user1->id));
241         $this->setUser($user1);
242         $reads = $DB->perf_get_reads();
243         $this->assertTrue(enrol_user_sees_own_courses());
244         $this->assertGreaterThan($reads, $DB->perf_get_reads());
246         $user1 = $DB->get_record('user', array('id'=>$user1->id));
247         $this->setUser($user1);
248         require_login($course3);
249         $reads = $DB->perf_get_reads();
250         $this->assertTrue(enrol_user_sees_own_courses());
251         $this->assertEquals($reads, $DB->perf_get_reads());
252     }
254     public function test_enrol_get_shared_courses() {
255         $this->resetAfterTest();
257         $user1 = $this->getDataGenerator()->create_user();
258         $user2 = $this->getDataGenerator()->create_user();
259         $user3 = $this->getDataGenerator()->create_user();
261         $course1 = $this->getDataGenerator()->create_course();
262         $this->getDataGenerator()->enrol_user($user1->id, $course1->id);
263         $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
265         $course2 = $this->getDataGenerator()->create_course();
266         $this->getDataGenerator()->enrol_user($user1->id, $course2->id);
268         // Test that user1 and user2 have courses in common.
269         $this->assertTrue(enrol_get_shared_courses($user1, $user2, false, true));
270         // Test that user1 and user3 have no courses in common.
271         $this->assertFalse(enrol_get_shared_courses($user1, $user3, false, true));
273         // Test retrieving the courses in common.
274         $sharedcourses = enrol_get_shared_courses($user1, $user2, true);
276         // Only should be one shared course.
277         $this->assertCount(1, $sharedcourses);
278         $sharedcourse = array_shift($sharedcourses);
279         // It should be course 1.
280         $this->assertEquals($sharedcourse->id, $course1->id);
281     }