MDL-35064 improve enrol test comments
[moodle.git] / enrol / manual / tests / lib_test.php
CommitLineData
84bc3773
PS
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 * Manual enrolment tests.
19 *
20 * @package enrol_manual
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 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * Manual enrolment tests.
31 *
32 * @package enrol_manual
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 */
37class enrol_manual_lib_testcase extends advanced_testcase {
38 /**
39 * Test enrol migration function used when uninstalling enrol plugins.
40 */
41 public function test_migrate_plugin_enrolments() {
42 global $DB, $CFG;
43 require_once($CFG->dirroot.'/enrol/manual/locallib.php');
44
45 $this->resetAfterTest();
46
47 $manplugin = enrol_get_plugin('manual');
48
49 // Setup a few courses and users.
50
51 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
52 $this->assertNotEmpty($studentrole);
53 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
54 $this->assertNotEmpty($teacherrole);
55
56 $course1 = $this->getDataGenerator()->create_course();
57 $course2 = $this->getDataGenerator()->create_course();
58 $course3 = $this->getDataGenerator()->create_course();
59 $course4 = $this->getDataGenerator()->create_course();
60 $course5 = $this->getDataGenerator()->create_course();
61
62 $context1 = context_course::instance($course1->id);
63 $context2 = context_course::instance($course2->id);
64 $context3 = context_course::instance($course3->id);
65 $context4 = context_course::instance($course4->id);
66
67 $user1 = $this->getDataGenerator()->create_user();
68 $user2 = $this->getDataGenerator()->create_user();
69 $user3 = $this->getDataGenerator()->create_user();
70 $user4 = $this->getDataGenerator()->create_user();
71
72 // We expect manual, self and guest instances to be created by default.
73
74 $this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'manual')));
75 $this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'self')));
76 $this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'guest')));
77 $this->assertEquals(15, $DB->count_records('enrol', array()));
78
79 $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
80
81 // Enrol some users to manual instances.
82
83 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
84 $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id));
85 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
86 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
87 $DB->delete_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'));
88 $DB->delete_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'));
89 $DB->delete_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'manual'));
90
91 $manplugin->enrol_user($maninstance1, $user1->id, $studentrole->id);
92 $manplugin->enrol_user($maninstance1, $user2->id, $studentrole->id);
93 $manplugin->enrol_user($maninstance1, $user3->id, $teacherrole->id);
94 $manplugin->enrol_user($maninstance2, $user3->id, $teacherrole->id);
95
96 $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
97
98 // Set up some bogus enrol plugin instances and enrolments.
99
100 $xxxinstance1 = $DB->insert_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_ENABLED));
101 $xxxinstance1 = $DB->get_record('enrol', array('id'=>$xxxinstance1));
102 $xxxinstance3 = $DB->insert_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_DISABLED));
103 $xxxinstance3 = $DB->get_record('enrol', array('id'=>$xxxinstance3));
104 $xxxinstance4 = $DB->insert_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_ENABLED));
105 $xxxinstance4 = $DB->get_record('enrol', array('id'=>$xxxinstance4));
106 $xxxinstance4b = $DB->insert_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_DISABLED));
107 $xxxinstance4b = $DB->get_record('enrol', array('id'=>$xxxinstance4b));
108
109
110 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance1->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_SUSPENDED));
111 role_assign($studentrole->id, $user1->id, $context1->id, 'enrol_xxx', $xxxinstance1->id);
112 role_assign($teacherrole->id, $user1->id, $context1->id, 'enrol_xxx', $xxxinstance1->id);
113 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance1->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE));
114 role_assign($studentrole->id, $user4->id, $context1->id, 'enrol_xxx', $xxxinstance1->id);
115 $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance1->id)));
116 $this->assertEquals(6, $DB->count_records('role_assignments', array('contextid'=>$context1->id)));
117
118
119 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance3->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE));
120 role_assign($studentrole->id, $user1->id, $context3->id, 'enrol_xxx', $xxxinstance3->id);
121 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance3->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_SUSPENDED));
122 $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance3->id)));
123 $this->assertEquals(1, $DB->count_records('role_assignments', array('contextid'=>$context3->id)));
124
125 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE));
126 role_assign($studentrole->id, $user1->id, $context4->id, 'enrol_xxx', $xxxinstance4->id);
127 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE));
128 role_assign($studentrole->id, $user2->id, $context4->id, 'enrol_xxx', $xxxinstance4->id);
129 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4b->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_SUSPENDED));
130 role_assign($teacherrole->id, $user1->id, $context4->id, 'enrol_xxx', $xxxinstance4b->id);
131 $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4b->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE));
132 role_assign($teacherrole->id, $user4->id, $context4->id, 'enrol_xxx', $xxxinstance4b->id);
133 $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4->id)));
134 $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4b->id)));
135 $this->assertEquals(4, $DB->count_records('role_assignments', array('contextid'=>$context4->id)));
136
137 // Finally do the migration.
138
139 enrol_manual_migrate_plugin_enrolments('xxx');
140
141 // Verify results.
142
143 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual')));
144 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course1->id, 'enrol'=>'xxx')));
145 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
146 $this->assertEquals(ENROL_INSTANCE_DISABLED, $maninstance1->status);
147 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE)));
148 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE)));
149 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_ACTIVE)));
150 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE)));
151 $this->assertEquals(4, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance1->id)));
152 $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance1->id)));
153 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>$DB->sql_empty(), 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id)));
154 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>$DB->sql_empty(), 'userid'=>$user1->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context1->id)));
155 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>$DB->sql_empty(), 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id)));
156 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>$DB->sql_empty(), 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context1->id)));
157 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>$DB->sql_empty(), 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id)));
158 $this->assertEquals(5, $DB->count_records('role_assignments', array('contextid'=>$context1->id)));
159
160
161 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual')));
162 $this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course2->id, 'enrol'=>'xxx')));
163 $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
164 $this->assertEquals(ENROL_INSTANCE_ENABLED, $maninstance2->status);
165
166
167 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual')));
168 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'xxx')));
169 $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
170 $this->assertEquals(ENROL_INSTANCE_DISABLED, $maninstance3->status);
171 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE)));
172 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_SUSPENDED)));
173 $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance3->id)));
174 $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance3->id)));
175 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>$DB->sql_empty(), 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context3->id)));
176 $this->assertEquals(1, $DB->count_records('role_assignments', array('contextid'=>$context3->id)));
177
178
179 $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual')));
180 $this->assertEquals(2, $DB->count_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx')));
181 $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
182 $this->assertEquals(ENROL_INSTANCE_ENABLED, $maninstance4->status);
183 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE)));
184 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE)));
185 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_SUSPENDED)));
186 $this->assertEquals(3, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance4->id)));
187 $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4->id)));
188 $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4b->id)));
189 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>$DB->sql_empty(), 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context4->id)));
190 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>$DB->sql_empty(), 'userid'=>$user1->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context4->id)));
191 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>$DB->sql_empty(), 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'contextid'=>$context4->id)));
192 $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>$DB->sql_empty(), 'userid'=>$user4->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context4->id)));
193 $this->assertEquals(4, $DB->count_records('role_assignments', array('contextid'=>$context4->id)));
194
195
196 $this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'manual')));
197 $this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'xxx')));
198
5d605db1 199 // Make sure wrong params do not produce errors or notices.
84bc3773
PS
200
201 enrol_manual_migrate_plugin_enrolments('manual');
202 enrol_manual_migrate_plugin_enrolments('yyyy');
203 }
204}