MDL-41268 fix cron execution in new log manager
[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
25defined('MOODLE_INTERNAL') || die;
26
27require_once (__DIR__ . '/fixtures/event.php');
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();
43 $module1 = $this->getDataGenerator()->create_module('resource', array('course'=>$course1));
44 $course2 = $this->getDataGenerator()->create_course();
45 $module2 = $this->getDataGenerator()->create_module('resource', array('course'=>$course2));
46
47 // Fake the settings, we will abuse the standard plugin table here...
48 $parts = explode('_', get_class($DB));
49 set_config('dbdriver', $parts[1].'/'.$parts[0], 'logstore_database');
50 set_config('dbhost', $CFG->dbhost, 'logstore_database');
51 set_config('dbuser', $CFG->dbuser, 'logstore_database');
52 set_config('dbpass', $CFG->dbpass, 'logstore_database');
53 set_config('dbname', $CFG->dbname, 'logstore_database');
54 set_config('dbtable', $CFG->prefix.'logstore_standard_log', 'logstore_database');
55 if (!empty($CFG->dboptions['dbpersist'])) {
56 set_config('dbpersist', 1, 'logstore_database');
57 } else {
58 set_config('dbpersist', 0, 'logstore_database');
59 }
60 if (!empty($CFG->dboptions['dbsocket'])) {
61 set_config('dbsocket', $CFG->dboptions['dbsocket'], 'logstore_database');
62 } else {
63 set_config('dbsocket', '', 'logstore_database');
64 }
65 if (!empty($CFG->dboptions['dbport'])) {
66 set_config('dbport', $CFG->dboptions['dbport'], 'logstore_database');
67 } else {
68 set_config('dbport', '', 'logstore_database');
69 }
70 if (!empty($CFG->dboptions['dbschema'])) {
71 set_config('dbschema', $CFG->dboptions['dbschema'], 'logstore_database');
72 } else {
73 set_config('dbschema', '', 'logstore_database');
74 }
75 if (!empty($CFG->dboptions['dbcollation'])) {
76 set_config('dbcollation', $CFG->dboptions['dbcollation'], 'logstore_database');
77 } else {
78 set_config('dbcollation', '', 'logstore_database');
79 }
80
81 // Enable logging plugin.
82 set_config('enabled_stores', 'logstore_database', 'tool_log');
83 set_config('buffersize', 0, 'logstore_database');
84 set_config('logguests', 1, 'logstore_database');
85 $manager = get_log_manager(true);
86
87 $stores = $manager->get_readers();
88 $this->assertCount(1, $stores);
89 $this->assertEquals(array('logstore_database'), array_keys($stores));
90 $store = $stores['logstore_database'];
91 $this->assertInstanceOf('logstore_database\log\store', $store);
92 $this->assertInstanceOf('tool_log\log\writer', $store);
93
94 $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
95 $this->assertCount(0, $logs);
96
97 $this->setCurrentTimeStart();
98
99 $this->setUser(0);
100 $event1 = \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)));
101 $event1->trigger();
102
103 $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
104 $this->assertCount(1, $logs);
105
106 $log1 = reset($logs);
107 unset($log1->id);
108 $log1->other = unserialize($log1->other);
109 $log1 = (array)$log1;
110 $data = $event1->get_data();
111 $data['origin'] = 'cli';
112 $data['ip'] = null;
113 $data['realuserid'] = null;
114 $this->assertEquals($data, $log1);
115
116 $this->setAdminUser();
117 \core\session\manager::loginas($user1->id, context_system::instance());
118 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
119
120 $event2 = \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module2->cmid), 'other'=>array('sample'=>6, 'xx'=>9)));
121 $event2->trigger();
122
123 $_SESSION['SESSION'] = new \stdClass();
124 $this->setUser(0);
125 $this->assertFalse(\core\session\manager::is_loggedinas());
126
127 $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
128 $this->assertCount(3, $logs);
129 array_shift($logs);
130 $log2 = array_shift($logs);
131 $this->assertSame('\core\event\user_loggedinas', $log2->eventname);
132
133 $log3 = array_shift($logs);
134 unset($log3->id);
135 $log3->other = unserialize($log3->other);
136 $log3 = (array)$log3;
137 $data = $event2->get_data();
138 $data['origin'] = 'cli';
139 $data['ip'] = null;
140 $data['realuserid'] = 2;
141 $this->assertEquals($data, $log3);
142
143 // Test reading.
144 $events = $store->get_events('', array(), 'id', 0, 0);
145 $this->assertCount(3, $events);
146 $resev1 = array_shift($events);
147 array_shift($events);
148 $resev2 = array_shift($events);
149 $this->assertEquals($event1->get_data(), $resev1->get_data());
150 $this->assertEquals($event2->get_data(), $resev2->get_data());
151
152 // Test buffering.
153 set_config('buffersize', 3, 'logstore_database');
154 $manager = get_log_manager(true);
155 $stores = $manager->get_readers();
156 /** @var \logstore_database\log\store $store */
157 $store = $stores['logstore_database'];
158 $DB->delete_records('logstore_standard_log');
159
160 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
161 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
162 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
163 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
164 $store->flush();
165 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
166 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
167 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
168 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
169 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
170 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
171 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
172 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
173 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
174 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
175 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
176 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
177 $this->assertEquals(8, $DB->count_records('logstore_standard_log'));
178
179 // Test guest logging setting.
180 set_config('logguests', 0, 'logstore_database');
181 set_config('buffersize', 0, 'logstore_database');
182 get_log_manager(true);
183 $DB->delete_records('logstore_standard_log');
184 get_log_manager(true);
185
186 $this->setUser(null);
187 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
188 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
189
190 $this->setGuestUser();
191 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
192 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
193
194 $this->setUser($user1);
195 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
196 $this->assertEquals(1, $DB->count_records('logstore_standard_log'));
197
198 $this->setUser($user2);
199 \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
200 $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
201
202 set_config('enabled_stores', '', 'tool_log');
203 get_log_manager(true);
204 }
205}