Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_question\event;
18
 
19
use context_module;
20
 
21
/**
22
 * Unit tests for question_category_viewed
23
 *
24
 * @package   core_question
25
 * @copyright 2024 onwards Catalyst IT EU {@link https://catalyst-eu.net}
26
 * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
27
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers \core_question\event\question_category_viewed
29
 */
30
final class question_category_viewed_test extends \advanced_testcase {
31
    /**
32
     * Test creating and triggering an event from a category instance.
33
     *
34
     * @covers ::create_from_question_category_instance
35
     */
36
    public function test_create_from_question_category_instance(): void {
37
        $this->resetAfterTest();
38
        // Create the category.
39
        $generator = $this->getDataGenerator();
40
        $course = $generator->create_course();
41
        $quiz = $generator->get_plugin_generator('mod_quiz')->create_instance(['course' => $course->id]);
42
        $context = context_module::instance($quiz->cmid);
43
        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
44
        // Create the category.
45
        $category = $questiongenerator->create_question_category([
46
            'contextid' => $context->id,
47
            'name' => 'New category',
48
            'info' => 'Description',
49
            'idnumber' => 'newcategory',
50
        ]);
51
 
52
        // Log the view of this category.
53
        $event = \core\event\question_category_viewed::create_from_question_category_instance($category, $context);
54
 
55
        // Trigger and capture the event.
56
        $sink = $this->redirectEvents();
57
        $event->trigger();
58
        $events = $sink->get_events();
59
        $event = reset($events);
60
 
61
        // Check that the event data is valid.
62
        $this->assertInstanceOf('\core\event\question_category_viewed', $event);
63
        $this->assertEquals($context, $event->get_context());
64
        $this->assertEquals($category->id, $event->objectid);
65
        $this->assertDebuggingNotCalled();
66
    }
67
}