MDL-44596 logging: Add callbacks to all reports that support log stores
[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');
bdaf91fd
28
29class logstore_database_store_testcase extends advanced_testcase {
30 public function test_log_writing() {
31 global $DB, $CFG;
32 $this->resetAfterTest();
33 $this->preventResetByRollback(); // Logging waits till the transaction gets committed.
34
35 $dbman = $DB->get_manager();
36 $this->assertTrue($dbman->table_exists('logstore_standard_log'));
37 $DB->delete_records('logstore_standard_log');
38
39 $this->setAdminUser();
40 $user1 = $this->getDataGenerator()->create_user();
41 $user2 = $this->getDataGenerator()->create_user();
42 $course1 = $this->getDataGenerator()->create_course();
0852f9c6 43 $module1 = $this->getDataGenerator()->create_module('resource', array('course' => $course1));
bdaf91fd 44 $course2 = $this->getDataGenerator()->create_course();
0852f9c6 45 $module2 = $this->getDataGenerator()->create_module('resource', array('course' => $course2));
bdaf91fd 46
bdae738e
47 // Test all plugins are disabled by this command.
48 set_config('enabled_stores', '', 'tool_log');
49 $manager = get_log_manager(true);
50 $stores = $manager->get_readers();
51 $this->assertCount(0, $stores);
52
bdaf91fd
53 // Fake the settings, we will abuse the standard plugin table here...
54 $parts = explode('_', get_class($DB));
0852f9c6 55 set_config('dbdriver', $parts[1] . '/' . $parts[0], '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
132 $_SESSION['SESSION'] = new \stdClass();
133 $this->setUser(0);
134 $this->assertFalse(\core\session\manager::is_loggedinas());
135
136 $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
137 $this->assertCount(3, $logs);
138 array_shift($logs);
139 $log2 = array_shift($logs);
140 $this->assertSame('\core\event\user_loggedinas', $log2->eventname);
141
142 $log3 = array_shift($logs);
143 unset($log3->id);
144 $log3->other = unserialize($log3->other);
145 $log3 = (array)$log3;
146 $data = $event2->get_data();
147 $data['origin'] = 'cli';
148 $data['ip'] = null;
149 $data['realuserid'] = 2;
150 $this->assertEquals($data, $log3);
151
152 // Test reading.
993d8d83
153 $this->assertSame(3, $store->get_events_select_count('', array()));
154 $events = $store->get_events_select('', array(), 'id', 0, 0);
bdaf91fd
155 $this->assertCount(3, $events);
156 $resev1 = array_shift($events);
157 array_shift($events);
158 $resev2 = array_shift($events);
159 $this->assertEquals($event1->get_data(), $resev1->get_data());
160 $this->assertEquals($event2->get_data(), $resev2->get_data());
161
162 // Test buffering.
163 set_config('buffersize', 3, 'logstore_database');
164 $manager = get_log_manager(true);
165 $stores = $manager->get_readers();
166 /** @var \logstore_database\log\store $store */
167 $store = $stores['logstore_database'];
168 $DB->delete_records('logstore_standard_log');
169
0852f9c6
170 \logstore_database\event\unittest_executed::create(
171 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 172 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
0852f9c6
173 \logstore_database\event\unittest_executed::create(
174 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
175 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
176 $store->flush();
177 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
0852f9c6
178 \logstore_database\event\unittest_executed::create(
179 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 180 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
0852f9c6
181 \logstore_database\event\unittest_executed::create(
182 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 183 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
0852f9c6
184 \logstore_database\event\unittest_executed::create(
185 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 186 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
0852f9c6
187 \logstore_database\event\unittest_executed::create(
188 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 189 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
0852f9c6
190 \logstore_database\event\unittest_executed::create(
191 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd 192 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
0852f9c6
193 \logstore_database\event\unittest_executed::create(
194 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
195 $this->assertEquals(8, $DB->count_records('logstore_standard_log'));
196
197 // Test guest logging setting.
198 set_config('logguests', 0, 'logstore_database');
199 set_config('buffersize', 0, 'logstore_database');
200 get_log_manager(true);
201 $DB->delete_records('logstore_standard_log');
202 get_log_manager(true);
203
204 $this->setUser(null);
0852f9c6
205 \logstore_database\event\unittest_executed::create(
206 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
207 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
208
209 $this->setGuestUser();
0852f9c6
210 \logstore_database\event\unittest_executed::create(
211 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
212 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
213
214 $this->setUser($user1);
0852f9c6
215 \logstore_database\event\unittest_executed::create(
216 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
217 $this->assertEquals(1, $DB->count_records('logstore_standard_log'));
218
219 $this->setUser($user2);
0852f9c6
220 \logstore_database\event\unittest_executed::create(
221 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
bdaf91fd
222 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
223
224 set_config('enabled_stores', '', 'tool_log');
225 get_log_manager(true);
226 }
227}