Merge branch 'MDL-53547' of git://github.com/timhunt/moodle
[moodle.git] / admin / tool / log / store / database / tests / store_test.php
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/>.
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  */
25 defined('MOODLE_INTERNAL') || die();
27 require_once(__DIR__ . '/fixtures/event.php');
28 require_once(__DIR__ . '/fixtures/store.php');
30 class 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.
36         $dbman = $DB->get_manager();
37         $this->assertTrue($dbman->table_exists('logstore_standard_log'));
38         $DB->delete_records('logstore_standard_log');
40         $this->setAdminUser();
41         $user1 = $this->getDataGenerator()->create_user();
42         $user2 = $this->getDataGenerator()->create_user();
43         $course1 = $this->getDataGenerator()->create_course();
44         $module1 = $this->getDataGenerator()->create_module('resource', array('course' => $course1));
45         $course2 = $this->getDataGenerator()->create_course();
46         $module2 = $this->getDataGenerator()->create_module('resource', array('course' => $course2));
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);
54         // Fake the settings, we will abuse the standard plugin table here...
55         set_config('dbdriver', $CFG->dblibrary . '/' . $CFG->dbtype, 'logstore_database');
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');
60         set_config('dbtable', $CFG->prefix . 'logstore_standard_log', 'logstore_database');
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         }
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);
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);
99         $this->assertTrue($store->is_logging());
101         $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
102         $this->assertCount(0, $logs);
104         $this->setCurrentTimeStart();
106         $this->setUser(0);
107         $event1 = \logstore_database\event\unittest_executed::create(
108             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
109         $event1->trigger();
111         $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
112         $this->assertCount(1, $logs);
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);
124         $this->setAdminUser();
125         \core\session\manager::loginas($user1->id, context_system::instance());
126         $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
128         $event2 = \logstore_database\event\unittest_executed::create(
129             array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9)));
130         $event2->trigger();
132         \core\session\manager::init_empty_session();
133         $this->assertFalse(\core\session\manager::is_loggedinas());
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);
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);
151         // Test reading.
152         $this->assertSame(3, $store->get_events_select_count('', array()));
153         $events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0); // Is actually sorted by "timecreated ASC, id ASC".
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());
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');
169         \logstore_database\event\unittest_executed::create(
170             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
171         $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
172         \logstore_database\event\unittest_executed::create(
173             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
174         $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
175         $store->flush();
176         $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
177         \logstore_database\event\unittest_executed::create(
178             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
179         $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
180         \logstore_database\event\unittest_executed::create(
181             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
182         $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
183         \logstore_database\event\unittest_executed::create(
184             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
185         $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
186         \logstore_database\event\unittest_executed::create(
187             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
188         $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
189         \logstore_database\event\unittest_executed::create(
190             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
191         $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
192         \logstore_database\event\unittest_executed::create(
193             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
194         $this->assertEquals(8, $DB->count_records('logstore_standard_log'));
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);
203         $this->setUser(null);
204         \logstore_database\event\unittest_executed::create(
205             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
206         $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
208         $this->setGuestUser();
209         \logstore_database\event\unittest_executed::create(
210             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
211         $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
213         $this->setUser($user1);
214         \logstore_database\event\unittest_executed::create(
215             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
216         $this->assertEquals(1, $DB->count_records('logstore_standard_log'));
218         $this->setUser($user2);
219         \logstore_database\event\unittest_executed::create(
220             array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
221         $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
223         set_config('enabled_stores', '', 'tool_log');
224         get_log_manager(true);
225     }
227     /**
228      * Test method is_event_ignored.
229      */
230     public function test_is_event_ignored() {
231         $this->resetAfterTest();
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));
242         set_config('logguests', 1, 'logstore_database');
243         $store = new \logstore_database\test\store($logmanager); // Reload.
244         $this->assertFalse($store->is_event_ignored($event));
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));
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));
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));
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));
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     }
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');
277         $supportedreports = array(
278             'report_log' => '/report/log',
279             'report_loglive' => '/report/loglive'
280         );
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     }