2 // This file is part of Moodle - http://moodle.org/
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.
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 namespace core_tests\event
;
20 * Fixtures for new event testing.
24 * @copyright 2013 Petr Skoda {@link http://skodak.org}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') ||
die();
31 class unittest_executed
extends \core\event\base
{
34 public static function get_name() {
38 public function get_description() {
42 protected function init() {
43 $this->data
['crud'] = 'u';
44 $this->data
['edulevel'] = self
::LEVEL_PARTICIPATING
;
47 public function get_url() {
48 return new \
moodle_url('/somepath/somefile.php', array('id'=>$this->data
['other']['sample']));
51 public static function get_legacy_eventname() {
55 protected function get_legacy_eventdata() {
56 return array($this->data
['courseid'], $this->data
['other']['sample']);
59 protected function get_legacy_logdata() {
60 return array($this->data
['courseid'], 'core_unittest', 'view', 'unittest.php?id='.$this->data
['other']['sample']);
65 class unittest_observer
{
66 public static $info = array();
67 public static $event = array();
69 public static function reset() {
70 self
::$info = array();
71 self
::$event = array();
74 public static function observe_one(unittest_executed
$event) {
75 self
::$info[] = 'observe_one-'.$event->other
['sample'];
76 self
::$event[] = $event;
79 public static function external_observer(unittest_executed
$event) {
80 self
::$info[] = 'external_observer-'.$event->other
['sample'];
81 self
::$event[] = $event;
84 public static function broken_observer(unittest_executed
$event) {
85 self
::$info[] = 'broken_observer-'.$event->other
['sample'];
86 self
::$event[] = $event;
87 throw new \
Exception('someerror');
90 public static function observe_all(\core\event\base
$event) {
91 if (!($event instanceof unittest_executed
)) {
92 self
::$info[] = 'observe_all-unknown';
93 self
::$event[] = $event;
96 self
::$event[] = $event;
97 if (!empty($event->nest
)) {
98 self
::$info[] = 'observe_all-nesting-'.$event->other
['sample'];
99 unittest_executed
::create(array('context'=>\context_system
::instance(), 'other'=>array('sample'=>666, 'xx'=>666)))->trigger();
101 self
::$info[] = 'observe_all-'.$event->other
['sample'];
105 public static function observe_all_alt(\core\event\base
$event) {
106 self
::$info[] = 'observe_all_alt';
107 self
::$event[] = $event;
110 public static function legacy_handler($data) {
111 self
::$info[] = 'legacy_handler-'.$data[0];
112 self
::$event[] = $data;
116 class bad_event1
extends \core\event\base
{
117 protected function init() {
118 //$this->data['crud'] = 'u';
119 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
123 class bad_event2
extends \core\event\base
{
124 protected function init() {
125 $this->data
['crud'] = 'u';
126 //$this->data['edulevel'] = 10;
130 class bad_event2b
extends \core\event\base
{
131 protected function init() {
132 $this->data
['crud'] = 'u';
133 // Invalid level value.
134 $this->data
['edulevel'] = -1;
138 class bad_event3
extends \core\event\base
{
139 protected function init() {
140 $this->data
['crud'] = 'u';
141 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
142 unset($this->data
['courseid']);
146 class bad_event4
extends \core\event\base
{
147 protected function init() {
148 $this->data
['crud'] = 'u';
149 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
150 $this->data
['xxx'] = 1;
154 class bad_event5
extends \core\event\base
{
155 protected function init() {
156 $this->data
['crud'] = 'x';
157 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
161 class bad_event6
extends \core\event\base
{
162 protected function init() {
163 $this->data
['crud'] = 'c';
164 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
165 $this->data
['objecttable'] = 'xxx_xxx_xx';
169 class bad_event7
extends \core\event\base
{
170 protected function init() {
171 $this->data
['crud'] = 'c';
172 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
173 $this->data
['objecttable'] = null;
177 class bad_event8
extends \core\event\base
{
178 protected function init() {
179 $this->data
['crud'] = 'c';
180 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
181 $this->data
['objecttable'] = 'user';
185 class problematic_event1
extends \core\event\base
{
186 protected function init() {
187 $this->data
['crud'] = 'u';
188 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
192 class problematic_event2
extends \core\event\base
{
193 protected function init() {
194 $this->data
['crud'] = 'c';
195 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
196 $this->context
= \context_system
::instance();
200 class problematic_event3
extends \core\event\base
{
201 protected function init() {
202 $this->data
['crud'] = 'c';
203 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
204 $this->context
= \context_system
::instance();
207 protected function validate_data() {
208 parent
::validate_data();
209 if (empty($this->data
['other'])) {
210 debugging('other is missing');
215 class deprecated_event1
extends \core\event\base
{
216 protected function init() {
217 $this->data
['crud'] = 'c';
218 $this->data
['level'] = self
::LEVEL_TEACHING
; // Tests edulevel hint.
219 $this->context
= \context_system
::instance();
223 class noname_event
extends \core\event\base
{
225 protected function init() {
226 $this->data
['crud'] = 'c';
227 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
228 $this->context
= \context_system
::instance();
233 * Class course_module_viewed.
235 * Wrapper for testing \core\event\course_module_viewed.
237 class course_module_viewed
extends \core\event\course_module_viewed
{
238 protected function init() {
239 $this->data
['crud'] = 'r';
240 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
241 $this->data
['objecttable'] = 'feedback';
246 * Class course_module_viewed_noinit.
248 * Wrapper for testing \core\event\course_module_viewed.
250 class course_module_viewed_noinit
extends \core\event\course_module_viewed
{
254 * Event to test context used in event functions
256 class context_used_in_event
extends \core\event\base
{
257 public function get_description() {
258 return $this->context
->instanceid
. " Description";
261 protected function init() {
262 $this->data
['crud'] = 'u';
263 $this->data
['edulevel'] = self
::LEVEL_PARTICIPATING
;
264 $this->context
= \context_system
::instance();
267 public function get_url() {
268 return new \
moodle_url('/somepath/somefile.php', array('id' => $this->context
->instanceid
));
271 protected function get_legacy_eventdata() {
272 return array($this->data
['courseid'], $this->context
->instanceid
);
275 protected function get_legacy_logdata() {
276 return array($this->data
['courseid'], 'core_unittest', 'view', 'unittest.php?id=' . $this->context
->instanceid
);
281 * This is an explanation of the event.
282 * - I'm making a point here.
283 * - I have a second {@link something} point here.
284 * - whitespace is intentional to test it's removal.
287 * I have something else *Yeah* that.
293 * @copyright 2014 Adrian Greeve <adrian@moodle.com>
294 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
296 class full_docblock
extends \core\event\base
{
298 protected function init() {
304 * We have only the description in the docblock
307 class docblock_test2
extends \core\event\base
{
309 protected function init() {
315 * Calendar event created event.
317 * @property-read array $other {
318 * Extra information about the event.
320 * - int timestart: timestamp for event time start.
321 * - string name: Name of the event.
322 * - int repeatid: Id of the parent event if present, else 0.
327 * @copyright 2014 onwards Adrian Greeve
328 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
330 class docblock_test3
extends \core\event\base
{
332 protected function init() {
337 class static_info_viewing
extends \core\event\base
{
339 protected function init() {
340 $this->data
['crud'] = 'r';
341 $this->data
['edulevel'] = self
::LEVEL_OTHER
;
342 $this->data
['objecttable'] = 'mod_unittest';