Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
 * Unit tests for mod_resource lib
19
 *
20
 * @package    mod_resource
21
 * @category   external
22
 * @copyright  2015 Juan Leyva <juan@moodle.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @since      Moodle 3.0
25
 */
26
namespace mod_resource;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
 
31
/**
32
 * Unit tests for mod_resource lib
33
 *
34
 * @package    mod_resource
35
 * @category   external
36
 * @copyright  2015 Juan Leyva <juan@moodle.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @since      Moodle 3.0
39
 */
1441 ariadna 40
final class lib_test extends \advanced_testcase {
1 efrain 41
 
42
    /**
43
     * Prepares things before this test case is initialised
44
     * @return void
45
     */
46
    public static function setUpBeforeClass(): void {
47
        global $CFG;
48
        require_once($CFG->dirroot . '/mod/resource/lib.php');
1441 ariadna 49
        parent::setUpBeforeClass();
1 efrain 50
    }
51
 
52
    /**
53
     * Test resource_view
54
     * @return void
55
     */
11 efrain 56
    public function test_resource_view(): void {
1 efrain 57
        global $CFG;
58
 
59
        $CFG->enablecompletion = 1;
60
        $this->resetAfterTest();
61
 
62
        $this->setAdminUser();
63
        // Setup test data.
64
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
65
        $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
66
                                                            array('completion' => 2, 'completionview' => 1));
67
        $context = \context_module::instance($resource->cmid);
68
        $cm = get_coursemodule_from_instance('resource', $resource->id);
69
 
70
        // Trigger and capture the event.
71
        $sink = $this->redirectEvents();
72
 
73
        resource_view($resource, $course, $cm, $context);
74
 
75
        $events = $sink->get_events();
76
        // 2 additional events thanks to completion.
77
        $this->assertCount(3, $events);
78
        $event = array_shift($events);
79
 
80
        // Checking that the event contains the expected values.
81
        $this->assertInstanceOf('\mod_resource\event\course_module_viewed', $event);
82
        $this->assertEquals($context, $event->get_context());
83
        $moodleurl = new \moodle_url('/mod/resource/view.php', array('id' => $cm->id));
84
        $this->assertEquals($moodleurl, $event->get_url());
85
        $this->assertEventContextNotUsed($event);
86
        $this->assertNotEmpty($event->get_name());
87
 
88
        // Check completion status.
89
        $completion = new \completion_info($course);
90
        $completiondata = $completion->get_data($cm);
91
        $this->assertEquals(1, $completiondata->completionstate);
92
 
93
    }
94
 
95
    /**
96
     * Tests the resource_get_coursemodule_info function.
97
     *
98
     * Note: This currently doesn't test every aspect of the function, mainly focusing on the icon.
99
     */
11 efrain 100
    public function test_get_coursemodule_info(): void {
1 efrain 101
        global $DB, $USER;
102
 
103
        $this->resetAfterTest();
104
        $this->setAdminUser();
105
 
106
        // Create course.
107
        $generator = $this->getDataGenerator();
108
        $course = $generator->create_course();
109
 
110
        // Create a resource with no files.
111
        $draftid = file_get_unused_draft_itemid();
112
        $resource1 = $generator->create_module('resource', array('course' => $course->id,
113
                'name' => 'R1', 'files' => $draftid));
114
 
115
        // Create a resource with one file.
116
        $draftid = file_get_unused_draft_itemid();
117
        $contextid = \context_user::instance($USER->id)->id;
118
        $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $contextid,
119
                'itemid' => $draftid, 'filename' => 'r2.txt', 'filepath' => '/');
120
        $fs = get_file_storage();
121
        $fs->create_file_from_string($filerecord, 'Test');
122
        $resource2 = $generator->create_module('resource', array('course' => $course->id,
123
                'name' => 'R2', 'files' => $draftid));
124
 
125
        // Create a resource with two files.
126
        $draftid = file_get_unused_draft_itemid();
127
        $filerecord = array('component' => 'user', 'filearea' => 'draft', 'contextid' => $contextid,
128
                'itemid' => $draftid, 'filename' => 'r3.txt', 'filepath' => '/', 'sortorder' => 1);
129
        $fs->create_file_from_string($filerecord, 'Test');
130
        $filerecord['filename'] = 'r3.doc';
131
        $filerecord['sortorder'] = 2;
132
        $fs->create_file_from_string($filerecord, 'Test');
133
        $resource3 = $generator->create_module('resource', array('course' => $course->id,
134
                'name' => 'R3', 'files' => $draftid));
135
 
136
        // Try get_coursemodule_info for first one.
137
        $info = resource_get_coursemodule_info(
138
                $DB->get_record('course_modules', array('id' => $resource1->cmid)));
139
 
140
        // The name should be set. There is no overridden icon.
141
        $this->assertEquals('R1', $info->name);
142
        $this->assertEmpty($info->icon);
143
 
144
        // For second one, there should be an overridden icon.
145
        $info = resource_get_coursemodule_info(
146
                $DB->get_record('course_modules', array('id' => $resource2->cmid)));
147
        $this->assertEquals('R2', $info->name);
148
        $this->assertEquals('f/text', $info->icon);
149
 
150
        // For third one, it should use the highest sortorder icon.
151
        $info = resource_get_coursemodule_info(
152
                $DB->get_record('course_modules', array('id' => $resource3->cmid)));
153
        $this->assertEquals('R3', $info->name);
154
        $this->assertEquals('f/document', $info->icon);
155
    }
156
 
11 efrain 157
    public function test_resource_core_calendar_provide_event_action(): void {
1 efrain 158
        $this->resetAfterTest();
159
        $this->setAdminUser();
160
 
161
        // Create the activity.
162
        $course = $this->getDataGenerator()->create_course();
163
        $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id));
164
 
165
        // Create a calendar event.
166
        $event = $this->create_action_event($course->id, $resource->id,
167
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
168
 
169
        // Create an action factory.
170
        $factory = new \core_calendar\action_factory();
171
 
172
        // Decorate action event.
173
        $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory);
174
 
175
        // Confirm the event was decorated.
176
        $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
177
        $this->assertEquals(get_string('view'), $actionevent->get_name());
178
        $this->assertInstanceOf('moodle_url', $actionevent->get_url());
179
        $this->assertEquals(1, $actionevent->get_item_count());
180
        $this->assertTrue($actionevent->is_actionable());
181
    }
182
 
11 efrain 183
    public function test_resource_core_calendar_provide_event_action_already_completed(): void {
1 efrain 184
        global $CFG;
185
 
186
        $this->resetAfterTest();
187
        $this->setAdminUser();
188
 
189
        $CFG->enablecompletion = 1;
190
 
191
        // Create the activity.
192
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
193
        $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
194
            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
195
 
196
        // Get some additional data.
197
        $cm = get_coursemodule_from_instance('resource', $resource->id);
198
 
199
        // Create a calendar event.
200
        $event = $this->create_action_event($course->id, $resource->id,
201
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
202
 
203
        // Mark the activity as completed.
204
        $completion = new \completion_info($course);
205
        $completion->set_module_viewed($cm);
206
 
207
        // Create an action factory.
208
        $factory = new \core_calendar\action_factory();
209
 
210
        // Decorate action event.
211
        $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory);
212
 
213
        // Ensure result was null.
214
        $this->assertNull($actionevent);
215
    }
216
 
217
    /**
218
     * Test mod_resource_core_calendar_provide_event_action with user override
219
     */
11 efrain 220
    public function test_resource_core_calendar_provide_event_action_user_override(): void {
1 efrain 221
        global $CFG, $USER;
222
 
223
        $this->resetAfterTest();
224
        $this->setAdminUser();
225
        $user = $this->getDataGenerator()->create_user();
226
        $CFG->enablecompletion = 1;
227
 
228
        // Create the activity.
229
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
230
        $resource = $this->getDataGenerator()->create_module('resource', array('course' => $course->id),
231
            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
232
 
233
        // Get some additional data.
234
        $cm = get_coursemodule_from_instance('resource', $resource->id);
235
 
236
        // Create a calendar event.
237
        $event = $this->create_action_event($course->id, $resource->id,
238
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
239
 
240
        // Mark the activity as completed.
241
        $completion = new \completion_info($course);
242
        $completion->set_module_viewed($cm);
243
 
244
        // Create an action factory.
245
        $factory = new \core_calendar\action_factory();
246
 
247
        // Decorate action event.
248
        $actionevent = mod_resource_core_calendar_provide_event_action($event, $factory, $USER->id);
249
 
250
        // Decorate action with a userid override.
251
        $actionevent2 = mod_resource_core_calendar_provide_event_action($event, $factory, $user->id);
252
 
253
        // Ensure result was null because it has been marked as completed for the associated user.
254
        // Logic was brought across from the "_already_completed" function.
255
        $this->assertNull($actionevent);
256
 
257
        // Confirm the event was decorated.
258
        $this->assertNotNull($actionevent2);
259
        $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2);
260
        $this->assertEquals(get_string('view'), $actionevent2->get_name());
261
        $this->assertInstanceOf('moodle_url', $actionevent2->get_url());
262
        $this->assertEquals(1, $actionevent2->get_item_count());
263
        $this->assertTrue($actionevent2->is_actionable());
264
    }
265
 
266
    /**
267
     * Creates an action event.
268
     *
269
     * @param int $courseid The course id.
270
     * @param int $instanceid The instance id.
271
     * @param string $eventtype The event type.
272
     * @return bool|calendar_event
273
     */
274
    private function create_action_event($courseid, $instanceid, $eventtype) {
275
        $event = new \stdClass();
276
        $event->name = 'Calendar event';
277
        $event->modulename  = 'resource';
278
        $event->courseid = $courseid;
279
        $event->instance = $instanceid;
280
        $event->type = CALENDAR_EVENT_TYPE_ACTION;
281
        $event->eventtype = $eventtype;
282
        $event->timestart = time();
283
 
284
        return \calendar_event::create($event);
285
    }
286
}