Merge branch 'MDL-53547' of git://github.com/timhunt/moodle
[moodle.git] / admin / tool / log / store / database / tests / store_test.php
CommitLineData
bdaf91fd
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 * External database log store tests.
19 *
20 * @package logstore_database
21 * @copyright 2014 Petr Skoda {@link http://skodak.org/}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
0852f9c6 25defined('MOODLE_INTERNAL') || die();
bdaf91fd 26
0852f9c6 27require_once(__DIR__ . '/fixtures/event.php');
a3cfbccd 28require_once(__DIR__ . '/fixtures/store.php');
bdaf91fd
29
30class logstore_database_store_testcase extends advanced_testcase {
31 public function test_log_writing() {
32 global $DB, $CFG;
33 $this->resetAfterTest();
34 $this->preventResetByRollback(); // Logging waits till the transaction gets committed.
35
36 $dbman = $DB->get_manager();
37 $this->assertTrue($dbman->table_exists('logstore_standard_log'));
38 $DB->delete_records('logstore_standard_log');
39
40 $this->setAdminUser();
41 $user1 = $this->getDataGenerator()->create_user();
42 $user2 = $this->getDataGenerator()->create_user();
43 $course1 = $this->getDataGenerator()->create_course();
0852f9c6 44 $module1 = $this->getDataGenerator()->create_module('resource', array('course' => $course1));
bdaf91fd 45 $course2 = $this->getDataGenerator()->create_course();
0852f9c6 46 $module2 = $this->getDataGenerator()->create_module('resource', array('course' => $course2));
bdaf91fd 47
bdae738e
48 // Test all plugins are disabled by this command.
49 set_config('enabled_stores', '', 'tool_log');
50 $manager = get_log_manager(true);
51 $stores = $manager->get_readers();
52 $this->assertCount(0, $stores);
53
bdaf91fd 54 // Fake the settings, we will abuse the standard plugin table here...
4c8c69ea 55 set_config('dbdriver', $CFG->dblibrary . '/' . $CFG->dbtype, 'logstore_database');
bdaf91fd
56 set_config('dbhost', $CFG->dbhost, 'logstore_database');
57 set_config('dbuser', $CFG->dbuser, 'logstore_database');
58 set_config('dbpass', $CFG->dbpass, 'logstore_database');
59 set_config('dbname', $CFG->dbname, 'logstore_database');
0852f9c6 60 set_config('dbtable', $CFG->prefix . 'logstore_standard_log', 'logstore_database');
bdaf91fd
61 if (!empty($CFG->dboptions['dbpersist'])) {
62 set_config('dbpersist', 1, 'logstore_database');
63 } else {
64 set_config('dbpersist', 0, 'logstore_database');
65 }
66 if (!empty($CFG->dboptions['dbsocket'])) {
67 set_config('dbsocket', $CFG->dboptions['dbsocket'], 'logstore_database');
68 } else {
69 set_config('dbsocket', '', 'logstore_database');
70 }
71 if (!empty($CFG->dboptions['dbport'])) {
72 set_config('dbport', $CFG->dboptions['dbport'], 'logstore_database');
73 } else {
74 set_config('dbport', '', 'logstore_database');
75 }
76 if (!empty($CFG->dboptions['dbschema'])) {
77 set_config('dbschema', $CFG->dboptions['dbschema'], 'logstore_database');
78 } else {
79 set_config('dbschema', '', 'logstore_database');
80 }
81 if (!empty($CFG->dboptions['dbcollation'])) {
82 set_config('dbcollation', $CFG->dboptions['dbcollation'], 'logstore_database');
83 } else {
84 set_config('dbcollation', '', 'logstore_database');
85 }
86
87 // Enable logging plugin.
88 set_config('enabled_stores', 'logstore_database', 'tool_log');
89 set_config('buffersize', 0, 'logstore_database');
90 set_config('logguests', 1, 'logstore_database');
91 $manager = get_log_manager(true);
92
93 $stores = $manager->get_readers();
94 $this->assertCount(1, $stores);
95 $this->assertEquals(array('logstore_database'), array_keys($stores));
96 $store = $stores['logstore_database'];
97 $this->assertInstanceOf('logstore_database\log\store', $store);
98 $this->assertInstanceOf('tool_log\log\writer', $store);
bdae738e 99 $this->assertTrue($store->is_logging());
bdaf91fd
100
101 $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
102 $this->assertCount(0, $logs);
103
104 $this->setCurrentTimeStart();
105
106 $this->setUser(0);
0852f9c6
107 $event1 = \logstore_database\event\unittest_executed::create(
108 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
bdaf91fd
109 $event1->trigger();
110
111 $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
112 $this->assertCount(1, $logs);
113
114 $log1 = reset($logs);
115 unset($log1->id);
116 $log1->other = unserialize($log1->other);
117 $log1 = (array)$log1;
118 $data = $event1->get_data();
119 $data['origin'] = 'cli';
120 $data['ip'] = null;
121 $data['realuserid'] = null;
122 $this->assertEquals($data, $log1);
123
124 $this->setAdminUser();
125 \core\session\manager::loginas($user1->id, context_system::instance());
126 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
127
0852f9c6
128 $event2 = \logstore_database\event\unittest_executed::create(
129 array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9)));
bdaf91fd
130 $event2->trigger();
131
2e00d01d 132 \core\session\manager::init_empty_session();
bdaf91fd
133 $this->assertFalse(\core\session\manager::is_loggedinas());
134
135 $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
136 $this->assertCount(3, $logs);
137 array_shift($logs);
138 $log2 = array_shift($logs);
139 $this->assertSame('\core\event\user_loggedinas', $log2->eventname);
140
141 $log3 = array_shift($logs);
142 unset($log3->id);
143 $log3->other = unserialize($log3->other);
144 $log3 = (array)$log3;
145 $data = $event2->get_data();
146 $data['origin'] = 'cli';
147 $data['ip'] = null;
148 $data['realuserid'] = 2;
149 $this->assertEquals($data, $log3);
150
151 // Test reading.
993d8d83 152 $this->assertSame(3, $store->get_events_select_count('', array()));
a8d7d617 153 $events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0); // Is actually sorted by "timecreated ASC, id ASC".
bdaf91fd
154 $this->assertCount(3, $events);
155 $resev1 = array_shift($events);
156 array_shift($events);
157 $resev2 = array_shift($events);
158 $this->assertEquals($event1->get_data(), $resev1->get_data());
159 $this->assertEquals($event2->get_data(), $resev2->get_data());
160
161 // Test buffering.
162 set_config('buffersize', 3, 'logstore_database');
163 $manager = get_log_manager(true);
164 $stores = $manager->get_readers();
165 /** @var \logstore_database\log\store $store */
166 $store = $stores['logstore_database'];
167 $DB->delete_records('logstore_standard_log');
168
0852f9c6
169 \logstore_database\event\unittest_executed::create(
170 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 171 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
0852f9c6
172 \logstore_database\event\unittest_executed::create(
173 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
174 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
175 $store->flush();
176 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
0852f9c6
177 \logstore_database\event\unittest_executed::create(
178 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 179 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
0852f9c6
180 \logstore_database\event\unittest_executed::create(
181 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 182 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
0852f9c6
183 \logstore_database\event\unittest_executed::create(
184 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 185 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
0852f9c6
186 \logstore_database\event\unittest_executed::create(
187 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 188 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
0852f9c6
189 \logstore_database\event\unittest_executed::create(
190 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 191 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
0852f9c6
192 \logstore_database\event\unittest_executed::create(
193 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
194 $this->assertEquals(8, $DB->count_records('logstore_standard_log'));
195
196 // Test guest logging setting.
197 set_config('logguests', 0, 'logstore_database');
198 set_config('buffersize', 0, 'logstore_database');
199 get_log_manager(true);
200 $DB->delete_records('logstore_standard_log');
201 get_log_manager(true);
202
203 $this->setUser(null);
0852f9c6
204 \logstore_database\event\unittest_executed::create(
205 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
206 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
207
208 $this->setGuestUser();
0852f9c6
209 \logstore_database\event\unittest_executed::create(
210 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
211 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
212
213 $this->setUser($user1);
0852f9c6
214 \logstore_database\event\unittest_executed::create(
215 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
216 $this->assertEquals(1, $DB->count_records('logstore_standard_log'));
217
218 $this->setUser($user2);
0852f9c6
219 \logstore_database\event\unittest_executed::create(
220 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
221 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
222
223 set_config('enabled_stores', '', 'tool_log');
224 get_log_manager(true);
225 }
96c8d59d 226
a3cfbccd
AA
227 /**
228 * Test method is_event_ignored.
229 */
230 public function test_is_event_ignored() {
231 $this->resetAfterTest();
232
233 // Test guest filtering.
234 set_config('logguests', 0, 'logstore_database');
235 $this->setGuestUser();
236 $event = \logstore_database\event\unittest_executed::create(
237 array('context' => context_system::instance(), 'other' => array('sample' => 5, 'xx' => 10)));
238 $logmanager = get_log_manager();
239 $store = new \logstore_database\test\store($logmanager);
240 $this->assertTrue($store->is_event_ignored($event));
241
242 set_config('logguests', 1, 'logstore_database');
243 $store = new \logstore_database\test\store($logmanager); // Reload.
244 $this->assertFalse($store->is_event_ignored($event));
245
246 // Test action/level filtering.
247 set_config('includelevels', '', 'logstore_database');
248 set_config('includeactions', '', 'logstore_database');
249 $store = new \logstore_database\test\store($logmanager); // Reload.
250 $this->assertTrue($store->is_event_ignored($event));
251
252 set_config('includelevels', '0,1', 'logstore_database');
253 $store = new \logstore_database\test\store($logmanager); // Reload.
254 $this->assertTrue($store->is_event_ignored($event));
255
256 set_config('includelevels', '0,1,2', 'logstore_database');
257 $store = new \logstore_database\test\store($logmanager); // Reload.
258 $this->assertFalse($store->is_event_ignored($event));
259
260 set_config('includelevels', '', 'logstore_database');
261 set_config('includeactions', 'c,r,d', 'logstore_database');
262 $store = new \logstore_database\test\store($logmanager); // Reload.
263 $this->assertTrue($store->is_event_ignored($event));
264
265 set_config('includeactions', 'c,r,u,d', 'logstore_database');
266 $store = new \logstore_database\test\store($logmanager); // Reload.
267 $this->assertFalse($store->is_event_ignored($event));
268 }
269
96c8d59d
AA
270 /**
271 * Test logmanager::get_supported_reports returns all reports that require this store.
272 */
273 public function test_get_supported_reports() {
274 $logmanager = get_log_manager();
275 $allreports = \core_component::get_plugin_list('report');
276
277 $supportedreports = array(
16e59c6a
AA
278 'report_log' => '/report/log',
279 'report_loglive' => '/report/loglive'
96c8d59d
AA
280 );
281
282 // Make sure all supported reports are installed.
283 $expectedreports = array_keys(array_intersect_key($allreports, $supportedreports));
284 $reports = $logmanager->get_supported_reports('logstore_database');
285 $reports = array_keys($reports);
286 foreach ($expectedreports as $expectedreport) {
287 $this->assertContains($expectedreport, $reports);
288 }
289 }
bdaf91fd 290}