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   * Tests for the \core_course\task\course_delete_modules class.
  19   *
  20   * @package    core
  21   * @subpackage course
  22   * @copyright  2021 Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace core_course;
  26  
  27  /**
  28   * Tests for the \core_course\task\course_delete_modules class.
  29   *
  30   * @package    core
  31   * @subpackage course
  32   * @copyright  2021 Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class course_delete_modules_test extends \advanced_testcase {
  36  
  37      /**
  38       * Test to have a no message for usual process.
  39       */
  40      public function test_delete_module_execution() {
  41          $this->resetAfterTest();
  42  
  43          // Generate test data.
  44          $generator = $this->getDataGenerator();
  45          $user = $generator->create_user();
  46          $course = $generator->create_course();
  47          $assign = $generator->create_module('assign', ['course' => $course]);
  48          $assigncm = get_coursemodule_from_id('assign', $assign->cmid);
  49  
  50          // The module exists in the course.
  51          $coursedmodules = get_course_mods($course->id);
  52          $this->assertCount(1, $coursedmodules);
  53  
  54          // Execute the task.
  55          $removaltask = new \core_course\task\course_delete_modules();
  56          $data = [
  57              'cms' => [$assigncm],
  58              'userid' => $user->id,
  59              'realuserid' => $user->id
  60          ];
  61          $removaltask->set_custom_data($data);
  62          $removaltask->execute();
  63  
  64          // The module has deleted from the course.
  65          $coursedmodules = get_course_mods($course->id);
  66          $this->assertCount(0, $coursedmodules);
  67  
  68      }
  69  
  70      /**
  71       * Test to have a message in the exception.
  72       */
  73      public function test_delete_module_exception() {
  74          global $DB;
  75          $this->resetAfterTest();
  76  
  77          // Generate test data.
  78          $generator = $this->getDataGenerator();
  79          $user = $generator->create_user();
  80          $course = $generator->create_course();
  81          $assign = $generator->create_module('assign', ['course' => $course]);
  82          $assigncm = get_coursemodule_from_id('assign', $assign->cmid);
  83  
  84          // Modify module name to make an exception in the course_delete_modules task.
  85          $module = $DB->get_record('modules', ['id' => $assigncm->module], 'id, name', MUST_EXIST);
  86          $module->name = 'TestModuleToDelete';
  87          $DB->update_record('modules', $module);
  88  
  89          // Execute the task.
  90          $removaltask = new \core_course\task\course_delete_modules();
  91          $data = [
  92              'cms' => [$assigncm],
  93              'userid' => $user->id,
  94              'realuserid' => $user->id
  95          ];
  96          $removaltask->set_custom_data($data);
  97          try {
  98              $removaltask->execute();
  99          } catch (\coding_exception $e) {
 100              // Assert exception.
 101              $this->assertInstanceOf(\coding_exception::class, $e);
 102              $errormsg = str_replace('\\', '/', $e->getMessage()); // Normalise dir separator.
 103              $this->assertStringContainsString('cannotdeletemodulemissinglib', $errormsg);
 104              $this->assertStringContainsString('course/lib.php', $errormsg);
 105              $this->assertStringContainsString('mod/TestModuleToDelete/lib.php is missing', $errormsg);
 106              // Get line numbers array which contains the exception name.
 107              $lines = array_keys(preg_grep("/cannotdeletemodulemissinglib/", file('course/lib.php')));
 108              // Increase 1 to keys to convert to actual line number.
 109              $lines = array_map(function($key) {
 110                  return ++$key;
 111              }, $lines);
 112              $regex = "/(\(" . implode('\))|(\(', $lines) . "\))/";
 113              // Assert the error message has correct line number.
 114              $this->assertMatchesRegularExpression($regex, $errormsg);
 115          }
 116      }
 117  }