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
 * Events tests.
19
 *
20
 * @package tool_recyclebin
21
 * @category test
22
 * @copyright 2016 Mark Nelson <markn@moodle.com>
23
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace tool_recyclebin\event;
27
 
28
/**
29
 * Events tests class.
30
 *
31
 * @package tool_recyclebin
32
 * @category test
33
 * @copyright 2016 Mark Nelson <markn@moodle.com>
34
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class events_test extends \advanced_testcase {
1 efrain 37
 
38
    /**
39
     * Test set up.
40
     *
41
     * This is executed before running any test in this file.
42
     */
43
    public function setUp(): void {
1441 ariadna 44
        parent::setUp();
1 efrain 45
        $this->resetAfterTest();
46
 
47
        // We want the category and course bin to be enabled.
48
        set_config('categorybinenable', 1, 'tool_recyclebin');
49
        set_config('coursebinenable', 1, 'tool_recyclebin');
50
    }
51
 
52
    /**
53
     * Test the category bin item created event.
54
     */
11 efrain 55
    public function test_category_bin_item_created(): void {
1 efrain 56
        // Create a course.
57
        $course = $this->getDataGenerator()->create_course();
58
 
59
        // Trigger and capture the event.
60
        $sink = $this->redirectEvents();
61
        delete_course($course, false);
62
        $events = $sink->get_events();
63
        $event = reset($events);
64
        // Need the second event here, the first is backup created.
65
        $event = next($events);
66
 
67
        // Get the item from the recycle bin.
68
        $rb = new \tool_recyclebin\category_bin($course->category);
69
        $items = $rb->get_items();
70
        $item = reset($items);
71
 
72
        // Check that the event contains the expected values.
73
        $this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_created', $event);
74
        $this->assertEquals(\context_coursecat::instance($course->category), $event->get_context());
75
        $this->assertEquals($item->id, $event->objectid);
76
        $this->assertEventContextNotUsed($event);
77
    }
78
 
79
    /**
80
     * Test the category bin item deleted event.
81
     */
11 efrain 82
    public function test_category_bin_item_deleted(): void {
1 efrain 83
        // Create a course.
84
        $course = $this->getDataGenerator()->create_course();
85
 
86
        // Delete the course.
87
        delete_course($course, false);
88
 
89
        // Get the item from the recycle bin.
90
        $rb = new \tool_recyclebin\category_bin($course->category);
91
        $items = $rb->get_items();
92
        $item = reset($items);
93
 
94
        // Trigger and capture the event.
95
        $sink = $this->redirectEvents();
96
        $rb->delete_item($item);
97
        $events = $sink->get_events();
98
        $this->assertCount(1, $events);
99
        $event = reset($events);
100
 
101
        // Check that the event contains the expected values.
102
        $this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_deleted', $event);
103
        $this->assertEquals(\context_coursecat::instance($course->category), $event->get_context());
104
        $this->assertEquals($item->id, $event->objectid);
105
        $this->assertEventContextNotUsed($event);
106
    }
107
 
108
    /**
109
     * Test the category bin item restored event.
110
     */
11 efrain 111
    public function test_category_bin_item_restored(): void {
1 efrain 112
        // Create a course.
113
        $course = $this->getDataGenerator()->create_course();
114
 
115
        // Delete the course.
116
        delete_course($course, false);
117
 
118
        // Get the item from the recycle bin.
119
        $rb = new \tool_recyclebin\category_bin($course->category);
120
        $items = $rb->get_items();
121
        $item = reset($items);
122
 
123
        // Trigger and capture the event.
124
        $sink = $this->redirectEvents();
125
        $rb->restore_item($item);
126
        $events = $sink->get_events();
127
        $event = $events[count($events) - 2];
128
 
129
        // Check that the event contains the expected values.
130
        $this->assertInstanceOf('\tooL_recyclebin\event\category_bin_item_restored', $event);
131
        $this->assertEquals(\context_coursecat::instance($course->category), $event->get_context());
132
        $this->assertEquals($item->id, $event->objectid);
133
        $this->assertEventContextNotUsed($event);
134
    }
135
 
136
    /**
137
     * Test the course bin item created event.
138
     */
11 efrain 139
    public function test_course_bin_item_created(): void {
1 efrain 140
        // Create a course.
141
        $course = $this->getDataGenerator()->create_course();
142
 
143
        // Create the assignment.
144
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
145
        $instance = $generator->create_instance(array('course' => $course->id));
146
 
147
        // Trigger and capture the event.
148
        $sink = $this->redirectEvents();
149
        course_delete_module($instance->cmid);
150
        $events = $sink->get_events();
151
        $event = reset($events);
152
 
153
        // Get the item from the recycle bin.
154
        $rb = new \tool_recyclebin\course_bin($course->id);
155
        $items = $rb->get_items();
156
        $item = reset($items);
157
 
158
        // Check that the event contains the expected values.
159
        $this->assertInstanceOf('\tooL_recyclebin\event\course_bin_item_created', $event);
160
        $this->assertEquals(\context_course::instance($course->id), $event->get_context());
161
        $this->assertEquals($item->id, $event->objectid);
162
        $this->assertEventContextNotUsed($event);
163
    }
164
 
165
    /**
166
     * Test the course bin item deleted event.
167
     */
11 efrain 168
    public function test_course_bin_item_deleted(): void {
1 efrain 169
        // Create a course.
170
        $course = $this->getDataGenerator()->create_course();
171
 
172
        // Create the assignment.
173
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
174
        $instance = $generator->create_instance(array('course' => $course->id));
175
 
176
        // Delete the module.
177
        course_delete_module($instance->cmid);
178
 
179
        // Get the item from the recycle bin.
180
        $rb = new \tool_recyclebin\course_bin($course->id);
181
        $items = $rb->get_items();
182
        $item = reset($items);
183
 
184
        // Trigger and capture the event.
185
        $sink = $this->redirectEvents();
186
        $rb->delete_item($item);
187
        $events = $sink->get_events();
188
        $this->assertCount(1, $events);
189
        $event = reset($events);
190
 
191
        // Check that the event contains the expected values.
192
        $this->assertInstanceOf('\tooL_recyclebin\event\course_bin_item_deleted', $event);
193
        $this->assertEquals(\context_course::instance($course->id), $event->get_context());
194
        $this->assertEquals($item->id, $event->objectid);
195
        $this->assertEventContextNotUsed($event);
196
    }
197
 
198
    /**
199
     * Test the course bin item restored event.
200
     */
11 efrain 201
    public function test_course_bin_item_restored(): void {
1 efrain 202
        // Create a course.
203
        $course = $this->getDataGenerator()->create_course();
204
 
205
        // Create the assignment.
206
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
207
        $instance = $generator->create_instance(array('course' => $course->id));
208
 
209
        course_delete_module($instance->cmid);
210
 
211
        // Get the item from the recycle bin.
212
        $rb = new \tool_recyclebin\course_bin($course->id);
213
        $items = $rb->get_items();
214
        $item = reset($items);
215
 
216
        // Trigger and capture the event.
217
        $sink = $this->redirectEvents();
218
        $rb->restore_item($item);
219
        $events = $sink->get_events();
220
        $eventscount = 0;
221
 
222
        foreach ($events as $event) {
223
            if ($event instanceof \tooL_recyclebin\event\course_bin_item_restored) {
224
                // Check that the event contains the expected values.
225
                $this->assertEquals(\context_course::instance($course->id), $event->get_context());
226
                $this->assertEquals($item->id, $event->objectid);
227
                $this->assertEventContextNotUsed($event);
228
                $eventscount++;
229
            }
230
        }
231
        // Only one course_bin_item_restored event should be triggered.
232
        $this->assertEquals(1, $eventscount);
233
    }
234
}