MDL-43931 add logging unit tests

This commit is contained in:
Petr Škoda
2014-02-28 14:52:27 +08:00
parent ed8f7c308b
commit bdaf91fd13
13 changed files with 752 additions and 38 deletions
+15 -8
View File
@@ -62,22 +62,29 @@ trait writer {
}
if ($this->count >= $this->buffersize) {
$events = $this->buffer;
$this->insert_events($events);
$this->count = 0;
$this->buffer = array();
$this->flush();
}
}
/**
* Flush event buffer.
*/
public function flush() {
if ($this->count == 0) {
return;
}
$events = $this->buffer;
$this->count = 0;
$this->buffer = array();
$this->insert_events($events);
}
/**
* Push any remaining events to the database. Insert_events() must be
* defined. override in stores if the store doesn't support buffering.
*
*/
public function dispose() {
$events = $this->buffer;
$this->insert_events($events);
$this->count = 0;
$this->buffer = array();
$this->flush();
}
}
@@ -129,7 +129,7 @@ class store implements \tool_log\log\writer, \core\log\reader {
// Ignore event if the store settings do not want to store it.
continue;
}
if (!CLI_SCRIPT and !$this->logguests) {
if ((!CLI_SCRIPT or PHPUNIT_TEST) and !$this->logguests) {
// Always log inside CLI scripts because we do not login there.
if (!isloggedin() or isguestuser()) {
continue;
+1 -1
View File
@@ -40,7 +40,7 @@ if ($hassiteconfig) {
$settings->add(new admin_setting_configtext('logstore_database/dbhost', get_string('databasehost', 'install'), '', ''));
$settings->add(new admin_setting_configtext('logstore_database/dbuser', get_string('databaseuser', 'install'), '', ''));
$settings->add(new admin_setting_configtext('logstore_database/dbpass', get_string('databasepass', 'install'), '', ''));
$settings->add(new admin_setting_configpasswordunmask('logstore_database/dbpass', get_string('databasepass', 'install'), '', ''));
$settings->add(new admin_setting_configtext('logstore_database/dbname', get_string('databasename', 'install'), '', ''));
$settings->add(new admin_setting_configtext('logstore_database/dbtable', get_string('databasetable', 'logstore_database'),
get_string('databasetable_help', 'logstore_database'), ''));
+47
View File
@@ -0,0 +1,47 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace logstore_database\event;
/**
* Fixtures for database log storage testing.
*
* @package logstore_database
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class unittest_executed extends \core\event\base {
public static function get_name() {
return 'xxx';
}
public function get_description() {
return 'yyy';
}
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}
public function get_url() {
return new \moodle_url('/somepath/somefile.php', array('id'=>$this->data['other']['sample']));
}
}
@@ -0,0 +1,205 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* External database log store tests.
*
* @package logstore_database
* @copyright 2014 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once (__DIR__ . '/fixtures/event.php');
class logstore_database_store_testcase extends advanced_testcase {
public function test_log_writing() {
global $DB, $CFG;
$this->resetAfterTest();
$this->preventResetByRollback(); // Logging waits till the transaction gets committed.
$dbman = $DB->get_manager();
$this->assertTrue($dbman->table_exists('logstore_standard_log'));
$DB->delete_records('logstore_standard_log');
$this->setAdminUser();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$module1 = $this->getDataGenerator()->create_module('resource', array('course'=>$course1));
$course2 = $this->getDataGenerator()->create_course();
$module2 = $this->getDataGenerator()->create_module('resource', array('course'=>$course2));
// Fake the settings, we will abuse the standard plugin table here...
$parts = explode('_', get_class($DB));
set_config('dbdriver', $parts[1].'/'.$parts[0], 'logstore_database');
set_config('dbhost', $CFG->dbhost, 'logstore_database');
set_config('dbuser', $CFG->dbuser, 'logstore_database');
set_config('dbpass', $CFG->dbpass, 'logstore_database');
set_config('dbname', $CFG->dbname, 'logstore_database');
set_config('dbtable', $CFG->prefix.'logstore_standard_log', 'logstore_database');
if (!empty($CFG->dboptions['dbpersist'])) {
set_config('dbpersist', 1, 'logstore_database');
} else {
set_config('dbpersist', 0, 'logstore_database');
}
if (!empty($CFG->dboptions['dbsocket'])) {
set_config('dbsocket', $CFG->dboptions['dbsocket'], 'logstore_database');
} else {
set_config('dbsocket', '', 'logstore_database');
}
if (!empty($CFG->dboptions['dbport'])) {
set_config('dbport', $CFG->dboptions['dbport'], 'logstore_database');
} else {
set_config('dbport', '', 'logstore_database');
}
if (!empty($CFG->dboptions['dbschema'])) {
set_config('dbschema', $CFG->dboptions['dbschema'], 'logstore_database');
} else {
set_config('dbschema', '', 'logstore_database');
}
if (!empty($CFG->dboptions['dbcollation'])) {
set_config('dbcollation', $CFG->dboptions['dbcollation'], 'logstore_database');
} else {
set_config('dbcollation', '', 'logstore_database');
}
// Enable logging plugin.
set_config('enabled_stores', 'logstore_database', 'tool_log');
set_config('buffersize', 0, 'logstore_database');
set_config('logguests', 1, 'logstore_database');
$manager = get_log_manager(true);
$stores = $manager->get_readers();
$this->assertCount(1, $stores);
$this->assertEquals(array('logstore_database'), array_keys($stores));
$store = $stores['logstore_database'];
$this->assertInstanceOf('logstore_database\log\store', $store);
$this->assertInstanceOf('tool_log\log\writer', $store);
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(0, $logs);
$this->setCurrentTimeStart();
$this->setUser(0);
$event1 = \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)));
$event1->trigger();
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(1, $logs);
$log1 = reset($logs);
unset($log1->id);
$log1->other = unserialize($log1->other);
$log1 = (array)$log1;
$data = $event1->get_data();
$data['origin'] = 'cli';
$data['ip'] = null;
$data['realuserid'] = null;
$this->assertEquals($data, $log1);
$this->setAdminUser();
\core\session\manager::loginas($user1->id, context_system::instance());
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
$event2 = \logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module2->cmid), 'other'=>array('sample'=>6, 'xx'=>9)));
$event2->trigger();
$_SESSION['SESSION'] = new \stdClass();
$this->setUser(0);
$this->assertFalse(\core\session\manager::is_loggedinas());
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(3, $logs);
array_shift($logs);
$log2 = array_shift($logs);
$this->assertSame('\core\event\user_loggedinas', $log2->eventname);
$log3 = array_shift($logs);
unset($log3->id);
$log3->other = unserialize($log3->other);
$log3 = (array)$log3;
$data = $event2->get_data();
$data['origin'] = 'cli';
$data['ip'] = null;
$data['realuserid'] = 2;
$this->assertEquals($data, $log3);
// Test reading.
$events = $store->get_events('', array(), 'id', 0, 0);
$this->assertCount(3, $events);
$resev1 = array_shift($events);
array_shift($events);
$resev2 = array_shift($events);
$this->assertEquals($event1->get_data(), $resev1->get_data());
$this->assertEquals($event2->get_data(), $resev2->get_data());
// Test buffering.
set_config('buffersize', 3, 'logstore_database');
$manager = get_log_manager(true);
$stores = $manager->get_readers();
/** @var \logstore_database\log\store $store */
$store = $stores['logstore_database'];
$DB->delete_records('logstore_standard_log');
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
$store->flush();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(5, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(5, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(5, $DB->count_records('logstore_standard_log'));
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(8, $DB->count_records('logstore_standard_log'));
// Test guest logging setting.
set_config('logguests', 0, 'logstore_database');
set_config('buffersize', 0, 'logstore_database');
get_log_manager(true);
$DB->delete_records('logstore_standard_log');
get_log_manager(true);
$this->setUser(null);
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
$this->setGuestUser();
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
$this->setUser($user1);
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(1, $DB->count_records('logstore_standard_log'));
$this->setUser($user2);
\logstore_database\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
set_config('enabled_stores', '', 'tool_log');
get_log_manager(true);
}
}
+63
View File
@@ -0,0 +1,63 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace logstore_legacy\event;
/**
* Fixtures for legacy logging testing.
*
* @package logstore_legacy
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class unittest_executed extends \core\event\base {
public static function get_name() {
return 'xxx';
}
public function get_description() {
return 'yyy';
}
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}
public function get_url() {
return new \moodle_url('/somepath/somefile.php', array('id'=>$this->data['other']['sample']));
}
public static function get_legacy_eventname() {
return 'test_legacy';
}
protected function get_legacy_eventdata() {
return array($this->data['courseid'], $this->data['other']['sample']);
}
protected function get_legacy_logdata() {
$cmid = 0;
if ($this->contextlevel == CONTEXT_MODULE) {
$cmid = $this->contextinstanceid;
}
return array($this->data['courseid'], 'core_unittest', 'view', 'unittest.php?id='.$this->data['other']['sample'], 'bbb', $cmid);
}
}
@@ -0,0 +1,149 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Legacy log store tests.
*
* @package logstore_legacy
* @copyright 2014 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once (__DIR__ . '/fixtures/event.php');
class logstore_legacy_store_testcase extends advanced_testcase {
public function test_log_writing() {
global $DB;
$this->resetAfterTest();
$this->setAdminUser();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$module1 = $this->getDataGenerator()->create_module('resource', array('course'=>$course1));
$course2 = $this->getDataGenerator()->create_course();
$module2 = $this->getDataGenerator()->create_module('resource', array('course'=>$course2));
// Enable legacy logging plugin.
set_config('enabled_stores', 'logstore_legacy', 'tool_log');
set_config('loglegacy', 1, 'logstore_legacy');
get_log_manager(true);
$logs = $DB->get_records('log', array(), 'id ASC');
$this->assertCount(0, $logs);
$this->setCurrentTimeStart();
$this->setUser(0);
$event1 = \logstore_legacy\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)));
$event1->trigger();
$this->setUser($user1);
$event2 = \logstore_legacy\event\unittest_executed::create(array('context'=>context_course::instance($course2->id), 'other'=>array('sample'=>6, 'xx'=>11)));
$event2->trigger();
$this->setUser($user2);
add_to_log($course1->id, 'xxxx', 'yyyy', '', '7', 0, 0);
//$this->assertDebuggingCalled();
add_to_log($course2->id, 'aaa', 'bbb', 'info.php', '666', $module2->cmid, $user1->id);
//$this->assertDebuggingCalled();
$logs = $DB->get_records('log', array(), 'id ASC');
$this->assertCount(4, $logs);
$log = array_shift($logs);
$this->assertNotEmpty($log->id);
$this->assertTimeCurrent($log->time);
$this->assertEquals(0, $log->userid);
$this->assertSame('0.0.0.0', $log->ip);
$this->assertEquals($course1->id, $log->course);
$this->assertSame('core_unittest', $log->module);
$this->assertEquals($module1->cmid, $log->cmid);
$this->assertSame('view', $log->action);
$this->assertSame('unittest.php?id=5', $log->url);
$this->assertSame('bbb', $log->info);
$oldlogid = $log->id;
$log = array_shift($logs);
$this->assertGreaterThan($oldlogid, $log->id);
$this->assertNotEmpty($log->id);
$this->assertTimeCurrent($log->time);
$this->assertEquals($user1->id, $log->userid);
$this->assertSame('0.0.0.0', $log->ip);
$this->assertEquals($course2->id, $log->course);
$this->assertSame('core_unittest', $log->module);
$this->assertEquals(0, $log->cmid);
$this->assertSame('view', $log->action);
$this->assertSame('unittest.php?id=6', $log->url);
$this->assertSame('bbb', $log->info);
$oldlogid = $log->id;
$log = array_shift($logs);
$this->assertGreaterThan($oldlogid, $log->id);
$this->assertNotEmpty($log->id);
$this->assertTimeCurrent($log->time);
$this->assertEquals($user2->id, $log->userid);
$this->assertSame('0.0.0.0', $log->ip);
$this->assertEquals($course1->id, $log->course);
$this->assertSame('xxxx', $log->module);
$this->assertEquals(0, $log->cmid);
$this->assertSame('yyyy', $log->action);
$this->assertSame('', $log->url);
$this->assertSame('7', $log->info);
$oldlogid = $log->id;
$log = array_shift($logs);
$this->assertGreaterThan($oldlogid, $log->id);
$this->assertNotEmpty($log->id);
$this->assertTimeCurrent($log->time);
$this->assertEquals($user1->id, $log->userid);
$this->assertSame('0.0.0.0', $log->ip);
$this->assertEquals($course2->id, $log->course);
$this->assertSame('aaa', $log->module);
$this->assertEquals($module2->cmid, $log->cmid);
$this->assertSame('bbb', $log->action);
$this->assertSame('info.php', $log->url);
$this->assertSame('666', $log->info);
// Test if disabling works.
set_config('enabled_stores', 'logstore_legacy', 'tool_log');
set_config('loglegacy', 0, 'logstore_legacy');
get_log_manager(true);
\logstore_legacy\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
add_to_log($course1->id, 'xxxx', 'yyyy', '', '7', 0, 0);
//$this->assertDebuggingCalled();
$this->assertEquals(4, $DB->count_records('log'));
// Another way to disable legacy completely.
set_config('enabled_stores', 'logstore_standard', 'tool_log');
set_config('loglegacy', 1, 'logstore_legacy');
get_log_manager(true);
\logstore_legacy\event\unittest_executed::create(array('context'=>\context_system::instance(), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
add_to_log($course1->id, 'xxxx', 'yyyy', '', '7', 0, 0);
//$this->assertDebuggingCalled();
$this->assertEquals(4, $DB->count_records('log'));
// Set everything back.
set_config('enabled_stores', '', 'tool_log');
set_config('loglegacy', 0, 'logstore_legacy');
get_log_manager(true);
}
}
@@ -52,7 +52,7 @@ class store implements \tool_log\log\writer, \core\log\sql_reader {
// Filter events.
foreach ($events as $event) {
if (!CLI_SCRIPT and !$this->logguests) {
if ((!CLI_SCRIPT or PHPUNIT_TEST) and !$this->logguests) {
// Always log inside CLI scripts because we do not login there.
if (!isloggedin() or isguestuser()) {
continue;
+47
View File
@@ -0,0 +1,47 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace logstore_standard\event;
/**
* Fixtures for standard log storage testing.
*
* @package logstore_standard
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class unittest_executed extends \core\event\base {
public static function get_name() {
return 'xxx';
}
public function get_description() {
return 'yyy';
}
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_PARTICIPATING;
}
public function get_url() {
return new \moodle_url('/somepath/somefile.php', array('id'=>$this->data['other']['sample']));
}
}
@@ -0,0 +1,167 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Standard log store tests.
*
* @package logstore_standard
* @copyright 2014 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once (__DIR__ . '/fixtures/event.php');
class logstore_standard_store_testcase extends advanced_testcase {
public function test_log_writing() {
global $DB;
$this->resetAfterTest();
$this->preventResetByRollback(); // Logging waits till the transaction gets committed.
$this->setAdminUser();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$module1 = $this->getDataGenerator()->create_module('resource', array('course'=>$course1));
$course2 = $this->getDataGenerator()->create_course();
$module2 = $this->getDataGenerator()->create_module('resource', array('course'=>$course2));
// Enable logging plugin.
set_config('enabled_stores', 'logstore_standard', 'tool_log');
set_config('buffersize', 0, 'logstore_standard');
set_config('logguests', 1, 'logstore_standard');
$manager = get_log_manager(true);
$stores = $manager->get_readers();
$this->assertCount(1, $stores);
$this->assertEquals(array('logstore_standard'), array_keys($stores));
$store = $stores['logstore_standard'];
$this->assertInstanceOf('logstore_standard\log\store', $store);
$this->assertInstanceOf('tool_log\log\writer', $store);
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(0, $logs);
$this->setCurrentTimeStart();
$this->setUser(0);
$event1 = \logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)));
$event1->trigger();
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(1, $logs);
$log1 = reset($logs);
unset($log1->id);
$log1->other = unserialize($log1->other);
$log1 = (array)$log1;
$data = $event1->get_data();
$data['origin'] = 'cli';
$data['ip'] = null;
$data['realuserid'] = null;
$this->assertEquals($data, $log1);
$this->setAdminUser();
\core\session\manager::loginas($user1->id, context_system::instance());
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
$event2 = \logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module2->cmid), 'other'=>array('sample'=>6, 'xx'=>9)));
$event2->trigger();
$_SESSION['SESSION'] = new \stdClass();
$this->setUser(0);
$this->assertFalse(\core\session\manager::is_loggedinas());
$logs = $DB->get_records('logstore_standard_log', array(), 'id ASC');
$this->assertCount(3, $logs);
array_shift($logs);
$log2 = array_shift($logs);
$this->assertSame('\core\event\user_loggedinas', $log2->eventname);
$log3 = array_shift($logs);
unset($log3->id);
$log3->other = unserialize($log3->other);
$log3 = (array)$log3;
$data = $event2->get_data();
$data['origin'] = 'cli';
$data['ip'] = null;
$data['realuserid'] = 2;
$this->assertEquals($data, $log3);
// Test reading.
$events = $store->get_events('', array(), 'id', 0, 0);
$this->assertCount(3, $events);
$resev1 = array_shift($events);
array_shift($events);
$resev2 = array_shift($events);
$this->assertEquals($event1->get_data(), $resev1->get_data());
$this->assertEquals($event2->get_data(), $resev2->get_data());
// Test buffering.
set_config('buffersize', 3, 'logstore_standard');
$manager = get_log_manager(true);
$stores = $manager->get_readers();
/** @var \logstore_standard\log\store $store */
$store = $stores['logstore_standard'];
$DB->delete_records('logstore_standard_log');
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
$store->flush();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(5, $DB->count_records('logstore_standard_log'));
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(5, $DB->count_records('logstore_standard_log'));
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(5, $DB->count_records('logstore_standard_log'));
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(8, $DB->count_records('logstore_standard_log'));
// Test guest logging setting.
set_config('logguests', 0, 'logstore_standard');
set_config('buffersize', 0, 'logstore_standard');
get_log_manager(true);
$DB->delete_records('logstore_standard_log');
get_log_manager(true);
$this->setUser(null);
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
$this->setGuestUser();
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(0, $DB->count_records('logstore_standard_log'));
$this->setUser($user1);
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(1, $DB->count_records('logstore_standard_log'));
$this->setUser($user2);
\logstore_standard\event\unittest_executed::create(array('context'=>context_module::instance($module1->cmid), 'other'=>array('sample'=>5, 'xx'=>10)))->trigger();
$this->assertEquals(2, $DB->count_records('logstore_standard_log'));
set_config('enabled_stores', '', 'tool_log');
get_log_manager(true);
}
}
+54
View File
@@ -0,0 +1,54 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Log manager and log API tests.
*
* @package tool_log
* @copyright 2014 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
class tool_log_manager_testcase extends advanced_testcase {
public function test_get_log_manager() {
global $CFG;
$this->resetAfterTest();
$manager = get_log_manager();
$this->assertInstanceOf('core\log\manager', $manager);
$stores = $manager->get_readers();
$this->assertInternalType('array', $stores);
$this->assertCount(0, $stores);
$this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/log/store/standard/version.php");
$this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/log/store/legacy/version.php");
set_config('enabled_stores', 'logstore_standard,logstore_legacy', 'tool_log');
$manager = get_log_manager(true);
$this->assertInstanceOf('core\log\manager', $manager);
$stores = $manager->get_readers();
$this->assertInternalType('array', $stores);
$this->assertCount(2, $stores);
foreach ($stores as $key => $store) {
$this->assertInternalType('string', $key);
$this->assertInstanceOf('core\log\reader', $store);
}
}
}
+1 -26
View File
@@ -476,12 +476,6 @@ class core_event_testcase extends advanced_testcase {
),
);
// Enable legacy logging plugin.
$this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/log/store/legacy/version.php");
set_config('enabled_stores', 'logstore_legacy', 'tool_log');
set_config('loglegacy', 1, 'logstore_legacy');
get_log_manager(true);
$DB->delete_records('log', array());
events_update_definition('unittest');
$DB->delete_records_select('events_handlers', "component <> 'unittest'");
@@ -508,26 +502,7 @@ class core_event_testcase extends advanced_testcase {
$this->assertSame(array(1, 5), \core_tests\event\unittest_observer::$event[2]);
$logs = $DB->get_records('log', array(), 'id ASC');
$this->assertCount(3, $logs);
$log = array_shift($logs);
$this->assertEquals(1, $log->course);
$this->assertSame('core_unittest', $log->module);
$this->assertSame('view', $log->action);
$log = array_shift($logs);
$this->assertEquals(2, $log->course);
$this->assertSame('core_unittest', $log->module);
$this->assertSame('view', $log->action);
$log = array_shift($logs);
$this->assertEquals(3, $log->course);
$this->assertSame('core_unittest', $log->module);
$this->assertSame('view', $log->action);
// Disable all logging again.
set_config('enabled_stores', 'logstore_legacy', 'tool_log');
get_log_manager(true);
$this->assertCount(0, $logs);
}
public function test_restore_event() {
+1 -1
View File
@@ -45,7 +45,7 @@ class unittest_executed extends \core\event\base {
}
public function get_url() {
return new moodle_url('/somepath/somefile.php', array('id'=>$this->data['other']['sample']));
return new \moodle_url('/somepath/somefile.php', array('id'=>$this->data['other']['sample']));
}
public static function get_legacy_eventname() {