| 1 | 
           efrain | 
           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
  | 
        
        
           | 1441 | 
           ariadna | 
           24 | 
            * @covers \core_course\task\course_delete_modules
  | 
        
        
           | 1 | 
           efrain | 
           25 | 
            */
  | 
        
        
            | 
            | 
           26 | 
           namespace core_course;
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           /**
  | 
        
        
            | 
            | 
           29 | 
            * Tests for the \core_course\task\course_delete_modules class.
  | 
        
        
            | 
            | 
           30 | 
            *
  | 
        
        
            | 
            | 
           31 | 
            * @package    core
  | 
        
        
            | 
            | 
           32 | 
            * @subpackage course
  | 
        
        
            | 
            | 
           33 | 
            * @copyright  2021 Tomo Tsuyuki <tomotsuyuki@catalyst-au.net>
  | 
        
        
            | 
            | 
           34 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           35 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           36 | 
           final class course_delete_modules_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
               /**
  | 
        
        
            | 
            | 
           39 | 
                * Test to have a no message for usual process.
  | 
        
        
            | 
            | 
           40 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           41 | 
               public function test_delete_module_execution(): void {
  | 
        
        
           | 1 | 
           efrain | 
           42 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
                   // Generate test data.
  | 
        
        
            | 
            | 
           45 | 
                   $generator = $this->getDataGenerator();
  | 
        
        
            | 
            | 
           46 | 
                   $user = $generator->create_user();
  | 
        
        
            | 
            | 
           47 | 
                   $course = $generator->create_course();
  | 
        
        
            | 
            | 
           48 | 
                   $assign = $generator->create_module('assign', ['course' => $course]);
  | 
        
        
            | 
            | 
           49 | 
                   $assigncm = get_coursemodule_from_id('assign', $assign->cmid);
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
                   // The module exists in the course.
  | 
        
        
            | 
            | 
           52 | 
                   $coursedmodules = get_course_mods($course->id);
  | 
        
        
            | 
            | 
           53 | 
                   $this->assertCount(1, $coursedmodules);
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
                   // Execute the task.
  | 
        
        
            | 
            | 
           56 | 
                   $removaltask = new \core_course\task\course_delete_modules();
  | 
        
        
            | 
            | 
           57 | 
                   $data = [
  | 
        
        
            | 
            | 
           58 | 
                       'cms' => [$assigncm],
  | 
        
        
            | 
            | 
           59 | 
                       'userid' => $user->id,
  | 
        
        
            | 
            | 
           60 | 
                       'realuserid' => $user->id,
  | 
        
        
            | 
            | 
           61 | 
                   ];
  | 
        
        
            | 
            | 
           62 | 
                   $removaltask->set_custom_data($data);
  | 
        
        
            | 
            | 
           63 | 
                   $removaltask->execute();
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
                   // The module has deleted from the course.
  | 
        
        
            | 
            | 
           66 | 
                   $coursedmodules = get_course_mods($course->id);
  | 
        
        
            | 
            | 
           67 | 
                   $this->assertCount(0, $coursedmodules);
  | 
        
        
            | 
            | 
           68 | 
               }
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
               /**
  | 
        
        
            | 
            | 
           71 | 
                * Test with failed and successful cms
  | 
        
        
            | 
            | 
           72 | 
                */
  | 
        
        
            | 
            | 
           73 | 
               public function test_delete_module_exception(): void {
  | 
        
        
            | 
            | 
           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 | 
                   // Generate successful test data.
  | 
        
        
            | 
            | 
           90 | 
                   $quiz1 = $generator->create_module('quiz', ['course' => $course]);
  | 
        
        
            | 
            | 
           91 | 
                   $quizcm1 = get_coursemodule_from_id('quiz', $quiz1->cmid);
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
            | 
            | 
           93 | 
                   $quiz2 = $generator->create_module('quiz', ['course' => $course]);
  | 
        
        
            | 
            | 
           94 | 
                   $quizcm2 = get_coursemodule_from_id('quiz', $quiz2->cmid);
  | 
        
        
            | 
            | 
           95 | 
              | 
        
        
            | 
            | 
           96 | 
                   // Execute the task.
  | 
        
        
            | 
            | 
           97 | 
                   $removaltask = new \core_course\task\course_delete_modules();
  | 
        
        
            | 
            | 
           98 | 
                   $data = [
  | 
        
        
            | 
            | 
           99 | 
                       'cms' => [$quizcm1, $assigncm, $quizcm2],
  | 
        
        
            | 
            | 
           100 | 
                       'userid' => $user->id,
  | 
        
        
            | 
            | 
           101 | 
                       'realuserid' => $user->id,
  | 
        
        
            | 
            | 
           102 | 
                   ];
  | 
        
        
            | 
            | 
           103 | 
                   $removaltask->set_custom_data($data);
  | 
        
        
            | 
            | 
           104 | 
                   try {
  | 
        
        
            | 
            | 
           105 | 
                       $removaltask->execute();
  | 
        
        
            | 
            | 
           106 | 
                   } catch (\coding_exception $e) {
  | 
        
        
            | 
            | 
           107 | 
                       // Assert exception.
  | 
        
        
            | 
            | 
           108 | 
                       $this->assertInstanceOf(\coding_exception::class, $e);
  | 
        
        
            | 
            | 
           109 | 
                       $errormsg = str_replace('\\', '/', $e->getMessage()); // Normalise dir separator.
  | 
        
        
            | 
            | 
           110 | 
                       $this->assertStringContainsString('cannotdeletemodulemissinglib', $errormsg);
  | 
        
        
            | 
            | 
           111 | 
                       $this->assertStringContainsString('course/lib.php', $errormsg);
  | 
        
        
            | 
            | 
           112 | 
                       $this->assertStringContainsString('mod/TestModuleToDelete/lib.php is missing', $errormsg);
  | 
        
        
            | 
            | 
           113 | 
                       // Get line numbers array which contains the exception name.
  | 
        
        
            | 
            | 
           114 | 
                       $lines = array_keys(preg_grep("/cannotdeletemodulemissinglib/", file('course/lib.php')));
  | 
        
        
            | 
            | 
           115 | 
                       // Increase 1 to keys to convert to actual line number.
  | 
        
        
            | 
            | 
           116 | 
                       $lines = array_map(function($key) {
  | 
        
        
            | 
            | 
           117 | 
                           return ++$key;
  | 
        
        
            | 
            | 
           118 | 
                       }, $lines);
  | 
        
        
            | 
            | 
           119 | 
                       $regex = "/(\(" . implode('\))|(\(', $lines) . "\))/";
  | 
        
        
            | 
            | 
           120 | 
                       // Assert the error message has correct line number.
  | 
        
        
            | 
            | 
           121 | 
                       $this->assertMatchesRegularExpression($regex, $errormsg);
  | 
        
        
            | 
            | 
           122 | 
                   }
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
                   // The success modules have been deleted from the course, and only the failed module is in the course.
  | 
        
        
            | 
            | 
           125 | 
                   $coursedmodules = get_course_mods($course->id);
  | 
        
        
            | 
            | 
           126 | 
                   $this->assertCount(1, $coursedmodules);
  | 
        
        
            | 
            | 
           127 | 
               }
  | 
        
        
            | 
            | 
           128 | 
           }
  |