Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 39 and 310]
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 * Legacy log store tests. 19 * 20 * @package logstore_legacy 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 25 defined('MOODLE_INTERNAL') || die(); 26 27 require_once (__DIR__ . '/fixtures/event.php'); 28 require_once (__DIR__ . '/fixtures/store.php'); 29 30 class logstore_legacy_store_testcase extends advanced_testcase { 31 public function test_log_writing() { 32 global $DB; 33 $this->resetAfterTest(); 34 35 $this->setAdminUser(); 36 $user1 = $this->getDataGenerator()->create_user(); 37 $course1 = $this->getDataGenerator()->create_course(); 38 $module1 = $this->getDataGenerator()->create_module('resource', array('course' => $course1)); 39 $course2 = $this->getDataGenerator()->create_course(); 40 41 // Enable legacy logging plugin. 42 set_config('enabled_stores', 'logstore_legacy', 'tool_log'); 43 set_config('loglegacy', 1, 'logstore_legacy'); 44 $manager = get_log_manager(true); 45 46 $stores = $manager->get_readers(); 47 $this->assertCount(1, $stores); 48 $this->assertEquals(array('logstore_legacy'), array_keys($stores)); 49 $store = $stores['logstore_legacy']; 50 $this->assertInstanceOf('logstore_legacy\log\store', $store); 51 $this->assertInstanceOf('core\log\sql_reader', $store); 52 $this->assertTrue($store->is_logging()); 53 54 $logs = $DB->get_records('log', array(), 'id ASC'); 55 $this->assertCount(0, $logs); 56 57 $this->setCurrentTimeStart(); 58 59 $this->setUser(0); 60 $event1 = \logstore_legacy\event\unittest_executed::create( 61 array('context' => context_module::instance($module1->cmid), 'other' => array('sample' => 5, 'xx' => 10))); 62 $event1->trigger(); 63 64 $this->setUser($user1); 65 $event2 = \logstore_legacy\event\unittest_executed::create( 66 array('context' => context_course::instance($course2->id), 'other' => array('sample' => 6, 'xx' => 11))); 67 $event2->trigger(); 68 69 $logs = $DB->get_records('log', array(), 'id ASC'); 70 $this->assertCount(2, $logs); 71 72 $log = array_shift($logs); 73 $this->assertNotEmpty($log->id); 74 $this->assertTimeCurrent($log->time); 75 $this->assertEquals(0, $log->userid); 76 $this->assertSame('0.0.0.0', $log->ip); 77 $this->assertEquals($course1->id, $log->course); 78 $this->assertSame('core_unittest', $log->module); 79 $this->assertEquals($module1->cmid, $log->cmid); 80 $this->assertSame('view', $log->action); 81 $this->assertSame('unittest.php?id=5', $log->url); 82 $this->assertSame('bbb', $log->info); 83 84 $oldlogid = $log->id; 85 $log = array_shift($logs); 86 $this->assertGreaterThan($oldlogid, $log->id); 87 $this->assertNotEmpty($log->id); 88 $this->assertTimeCurrent($log->time); 89 $this->assertEquals($user1->id, $log->userid); 90 $this->assertSame('0.0.0.0', $log->ip); 91 $this->assertEquals($course2->id, $log->course); 92 $this->assertSame('core_unittest', $log->module); 93 $this->assertEquals(0, $log->cmid); 94 $this->assertSame('view', $log->action); 95 $this->assertSame('unittest.php?id=6', $log->url); 96 $this->assertSame('bbb', $log->info); 97 98 // Test if disabling works. 99 set_config('enabled_stores', 'logstore_legacy', 'tool_log'); 100 set_config('loglegacy', 0, 'logstore_legacy'); 101 $manager = get_log_manager(true); 102 $stores = $manager->get_readers(); 103 $store = $stores['logstore_legacy']; 104 $this->assertFalse($store->is_logging()); 105 106 \logstore_legacy\event\unittest_executed::create( 107 array('context' => \context_system::instance(), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); 108 $this->assertEquals(2, $DB->count_records('log')); 109 110 // Another way to disable legacy completely. 111 set_config('enabled_stores', 'logstore_standard', 'tool_log'); 112 set_config('loglegacy', 1, 'logstore_legacy'); 113 get_log_manager(true); 114 115 \logstore_legacy\event\unittest_executed::create( 116 array('context' => \context_system::instance(), 'other' => array('sample' => 5, 'xx' => 10)))->trigger(); 117 $this->assertEquals(2, $DB->count_records('log')); 118 // Set everything back. 119 set_config('enabled_stores', '', 'tool_log'); 120 set_config('loglegacy', 0, 'logstore_legacy'); 121 get_log_manager(true); 122 } 123 124 /** 125 * Test replace_sql_legacy() 126 */ 127 public function test_replace_sql_legacy() { 128 $selectwhere = "userid = :userid AND courseid = :courseid AND eventname = :eventname AND timecreated > :since"; 129 $params = array('userid' => 2, 'since' => 3, 'courseid' => 4, 'eventname' => '\core\event\course_created'); 130 $expectedselect = "module = 'course' AND action = 'new' AND userid = :userid AND url = :url AND time > :since"; 131 $expectedparams = $params + array('url' => "view.php?id=4"); 132 133 list($replaceselect, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 134 $this->assertEquals($replaceselect, $expectedselect); 135 $this->assertEquals($replaceparams, $expectedparams); 136 137 // Test CRUD related changes. 138 $selectwhere = "edulevel = 0"; 139 list($updatewhere, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 140 $this->assertEquals($selectwhere, $updatewhere); 141 142 $selectwhere = "edulevel = 0 and crud = 'u'"; 143 list($updatewhere, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 144 $this->assertEquals("edulevel = 0 and action LIKE '%update%'", $updatewhere); 145 146 $selectwhere = "edulevel = 0 and crud != 'u'"; 147 list($updatewhere, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 148 $this->assertEquals("edulevel = 0 and action NOT LIKE '%update%'", $updatewhere); 149 150 $selectwhere = "edulevel = 0 and crud <> 'u'"; 151 list($updatewhere, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 152 $this->assertEquals("edulevel = 0 and action NOT LIKE '%update%'", $updatewhere); 153 154 $selectwhere = "edulevel = 0 and crud = 'r'"; 155 list($updatewhere, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 156 $this->assertEquals("edulevel = 0 and action LIKE '%view%' OR action LIKE '%report%'", $updatewhere); 157 158 $selectwhere = "edulevel = 0 and crud != 'r'"; 159 list($updatewhere, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 160 $this->assertEquals("edulevel = 0 and action NOT LIKE '%view%' AND action NOT LIKE '%report%'", $updatewhere); 161 162 $selectwhere = "edulevel = 0 and crud <> 'r'"; 163 list($updatewhere, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 164 $this->assertEquals("edulevel = 0 and action NOT LIKE '%view%' AND action NOT LIKE '%report%'", $updatewhere); 165 166 // The slq is incorrect, since quotes must not be present. Make sure this is not parsed. 167 $selectwhere = "edulevel = 0 and 'crud' != 'u'"; 168 list($updatewhere, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 169 $this->assertNotEquals("edulevel = 0 and action NOT LIKE '%update%'", $updatewhere); 170 171 $selectwhere = "edulevel = 0 and crud = 'u' OR crud != 'r' or crud <> 'd'"; 172 list($updatewhere, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 173 $this->assertEquals("edulevel = 0 and action LIKE '%update%' OR action NOT LIKE '%view%' AND action NOT LIKE '%report%' or action NOT LIKE '%delete%'", $updatewhere); 174 175 // The anonymous select returns all data. 176 $selectwhere = "anonymous = 0"; 177 list($updatewhere, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 178 $this->assertSame("0 = 0", $updatewhere); 179 180 // Test legacy field names are mapped. 181 $selectwhere = "timecreated = :timecreated and courseid = :courseid and contextinstanceid = :contextinstanceid and origin = :origin"; 182 $params = array('timecreated' => 2, 'courseid' => 3, 'contextinstanceid' => 4, 'origin' => 5 ); 183 $expectedparams = array('time' => 2, 'course' => 3, 'cmid' => 4, 'ip' => 5); 184 list($updatewhere, $replaceparams) = \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params); 185 $this->assertEquals("time = :time and course = :course and cmid = :cmid and ip = :ip", $updatewhere); 186 $this->assertSame($expectedparams, $replaceparams); 187 188 // Test sorting parameters. 189 $selectwhere = "courseid = 3"; 190 $params = array(); 191 $sort = 'timecreated DESC'; 192 list($updatewhere, $replaceparams, $sort) = 193 \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params, $sort); 194 $this->assertSame('time DESC', $sort); 195 196 $sort = 'courseid DESC'; 197 list($updatewhere, $replaceparams, $sort) = 198 \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params, $sort); 199 $this->assertSame('course DESC', $sort); 200 201 $sort = 'contextinstanceid DESC'; 202 list($updatewhere, $replaceparams, $sort) = 203 \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params, $sort); 204 $this->assertSame('cmid DESC', $sort); 205 206 $sort = 'origin DESC'; 207 list($updatewhere, $replaceparams, $sort) = 208 \logstore_legacy\test\unittest_logstore_legacy::replace_sql_legacy($selectwhere, $params, $sort); 209 $this->assertSame('ip DESC', $sort); 210 } 211 212 /* 213 * Test logmanager::get_supported_reports returns all reports that require this store. 214 */ 215 public function test_get_supported_reports() { 216 $logmanager = get_log_manager(); 217 $allreports = \core_component::get_plugin_list('report'); 218 219 $supportedreports = array( 220 'report_log' => '/report/log', 221 'report_loglive' => '/report/loglive', 222 'report_outline' => '/report/outline', 223 'report_participation' => '/report/participation', 224 'report_stats' => '/report/stats' 225 ); 226 227 // Make sure all supported reports are installed. 228 $expectedreports = array_keys(array_intersect_key($allreports, $supportedreports)); 229 $reports = $logmanager->get_supported_reports('logstore_legacy'); 230 $reports = array_keys($reports); 231 foreach ($expectedreports as $expectedreport) { 232 $this->assertContains($expectedreport, $reports); 233 } 234 } 235 236 /** 237 * Test that the legacy log cleanup works correctly. 238 */ 239 public function test_cleanup_task() { 240 global $DB; 241 242 $this->resetAfterTest(); 243 244 // Create some records spread over various days; test multiple iterations in cleanup. 245 $record = (object) array('time' => time()); 246 $DB->insert_record('log', $record); 247 $record->time -= 3600 * 24 * 30; 248 $DB->insert_record('log', $record); 249 $record->time -= 3600 * 24 * 30; 250 $DB->insert_record('log', $record); 251 $record->time -= 3600 * 24 * 30; 252 $DB->insert_record('log', $record); 253 $this->assertEquals(4, $DB->count_records('log')); 254 255 // Remove all logs before "today". 256 set_config('loglifetime', 1); 257 258 $this->expectOutputString(" Deleted old legacy log records\n"); 259 $clean = new \logstore_legacy\task\cleanup_task(); 260 $clean->execute(); 261 262 $this->assertEquals(1, $DB->count_records('log')); 263 } 264 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body