Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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  namespace enrol_paypal;
  18  
  19  /**
  20   * paypal enrolment plugin tests.
  21   *
  22   * @package    enrol_paypal
  23   * @category   test
  24   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class paypal_test extends \advanced_testcase {
  28  
  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));
  34      }
  35  
  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));
  41      }
  42  
  43      public function test_basics() {
  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'));
  48      }
  49  
  50      public function test_sync_nothing() {
  51          $this->resetAfterTest();
  52  
  53          $this->enable_plugin();
  54          $paypalplugin = enrol_get_plugin('paypal');
  55  
  56          // Just make sure the sync does not throw any errors when nothing to do.
  57          $paypalplugin->sync(new \null_progress_trace());
  58      }
  59  
  60      public function test_expired() {
  61          global $DB;
  62          $this->resetAfterTest();
  63  
  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);
  69  
  70          $now = time();
  71          $trace = new \null_progress_trace();
  72          $this->enable_plugin();
  73  
  74  
  75          // Prepare some data.
  76  
  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);
  83  
  84          $user1 = $this->getDataGenerator()->create_user();
  85          $user2 = $this->getDataGenerator()->create_user();
  86          $user3 = $this->getDataGenerator()->create_user();
  87          $user4 = $this->getDataGenerator()->create_user();
  88  
  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);
  93  
  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));
 103  
 104          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 105  
 106          $manualplugin->enrol_user($maninstance1, $user3->id, $studentrole->id);
 107  
 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)));
 111  
 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);
 115  
 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);
 119  
 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);
 122  
 123          role_assign($managerrole->id, $user3->id, $context1->id);
 124  
 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)));
 130  
 131          // Execute tests.
 132  
 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'));
 138  
 139  
 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)));
 150  
 151  
 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)));
 168      }
 169  
 170      /**
 171       * Test for getting user enrolment actions.
 172       */
 173      public function test_get_user_enrolment_actions() {
 174          global $CFG, $PAGE;
 175          $this->resetAfterTest();
 176  
 177          // Set page URL to prevent debugging messages.
 178          $PAGE->set_url('/enrol/editinstance.php');
 179  
 180          $pluginname = 'paypal';
 181  
 182          // Only enable the paypal enrol plugin.
 183          $CFG->enrol_plugins_enabled = $pluginname;
 184  
 185          $generator = $this->getDataGenerator();
 186  
 187          // Get the enrol plugin.
 188          $plugin = enrol_get_plugin($pluginname);
 189  
 190          // Create a course.
 191          $course = $generator->create_course();
 192          // Enable this enrol plugin for the course.
 193          $plugin->add_instance($course);
 194  
 195          // Create a student.
 196          $student = $generator->create_user();
 197          // Enrol the student to the course.
 198          $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
 199  
 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);
 204  
 205          $ue = reset($userenrolments);
 206  
 207          // Login as admin to see all enrol actions.
 208          $this->setAdminUser();
 209          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
 210  
 211          // Paypal enrolment has 2 enrol actions for active users when logged in as admin: edit and unenrol.
 212          $this->assertCount(2, $actions);
 213  
 214          // Enrol actions when viewing as a teacher.
 215          // Create 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);
 224      }
 225  }