Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 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 delete_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 delete_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 delete_instances no instances.
  45       */
  46      public function test_delete_instances_no_instances() {
  47          $this->expectException(\invalid_parameter_exception::class);
  48          delete_instances::execute([]);
  49      }
  50  
  51      /**
  52       * Test delete_instances missing courses.
  53       */
  54      public function test_delete_instances_missing_courses() {
  55          $course = self::getDataGenerator()->create_course();
  56  
  57          // Missing meta course.
  58          try {
  59              delete_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              delete_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 delete_instances missing capabilities.
  76       */
  77      public function test_delete_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              delete_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          $result = delete_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
  98          $this->assertNotEmpty($result);
  99      }
 100  
 101      /**
 102       * Test delete_instances.
 103       */
 104      public function test_delete_instances() {
 105          global $DB;
 106          $metacourse = self::getDataGenerator()->create_course();
 107          $course = self::getDataGenerator()->create_course();
 108  
 109          // Create instance.
 110          $enrolplugin = enrol_get_plugin('meta');
 111          $fields = [
 112              'customint1' => $course->id,
 113              'customint2' => 0,
 114          ];
 115          $enrolplugin->add_instance($metacourse, $fields);
 116  
 117          // Sanity check.
 118          $enrolrecords = $DB->count_records('enrol',
 119              ['enrol' => 'meta', 'courseid' => $metacourse->id, 'customint1' => $course->id]);
 120          $this->assertEquals(1, $enrolrecords);
 121  
 122          // Delete instance.
 123          $result = delete_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
 124          $result = \external_api::clean_returnvalue(add_instances::execute_returns(), $result);
 125          $this->assertEquals($result[0]['metacourseid'], $metacourse->id);
 126          $this->assertEquals($result[0]['courseid'], $course->id);
 127          $this->assertEquals($result[0]['status'], 1);
 128  
 129          // Check instance was deleted.
 130          $enrolrecords = $DB->count_records('enrol',
 131              ['enrol' => 'meta', 'courseid' => $result[0]['metacourseid'], 'customint1' => $result[0]['courseid']]);
 132          $this->assertEquals(0, $enrolrecords);
 133  
 134          // Delete same instance.
 135          $result = delete_instances::execute([['metacourseid' => $metacourse->id, 'courseid' => $course->id]]);
 136          $result = \external_api::clean_returnvalue(add_instances::execute_returns(), $result);
 137          $this->assertEquals($result[0]['metacourseid'], $metacourse->id);
 138          $this->assertEquals($result[0]['courseid'], $course->id);
 139          $this->assertEquals($result[0]['status'], 0);
 140      }
 141  }