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