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_page lib
19
 *
20
 * @package    mod_page
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_page;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
 
30
 
31
/**
32
 * Unit tests for mod_page lib
33
 *
34
 * @package    mod_page
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/page/lib.php');
1441 ariadna 49
        parent::setUpBeforeClass();
1 efrain 50
    }
51
 
52
    /**
53
     * Test page_view
54
     * @return void
55
     */
11 efrain 56
    public function test_page_view(): void {
1 efrain 57
        global $CFG;
58
 
59
        $CFG->enablecompletion = 1;
60
        $this->resetAfterTest();
61
 
62
        // Setup test data.
63
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
64
        $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
65
                                                            array('completion' => 2, 'completionview' => 1));
66
        $context = \context_module::instance($page->cmid);
67
        $cm = get_coursemodule_from_instance('page', $page->id);
68
 
69
        // Trigger and capture the event.
70
        $sink = $this->redirectEvents();
71
 
72
        $this->setAdminUser();
73
        page_view($page, $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_page\event\course_module_viewed', $event);
82
        $this->assertEquals($context, $event->get_context());
83
        $moodleurl = new \moodle_url('/mod/page/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
 
11 efrain 94
    public function test_page_core_calendar_provide_event_action(): void {
1 efrain 95
        $this->resetAfterTest();
96
        $this->setAdminUser();
97
 
98
        // Create the activity.
99
        $course = $this->getDataGenerator()->create_course();
100
        $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
101
 
102
        // Create a calendar event.
103
        $event = $this->create_action_event($course->id, $page->id,
104
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
105
 
106
        // Create an action factory.
107
        $factory = new \core_calendar\action_factory();
108
 
109
        // Decorate action event.
110
        $actionevent = mod_page_core_calendar_provide_event_action($event, $factory);
111
 
112
        // Confirm the event was decorated.
113
        $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
114
        $this->assertEquals(get_string('view'), $actionevent->get_name());
115
        $this->assertInstanceOf('moodle_url', $actionevent->get_url());
116
        $this->assertEquals(1, $actionevent->get_item_count());
117
        $this->assertTrue($actionevent->is_actionable());
118
    }
119
 
11 efrain 120
    public function test_page_core_calendar_provide_event_action_already_completed(): void {
1 efrain 121
        global $CFG;
122
 
123
        $this->resetAfterTest();
124
        $this->setAdminUser();
125
 
126
        $CFG->enablecompletion = 1;
127
 
128
        // Create the activity.
129
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
130
        $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
131
            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
132
 
133
        // Get some additional data.
134
        $cm = get_coursemodule_from_instance('page', $page->id);
135
 
136
        // Create a calendar event.
137
        $event = $this->create_action_event($course->id, $page->id,
138
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
139
 
140
        // Mark the activity as completed.
141
        $completion = new \completion_info($course);
142
        $completion->set_module_viewed($cm);
143
 
144
        // Create an action factory.
145
        $factory = new \core_calendar\action_factory();
146
 
147
        // Decorate action event.
148
        $actionevent = mod_page_core_calendar_provide_event_action($event, $factory);
149
 
150
        // Ensure result was null.
151
        $this->assertNull($actionevent);
152
    }
153
 
154
    /**
155
     * Test mod_page_core_calendar_provide_event_action with user override
156
     */
11 efrain 157
    public function test_page_core_calendar_provide_event_action_user_override(): void {
1 efrain 158
        global $CFG, $USER;
159
 
160
        $this->resetAfterTest();
161
        $this->setAdminUser();
162
        $user = $this->getDataGenerator()->create_user();
163
        $CFG->enablecompletion = 1;
164
 
165
        // Create the activity.
166
        $course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
167
        $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id),
168
            array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
169
 
170
        // Get some additional data.
171
        $cm = get_coursemodule_from_instance('page', $page->id);
172
 
173
        // Create a calendar event.
174
        $event = $this->create_action_event($course->id, $page->id,
175
            \core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
176
 
177
        // Mark the activity as completed.
178
        $completion = new \completion_info($course);
179
        $completion->set_module_viewed($cm);
180
 
181
        // Create an action factory.
182
        $factory = new \core_calendar\action_factory();
183
 
184
        // Decorate action event.
185
        $actionevent = mod_page_core_calendar_provide_event_action($event, $factory, $USER->id);
186
 
187
        // Decorate action with a userid override.
188
        $actionevent2 = mod_page_core_calendar_provide_event_action($event, $factory, $user->id);
189
 
190
        // Ensure result was null because it has been marked as completed for the associated user.
191
        // Logic was brought across from the "_already_completed" function.
192
        $this->assertNull($actionevent);
193
 
194
        // Confirm the event was decorated.
195
        $this->assertNotNull($actionevent2);
196
        $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent2);
197
        $this->assertEquals(get_string('view'), $actionevent2->get_name());
198
        $this->assertInstanceOf('moodle_url', $actionevent2->get_url());
199
        $this->assertEquals(1, $actionevent2->get_item_count());
200
        $this->assertTrue($actionevent2->is_actionable());
201
    }
202
 
203
    /**
204
     * Creates an action event.
205
     *
206
     * @param int $courseid The course id.
207
     * @param int $instanceid The instance id.
208
     * @param string $eventtype The event type.
209
     * @return bool|calendar_event
210
     */
211
    private function create_action_event($courseid, $instanceid, $eventtype) {
212
        $event = new \stdClass();
213
        $event->name = 'Calendar event';
214
        $event->modulename  = 'page';
215
        $event->courseid = $courseid;
216
        $event->instance = $instanceid;
217
        $event->type = CALENDAR_EVENT_TYPE_ACTION;
218
        $event->eventtype = $eventtype;
219
        $event->timestart = time();
220
 
221
        return \calendar_event::create($event);
222
    }
223
}