Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
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
     */
11 efrain 40
    public function test_delete_module_execution(): void {
1 efrain 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
     * Test with failed and successful cms
71
     *
72
     * @covers ::course_delete_modules
73
     */
74
    public function test_delete_module_exception(): void {
75
        global $DB;
76
        $this->resetAfterTest();
77
 
78
        // Generate test data.
79
        $generator = $this->getDataGenerator();
80
        $user = $generator->create_user();
81
        $course = $generator->create_course();
82
        $assign = $generator->create_module('assign', ['course' => $course]);
83
        $assigncm = get_coursemodule_from_id('assign', $assign->cmid);
84
 
85
        // Modify module name to make an exception in the course_delete_modules task.
86
        $module = $DB->get_record('modules', ['id' => $assigncm->module], 'id, name', MUST_EXIST);
87
        $module->name = 'TestModuleToDelete';
88
        $DB->update_record('modules', $module);
89
 
90
        // Generate successful test data.
91
        $quiz1 = $generator->create_module('quiz', ['course' => $course]);
92
        $quizcm1 = get_coursemodule_from_id('quiz', $quiz1->cmid);
93
 
94
        $quiz2 = $generator->create_module('quiz', ['course' => $course]);
95
        $quizcm2 = get_coursemodule_from_id('quiz', $quiz2->cmid);
96
 
97
        // Execute the task.
98
        $removaltask = new \core_course\task\course_delete_modules();
99
        $data = [
100
            'cms' => [$quizcm1, $assigncm, $quizcm2],
101
            'userid' => $user->id,
102
            'realuserid' => $user->id,
103
        ];
104
        $removaltask->set_custom_data($data);
105
        try {
106
            $removaltask->execute();
107
        } catch (\coding_exception $e) {
108
            // Assert exception.
109
            $this->assertInstanceOf(\coding_exception::class, $e);
110
            $errormsg = str_replace('\\', '/', $e->getMessage()); // Normalise dir separator.
111
            $this->assertStringContainsString('cannotdeletemodulemissinglib', $errormsg);
112
            $this->assertStringContainsString('course/lib.php', $errormsg);
113
            $this->assertStringContainsString('mod/TestModuleToDelete/lib.php is missing', $errormsg);
114
            // Get line numbers array which contains the exception name.
115
            $lines = array_keys(preg_grep("/cannotdeletemodulemissinglib/", file('course/lib.php')));
116
            // Increase 1 to keys to convert to actual line number.
117
            $lines = array_map(function($key) {
118
                return ++$key;
119
            }, $lines);
120
            $regex = "/(\(" . implode('\))|(\(', $lines) . "\))/";
121
            // Assert the error message has correct line number.
122
            $this->assertMatchesRegularExpression($regex, $errormsg);
123
        }
124
 
125
        // The success modules have been deleted from the course, and only the failed module is in the course.
126
        $coursedmodules = get_course_mods($course->id);
127
        $this->assertCount(1, $coursedmodules);
128
    }
129
}