MDL-40912 coursecat: replaced 'hide' and 'show' add_to_log calls with an event
[moodle.git] / lib / tests / events_test.php
CommitLineData
03ea10e6
MN
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
21 * @category test
22 * @copyright 2014 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28class core_events_testcase extends advanced_testcase {
29
30 /**
31 * Test set up.
32 *
33 * This is executed before running any test in this file.
34 */
35 public function setUp() {
36 $this->resetAfterTest();
37 }
38
39 /**
40 * Test the course category created event.
41 */
42 public function test_course_category_created() {
43 // Trigger and capture the event.
44 $sink = $this->redirectEvents();
45 $category = $this->getDataGenerator()->create_category();
46 $events = $sink->get_events();
47 $event = reset($events);
48
49 // Check that the event data is valid.
50 $this->assertInstanceOf('\core\event\course_category_created', $event);
51 $this->assertEquals(context_coursecat::instance($category->id), $event->get_context());
52 $expected = array(SITEID, 'category', 'add', 'editcategory.php?id=' . $category->id, $category->id);
53 $this->assertEventLegacyLogData($expected, $event);
54 }
c4cea8cb
MN
55
56 /**
57 * Test the course category updated event.
58 */
59 public function test_course_category_updated() {
60 // Create a category.
61 $category = $this->getDataGenerator()->create_category();
62
63 // Create some data we are going to use to update this category.
64 $data = new stdClass();
65 $data->name = 'Category name change';
66
d86c7206 67 // Trigger and capture the event for updating a category.
c4cea8cb
MN
68 $sink = $this->redirectEvents();
69 $category->update($data);
70 $events = $sink->get_events();
71 $event = reset($events);
72
73 // Check that the event data is valid.
74 $this->assertInstanceOf('\core\event\course_category_updated', $event);
75 $this->assertEquals(context_coursecat::instance($category->id), $event->get_context());
76 $expected = array(SITEID, 'category', 'update', 'editcategory.php?id=' . $category->id, $category->id);
77 $this->assertEventLegacyLogData($expected, $event);
d86c7206
MN
78
79 // Create another category and a child category.
80 $category2 = $this->getDataGenerator()->create_category();
81 $childcat = $this->getDataGenerator()->create_category(array('parent' => $category2->id));
82
83 // Trigger and capture the event for changing the parent of a category.
84 $sink = $this->redirectEvents();
85 $childcat->change_parent($category);
86 $events = $sink->get_events();
87 $event = reset($events);
88
89 // Check that the event data is valid.
90 $this->assertInstanceOf('\core\event\course_category_updated', $event);
91 $this->assertEquals(context_coursecat::instance($childcat->id), $event->get_context());
92 $expected = array(SITEID, 'category', 'move', 'editcategory.php?id=' . $childcat->id, $childcat->id);
93 $this->assertEventLegacyLogData($expected, $event);
94
95 // Trigger and capture the event for changing the sortorder of a category.
96 $sink = $this->redirectEvents();
97 $category2->change_sortorder_by_one(true);
98 $events = $sink->get_events();
99 $event = reset($events);
100
101 // Check that the event data is valid.
102 $this->assertInstanceOf('\core\event\course_category_updated', $event);
103 $this->assertEquals(context_coursecat::instance($category2->id), $event->get_context());
104 $expected = array(SITEID, 'category', 'move', 'management.php?categoryid=' . $category2->id, $category2->id);
105 $this->assertEventLegacyLogData($expected, $event);
106
107 // Trigger and capture the event for deleting a category and moving it's children to another.
108 $sink = $this->redirectEvents();
109 $category->delete_move($category->id);
110 $events = $sink->get_events();
111 $event = reset($events);
112
113 // Check that the event data is valid.
114 $this->assertInstanceOf('\core\event\course_category_updated', $event);
115 $this->assertEquals(context_coursecat::instance($childcat->id), $event->get_context());
116 $expected = array(SITEID, 'category', 'move', 'editcategory.php?id=' . $childcat->id, $childcat->id);
117 $this->assertEventLegacyLogData($expected, $event);
001f0954
MN
118
119 // Trigger and capture the event for hiding a category.
120 $sink = $this->redirectEvents();
121 $category2->hide();
122 $events = $sink->get_events();
123 $event = reset($events);
124
125 // Check that the event data is valid.
126 $this->assertInstanceOf('\core\event\course_category_updated', $event);
127 $this->assertEquals(context_coursecat::instance($category2->id), $event->get_context());
128 $expected = array(SITEID, 'category', 'hide', 'editcategory.php?id=' . $category2->id, $category2->id);
129 $this->assertEventLegacyLogData($expected, $event);
130
131 // Trigger and capture the event for unhiding a category.
132 $sink = $this->redirectEvents();
133 $category2->show();
134 $events = $sink->get_events();
135 $event = reset($events);
136
137 // Check that the event data is valid.
138 $this->assertInstanceOf('\core\event\course_category_updated', $event);
139 $this->assertEquals(context_coursecat::instance($category2->id), $event->get_context());
140 $expected = array(SITEID, 'category', 'show', 'editcategory.php?id=' . $category2->id, $category2->id);
141 $this->assertEventLegacyLogData($expected, $event);
c4cea8cb 142 }
03ea10e6 143}