Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * paypal enrolment plugin tests.
  19   *
  20   * @package    enrol_paypal
  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  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  class enrol_paypal_testcase extends advanced_testcase {
  30  
  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));
  36      }
  37  
  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));
  43      }
  44  
  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'));
  50      }
  51  
  52      public function test_sync_nothing() {
  53          $this->resetAfterTest();
  54  
  55          $this->enable_plugin();
  56          $paypalplugin = enrol_get_plugin('paypal');
  57  
  58          // Just make sure the sync does not throw any errors when nothing to do.
  59          $paypalplugin->sync(new null_progress_trace());
  60      }
  61  
  62      public function test_expired() {
  63          global $DB;
  64          $this->resetAfterTest();
  65  
  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);
  71  
  72          $now = time();
  73          $trace = new null_progress_trace();
  74          $this->enable_plugin();
  75  
  76  
  77          // Prepare some data.
  78  
  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);
  85  
  86          $user1 = $this->getDataGenerator()->create_user();
  87          $user2 = $this->getDataGenerator()->create_user();
  88          $user3 = $this->getDataGenerator()->create_user();
  89          $user4 = $this->getDataGenerator()->create_user();
  90  
  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);
  95  
  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));
 105  
 106          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 107  
 108          $manualplugin->enrol_user($maninstance1, $user3->id, $studentrole->id);
 109  
 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)));
 113  
 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);
 117  
 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);
 121  
 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);
 124  
 125          role_assign($managerrole->id, $user3->id, $context1->id);
 126  
 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)));
 132  
 133          // Execute tests.
 134  
 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'));
 140  
 141  
 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)));
 152  
 153  
 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)));
 170      }
 171  
 172      /**
 173       * Test for getting user enrolment actions.
 174       */
 175      public function test_get_user_enrolment_actions() {
 176          global $CFG, $PAGE;
 177          $this->resetAfterTest();
 178  
 179          // Set page URL to prevent debugging messages.
 180          $PAGE->set_url('/enrol/editinstance.php');
 181  
 182          $pluginname = 'paypal';
 183  
 184          // Only enable the paypal enrol plugin.
 185          $CFG->enrol_plugins_enabled = $pluginname;
 186  
 187          $generator = $this->getDataGenerator();
 188  
 189          // Get the enrol plugin.
 190          $plugin = enrol_get_plugin($pluginname);
 191  
 192          // Create a course.
 193          $course = $generator->create_course();
 194          // Enable this enrol plugin for the course.
 195          $plugin->add_instance($course);
 196  
 197          // Create a student.
 198          $student = $generator->create_user();
 199          // Enrol the student to the course.
 200          $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
 201  
 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);
 206  
 207          $ue = reset($userenrolments);
 208  
 209          // Login as admin to see all enrol actions.
 210          $this->setAdminUser();
 211          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
 212  
 213          // Paypal enrolment has 2 enrol actions for active users when logged in as admin: edit and unenrol.
 214          $this->assertCount(2, $actions);
 215  
 216          // Enrol actions when viewing as a teacher.
 217          // Create 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);
 226      }
 227  }