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/>.
17 namespace enrol_paypal
;
20 * paypal enrolment plugin tests.
22 * @package enrol_paypal
24 * @copyright 2012 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 class paypal_test
extends \advanced_testcase
{
29 protected function enable_plugin() {
30 $enabled = enrol_get_plugins(true);
31 $enabled['paypal'] = true;
32 $enabled = array_keys($enabled);
33 set_config('enrol_plugins_enabled', implode(',', $enabled));
36 protected function disable_plugin() {
37 $enabled = enrol_get_plugins(true);
38 unset($enabled['paypal']);
39 $enabled = array_keys($enabled);
40 set_config('enrol_plugins_enabled', implode(',', $enabled));
43 public function test_basics(): void
{
44 $this->assertFalse(enrol_is_enabled('paypal'));
45 $plugin = enrol_get_plugin('paypal');
46 $this->assertInstanceOf('enrol_paypal_plugin', $plugin);
47 $this->assertEquals(ENROL_EXT_REMOVED_SUSPENDNOROLES
, get_config('enrol_paypal', 'expiredaction'));
50 public function test_sync_nothing(): void
{
51 $this->resetAfterTest();
53 $this->enable_plugin();
54 $paypalplugin = enrol_get_plugin('paypal');
56 // Just make sure the sync does not throw any errors when nothing to do.
57 $paypalplugin->sync(new \null_progress_trace
());
60 public function test_expired(): void
{
62 $this->resetAfterTest();
64 /** @var \enrol_paypal_plugin $paypalplugin */
65 $paypalplugin = enrol_get_plugin('paypal');
66 /** @var \enrol_manual_plugin $manualplugin */
67 $manualplugin = enrol_get_plugin('manual');
68 $this->assertNotEmpty($manualplugin);
71 $trace = new \null_progress_trace
();
72 $this->enable_plugin();
77 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
78 $this->assertNotEmpty($studentrole);
79 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
80 $this->assertNotEmpty($teacherrole);
81 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
82 $this->assertNotEmpty($managerrole);
84 $user1 = $this->getDataGenerator()->create_user();
85 $user2 = $this->getDataGenerator()->create_user();
86 $user3 = $this->getDataGenerator()->create_user();
87 $user4 = $this->getDataGenerator()->create_user();
89 $course1 = $this->getDataGenerator()->create_course();
90 $course2 = $this->getDataGenerator()->create_course();
91 $context1 = \context_course
::instance($course1->id
);
92 $context2 = \context_course
::instance($course2->id
);
94 $data = array('roleid'=>$studentrole->id
, 'courseid'=>$course1->id
);
95 $id = $paypalplugin->add_instance($course1, $data);
96 $instance1 = $DB->get_record('enrol', array('id'=>$id));
97 $data = array('roleid'=>$studentrole->id
, 'courseid'=>$course2->id
);
98 $id = $paypalplugin->add_instance($course2, $data);
99 $instance2 = $DB->get_record('enrol', array('id'=>$id));
100 $data = array('roleid'=>$teacherrole->id
, 'courseid'=>$course2->id
);
101 $id = $paypalplugin->add_instance($course2, $data);
102 $instance3 = $DB->get_record('enrol', array('id'=>$id));
104 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course2->id
, 'enrol'=>'manual'), '*', MUST_EXIST
);
106 $manualplugin->enrol_user($maninstance1, $user3->id
, $studentrole->id
);
108 $this->assertEquals(1, $DB->count_records('user_enrolments'));
109 $this->assertEquals(1, $DB->count_records('role_assignments'));
110 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id
)));
112 $paypalplugin->enrol_user($instance1, $user1->id
, $studentrole->id
);
113 $paypalplugin->enrol_user($instance1, $user2->id
, $studentrole->id
);
114 $paypalplugin->enrol_user($instance1, $user3->id
, $studentrole->id
, 0, $now-60);
116 $paypalplugin->enrol_user($instance2, $user1->id
, $studentrole->id
, 0, 0);
117 $paypalplugin->enrol_user($instance2, $user2->id
, $studentrole->id
, 0, $now-60*60);
118 $paypalplugin->enrol_user($instance2, $user3->id
, $studentrole->id
, 0, $now+
60*60);
120 $paypalplugin->enrol_user($instance3, $user1->id
, $teacherrole->id
, $now-60*60*24*7, $now-60);
121 $paypalplugin->enrol_user($instance3, $user4->id
, $teacherrole->id
);
123 role_assign($managerrole->id
, $user3->id
, $context1->id
);
125 $this->assertEquals(9, $DB->count_records('user_enrolments'));
126 $this->assertEquals(9, $DB->count_records('role_assignments'));
127 $this->assertEquals(6, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id
)));
128 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id
)));
129 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id
)));
133 $paypalplugin->set_config('expiredaction', ENROL_EXT_REMOVED_KEEP
);
134 $code = $paypalplugin->sync($trace);
135 $this->assertSame(0, $code);
136 $this->assertEquals(9, $DB->count_records('user_enrolments'));
137 $this->assertEquals(9, $DB->count_records('role_assignments'));
140 $paypalplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES
);
141 $paypalplugin->sync($trace);
142 $this->assertEquals(9, $DB->count_records('user_enrolments'));
143 $this->assertEquals(6, $DB->count_records('role_assignments'));
144 $this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id
)));
145 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id
)));
146 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id
, 'userid'=>$user3->id
, 'roleid'=>$studentrole->id
)));
147 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id
, 'userid'=>$user2->id
, 'roleid'=>$studentrole->id
)));
148 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id
, 'userid'=>$user1->id
, 'roleid'=>$teacherrole->id
)));
149 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id
, 'userid'=>$user1->id
, 'roleid'=>$studentrole->id
)));
152 $paypalplugin->set_config('expiredaction', ENROL_EXT_REMOVED_UNENROL
);
153 role_assign($studentrole->id
, $user3->id
, $context1->id
);
154 role_assign($studentrole->id
, $user2->id
, $context2->id
);
155 role_assign($teacherrole->id
, $user1->id
, $context2->id
);
156 $this->assertEquals(9, $DB->count_records('user_enrolments'));
157 $this->assertEquals(9, $DB->count_records('role_assignments'));
158 $this->assertEquals(6, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id
)));
159 $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id
)));
160 $paypalplugin->sync($trace);
161 $this->assertEquals(6, $DB->count_records('user_enrolments'));
162 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id
, 'userid'=>$user3->id
)));
163 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance2->id
, 'userid'=>$user2->id
)));
164 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id
, 'userid'=>$user1->id
)));
165 $this->assertEquals(5, $DB->count_records('role_assignments'));
166 $this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id
)));
167 $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id
)));
171 * Test for getting user enrolment actions.
173 public function test_get_user_enrolment_actions(): void
{
175 $this->resetAfterTest();
177 // Set page URL to prevent debugging messages.
178 $PAGE->set_url('/enrol/editinstance.php');
180 $pluginname = 'paypal';
182 // Only enable the paypal enrol plugin.
183 $CFG->enrol_plugins_enabled
= $pluginname;
185 $generator = $this->getDataGenerator();
187 // Get the enrol plugin.
188 $plugin = enrol_get_plugin($pluginname);
191 $course = $generator->create_course();
192 // Enable this enrol plugin for the course.
193 $plugin->add_instance($course);
196 $student = $generator->create_user();
197 // Enrol the student to the course.
198 $generator->enrol_user($student->id
, $course->id
, 'student', $pluginname);
200 require_once($CFG->dirroot
. '/enrol/locallib.php');
201 $manager = new \
course_enrolment_manager($PAGE, $course);
202 $userenrolments = $manager->get_user_enrolments($student->id
);
203 $this->assertCount(1, $userenrolments);
205 $ue = reset($userenrolments);
207 // Login as admin to see all enrol actions.
208 $this->setAdminUser();
209 $actions = $plugin->get_user_enrolment_actions($manager, $ue);
211 // Paypal enrolment has 2 enrol actions for active users when logged in as admin: edit and unenrol.
212 $this->assertCount(2, $actions);
214 // Enrol actions when viewing as a teacher.
216 $teacher = $generator->create_user();
217 // Enrol the teacher to the course.
218 $generator->enrol_user($teacher->id
, $course->id
, 'editingteacher', $pluginname);
219 // Login as the teacher.
220 $this->setUser($teacher);
221 $actions = $plugin->get_user_enrolment_actions($manager, $ue);
222 // Teachers don't have the enrol/paypal:unenrol capability by default, but have enrol/paypal:manage.
223 $this->assertCount(1, $actions);