Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

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