MDL-58651 logstore_database: Add ability to not send database options
[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 }
556ceb20
AO
86 if (!empty($CFG->dboptions['dbhandlesoptions'])) {
87 set_config('dbhandlesoptions', $CFG->dboptions['dbhandlesoptions'], 'logstore_database');
88 } else {
89 set_config('dbhandlesoptions', false, 'logstore_database');
90 }
bdaf91fd
91
92 // Enable logging plugin.
93 set_config('enabled_stores', 'logstore_database', 'tool_log');
94 set_config('buffersize', 0, 'logstore_database');
95 set_config('logguests', 1, 'logstore_database');
96 $manager = get_log_manager(true);
97
98 $stores = $manager->get_readers();
99 $this->assertCount(1, $stores);
100 $this->assertEquals(array('logstore_database'), array_keys($stores));
101 $store = $stores['logstore_database'];
102 $this->assertInstanceOf('logstore_database\log\store', $store);
103 $this->assertInstanceOf('tool_log\log\writer', $store);
bdae738e 104 $this->assertTrue($store->is_logging());
bdaf91fd
105
106 $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
107 $this->assertCount(0, $logs);
108
109 $this->setCurrentTimeStart();
110
111 $this->setUser(0);
0852f9c6
112 $event1 = \logstore_database\event\unittest_executed::create(
113 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
bdaf91fd
114 $event1->trigger();
115
116 $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
117 $this->assertCount(1, $logs);
118
119 $log1 = reset($logs);
120 unset($log1->id);
121 $log1->other = unserialize($log1->other);
122 $log1 = (array)$log1;
123 $data = $event1->get_data();
124 $data['origin'] = 'cli';
125 $data['ip'] = null;
126 $data['realuserid'] = null;
127 $this->assertEquals($data, $log1);
128
129 $this->setAdminUser();
130 \core\session\manager::loginas($user1->id, context_system::instance());
131 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
132
0852f9c6
133 $event2 = \logstore_database\event\unittest_executed::create(
134 array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9)));
bdaf91fd
135 $event2->trigger();
136
2e00d01d 137 \core\session\manager::init_empty_session();
bdaf91fd
138 $this->assertFalse(\core\session\manager::is_loggedinas());
139
140 $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
141 $this->assertCount(3, $logs);
142 array_shift($logs);
143 $log2 = array_shift($logs);
144 $this->assertSame('\core\event\user_loggedinas', $log2->eventname);
145
146 $log3 = array_shift($logs);
147 unset($log3->id);
148 $log3->other = unserialize($log3->other);
149 $log3 = (array)$log3;
150 $data = $event2->get_data();
151 $data['origin'] = 'cli';
152 $data['ip'] = null;
153 $data['realuserid'] = 2;
154 $this->assertEquals($data, $log3);
155
156 // Test reading.
993d8d83 157 $this->assertSame(3, $store->get_events_select_count('', array()));
a8d7d617 158 $events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0); // Is actually sorted by "timecreated ASC, id ASC".
bdaf91fd
159 $this->assertCount(3, $events);
160 $resev1 = array_shift($events);
161 array_shift($events);
162 $resev2 = array_shift($events);
163 $this->assertEquals($event1->get_data(), $resev1->get_data());
164 $this->assertEquals($event2->get_data(), $resev2->get_data());
165
166 // Test buffering.
167 set_config('buffersize', 3, 'logstore_database');
168 $manager = get_log_manager(true);
169 $stores = $manager->get_readers();
170 /** @var \logstore_database\log\store $store */
171 $store = $stores['logstore_database'];
172 $DB->delete_records('logstore_standard_log');
173
0852f9c6
174 \logstore_database\event\unittest_executed::create(
175 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 176 $this->assertEquals(0, $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(0, $DB->count_records('logstore_standard_log'));
180 $store->flush();
181 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
0852f9c6
182 \logstore_database\event\unittest_executed::create(
183 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 184 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
0852f9c6
185 \logstore_database\event\unittest_executed::create(
186 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 187 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
0852f9c6
188 \logstore_database\event\unittest_executed::create(
189 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 190 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
0852f9c6
191 \logstore_database\event\unittest_executed::create(
192 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 193 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
0852f9c6
194 \logstore_database\event\unittest_executed::create(
195 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 196 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
0852f9c6
197 \logstore_database\event\unittest_executed::create(
198 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
199 $this->assertEquals(8, $DB->count_records('logstore_standard_log'));
200
201 // Test guest logging setting.
202 set_config('logguests', 0, 'logstore_database');
203 set_config('buffersize', 0, 'logstore_database');
204 get_log_manager(true);
205 $DB->delete_records('logstore_standard_log');
206 get_log_manager(true);
207
208 $this->setUser(null);
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->setGuestUser();
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(0, $DB->count_records('logstore_standard_log'));
217
218 $this->setUser($user1);
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(1, $DB->count_records('logstore_standard_log'));
222
223 $this->setUser($user2);
0852f9c6
224 \logstore_database\event\unittest_executed::create(
225 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
226 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
227
228 set_config('enabled_stores', '', 'tool_log');
229 get_log_manager(true);
230 }
96c8d59d 231
a3cfbccd
AA
232 /**
233 * Test method is_event_ignored.
234 */
235 public function test_is_event_ignored() {
236 $this->resetAfterTest();
237
238 // Test guest filtering.
239 set_config('logguests', 0, 'logstore_database');
240 $this->setGuestUser();
241 $event = \logstore_database\event\unittest_executed::create(
242 array('context' => context_system::instance(), 'other' => array('sample' => 5, 'xx' => 10)));
243 $logmanager = get_log_manager();
244 $store = new \logstore_database\test\store($logmanager);
245 $this->assertTrue($store->is_event_ignored($event));
246
247 set_config('logguests', 1, 'logstore_database');
248 $store = new \logstore_database\test\store($logmanager); // Reload.
249 $this->assertFalse($store->is_event_ignored($event));
250
251 // Test action/level filtering.
252 set_config('includelevels', '', 'logstore_database');
253 set_config('includeactions', '', 'logstore_database');
254 $store = new \logstore_database\test\store($logmanager); // Reload.
255 $this->assertTrue($store->is_event_ignored($event));
256
257 set_config('includelevels', '0,1', 'logstore_database');
258 $store = new \logstore_database\test\store($logmanager); // Reload.
259 $this->assertTrue($store->is_event_ignored($event));
260
261 set_config('includelevels', '0,1,2', 'logstore_database');
262 $store = new \logstore_database\test\store($logmanager); // Reload.
263 $this->assertFalse($store->is_event_ignored($event));
264
265 set_config('includelevels', '', 'logstore_database');
266 set_config('includeactions', 'c,r,d', 'logstore_database');
267 $store = new \logstore_database\test\store($logmanager); // Reload.
268 $this->assertTrue($store->is_event_ignored($event));
269
270 set_config('includeactions', 'c,r,u,d', 'logstore_database');
271 $store = new \logstore_database\test\store($logmanager); // Reload.
272 $this->assertFalse($store->is_event_ignored($event));
273 }
274
96c8d59d
AA
275 /**
276 * Test logmanager::get_supported_reports returns all reports that require this store.
277 */
278 public function test_get_supported_reports() {
279 $logmanager = get_log_manager();
280 $allreports = \core_component::get_plugin_list('report');
281
282 $supportedreports = array(
16e59c6a
AA
283 'report_log' => '/report/log',
284 'report_loglive' => '/report/loglive'
96c8d59d
AA
285 );
286
287 // Make sure all supported reports are installed.
288 $expectedreports = array_keys(array_intersect_key($allreports, $supportedreports));
289 $reports = $logmanager->get_supported_reports('logstore_database');
290 $reports = array_keys($reports);
291 foreach ($expectedreports as $expectedreport) {
292 $this->assertContains($expectedreport, $reports);
293 }
294 }
bdaf91fd 295}