Merge branch 'MDL-66990-master-enfix' of git://github.com/mudrd8mz/moodle
[moodle.git] / course / tests / events_test.php
blob0a30f7e55ef1bc1f57eae22977ada65dc779a18d
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.
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/>.
17 /**
18 * This file contains the class that handles testing of course events.
20 * @package core
21 * @copyright 2016 Stephen Bourget
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * This file contains the class that handles testing of course events.
30 * @package core_course
31 * @copyright 2016 Stephen Bourget
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class core_course_events_testcase extends advanced_testcase {
36 /**
37 * Tests set up
39 protected function setUp() {
40 global $CFG;
41 require_once($CFG->dirroot . '/course/lib.php');
42 $this->resetAfterTest();
45 /**
46 * Test the course category viewed.
48 * There is no external API for viewing a category, so the unit test will simply
49 * create and trigger the event and ensure data is returned as expected.
51 public function test_course_category_viewed_event() {
53 // Create a category.
54 $category = $this->getDataGenerator()->create_category();
56 // Trigger an event: course category viewed.
57 $eventparams = array(
58 'objectid' => $category->id,
59 'context' => context_system::instance(),
62 $event = \core\event\course_category_viewed::create($eventparams);
63 // Trigger and capture the event.
64 $sink = $this->redirectEvents();
65 $event->trigger();
66 $events = $sink->get_events();
67 $event = reset($events);
69 // Check that the event data is valid.
70 $this->assertInstanceOf('\core\event\course_category_viewed', $event);
71 $this->assertEquals($category->id, $event->objectid);
72 $this->assertDebuggingNotCalled();
73 $sink->close();
77 /**
78 * Test the course information viewed.
80 * There is no external API for viewing course information so the unit test will simply
81 * create and trigger the event and ensure data is returned as expected.
83 public function test_course_information_viewed_event() {
85 // Create a course.
86 $data = new stdClass();
87 $course = $this->getDataGenerator()->create_course($data);
89 // Trigger an event: course category viewed.
90 $eventparams = array(
91 'objectid' => $course->id,
92 'context' => context_course::instance($course->id),
95 $event = \core\event\course_information_viewed::create($eventparams);
96 // Trigger and capture the event.
97 $sink = $this->redirectEvents();
98 $event->trigger();
99 $events = $sink->get_events();
100 $event = reset($events);
102 // Check that the event data is valid.
103 $this->assertInstanceOf('\core\event\course_information_viewed', $event);
104 $this->assertEquals($course->id, $event->objectid);
105 $this->assertDebuggingNotCalled();
106 $sink->close();
111 * Test the courses searched.
113 * There is no external API for viewing course information so the unit test will simply
114 * create and trigger the event and ensure data is returned as expected.
116 public function test_courses_searched_event() {
118 // Trigger an event: courses searched.
119 $search = 'mysearch';
120 $eventparams = array(
121 'context' => context_system::instance(),
122 'other' => array('query' => $search)
125 $event = \core\event\courses_searched::create($eventparams);
126 // Trigger and capture the event.
127 $sink = $this->redirectEvents();
128 $event->trigger();
129 $events = $sink->get_events();
130 $event = reset($events);
132 // Check that the event data is valid.
133 $this->assertInstanceOf('\core\event\courses_searched', $event);
134 $this->assertEquals($search, $event->other['query']);
135 $this->assertDebuggingNotCalled();
136 $sink->close();