2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Tests for ../statslib.php
22 * @copyright 2012 Tyler Bannister
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir . '/adminlib.php');
30 require_once($CFG->libdir . '/statslib.php');
31 require_once($CFG->libdir . '/cronlib.php');
32 require_once(__DIR__ . '/fixtures/stats_events.php');
35 * Test functions that affect daily stats.
37 class core_statslib_testcase extends advanced_testcase {
38 /** The day to use for testing **/
39 const DAY = 1272672000;
41 /** The timezone to use for testing **/
44 /** @var array The list of temporary tables created for the statistic calculations **/
45 protected $tables = array('temp_log1', 'temp_log2', 'temp_stats_daily', 'temp_stats_user_daily');
47 /** @var array The replacements to be used when loading XML files **/
48 protected $replacements = null;
53 * - Allow changes to CFG->debug for testing purposes.
55 protected function setUp() {
59 // Settings to force statistic to run during testing.
60 $this->setTimezone(self::TIMEZONE);
61 core_date::set_default_server_timezone();
62 $CFG->statsfirstrun = 'all';
63 $CFG->statslastdaily = 0;
65 // Figure out the broken day start so I can figure out when to the start time should be.
67 // This nonsense needs to be rewritten.
68 $date = new DateTime('now', core_date::get_server_timezone_object());
69 $offset = $date->getOffset();
70 $stime = $time + $offset;
71 $stime = intval($stime / (60*60*24)) * 60*60*24;
74 $shour = intval(($time - $stime) / (60*60));
76 if ($DB->record_exists('user', array('username' => 'user1'))) {
80 // Set up the database.
81 $datagen = self::getDataGenerator();
83 $user1 = $datagen->create_user(array('username'=>'user1'));
84 $user2 = $datagen->create_user(array('username'=>'user2'));
86 $course1 = $datagen->create_course(array('shortname'=>'course1'));
87 $datagen->enrol_user($user1->id, $course1->id);
89 $this->generate_replacement_list();
91 // Reset between tests.
92 $this->resetAfterTest();
96 * Function to setup database.
98 * @param array $dataset An array of tables including the log table.
99 * @param array $tables
101 protected function prepare_db($dataset, $tables) {
104 foreach ($tables as $tablename) {
105 $DB->delete_records($tablename);
107 foreach ($dataset as $name => $table) {
109 if ($tablename == $name) {
111 $rows = $table->getRowCount();
113 for ($i = 0; $i < $rows; $i++) {
114 $row = $table->getRow($i);
116 $DB->insert_record($tablename, $row, false, true);
124 * Load dataset from XML file.
126 protected function generate_replacement_list() {
129 if ($this->replacements !== null) {
133 $this->setTimezone(self::TIMEZONE);
135 $guest = $DB->get_record('user', array('id' => $CFG->siteguest));
136 $user1 = $DB->get_record('user', array('username' => 'user1'));
137 $user2 = $DB->get_record('user', array('username' => 'user2'));
139 if (($guest === false) || ($user1 === false) || ($user2 === false)) {
140 trigger_error('User setup incomplete', E_USER_ERROR);
143 $site = $DB->get_record('course', array('id' => SITEID));
144 $course1 = $DB->get_record('course', array('shortname' => 'course1'));
146 if (($site === false) || ($course1 === false)) {
147 trigger_error('Course setup incomplete', E_USER_ERROR);
150 $start = stats_get_base_daily(self::DAY + 3600);
151 $startnolog = stats_get_base_daily(stats_get_start_from('daily'));
152 $gr = get_guest_role();
153 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
155 $this->replacements = array(
156 // Start and end times.
157 '[start_0]' => $start - 14410, // 4 hours before.
158 '[start_1]' => $start + 14410, // 4 hours after.
159 '[start_2]' => $start + 14420,
160 '[start_3]' => $start + 14430,
161 '[start_4]' => $start + 100800, // 28 hours after.
162 '[end]' => stats_get_next_day_start($start),
163 '[end_no_logs]' => stats_get_next_day_start($startnolog),
166 '[guest_id]' => $guest->id,
167 '[user1_id]' => $user1->id,
168 '[user2_id]' => $user2->id,
171 '[course1_id]' => $course1->id,
172 '[site_id]' => SITEID,
175 '[frontpage_roleid]' => (int) $CFG->defaultfrontpageroleid,
176 '[guest_roleid]' => $gr->id,
177 '[student_roleid]' => $studentrole->id,
182 * Load dataset from XML file.
184 * @param string $file The name of the file to load
187 protected function load_xml_data_file($file) {
188 static $replacements = null;
190 $raw = $this->createXMLDataSet($file);
191 $clean = new PHPUnit_Extensions_Database_DataSet_ReplacementDataSet($raw);
193 foreach ($this->replacements as $placeholder => $value) {
194 $clean->addFullReplacement($placeholder, $value);
197 $logs = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($clean);
198 $logs->addIncludeTables(array('log'));
200 $stats = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($clean);
201 $stats->addIncludeTables(array('stats_daily', 'stats_user_daily'));
203 return array($logs, $stats);
207 * Provides the log data for test_statslib_cron_daily.
209 * @return array of fixture XML log file names.
211 public function daily_log_provider() {
213 $fileno = array('00', '01', '02', '03', '04', '05', '06', '07', '08');
215 foreach ($fileno as $no) {
216 $logfiles[] = array("statslib-test{$no}.xml");
223 * Compare the expected stats to those in the database.
225 * @param array $expected
226 * @param string $output
228 protected function verify_stats($expected, $output = '') {
231 // Note: We can not use $this->assertDataSetEqual($expected, $actual) because there's no
232 // $this->getConnection() in advanced_testcase.
234 foreach ($expected as $type => $table) {
235 $records = $DB->get_records($type);
237 $rows = $table->getRowCount();
239 $message = 'Incorrect number of results returned for '. $type;
242 $message .= "\nCron output:\n$output";
245 $this->assertCount($rows, $records, $message);
247 for ($i = 0; $i < $rows; $i++) {
248 $row = $table->getRow($i);
251 foreach ($records as $key => $record) {
252 $record = (array) $record;
253 unset($record['id']);
254 $diff = array_merge(array_diff_assoc($row, $record),
255 array_diff_assoc($record, $row));
263 $this->assertGreaterThan(0, $found, 'Expected log '. var_export($row, true)
264 ." was not found in $type ". var_export($records, true));
265 unset($records[$found]);
271 * Test progress output when debug is on.
273 public function test_statslib_progress_debug() {
274 set_debugging(DEBUG_ALL);
275 $this->expectOutputString('1:0 ');
276 stats_progress('init');
278 $this->resetDebugging();
282 * Test progress output when debug is off.
284 public function test_statslib_progress_no_debug() {
285 set_debugging(DEBUG_NONE);
286 $this->expectOutputString('.');
287 stats_progress('init');
289 $this->resetDebugging();
293 * Test the function that gets the start date from the config.
295 public function test_statslib_get_start_from() {
298 $dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test01.xml");
299 $DB->delete_records('log');
301 $date = new DateTime('now', core_date::get_server_timezone_object());
302 $day = self::DAY - $date->getOffset();
304 $CFG->statsfirstrun = 'all';
305 // Allow 1 second difference in case we cross a second boundary.
306 // Note: within 3 days of a DST change - -3 days != 3 * 24 hours (it may be more or less).
307 $this->assertLessThanOrEqual(1, stats_get_start_from('daily') - strtotime('-3 days', time()), 'All start time');
309 $this->prepare_db($dataset[0], array('log'));
310 $records = $DB->get_records('log');
312 $this->assertEquals($day + 14410, stats_get_start_from('daily'), 'Log entry start');
314 $CFG->statsfirstrun = 'none';
315 $this->assertLessThanOrEqual(1, stats_get_start_from('daily') - strtotime('-3 days', time()), 'None start time');
317 $CFG->statsfirstrun = 14515200;
318 $this->assertLessThanOrEqual(1, stats_get_start_from('daily') - (time() - (14515200)), 'Specified start time');
320 $this->prepare_db($dataset[1], array('stats_daily'));
321 $this->assertEquals($day + DAYSECS, stats_get_start_from('daily'), 'Daily stats start time');
324 $this->preventResetByRollback();
326 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/log/store/standard/version.php");
327 set_config('enabled_stores', 'logstore_standard', 'tool_log');
328 set_config('buffersize', 0, 'logstore_standard');
329 set_config('logguests', 1, 'logstore_standard');
330 get_log_manager(true);
332 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
334 $DB->delete_records('stats_daily');
335 $CFG->statsfirstrun = 'all';
336 $firstoldtime = $DB->get_field_sql('SELECT MIN(time) FROM {log}');
338 $this->assertEquals($firstoldtime, stats_get_start_from('daily'));
341 \core_tests\event\create_executed::create(array('context' => context_system::instance()))->trigger();
342 $DB->set_field('logstore_standard_log', 'timecreated', $time++, [
343 'eventname' => '\\core_tests\\event\\create_executed',
346 \core_tests\event\read_executed::create(array('context' => context_system::instance()))->trigger();
347 $DB->set_field('logstore_standard_log', 'timecreated', $time++, [
348 'eventname' => '\\core_tests\\event\\read_executed',
351 \core_tests\event\update_executed::create(array('context' => context_system::instance()))->trigger();
352 $DB->set_field('logstore_standard_log', 'timecreated', $time++, [
353 'eventname' => '\\core_tests\\event\\update_executed',
356 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
357 $DB->set_field('logstore_standard_log', 'timecreated', $time++, [
358 'eventname' => '\\core_tests\\event\\delete_executed',
361 $DB->set_field('logstore_standard_log', 'origin', 'web', array());
362 $logs = $DB->get_records('logstore_standard_log', null, 'timecreated ASC');
363 $this->assertCount(4, $logs);
365 $firstnew = reset($logs);
366 $this->assertGreaterThan($firstoldtime, $firstnew->timecreated);
367 $DB->set_field('logstore_standard_log', 'timecreated', 10, array('id' => $firstnew->id));
368 $this->assertEquals(10, stats_get_start_from('daily'));
370 $DB->set_field('logstore_standard_log', 'timecreated', $firstnew->timecreated, array('id' => $firstnew->id));
371 $DB->delete_records('log');
372 $this->assertEquals($firstnew->timecreated, stats_get_start_from('daily'));
374 set_config('enabled_stores', '', 'tool_log');
375 get_log_manager(true);
379 * Test the function that calculates the start of the day.
381 * NOTE: I don't think this is the way this function should work.
382 * This test documents the current functionality.
384 public function test_statslib_get_base_daily() {
387 for ($x = 0; $x < 13; $x += 1) {
388 $this->setTimezone($x);
390 $start = 1272672000 - ($x * 3600);
392 $start += (24 * 3600);
395 $this->assertEquals($start, stats_get_base_daily(1272686410), "Timezone $x check");
400 * Test the function that gets the start of the next day.
402 public function test_statslib_get_next_day_start() {
403 $this->setTimezone(0);
404 $this->assertEquals(1272758400, stats_get_next_day_start(1272686410));
406 // Try setting timezone to some place in the US.
407 $this->setTimezone('America/New_York', 'America/New_York');
408 // Then set the time for midnight before daylight savings.
409 // 1425790800 is midnight in New York (2015-03-08) Daylight saving will occur in 2 hours time.
410 // 1425873600 is midnight the next day.
411 $this->assertEquals(1425873600, stats_get_next_day_start(1425790800));
412 $this->assertEquals(23, ((1425873600 - 1425790800) / 60 ) / 60);
413 // Then set the time for midnight before daylight savings ends.
414 // 1446350400 is midnight in New York (2015-11-01) Daylight saving will finish in 2 hours time.
415 // 1446440400 is midnight the next day.
416 $this->assertEquals(1446440400, stats_get_next_day_start(1446350400));
417 $this->assertEquals(25, ((1446440400 - 1446350400) / 60 ) / 60);
418 // The next day should be normal.
419 $this->assertEquals(1446526800, stats_get_next_day_start(1446440400));
420 $this->assertEquals(24, ((1446526800 - 1446440400) / 60 ) / 60);
424 * Test the function that gets the action names.
426 * Note: The function results depend on installed modules. The hard coded lists are the
427 * defaults for a new Moodle 2.3 install.
429 public function test_statslib_get_action_names() {
430 $basepostactions = array (
436 5 => 'edit sectionenrol',
444 13 => 'submit for grading',
447 16 => 'choose again',
448 17 => 'record delete',
449 18 => 'add discussion',
451 20 => 'delete discussion',
453 22 => 'move discussion',
456 25 => 'add category',
458 27 => 'approve entry',
459 28 => 'delete category',
460 29 => 'delete entry',
461 30 => 'edit category',
462 31 => 'update entry',
466 35 => 'close attempt',
468 37 => 'editquestions',
469 38 => 'delete attempt',
473 $baseviewactions = array (
477 3 => 'view submission',
478 4 => 'view feedback',
481 7 => 'view discussion',
496 $postactions = stats_get_action_names('post');
498 foreach ($basepostactions as $action) {
499 $this->assertContains($action, $postactions);
502 $viewactions = stats_get_action_names('view');
504 foreach ($baseviewactions as $action) {
505 $this->assertContains($action, $viewactions);
510 * Test the temporary table creation and deletion.
512 public function test_statslib_temp_table_create_and_drop() {
515 foreach ($this->tables as $table) {
516 $this->assertFalse($DB->get_manager()->table_exists($table));
519 stats_temp_table_create();
521 foreach ($this->tables as $table) {
522 $this->assertTrue($DB->get_manager()->table_exists($table));
525 stats_temp_table_drop();
527 foreach ($this->tables as $table) {
528 $this->assertFalse($DB->get_manager()->table_exists($table));
533 * Test the temporary table creation and deletion.
535 * @depends test_statslib_temp_table_create_and_drop
537 public function test_statslib_temp_table_fill() {
538 global $CFG, $DB, $USER;
540 $dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test09.xml");
542 $this->prepare_db($dataset[0], array('log'));
544 // This nonsense needs to be rewritten.
545 $date = new DateTime('now', core_date::get_server_timezone_object());
546 $start = self::DAY - $date->getOffset();
547 $end = $start + (24 * 3600);
549 stats_temp_table_create();
550 stats_temp_table_fill($start, $end);
552 $this->assertEquals(1, $DB->count_records('temp_log1'));
553 $this->assertEquals(1, $DB->count_records('temp_log2'));
555 stats_temp_table_drop();
558 $this->preventResetByRollback();
559 stats_temp_table_create();
561 $course = $this->getDataGenerator()->create_course();
562 $context = context_course::instance($course->id);
563 $fcontext = context_course::instance(SITEID);
564 $user = $this->getDataGenerator()->create_user();
565 $this->setUser($user);
567 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/log/store/standard/version.php");
568 set_config('enabled_stores', 'logstore_standard', 'tool_log');
569 set_config('buffersize', 0, 'logstore_standard');
570 set_config('logguests', 1, 'logstore_standard');
571 get_log_manager(true);
573 $DB->delete_records('logstore_standard_log');
575 \core_tests\event\create_executed::create(array('context' => $fcontext, 'courseid' => SITEID))->trigger();
576 \core_tests\event\read_executed::create(array('context' => $context, 'courseid' => $course->id))->trigger();
577 \core_tests\event\update_executed::create(array('context' => context_system::instance()))->trigger();
578 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
580 \core\event\user_loggedin::create(
582 'userid' => $USER->id,
583 'objectid' => $USER->id,
584 'other' => array('username' => $USER->username),
588 $DB->set_field('logstore_standard_log', 'timecreated', 10);
590 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
592 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
593 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
595 // Fake the origin of events.
596 $DB->set_field('logstore_standard_log', 'origin', 'web', array());
598 $this->assertEquals(7, $DB->count_records('logstore_standard_log'));
600 stats_temp_table_fill(9, 11);
602 $logs1 = $DB->get_records('temp_log1');
603 $logs2 = $DB->get_records('temp_log2');
604 $this->assertCount(5, $logs1);
605 $this->assertCount(5, $logs2);
606 // The order of records in the temp tables is not guaranteed...
611 foreach ($logs1 as $log) {
612 if ($log->course == $course->id) {
613 $this->assertEquals('view', $log->action);
616 $this->assertTrue(in_array($log->action, array('update', 'login')));
617 if ($log->action === 'update') {
622 $this->assertEquals(SITEID, $log->course);
624 $this->assertEquals($user->id, $log->userid);
626 $this->assertEquals(1, $viewcount);
627 $this->assertEquals(3, $updatecount);
628 $this->assertEquals(1, $logincount);
630 set_config('enabled_stores', '', 'tool_log');
631 get_log_manager(true);
632 stats_temp_table_drop();
636 * Test the temporary table creation and deletion.
638 * @depends test_statslib_temp_table_create_and_drop
640 public function test_statslib_temp_table_setup() {
644 $this->prepare_db($logs, array('log'));
646 stats_temp_table_create();
647 stats_temp_table_setup();
649 $this->assertEquals(1, $DB->count_records('temp_enroled'));
651 stats_temp_table_drop();
655 * Test the function that clean out the temporary tables.
657 * @depends test_statslib_temp_table_create_and_drop
659 public function test_statslib_temp_table_clean() {
663 'temp_log1' => array('id' => 1, 'course' => 1),
664 'temp_log2' => array('id' => 1, 'course' => 1),
665 'temp_stats_daily' => array('id' => 1, 'courseid' => 1),
666 'temp_stats_user_daily' => array('id' => 1, 'courseid' => 1),
669 stats_temp_table_create();
671 foreach ($rows as $table => $row) {
672 $DB->insert_record_raw($table, $row);
673 $this->assertEquals(1, $DB->count_records($table));
676 stats_temp_table_clean();
678 foreach ($rows as $table => $row) {
679 $this->assertEquals(0, $DB->count_records($table));
682 $this->assertEquals(1, $DB->count_records('stats_daily'));
683 $this->assertEquals(1, $DB->count_records('stats_user_daily'));
685 stats_temp_table_drop();
689 * Test the daily stats function.
691 * @depends test_statslib_get_base_daily
692 * @depends test_statslib_get_next_day_start
693 * @depends test_statslib_get_start_from
694 * @depends test_statslib_temp_table_create_and_drop
695 * @depends test_statslib_temp_table_setup
696 * @depends test_statslib_temp_table_fill
697 * @dataProvider daily_log_provider
699 public function test_statslib_cron_daily($xmlfile) {
702 $dataset = $this->load_xml_data_file(__DIR__."/fixtures/{$xmlfile}");
704 list($logs, $stats) = $dataset;
705 $this->prepare_db($logs, array('log'));
707 // Stats cron daily uses mtrace, turn on buffering to silence output.
710 $output = ob_get_contents();
713 $this->verify_stats($stats, $output);
717 * Test the daily stats function.
719 * @depends test_statslib_get_base_daily
720 * @depends test_statslib_get_next_day_start
722 public function test_statslib_cron_daily_no_default_profile_id() {
724 $CFG->defaultfrontpageroleid = 0;
726 $course1 = $DB->get_record('course', array('shortname' => 'course1'));
727 $guestid = $CFG->siteguest;
728 $start = stats_get_base_daily(1272758400);
729 $end = stats_get_next_day_start($start);
730 $fpid = (int) $CFG->defaultfrontpageroleid;
731 $gr = get_guest_role();
733 $dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test10.xml");
735 $this->prepare_db($dataset[0], array('log'));
737 // Stats cron daily uses mtrace, turn on buffering to silence output.
739 stats_cron_daily($maxdays=1);
740 $output = ob_get_contents();
743 $this->verify_stats($dataset[1], $output);