Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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   * Unit tests for the enrol_fee's payment subsystem callback implementation.
  19   *
  20   * @package    enrol_fee
  21   * @category   test
  22   * @copyright  2021 Shamim Rezaie <shamim@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace enrol_fee\payment;
  27  
  28  /**
  29   * Unit tests for the enrol_fee's payment subsystem callback implementation.
  30   *
  31   * @coversDefaultClass \enrol_fee\payment\service_provider
  32   */
  33  class service_provider_test extends \advanced_testcase {
  34  
  35      /**
  36       * Test for service_provider::get_payable().
  37       *
  38       * @covers ::get_payable
  39       */
  40      public function test_get_payable() {
  41          global $DB;
  42          $this->resetAfterTest();
  43  
  44          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
  45          $feeplugin = enrol_get_plugin('fee');
  46          $generator = $this->getDataGenerator();
  47          $account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']);
  48          $course = $generator->create_course();
  49  
  50          $data = [
  51              'courseid' => $course->id,
  52              'customint1' => $account->get('id'),
  53              'cost' => 250,
  54              'currency' => 'USD',
  55              'roleid' => $studentrole->id,
  56          ];
  57          $id = $feeplugin->add_instance($course, $data);
  58  
  59          $payable = service_provider::get_payable('fee', $id);
  60  
  61          $this->assertEquals($account->get('id'), $payable->get_account_id());
  62          $this->assertEquals(250, $payable->get_amount());
  63          $this->assertEquals('USD', $payable->get_currency());
  64      }
  65  
  66      /**
  67       * Test for service_provider::get_success_url().
  68       *
  69       * @covers ::get_success_url
  70       */
  71      public function test_get_success_url() {
  72          global $CFG, $DB;
  73          $this->resetAfterTest();
  74  
  75          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
  76          $feeplugin = enrol_get_plugin('fee');
  77          $generator = $this->getDataGenerator();
  78          $account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']);
  79          $course = $generator->create_course();
  80  
  81          $data = [
  82              'courseid' => $course->id,
  83              'customint1' => $account->get('id'),
  84              'cost' => 250,
  85              'currency' => 'USD',
  86              'roleid' => $studentrole->id,
  87          ];
  88          $id = $feeplugin->add_instance($course, $data);
  89  
  90          $successurl = service_provider::get_success_url('fee', $id);
  91          $this->assertEquals(
  92              $CFG->wwwroot . '/course/view.php?id=' . $course->id,
  93              $successurl->out(false)
  94          );
  95      }
  96  
  97      /**
  98       * Test for service_provider::deliver_order().
  99       *
 100       * @covers ::deliver_order
 101       */
 102      public function test_deliver_order() {
 103          global $DB;
 104          $this->resetAfterTest();
 105  
 106          $studentrole = $DB->get_record('role', ['shortname' => 'student']);
 107          $feeplugin = enrol_get_plugin('fee');
 108          $generator = $this->getDataGenerator();
 109          $account = $generator->get_plugin_generator('core_payment')->create_payment_account(['gateways' => 'paypal']);
 110          $course = $generator->create_course();
 111          $context = \context_course::instance($course->id);
 112          $user = $generator->create_user();
 113  
 114          $data = [
 115              'courseid' => $course->id,
 116              'customint1' => $account->get('id'),
 117              'cost' => 250,
 118              'currency' => 'USD',
 119              'roleid' => $studentrole->id,
 120          ];
 121          $id = $feeplugin->add_instance($course, $data);
 122  
 123          $paymentid = $generator->get_plugin_generator('core_payment')->create_payment([
 124              'accountid' => $account->get('id'),
 125              'amount' => 10,
 126              'userid' => $user->id
 127          ]);
 128  
 129          service_provider::deliver_order('fee', $id, $paymentid, $user->id);
 130          $this->assertTrue(is_enrolled($context, $user));
 131          $this->assertTrue(user_has_role_assignment($user->id, $studentrole->id, $context->id));
 132      }
 133  }