Proyectos de Subversion Moodle

Rev

| 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
namespace core_courseformat\local;
18
 
19
use core_courseformat\hook\after_cm_name_edited;
20
 
21
/**
22
 * Course module format actions class tests.
23
 *
24
 * @package    core_courseformat
25
 * @copyright  2024 Ferran Recio <ferran@moodle.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @coversDefaultClass \core_courseformat\cmactions
28
 */
29
final class cmactions_test extends \advanced_testcase {
30
    /**
31
     * Setup to ensure that fixtures are loaded.
32
     */
33
    public static function setUpBeforeClass(): void {
34
        global $CFG;
35
        require_once($CFG->dirroot . '/course/lib.php');
36
    }
37
 
38
    /**
39
     * Test renaming a course module.
40
     *
41
     * @dataProvider provider_test_rename
42
     * @covers ::rename
43
     * @param string $newname The new name for the course module.
44
     * @param bool $expected Whether the course module was renamed.
45
     * @param bool $expectexception Whether an exception is expected.
46
     */
47
    public function test_rename(string $newname, bool $expected, bool $expectexception): void {
48
        global $DB;
49
        $this->resetAfterTest();
50
 
51
        $course = $this->getDataGenerator()->create_course(['format' => 'topics']);
52
        $activity = $this->getDataGenerator()->create_module(
53
            'assign',
54
            ['course' => $course->id, 'name' => 'Old name']
55
        );
56
 
57
        $cmactions = new cmactions($course);
58
 
59
        if ($expectexception) {
60
            $this->expectException(\moodle_exception::class);
61
        }
62
        $result = $cmactions->rename($activity->cmid, $newname);
63
        $this->assertEquals($expected, $result);
64
 
65
        $cminfo = get_fast_modinfo($course)->get_cm($activity->cmid);
66
        if ($result) {
67
            $this->assertEquals('New name', $cminfo->name);
68
        } else {
69
            $this->assertEquals('Old name', $cminfo->name);
70
        }
71
    }
72
 
73
    /**
74
     * Data provider for test_rename.
75
     *
76
     * @return array
77
     */
78
    public static function provider_test_rename(): array {
79
        return [
80
            'Empty name' => [
81
                'newname' => '',
82
                'expected' => false,
83
                'expectexception' => false,
84
            ],
85
            'Maximum length' => [
86
                'newname' => str_repeat('a', 256),
87
                'expected' => false,
88
                'expectexception' => true,
89
            ],
90
            'Valid name' => [
91
                'newname' => 'New name',
92
                'expected' => true,
93
                'expectexception' => false,
94
            ],
95
        ];
96
    }
97
 
98
    /**
99
     * Test rename an activity also rename the calendar events.
100
     *
101
     * @covers ::rename
102
     */
103
    public function test_rename_calendar_events(): void {
104
        global $DB;
105
        $this->resetAfterTest();
106
 
107
        $this->setAdminUser();
108
        set_config('enablecompletion', 1);
109
 
110
        $course = $this->getDataGenerator()->create_course(['enablecompletion' => COMPLETION_ENABLED]);
111
        $activity = $this->getDataGenerator()->create_module(
112
            'assign',
113
            [
114
                'name' => 'Old name',
115
                'course' => $course,
116
                'completionexpected' => time(),
117
                'duedate' => time(),
118
            ]
119
        );
120
        $cm = get_coursemodule_from_instance('assign', $activity->id, $course->id);
121
 
122
        // Validate course events naming.
123
        $this->assertEquals(2, $DB->count_records('event'));
124
 
125
        $event = $DB->get_record(
126
            'event',
127
            ['modulename' => 'assign', 'instance' => $activity->id, 'eventtype' => 'due']
128
        );
129
        $this->assertEquals(
130
            get_string('calendardue', 'assign', 'Old name'),
131
            $event->name
132
        );
133
 
134
        $event = $DB->get_record(
135
            'event',
136
            ['modulename' => 'assign', 'instance' => $activity->id, 'eventtype' => 'expectcompletionon']
137
        );
138
        $this->assertEquals(
139
            get_string('completionexpectedfor', 'completion', (object) ['instancename' => 'Old name']),
140
            $event->name
141
        );
142
 
143
        // Rename activity.
144
        $cmactions = new cmactions($course);
145
        $result = $cmactions->rename($activity->cmid, 'New name');
146
        $this->assertTrue($result);
147
 
148
        // Validate event renaming.
149
        $event = $DB->get_record(
150
            'event',
151
            ['modulename' => 'assign', 'instance' => $activity->id, 'eventtype' => 'due']
152
        );
153
        $this->assertEquals(
154
            get_string('calendardue', 'assign', 'New name'),
155
            $event->name
156
        );
157
 
158
        $event = $DB->get_record(
159
            'event',
160
            ['modulename' => 'assign', 'instance' => $activity->id, 'eventtype' => 'expectcompletionon']
161
        );
162
        $this->assertEquals(
163
            get_string('completionexpectedfor', 'completion', (object) ['instancename' => 'New name']),
164
            $event->name
165
        );
166
    }
167
 
168
    /**
169
     * Test renaming an activity trigger a course update log event.
170
     *
171
     * @covers ::rename
172
     */
173
    public function test_rename_course_module_updated_event(): void {
174
        global $DB;
175
        $this->resetAfterTest();
176
 
177
        $course = $this->getDataGenerator()->create_course();
178
        $activity = $this->getDataGenerator()->create_module(
179
            'assign',
180
            ['course' => $course->id, 'name' => 'Old name']
181
        );
182
 
183
        $sink = $this->redirectEvents();
184
 
185
        $cmactions = new cmactions($course);
186
        $result = $cmactions->rename($activity->cmid, 'New name');
187
        $this->assertTrue($result);
188
 
189
        $events = $sink->get_events();
190
        $event = reset($events);
191
 
192
        // Check that the event data is valid.
193
        $this->assertInstanceOf('\core\event\course_module_updated', $event);
194
        $this->assertEquals(\context_module::instance($activity->cmid), $event->get_context());
195
    }
196
 
197
    /**
198
     * Test renaming an activity triggers the after_cm_name_edited hook.
199
     * @covers ::rename
200
     */
201
    public function test_rename_after_cm_name_edited_hook(): void {
202
        $this->resetAfterTest();
203
 
204
        $course = $this->getDataGenerator()->create_course();
205
        $activity = $this->getDataGenerator()->create_module(
206
            'assign',
207
            ['course' => $course->id, 'name' => 'Old name']
208
        );
209
 
210
        $executedhook = null;
211
 
212
        $testcallback = function(after_cm_name_edited $hook) use (&$executedhook): void {
213
            $executedhook = $hook;
214
        };
215
        $this->redirectHook(after_cm_name_edited::class, $testcallback);
216
 
217
        $cmactions = new cmactions($course);
218
        $result = $cmactions->rename($activity->cmid, 'New name');
219
        $this->assertTrue($result);
220
 
221
        $this->assertEquals($activity->cmid, $executedhook->get_cm()->id);
222
        $this->assertEquals('New name', $executedhook->get_newname());
223
    }
224
}