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