event_fixtures.php 7.81 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.

namespace core_tests\event;

/**
 * Fixtures for new event testing.
 *
 * @package    core
 * @category   phpunit
 * @copyright  2013 Petr Skoda {@link http://skodak.org}
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();


class unittest_executed extends \core\event\base {
    public $nest = false;

34
35
36
37
38
39
40
41
    public static function get_name() {
        return 'xxx';
    }

    public function get_description() {
        return 'yyy';
    }

42
43
    protected function init() {
        $this->data['crud'] = 'u';
44
        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
45
46
47
    }

    public function get_url() {
Petr Škoda's avatar
Petr Škoda committed
48
        return new \moodle_url('/somepath/somefile.php', array('id'=>$this->data['other']['sample']));
49
50
    }

51
    public static function get_legacy_eventname() {
52
53
54
        return 'test_legacy';
    }

55
    protected function get_legacy_eventdata() {
56
        return array($this->data['courseid'], $this->data['other']['sample']);
57
58
    }

59
    protected function get_legacy_logdata() {
60
        return array($this->data['courseid'], 'core_unittest', 'view', 'unittest.php?id='.$this->data['other']['sample']);
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    }
}


class unittest_observer {
    public static $info = array();
    public static $event = array();

    public static function reset() {
        self::$info = array();
        self::$event = array();
    }

    public static function observe_one(unittest_executed $event) {
        self::$info[] = 'observe_one-'.$event->courseid;
        self::$event[] = $event;
    }

79
    public static function external_observer(unittest_executed $event) {
80
81
82
83
        self::$info[] = 'external_observer-'.$event->courseid;
        self::$event[] = $event;
    }

84
    public static function broken_observer(unittest_executed $event) {
85
86
87
88
89
        self::$info[] = 'broken_observer-'.$event->courseid;
        self::$event[] = $event;
        throw new \Exception('someerror');
    }

90
91
92
93
94
95
    public static function observe_all(\core\event\base $event) {
        if (!($event instanceof unittest_executed)) {
            self::$info[] = 'observe_all-unknown';
            self::$event[] = $event;
            return;
        }
96
        self::$event[] = $event;
97
        if (!empty($event->nest)) {
98
            self::$info[] = 'observe_all-nesting-'.$event->courseid;
99
            unittest_executed::create(array('courseid'=>3, 'context'=>\context_system::instance(), 'other'=>array('sample'=>666, 'xx'=>666)))->trigger();
100
101
102
103
104
        } else {
            self::$info[] = 'observe_all-'.$event->courseid;
        }
    }

105
106
107
108
109
    public static function observe_all_alt(\core\event\base $event) {
        self::$info[] = 'observe_all_alt';
        self::$event[] = $event;
    }

110
111
112
113
114
115
116
117
118
    public static function legacy_handler($data) {
        self::$info[] = 'legacy_handler-'.$data[0];
        self::$event[] = $data;
    }
}

class bad_event1 extends \core\event\base {
    protected function init() {
        //$this->data['crud'] = 'u';
119
        $this->data['edulevel'] = self::LEVEL_OTHER;
120
121
122
123
124
125
    }
}

class bad_event2 extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'u';
126
        //$this->data['edulevel'] = 10;
127
128
129
    }
}

130
131
132
133
class bad_event2b extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'u';
        // Invalid level value.
134
        $this->data['edulevel'] = -1;
135
136
137
    }
}

138
139
140
class bad_event3 extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'u';
141
        $this->data['edulevel'] = self::LEVEL_OTHER;
142
143
144
145
146
147
148
        unset($this->data['courseid']);
    }
}

class bad_event4 extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'u';
149
        $this->data['edulevel'] = self::LEVEL_OTHER;
150
151
152
153
154
155
156
        $this->data['xxx'] = 1;
    }
}

class bad_event5 extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'x';
157
        $this->data['edulevel'] = self::LEVEL_OTHER;
158
159
160
    }
}

161
162
163
class bad_event6 extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'c';
164
        $this->data['edulevel'] = self::LEVEL_OTHER;
165
166
167
168
169
170
171
        $this->data['objecttable'] = 'xxx_xxx_xx';
    }
}

class bad_event7 extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'c';
172
        $this->data['edulevel'] = self::LEVEL_OTHER;
173
174
175
176
        $this->data['objecttable'] = null;
    }
}

177
178
179
class bad_event8 extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'c';
180
        $this->data['edulevel'] = self::LEVEL_OTHER;
181
182
183
184
        $this->data['objecttable'] = 'user';
    }
}

185
186
187
class problematic_event1 extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'u';
188
        $this->data['edulevel'] = self::LEVEL_OTHER;
189
190
    }
}
191
192
193
194

class problematic_event2 extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'c';
195
        $this->data['edulevel'] = self::LEVEL_OTHER;
196
197
198
199
        $this->context = \context_system::instance();
    }
}

200
class problematic_event3 extends \core\event\base {
201
202
    protected function init() {
        $this->data['crud'] = 'c';
203
        $this->data['edulevel'] = self::LEVEL_OTHER;
204
205
206
207
208
209
210
211
212
        $this->context = \context_system::instance();
    }

    protected function validate_data() {
        if (empty($this->data['other'])) {
            debugging('other is missing');
        }
    }
}
213

214
215
216
217
218
219
220
221
class deprecated_event1 extends \core\event\base {
    protected function init() {
        $this->data['crud'] = 'c';
        $this->data['level'] = self::LEVEL_TEACHING; // Tests edulevel hint.
        $this->context = \context_system::instance();
    }
}

222
223
224
225
class noname_event extends \core\event\base {

    protected function init() {
        $this->data['crud'] = 'c';
226
        $this->data['edulevel'] = self::LEVEL_OTHER;
227
228
229
        $this->context = \context_system::instance();
    }
}
230

231
232
233
234
235
236
237
238
/**
 * Class course_module_viewed.
 *
 * Wrapper for testing \core\event\course_module_viewed.
 */
class course_module_viewed extends \core\event\course_module_viewed {
    protected function init() {
        $this->data['crud'] = 'r';
239
        $this->data['edulevel'] = self::LEVEL_OTHER;
240
241
242
243
244
245
246
247
248
249
250
251
        $this->data['objecttable'] = 'feedback';
    }
}

/**
 * Class course_module_viewed_noinit.
 *
 * Wrapper for testing \core\event\course_module_viewed.
 */
class course_module_viewed_noinit extends \core\event\course_module_viewed {
}

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
 * Event to test context used in event functions
 */
class context_used_in_event extends \core\event\base {
    public function get_description() {
        return $this->context->instanceid . " Description";
    }

    protected function init() {
        $this->data['crud'] = 'u';
        $this->data['edulevel'] = self::LEVEL_PARTICIPATING;
        $this->context = \context_system::instance();
    }

    public function get_url() {
        return new \moodle_url('/somepath/somefile.php', array('id' => $this->context->instanceid));
    }

    protected function get_legacy_eventdata() {
        return array($this->data['courseid'], $this->context->instanceid);
    }

    protected function get_legacy_logdata() {
        return array($this->data['courseid'], 'core_unittest', 'view', 'unittest.php?id=' . $this->context->instanceid);
    }
}