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 core_question
21
 * @copyright 2014 Mark Nelson <markn@moodle.com>
22
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace core_question\event;
26
 
27
use qtype_description;
28
use qtype_description_edit_form;
29
use qtype_description_test_helper;
30
use test_question_maker;
31
 
32
defined('MOODLE_INTERNAL') || die();
33
 
34
global $CFG;
35
 
36
require_once($CFG->dirroot . '/question/editlib.php');
37
 
1441 ariadna 38
final class events_test extends \advanced_testcase {
1 efrain 39
 
40
    /**
41
     * Tests set up.
42
     */
43
    public function setUp(): void {
1441 ariadna 44
        parent::setUp();
1 efrain 45
        $this->resetAfterTest();
46
    }
47
 
48
    /**
49
     * Test the questions imported event.
50
     * There is no easy way to trigger this event using the API, so the unit test will simply
51
     * create and trigger the event and ensure data is returned as expected.
52
     */
11 efrain 53
    public function test_questions_imported(): void {
1 efrain 54
 
55
        $this->setAdminUser();
56
        $course = $this->getDataGenerator()->create_course();
57
        $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
1441 ariadna 58
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
1 efrain 59
 
60
        $contexts = new \core_question\local\bank\question_edit_contexts(\context_module::instance($quiz->cmid));
61
 
1441 ariadna 62
        $defaultcategory = question_get_default_category($contexts->lowest()->id, true);
1 efrain 63
 
1441 ariadna 64
        $category = $questiongenerator->create_question_category([
65
            'name' => 'newcategory',
66
            'parent' => $defaultcategory->id,
67
        ]);
1 efrain 68
 
69
        // Log the view of this category.
70
        $params = [
71
                'context' => \context_module::instance($quiz->cmid),
1441 ariadna 72
                'other' => ['categoryid' => $category->id, 'format' => 'testformat'],
1 efrain 73
        ];
74
 
75
        $event = \core\event\questions_imported::create($params);
76
 
77
        // Trigger and capture the event.
78
        $sink = $this->redirectEvents();
79
        $event->trigger();
80
        $events = $sink->get_events();
81
        $event = reset($events);
82
 
83
        // Check that the event data is valid.
84
        $this->assertInstanceOf('\core\event\questions_imported', $event);
85
        $this->assertEquals(\context_module::instance($quiz->cmid), $event->get_context());
1441 ariadna 86
        $this->assertEquals($category->id, $event->other['categoryid']);
1 efrain 87
        $this->assertEquals('testformat', $event->other['format']);
88
        $this->assertDebuggingNotCalled();
89
 
90
    }
91
 
92
    /**
93
     * Test the questions exported event.
94
     * There is no easy way to trigger this event using the API, so the unit test will simply
95
     * create and trigger the event and ensure data is returned as expected.
96
     */
11 efrain 97
    public function test_questions_exported(): void {
1 efrain 98
 
99
        $this->setAdminUser();
100
        $course = $this->getDataGenerator()->create_course();
101
        $quiz = $this->getDataGenerator()->create_module('quiz', ['course' => $course->id]);
1441 ariadna 102
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
1 efrain 103
 
104
        $contexts = new \core_question\local\bank\question_edit_contexts(\context_module::instance($quiz->cmid));
105
 
1441 ariadna 106
        $defaultcategory = question_get_default_category($contexts->lowest()->id, true);
1 efrain 107
 
1441 ariadna 108
        $category = $questiongenerator->create_question_category([
109
            'name' => 'newcategory',
110
            'parent' => $defaultcategory->id,
111
        ]);
1 efrain 112
 
113
        // Log the view of this category.
114
        $params = [
115
                'context' => \context_module::instance($quiz->cmid),
1441 ariadna 116
                'other' => ['categoryid' => $category->id, 'format' => 'testformat'],
1 efrain 117
        ];
118
 
119
        $event = \core\event\questions_exported::create($params);
120
 
121
        // Trigger and capture the event.
122
        $sink = $this->redirectEvents();
123
        $event->trigger();
124
        $events = $sink->get_events();
125
        $event = reset($events);
126
 
127
        // Check that the event data is valid.
128
        $this->assertInstanceOf('\core\event\questions_exported', $event);
129
        $this->assertEquals(\context_module::instance($quiz->cmid), $event->get_context());
1441 ariadna 130
        $this->assertEquals($category->id, $event->other['categoryid']);
1 efrain 131
        $this->assertEquals('testformat', $event->other['format']);
132
        $this->assertDebuggingNotCalled();
133
 
134
    }
135
 
136
    /**
137
     * Test the question created event.
138
     */
11 efrain 139
    public function test_question_created(): void {
1 efrain 140
 
141
        $this->setAdminUser();
142
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
143
 
144
        $cat = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1]);
145
 
146
        // Trigger and capture the event.
147
        $sink = $this->redirectEvents();
148
        $questiondata = $generator->create_question('description', null, ['category' => $cat->id]);
149
        $question = \question_bank::load_question($questiondata->id);
150
 
151
        $events = $sink->get_events();
152
        $event = reset($events);
153
 
154
        // Check that the event data is valid.
155
        $this->assertInstanceOf('\core\event\question_created', $event);
156
        $this->assertEquals($question->id, $event->objectid);
157
        $this->assertEquals($cat->id, $event->other['categoryid']);
158
        $this->assertDebuggingNotCalled();
159
 
160
    }
161
 
162
    /**
163
     * Test the question deleted event.
164
     */
11 efrain 165
    public function test_question_deleted(): void {
1 efrain 166
 
167
        $this->setAdminUser();
168
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
169
 
170
        $cat = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1]);
171
 
172
        $questiondata = $generator->create_question('description', null, ['category' => $cat->id]);
173
        $question = \question_bank::load_question($questiondata->id);
174
 
175
        // Trigger and capture the event.
176
        $sink = $this->redirectEvents();
177
        question_delete_question($question->id);
178
        $events = $sink->get_events();
179
        $event = reset($events);
180
 
181
        // Check that the event data is valid.
182
        $this->assertInstanceOf('\core\event\question_deleted', $event);
183
        $this->assertEquals($question->id, $event->objectid);
184
        $this->assertEquals($cat->id, $event->other['categoryid']);
185
        $this->assertDebuggingNotCalled();
186
 
187
    }
188
 
189
    /**
190
     * Test the question updated event.
191
     */
11 efrain 192
    public function test_question_updated(): void {
1 efrain 193
 
194
        global $CFG;
195
        require_once($CFG->dirroot . '/question/type/description/questiontype.php');
196
        require_once($CFG->dirroot . '/question/type/edit_question_form.php');
197
        require_once($CFG->dirroot . '/question/type/description/edit_description_form.php');
198
 
199
        $this->setAdminUser();
200
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
201
 
202
        $cat = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1]);
203
 
204
        $questiondata = $generator->create_question('description', null, ['category' => $cat->id]);
205
        $question = \question_bank::load_question($questiondata->id);
206
 
207
        $qtype = new qtype_description();
208
        $formdata = test_question_maker::get_question_form_data('description');
209
        $formdata->category = "{$cat->id},{$cat->contextid}";
210
        qtype_description_edit_form::mock_submit((array) $formdata);
211
 
212
        $form = qtype_description_test_helper::get_question_editing_form($cat, $questiondata);
213
        $fromform = $form->get_data();
214
 
215
        // Trigger and capture the event.
216
        $sink = $this->redirectEvents();
217
        $question = $qtype->save_question($questiondata, $fromform);
218
        $events = $sink->get_events();
219
        $event = reset($events);
220
 
221
        // Check that the event data is valid.
222
        // Every save is a new question after Moodle 4.0.
223
        $this->assertInstanceOf('\core\event\question_created', $event);
224
        $this->assertEquals($question->id, $event->objectid);
225
        $this->assertEquals($cat->id, $event->other['categoryid']);
226
        $this->assertDebuggingNotCalled();
227
 
228
    }
229
 
230
    /**
231
     * Test the question moved event.
232
     */
11 efrain 233
    public function test_question_moved(): void {
1 efrain 234
 
235
        $this->setAdminUser();
236
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
237
 
238
        $cat1 = $generator->create_question_category([
239
                'name' => 'My category 1', 'sortorder' => 1]);
240
 
241
        $cat2 = $generator->create_question_category([
242
                'name' => 'My category 2', 'sortorder' => 2]);
243
 
244
        $questiondata = $generator->create_question('description', null, ['category' => $cat1->id]);
245
        $question = \question_bank::load_question($questiondata->id);
246
 
247
        // Trigger and capture the event.
248
        $sink = $this->redirectEvents();
249
        question_move_questions_to_category([$question->id], $cat2->id);
250
        $events = $sink->get_events();
251
        $event = reset($events);
252
 
253
        // Check that the event data is valid.
254
        $this->assertInstanceOf('\core\event\question_moved', $event);
255
        $this->assertEquals($question->id, $event->objectid);
256
        $this->assertEquals($cat1->id, $event->other['oldcategoryid']);
257
        $this->assertEquals($cat2->id, $event->other['newcategoryid']);
258
        $this->assertDebuggingNotCalled();
259
 
260
    }
261
 
262
    /**
263
     * Test the question viewed event.
264
     * There is no external API for viewing the question, so the unit test will simply
265
     * create and trigger the event and ensure data is returned as expected.
266
     */
11 efrain 267
    public function test_question_viewed(): void {
1 efrain 268
 
269
        $this->setAdminUser();
270
        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
271
 
272
        $cat = $generator->create_question_category(['name' => 'My category', 'sortorder' => 1]);
273
 
274
        $questiondata = $generator->create_question('description', null, ['category' => $cat->id]);
275
        $question = \question_bank::load_question($questiondata->id);
276
 
277
        $event = \core\event\question_viewed::create_from_question_instance($question, \context::instance_by_id($cat->contextid));
278
 
279
        // Trigger and capture the event.
280
        $sink = $this->redirectEvents();
281
        $event->trigger();
282
        $events = $sink->get_events();
283
        $event = reset($events);
284
 
285
        // Check that the event data is valid.
286
        $this->assertInstanceOf('\core\event\question_viewed', $event);
287
        $this->assertEquals($question->id, $event->objectid);
288
        $this->assertEquals($cat->id, $event->other['categoryid']);
289
        $this->assertDebuggingNotCalled();
290
 
291
    }
292
}