source: moodle/trunk/fuentes/admin/tool/log/store/standard/tests/store_test.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 4 years ago

Ported code to xenial

File size: 10.4 KB
Line 
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 * Standard log store tests.
19 *
20 * @package    logstore_standard
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');
28require_once(__DIR__ . '/fixtures/restore_hack.php');
29
30class logstore_standard_store_testcase extends advanced_testcase {
31    public function test_log_writing() {
32        global $DB;
33        $this->resetAfterTest();
34        $this->preventResetByRollback(); // Logging waits till the transaction gets committed.
35
36        $this->setAdminUser();
37        $user1 = $this->getDataGenerator()->create_user();
38        $user2 = $this->getDataGenerator()->create_user();
39        $course1 = $this->getDataGenerator()->create_course();
40        $module1 = $this->getDataGenerator()->create_module('resource', array('course' => $course1));
41        $course2 = $this->getDataGenerator()->create_course();
42        $module2 = $this->getDataGenerator()->create_module('resource', array('course' => $course2));
43
44        // Test all plugins are disabled by this command.
45        set_config('enabled_stores', '', 'tool_log');
46        $manager = get_log_manager(true);
47        $stores = $manager->get_readers();
48        $this->assertCount(0, $stores);
49
50        // Enable logging plugin.
51        set_config('enabled_stores', 'logstore_standard', 'tool_log');
52        set_config('buffersize', 0, 'logstore_standard');
53        set_config('logguests', 1, 'logstore_standard');
54        $manager = get_log_manager(true);
55
56        $stores = $manager->get_readers();
57        $this->assertCount(1, $stores);
58        $this->assertEquals(array('logstore_standard'), array_keys($stores));
59        /** @var \logstore_standard\log\store $store */
60        $store = $stores['logstore_standard'];
61        $this->assertInstanceOf('logstore_standard\log\store', $store);
62        $this->assertInstanceOf('tool_log\log\writer', $store);
63        $this->assertTrue($store->is_logging());
64
65        $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
66        $this->assertCount(0, $logs);
67
68        $this->setCurrentTimeStart();
69
70        $this->setUser(0);
71        $event1 = \logstore_standard\event\unittest_executed::create(
72            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)));
73        $event1->trigger();
74
75        $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
76        $this->assertCount(1, $logs);
77
78        $log1 = reset($logs);
79        unset($log1->id);
80        $log1->other = unserialize($log1->other);
81        $log1 = (array)$log1;
82        $data = $event1->get_data();
83        $data['origin'] = 'cli';
84        $data['ip'] = null;
85        $data['realuserid'] = null;
86        $this->assertEquals($data, $log1);
87
88        $this->setAdminUser();
89        \core\session\manager::loginas($user1->id, context_system::instance());
90        $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
91
92        logstore_standard_restore::hack_executing(1);
93        $event2 = \logstore_standard\event\unittest_executed::create(
94            array('context' => context_module::instance($module2->cmid), 'other' => array('sample' => 6, 'xx' => 9)));
95        $event2->trigger();
96        logstore_standard_restore::hack_executing(0);
97
98        \core\session\manager::init_empty_session();
99        $this->assertFalse(\core\session\manager::is_loggedinas());
100
101        $logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
102        $this->assertCount(3, $logs);
103        array_shift($logs);
104        $log2 = array_shift($logs);
105        $this->assertSame('\core\event\user_loggedinas', $log2->eventname);
106        $this->assertSame('cli', $log2->origin);
107
108        $log3 = array_shift($logs);
109        unset($log3->id);
110        $log3->other = unserialize($log3->other);
111        $log3 = (array)$log3;
112        $data = $event2->get_data();
113        $data['origin'] = 'restore';
114        $data['ip'] = null;
115        $data['realuserid'] = 2;
116        $this->assertEquals($data, $log3);
117
118        // Test table exists.
119        $tablename = $store->get_internal_log_table_name();
120        $this->assertTrue($DB->get_manager()->table_exists($tablename));
121
122        // Test reading.
123        $this->assertSame(3, $store->get_events_select_count('', array()));
124        $events = $store->get_events_select('', array(), 'timecreated ASC', 0, 0); // Is actually sorted by "timecreated ASC, id ASC".
125        $this->assertCount(3, $events);
126        $resev1 = array_shift($events);
127        array_shift($events);
128        $resev2 = array_shift($events);
129        $this->assertEquals($event1->get_data(), $resev1->get_data());
130        $this->assertEquals($event2->get_data(), $resev2->get_data());
131
132        // Test buffering.
133        set_config('buffersize', 3, 'logstore_standard');
134        $manager = get_log_manager(true);
135        $stores = $manager->get_readers();
136        /** @var \logstore_standard\log\store $store */
137        $store = $stores['logstore_standard'];
138        $DB->delete_records('logstore_standard_log');
139
140        \logstore_standard\event\unittest_executed::create(
141            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
142        $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
143        \logstore_standard\event\unittest_executed::create(
144            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
145        $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
146        $store->flush();
147        $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
148        \logstore_standard\event\unittest_executed::create(
149            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
150        $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
151        \logstore_standard\event\unittest_executed::create(
152            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
153        $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
154        \logstore_standard\event\unittest_executed::create(
155            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
156        $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
157        \logstore_standard\event\unittest_executed::create(
158            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
159        $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
160        \logstore_standard\event\unittest_executed::create(
161            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
162        $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
163        \logstore_standard\event\unittest_executed::create(
164            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
165        $this->assertEquals(8, $DB->count_records('logstore_standard_log'));
166
167        // Test guest logging setting.
168        set_config('logguests', 0, 'logstore_standard');
169        set_config('buffersize', 0, 'logstore_standard');
170        get_log_manager(true);
171        $DB->delete_records('logstore_standard_log');
172        get_log_manager(true);
173
174        $this->setUser(null);
175        \logstore_standard\event\unittest_executed::create(
176            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
177        $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
178
179        $this->setGuestUser();
180        \logstore_standard\event\unittest_executed::create(
181            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
182        $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
183
184        $this->setUser($user1);
185        \logstore_standard\event\unittest_executed::create(
186            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
187        $this->assertEquals(1, $DB->count_records('logstore_standard_log'));
188
189        $this->setUser($user2);
190        \logstore_standard\event\unittest_executed::create(
191            array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10)))->trigger();
192        $this->assertEquals(2, $DB->count_records('logstore_standard_log'));
193
194        set_config('enabled_stores', '', 'tool_log');
195        get_log_manager(true);
196    }
197
198    /**
199     * Test logmanager::get_supported_reports returns all reports that require this store.
200     */
201    public function test_get_supported_reports() {
202        $logmanager = get_log_manager();
203        $allreports = \core_component::get_plugin_list('report');
204
205        $supportedreports = array(
206            'report_log' => '/report/log',
207            'report_loglive' => '/report/loglive',
208            'report_outline' => '/report/outline',
209            'report_participation' => '/report/participation',
210            'report_stats' => '/report/stats'
211        );
212
213        // Make sure all supported reports are installed.
214        $expectedreports = array_keys(array_intersect_key($allreports, $supportedreports));
215        $reports = $logmanager->get_supported_reports('logstore_standard');
216        $reports = array_keys($reports);
217        foreach ($expectedreports as $expectedreport) {
218            $this->assertContains($expectedreport, $reports);
219        }
220    }
221}
Note: See TracBrowser for help on using the repository browser.