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.

Differences Between: [Versions 401 and 402] [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_meta\external;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  global $CFG;
  21  require_once($CFG->libdir . '/externallib.php');
  22  require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  23  
  24  /**
  25   * Tests for add_instances external class
  26   *
  27   * @package    enrol_meta
  28   * @group      enrol_meta
  29   * @category   test
  30   * @copyright  2021 WKS KV Bildung
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class add_instances_test extends \externallib_advanced_testcase {
  34  
  35      /**
  36       * Test setup
  37       */
  38      public function setUp(): void {
  39          $this->resetAfterTest();
  40          $this->setAdminUser();
  41      }
  42  
  43      /**
  44       * Test add_instances no instances.
  45       */
  46      public function test_add_instances_no_instances() {
  47          $this->expectException(\invalid_parameter_exception::class);
  48          add_instances::execute([]);
  49      }
  50  
  51      /**
  52       * Test add_instances missing courses.
  53       */
  54      public function test_add_instances_missing_courses() {
  55          $course = self::getDataGenerator()->create_course();
  56  
  57          // Missing meta course.
  58          try {
  59              add_instances::execute([['metacourseid' => 1000, 'courseid' => $course->id]]);
  60              $this->fail('Exception expected');
  61          } catch (\moodle_exception $e) {
  62              $this->assertStringContainsString(get_string('wsinvalidmetacourse', 'enrol_meta', 1000), $e->getMessage());
  63          }
  64  
  65          // Missing linked course.
  66          try {
  67              add_instances::execute([['metacourseid' => $course->id, 'courseid' => 1000]]);
  68              $this->fail('Exception expected');
  69          } catch (\moodle_exception $e) {
  70              $this->assertStringContainsString(get_string('wsinvalidcourse', 'enrol_meta', 1000), $e->getMessage());
  71          }
  72      }
  73  
  74      /**
  75       * Test add_instances missing capabilities.
  76       */
  77      public function test_add_instances_missing_capabilities() {
  78          $metacourse = self::getDataGenerator()->create_course();
  79          $course = self::getDataGenerator()->create_course();
  80          $user = self::getDataGenerator()->create_user();
  81          $this::setUser($user);
  82  
  83          // Missing rights in meta course.
  84          try {
  85              add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
  86              $this->fail('Exception expected');
  87          } catch (\moodle_exception $e) {
  88              $this->assertStringContainsString(get_string('wsinvalidmetacourse', 'enrol_meta', $metacourse->id), $e->getMessage());
  89          }
  90  
  91          // Add rights for metacourse.
  92          $metacontext = \context_course::instance($metacourse->id);
  93          $roleid = $this->assignUserCapability('enrol/meta:config', $metacontext->id);
  94          $this->assignUserCapability('moodle/course:view', $metacontext->id, $roleid);
  95          $this->assignUserCapability('moodle/course:enrolconfig', $metacontext->id, $roleid);
  96  
  97          // Missing rights for linked course.
  98          try {
  99              add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
 100              $this->fail('Exception expected');
 101          } catch (\moodle_exception $e) {
 102              $this->assertStringContainsString(get_string('wsinvalidcourse', 'enrol_meta', $course->id), $e->getMessage());
 103          }
 104  
 105          // Add rights for linked course.
 106          $context = \context_course::instance($course->id);
 107          $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
 108          $this->assignUserCapability('enrol/meta:selectaslinked', $context->id, $roleid);
 109  
 110          $result = add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
 111          $this->assertNotEmpty($result);
 112      }
 113  
 114      /**
 115       * Test add_instances.
 116       */
 117      public function test_add_instances() {
 118          global $DB;
 119          $metacourse = self::getDataGenerator()->create_course();
 120          $course = self::getDataGenerator()->create_course();
 121  
 122          // Sanity check.
 123          $enrolrecords = $DB->count_records('enrol',
 124              ['enrol' => 'meta', 'courseid' => $metacourse->id, 'customint1' => $course->id]);
 125          $this->assertEquals(0, $enrolrecords);
 126  
 127          // Add instance.
 128          $result = add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
 129          $result = \external_api::clean_returnvalue(add_instances::execute_returns(), $result);
 130          $this->assertEquals($result[0]['metacourseid'], $metacourse->id);
 131          $this->assertEquals($result[0]['courseid'], $course->id);
 132          $this->assertEquals($result[0]['status'], 1);
 133  
 134          // Check instance was created.
 135          $enrolrecords = $DB->count_records('enrol',
 136              ['enrol' => 'meta', 'courseid' => $result[0]['metacourseid'], 'customint1' => $result[0]['courseid']]);
 137          $this->assertEquals(1, $enrolrecords);
 138  
 139          // Add same instance.
 140          $result = add_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
 141          $result = \external_api::clean_returnvalue(add_instances::execute_returns(), $result);
 142          $this->assertEquals($result[0]['metacourseid'], $metacourse->id);
 143          $this->assertEquals($result[0]['courseid'], $course->id);
 144          $this->assertEquals($result[0]['status'], 0);
 145  
 146          // Check no new instance was created.
 147          $enrolrecords = $DB->count_records('enrol',
 148              ['enrol' => 'meta', 'courseid' => $result[0]['metacourseid'], 'customint1' => $result[0]['courseid']]);
 149          $this->assertEquals(1, $enrolrecords);
 150      }
 151  }