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
 * This file contains the class that handles testing of course events.
19
 *
20
 * @package core_course
21
 * @copyright  2016 Stephen Bourget
22
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_course\event;
26
 
27
/**
28
 * This file contains the class that handles testing of course events.
29
 *
30
 * @package core_course
31
 * @copyright  2016 Stephen Bourget
32
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
1441 ariadna 34
final class events_test extends \advanced_testcase {
1 efrain 35
 
36
    /**
37
     * Tests set up
38
     */
39
    protected function setUp(): void {
40
        global $CFG;
41
        require_once($CFG->dirroot . '/course/lib.php');
1441 ariadna 42
        parent::setUp();
1 efrain 43
        $this->resetAfterTest();
44
    }
45
 
46
    /**
47
     * Test the course category viewed.
48
     *
49
     * There is no external API for viewing a category, so the unit test will simply
50
     * create and trigger the event and ensure data is returned as expected.
51
     */
11 efrain 52
    public function test_course_category_viewed_event(): void {
1 efrain 53
 
54
        // Create a category.
55
        $category = $this->getDataGenerator()->create_category();
56
 
57
        // Trigger an event: course category viewed.
58
        $eventparams = array(
59
            'objectid' => $category->id,
60
            'context' => \context_system::instance(),
61
        );
62
 
63
        $event = \core\event\course_category_viewed::create($eventparams);
64
        // Trigger and capture the event.
65
        $sink = $this->redirectEvents();
66
        $event->trigger();
67
        $events = $sink->get_events();
68
        $event = reset($events);
69
 
70
        // Check that the event data is valid.
71
        $this->assertInstanceOf('\core\event\course_category_viewed', $event);
72
        $this->assertEquals($category->id, $event->objectid);
73
        $this->assertDebuggingNotCalled();
74
        $sink->close();
75
 
76
    }
77
 
78
    /**
79
     * Test the course information viewed.
80
     *
81
     * There is no external API for viewing course information so the unit test will simply
82
     * create and trigger the event and ensure data is returned as expected.
83
     */
11 efrain 84
    public function test_course_information_viewed_event(): void {
1 efrain 85
 
86
        // Create a course.
87
        $data = new \stdClass();
88
        $course = $this->getDataGenerator()->create_course($data);
89
 
90
        // Trigger an event: course category viewed.
91
        $eventparams = array(
92
            'objectid' => $course->id,
93
            'context' => \context_course::instance($course->id),
94
        );
95
 
96
        $event = \core\event\course_information_viewed::create($eventparams);
97
        // Trigger and capture the event.
98
        $sink = $this->redirectEvents();
99
        $event->trigger();
100
        $events = $sink->get_events();
101
        $event = reset($events);
102
 
103
        // Check that the event data is valid.
104
        $this->assertInstanceOf('\core\event\course_information_viewed', $event);
105
        $this->assertEquals($course->id, $event->objectid);
106
        $this->assertDebuggingNotCalled();
107
        $sink->close();
108
 
109
    }
110
 
111
    /**
112
     * Test the courses searched.
113
     *
114
     * There is no external API for viewing course information so the unit test will simply
115
     * create and trigger the event and ensure data is returned as expected.
116
     */
11 efrain 117
    public function test_courses_searched_event(): void {
1 efrain 118
 
119
        // Trigger an event: courses searched.
120
        $search = 'mysearch';
121
        $eventparams = array(
122
            'context' => \context_system::instance(),
123
            'other' => array('query' => $search)
124
        );
125
 
126
        $event = \core\event\courses_searched::create($eventparams);
127
        // Trigger and capture the event.
128
        $sink = $this->redirectEvents();
129
        $event->trigger();
130
        $events = $sink->get_events();
131
        $event = reset($events);
132
 
133
        // Check that the event data is valid.
134
        $this->assertInstanceOf('\core\event\courses_searched', $event);
135
        $this->assertEquals($search, $event->other['query']);
136
        $this->assertDebuggingNotCalled();
137
        $sink->close();
138
 
139
    }
1441 ariadna 140
 
141
    /**
142
     * Test the course activities overview page viewed.
143
     *
144
     * There is no external API for viewing course information so the unit test will simply
145
     * create and trigger the event and ensure data is returned as expected.
146
     *
147
     * @covers \core\event\course_overview_viewed
148
     */
149
    public function test_course_overview_viewed_event(): void {
150
 
151
        // Create a course.
152
        $data = new \stdClass();
153
        $course = $this->getDataGenerator()->create_course($data);
154
 
155
        $eventparams = [
156
            'context' => \context_course::instance($course->id),
157
        ];
158
        $event = \core\event\course_overview_viewed::create($eventparams);
159
 
160
        // Trigger and capture the event.
161
        $sink = $this->redirectEvents();
162
        $event->trigger();
163
        $events = $sink->get_events();
164
        $event = reset($events);
165
 
166
        // Check that the event data is valid.
167
        $this->assertInstanceOf('\core\event\course_overview_viewed', $event);
168
        $this->assertEquals($course->id, $event->courseid);
169
        $this->assertDebuggingNotCalled();
170
        $sink->close();
171
 
172
    }
1 efrain 173
}