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 * paypal enrolment plugin tests.
20 * @package enrol_paypal
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 class enrol_paypal_testcase
extends advanced_testcase
{
31 protected function enable_plugin() {
32 $enabled = enrol_get_plugins(true);
33 $enabled['paypal'] = true;
34 $enabled = array_keys($enabled);
35 set_config('enrol_plugins_enabled', implode(',', $enabled));
38 protected function disable_plugin() {
39 $enabled = enrol_get_plugins(true);
40 unset($enabled['paypal']);
41 $enabled = array_keys($enabled);
42 set_config('enrol_plugins_enabled', implode(',', $enabled));
45 public function test_basics() {
46 $this->assertFalse(enrol_is_enabled('paypal'));
47 $plugin = enrol_get_plugin('paypal');
48 $this->assertInstanceOf('enrol_paypal_plugin', $plugin);
49 $this->assertEquals(ENROL_EXT_REMOVED_SUSPENDNOROLES
, get_config('enrol_paypal', 'expiredaction'));
52 public function test_sync_nothing() {
53 $this->resetAfterTest();
55 $this->enable_plugin();
56 $paypalplugin = enrol_get_plugin('paypal');
58 // Just make sure the sync does not throw any errors when nothing to do.
59 $paypalplugin->sync(new null_progress_trace());
62 public function test_expired() {
64 $this->resetAfterTest();
66 /** @var enrol_paypal_plugin $paypalplugin */
67 $paypalplugin = enrol_get_plugin('paypal');
68 /** @var enrol_manual_plugin $manualplugin */
69 $manualplugin = enrol_get_plugin('manual');
70 $this->assertNotEmpty($manualplugin);
73 $trace = new null_progress_trace();
74 $this->enable_plugin();
79 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
80 $this->assertNotEmpty($studentrole);
81 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
82 $this->assertNotEmpty($teacherrole);
83 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
84 $this->assertNotEmpty($managerrole);
86 $user1 = $this->getDataGenerator()->create_user();
87 $user2 = $this->getDataGenerator()->create_user();
88 $user3 = $this->getDataGenerator()->create_user();
89 $user4 = $this->getDataGenerator()->create_user();
91 $course1 = $this->getDataGenerator()->create_course();
92 $course2 = $this->getDataGenerator()->create_course();
93 $context1 = context_course
::instance($course1->id
);
94 $context2 = context_course
::instance($course2->id
);
96 $data = array('roleid'=>$studentrole->id
, 'courseid'=>$course1->id
);
97 $id = $paypalplugin->add_instance($course1, $data);
98 $instance1 = $DB->get_record('enrol', array('id'=>$id));
99 $data = array('roleid'=>$studentrole->id
, 'courseid'=>$course2->id
);
100 $id = $paypalplugin->add_instance($course2, $data);
101 $instance2 = $DB->get_record('enrol', array('id'=>$id));
102 $data = array('roleid'=>$teacherrole->id
, 'courseid'=>$course2->id
);
103 $id = $paypalplugin->add_instance($course2, $data);
104 $instance3 = $DB->get_record('enrol', array('id'=>$id));
106 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course2->id
, 'enrol'=>'manual'), '*', MUST_EXIST
);
108 $manualplugin->enrol_user($maninstance1, $user3->id
, $studentrole->id
);
110 $this->assertEquals(1, $DB->count_records('user_enrolments'));
111 $this->assertEquals(1, $DB->count_records('role_assignments'));
112 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id
)));
114 $paypalplugin->enrol_user($instance1, $user1->id
, $studentrole->id
);
115 $paypalplugin->enrol_user($instance1, $user2->id
, $studentrole->id
);
116 $paypalplugin->enrol_user($instance1, $user3->id
, $studentrole->id
, 0, $now-60);
118 $paypalplugin->enrol_user($instance2, $user1->id
, $studentrole->id
, 0, 0);
119 $paypalplugin->enrol_user($instance2, $user2->id
, $studentrole->id
, 0, $now-60*60);
120 $paypalplugin->enrol_user($instance2, $user3->id
, $studentrole->id
, 0, $now+
60*60);
122 $paypalplugin->enrol_user($instance3, $user1->id
, $teacherrole->id
, $now-60*60*24*7, $now-60);
123 $paypalplugin->enrol_user($instance3, $user4->id
, $teacherrole->id
);
125 role_assign($managerrole->id
, $user3->id
, $context1->id
);
127 $this->assertEquals(9, $DB->count_records('user_enrolments'));
128 $this->assertEquals(9, $DB->count_records('role_assignments'));
129 $this->assertEquals(6, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id
)));
130 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id
)));
131 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id
)));
135 $paypalplugin->set_config('expiredaction', ENROL_EXT_REMOVED_KEEP
);
136 $code = $paypalplugin->sync($trace);
137 $this->assertSame(0, $code);
138 $this->assertEquals(9, $DB->count_records('user_enrolments'));
139 $this->assertEquals(9, $DB->count_records('role_assignments'));
142 $paypalplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES
);
143 $paypalplugin->sync($trace);
144 $this->assertEquals(9, $DB->count_records('user_enrolments'));
145 $this->assertEquals(6, $DB->count_records('role_assignments'));
146 $this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id
)));
147 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id
)));
148 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id
, 'userid'=>$user3->id
, 'roleid'=>$studentrole->id
)));
149 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id
, 'userid'=>$user2->id
, 'roleid'=>$studentrole->id
)));
150 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id
, 'userid'=>$user1->id
, 'roleid'=>$teacherrole->id
)));
151 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id
, 'userid'=>$user1->id
, 'roleid'=>$studentrole->id
)));
154 $paypalplugin->set_config('expiredaction', ENROL_EXT_REMOVED_UNENROL
);
155 role_assign($studentrole->id
, $user3->id
, $context1->id
);
156 role_assign($studentrole->id
, $user2->id
, $context2->id
);
157 role_assign($teacherrole->id
, $user1->id
, $context2->id
);
158 $this->assertEquals(9, $DB->count_records('user_enrolments'));
159 $this->assertEquals(9, $DB->count_records('role_assignments'));
160 $this->assertEquals(6, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id
)));
161 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id
)));
162 $paypalplugin->sync($trace);
163 $this->assertEquals(6, $DB->count_records('user_enrolments'));
164 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id
, 'userid'=>$user3->id
)));
165 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance2->id
, 'userid'=>$user2->id
)));
166 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id
, 'userid'=>$user1->id
)));
167 $this->assertEquals(5, $DB->count_records('role_assignments'));
168 $this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id
)));
169 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id
)));
173 * Test for getting user enrolment actions.
175 public function test_get_user_enrolment_actions() {
177 $this->resetAfterTest();
179 // Set page URL to prevent debugging messages.
180 $PAGE->set_url('/enrol/editinstance.php');
182 $pluginname = 'paypal';
184 // Only enable the paypal enrol plugin.
185 $CFG->enrol_plugins_enabled
= $pluginname;
187 $generator = $this->getDataGenerator();
189 // Get the enrol plugin.
190 $plugin = enrol_get_plugin($pluginname);
193 $course = $generator->create_course();
194 // Enable this enrol plugin for the course.
195 $plugin->add_instance($course);
198 $student = $generator->create_user();
199 // Enrol the student to the course.
200 $generator->enrol_user($student->id
, $course->id
, 'student', $pluginname);
202 require_once($CFG->dirroot
. '/enrol/locallib.php');
203 $manager = new course_enrolment_manager($PAGE, $course);
204 $userenrolments = $manager->get_user_enrolments($student->id
);
205 $this->assertCount(1, $userenrolments);
207 $ue = reset($userenrolments);
209 // Login as admin to see all enrol actions.
210 $this->setAdminUser();
211 $actions = $plugin->get_user_enrolment_actions($manager, $ue);
213 // Paypal enrolment has 2 enrol actions for active users when logged in as admin: edit and unenrol.
214 $this->assertCount(2, $actions);
216 // Enrol actions when viewing as a teacher.
218 $teacher = $generator->create_user();
219 // Enrol the teacher to the course.
220 $generator->enrol_user($teacher->id
, $course->id
, 'editingteacher', $pluginname);
221 // Login as the teacher.
222 $this->setUser($teacher);
223 $actions = $plugin->get_user_enrolment_actions($manager, $ue);
224 // Teachers don't have the enrol/paypal:unenrol capability by default, but have enrol/paypal:manage.
225 $this->assertCount(1, $actions);