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/>.
19 defined('MOODLE_INTERNAL') ||
die();
22 require_once($CFG->libdir
. '/adminlib.php');
23 require_once($CFG->libdir
. '/statslib.php');
24 require_once(__DIR__
. '/fixtures/stats_events.php');
27 * Test functions that affect daily stats.
31 * @copyright 2012 Tyler Bannister
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class statslib_test
extends \advanced_testcase
{
35 /** The day to use for testing **/
36 const DAY
= 1272672000;
38 /** The timezone to use for testing **/
41 /** @var array The list of temporary tables created for the statistic calculations **/
42 protected $tables = array('temp_log1', 'temp_log2', 'temp_stats_daily', 'temp_stats_user_daily');
44 /** @var array The replacements to be used when loading XML files **/
45 protected $replacements = null;
50 * - Allow changes to CFG->debug for testing purposes.
52 protected function setUp(): void
{
56 // Settings to force statistic to run during testing.
57 $this->setTimezone(self
::TIMEZONE
);
58 \core_date
::set_default_server_timezone();
59 $CFG->statsfirstrun
= 'all';
60 $CFG->statslastdaily
= 0;
62 // Figure out the broken day start so I can figure out when to the start time should be.
64 // This nonsense needs to be rewritten.
65 $date = new \
DateTime('now', \core_date
::get_server_timezone_object());
66 $offset = $date->getOffset();
67 $stime = $time +
$offset;
68 $stime = intval($stime / (60*60*24)) * 60*60*24;
71 $shour = intval(($time - $stime) / (60*60));
73 if ($DB->record_exists('user', array('username' => 'user1'))) {
77 // Set up the database.
78 $datagen = self
::getDataGenerator();
80 $user1 = $datagen->create_user(array('username'=>'user1'));
81 $user2 = $datagen->create_user(array('username'=>'user2'));
83 $course1 = $datagen->create_course(array('shortname'=>'course1'));
84 $datagen->enrol_user($user1->id
, $course1->id
);
86 $this->generate_replacement_list();
88 // Reset between tests.
89 $this->resetAfterTest();
93 * Function to setup database.
95 * @param phpunit_dataset $dataset Containing all the information loaded from fixtures.
96 * @param array $filter Tables to be sent to database.
98 protected function prepare_db($dataset, $tables) {
101 foreach ($tables as $tablename) {
102 $DB->delete_records($tablename);
103 $dataset->to_database([$tablename]);
108 * Load dataset from XML file.
110 protected function generate_replacement_list() {
113 if ($this->replacements
!== null) {
117 $this->setTimezone(self
::TIMEZONE
);
119 $guest = $DB->get_record('user', array('id' => $CFG->siteguest
));
120 $user1 = $DB->get_record('user', array('username' => 'user1'));
121 $user2 = $DB->get_record('user', array('username' => 'user2'));
123 if (($guest === false) ||
($user1 === false) ||
($user2 === false)) {
124 trigger_error('User setup incomplete', E_USER_ERROR
);
127 $site = $DB->get_record('course', array('id' => SITEID
));
128 $course1 = $DB->get_record('course', array('shortname' => 'course1'));
130 if (($site === false) ||
($course1 === false)) {
131 trigger_error('Course setup incomplete', E_USER_ERROR
);
134 $start = stats_get_base_daily(self
::DAY +
3600);
135 $startnolog = stats_get_base_daily(stats_get_start_from('daily'));
136 $gr = get_guest_role();
137 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
139 $this->replacements
= array(
140 // Start and end times.
141 '[start_0]' => $start - 14410, // 4 hours before.
142 '[start_1]' => $start +
14410, // 4 hours after.
143 '[start_2]' => $start +
14420,
144 '[start_3]' => $start +
14430,
145 '[start_4]' => $start +
100800, // 28 hours after.
146 '[end]' => stats_get_next_day_start($start),
147 '[end_no_logs]' => stats_get_next_day_start($startnolog),
150 '[guest_id]' => $guest->id
,
151 '[user1_id]' => $user1->id
,
152 '[user2_id]' => $user2->id
,
155 '[course1_id]' => $course1->id
,
156 '[site_id]' => SITEID
,
159 '[frontpage_roleid]' => (int) $CFG->defaultfrontpageroleid
,
160 '[guest_roleid]' => $gr->id
,
161 '[student_roleid]' => $studentrole->id
,
166 * Load dataset from XML file.
168 * @param string $file The name of the file to load
169 * @return phpunit_dataset
171 protected function load_xml_data_file($file) {
173 $xml = file_get_contents($file);
175 // Apply all the replacements straight in xml.
176 foreach ($this->replacements
as $placeholder => $value) {
177 $placeholder = preg_quote($placeholder, '/');
178 $xml = preg_replace('/' . $placeholder . '/', $value, $xml);
181 return $this->dataset_from_string($xml, 'xml');
185 * Provides the log data for test_statslib_cron_daily.
187 * @return array of fixture XML log file names.
189 public function daily_log_provider() {
191 $fileno = array('00', '01', '02', '03', '04', '05', '06', '07', '08');
193 foreach ($fileno as $no) {
194 $logfiles[] = array("statslib-test{$no}.xml");
201 * Set of data for test_statlibs_get_base_weekly
203 * @return array Dates and timezones for which the first day of the week will be calculated
205 public function get_base_weekly_provider() {
209 "timezone" => 'America/Chicago',
210 "timestart" => '18-03-2017 22:00',
211 "expected" => '12-03-2017 00:00:00'
215 "timezone" => 'America/Chicago',
216 "date" => '25-03-2017 22:00',
217 "expected" => '19-03-2017 00:00:00'
221 "timezone" => 'Atlantic/Canary',
222 "date" => '06-08-2018 22:00',
223 "expected" => '06-08-2018 00:00:00'
229 * Compare the expected stats to those in the database.
231 * @param array $expected
232 * @param string $output
234 protected function verify_stats($expected, $output = '') {
237 // Note: We can not use $this->assertDataSetEqual($expected, $actual) because there's no
238 // $this->getConnection() in advanced_testcase.
240 foreach ($expected as $type => $table) {
241 $records = $DB->get_records($type);
243 $rows = count($table);
245 $message = 'Incorrect number of results returned for '. $type;
248 $message .= "\nCron output:\n$output";
251 $this->assertCount($rows, $records, $message);
253 for ($i = 0; $i < $rows; $i++
) {
257 foreach ($records as $key => $record) {
258 $record = (array) $record;
259 unset($record['id']);
260 $diff = array_merge(array_diff_assoc($row, $record),
261 array_diff_assoc($record, $row));
269 $this->assertGreaterThan(0, $found, 'Expected log '. var_export($row, true)
270 ." was not found in $type ". var_export($records, true));
271 unset($records[$found]);
277 * Test progress output when debug is on.
279 public function test_statslib_progress_debug() {
280 set_debugging(DEBUG_ALL
);
281 $this->expectOutputString('1:0 ');
282 stats_progress('init');
284 $this->resetDebugging();
288 * Test progress output when debug is off.
290 public function test_statslib_progress_no_debug() {
291 set_debugging(DEBUG_NONE
);
292 $this->expectOutputString('.');
293 stats_progress('init');
295 $this->resetDebugging();
299 * Test the function that gets the start date from the config.
301 public function test_statslib_get_start_from() {
304 $dataset = $this->load_xml_data_file(__DIR__
."/fixtures/statslib-test01.xml");
305 $DB->delete_records('log');
307 $date = new \
DateTime('now', \core_date
::get_server_timezone_object());
308 $day = self
::DAY
- $date->getOffset();
310 $CFG->statsfirstrun
= 'all';
311 // Allow 1 second difference in case we cross a second boundary.
312 // Note: within 3 days of a DST change - -3 days != 3 * 24 hours (it may be more or less).
313 $this->assertLessThanOrEqual(1, stats_get_start_from('daily') - strtotime('-3 days', time()), 'All start time');
315 $this->prepare_db($dataset, array('log'));
316 $records = $DB->get_records('log');
318 $this->assertEquals($day +
14410, stats_get_start_from('daily'), 'Log entry start');
320 $CFG->statsfirstrun
= 'none';
321 $this->assertLessThanOrEqual(1, stats_get_start_from('daily') - strtotime('-3 days', time()), 'None start time');
323 $CFG->statsfirstrun
= 14515200;
324 $this->assertLessThanOrEqual(1, stats_get_start_from('daily') - (time() - (14515200)), 'Specified start time');
326 $this->prepare_db($dataset, array('stats_daily'));
327 $this->assertEquals($day + DAYSECS
, stats_get_start_from('daily'), 'Daily stats start time');
330 $this->preventResetByRollback();
332 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/log/store/standard/version.php");
333 set_config('enabled_stores', 'logstore_standard', 'tool_log');
334 set_config('buffersize', 0, 'logstore_standard');
335 set_config('logguests', 1, 'logstore_standard');
336 get_log_manager(true);
338 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
340 $DB->delete_records('stats_daily');
341 $CFG->statsfirstrun
= 'all';
342 $firstoldtime = $DB->get_field_sql('SELECT MIN(time) FROM {log}');
344 $this->assertEquals($firstoldtime, stats_get_start_from('daily'));
347 \core_tests\event\create_executed
::create(array('context' => \context_system
::instance()))->trigger();
348 $DB->set_field('logstore_standard_log', 'timecreated', $time++
, [
349 'eventname' => '\\core_tests\\event\\create_executed',
352 \core_tests\event\read_executed
::create(array('context' => \context_system
::instance()))->trigger();
353 $DB->set_field('logstore_standard_log', 'timecreated', $time++
, [
354 'eventname' => '\\core_tests\\event\\read_executed',
357 \core_tests\event\update_executed
::create(array('context' => \context_system
::instance()))->trigger();
358 $DB->set_field('logstore_standard_log', 'timecreated', $time++
, [
359 'eventname' => '\\core_tests\\event\\update_executed',
362 \core_tests\event\delete_executed
::create(array('context' => \context_system
::instance()))->trigger();
363 $DB->set_field('logstore_standard_log', 'timecreated', $time++
, [
364 'eventname' => '\\core_tests\\event\\delete_executed',
367 $DB->set_field('logstore_standard_log', 'origin', 'web', array());
368 $logs = $DB->get_records('logstore_standard_log', null, 'timecreated ASC');
369 $this->assertCount(4, $logs);
371 $firstnew = reset($logs);
372 $this->assertGreaterThan($firstoldtime, $firstnew->timecreated
);
373 $DB->set_field('logstore_standard_log', 'timecreated', 10, array('id' => $firstnew->id
));
374 $this->assertEquals(10, stats_get_start_from('daily'));
376 $DB->set_field('logstore_standard_log', 'timecreated', $firstnew->timecreated
, array('id' => $firstnew->id
));
377 $DB->delete_records('log');
378 $this->assertEquals($firstnew->timecreated
, stats_get_start_from('daily'));
380 set_config('enabled_stores', '', 'tool_log');
381 get_log_manager(true);
385 * Test the function that calculates the start of the day.
387 * NOTE: I don't think this is the way this function should work.
388 * This test documents the current functionality.
390 public function test_statslib_get_base_daily() {
393 for ($x = 0; $x < 13; $x +
= 1) {
394 $this->setTimezone($x);
396 $start = 1272672000 - ($x * 3600);
398 $start +
= (24 * 3600);
401 $this->assertEquals($start, stats_get_base_daily(1272686410), "Timezone $x check");
406 * Test the function that gets the start of the next day.
408 public function test_statslib_get_next_day_start() {
409 $this->setTimezone(0);
410 $this->assertEquals(1272758400, stats_get_next_day_start(1272686410));
412 // Try setting timezone to some place in the US.
413 $this->setTimezone('America/New_York', 'America/New_York');
414 // Then set the time for midnight before daylight savings.
415 // 1425790800 is midnight in New York (2015-03-08) Daylight saving will occur in 2 hours time.
416 // 1425873600 is midnight the next day.
417 $this->assertEquals(1425873600, stats_get_next_day_start(1425790800));
418 $this->assertEquals(23, ((1425873600 - 1425790800) / 60 ) / 60);
419 // Then set the time for midnight before daylight savings ends.
420 // 1446350400 is midnight in New York (2015-11-01) Daylight saving will finish in 2 hours time.
421 // 1446440400 is midnight the next day.
422 $this->assertEquals(1446440400, stats_get_next_day_start(1446350400));
423 $this->assertEquals(25, ((1446440400 - 1446350400) / 60 ) / 60);
424 // The next day should be normal.
425 $this->assertEquals(1446526800, stats_get_next_day_start(1446440400));
426 $this->assertEquals(24, ((1446526800 - 1446440400) / 60 ) / 60);
430 * Test the function that calculates the start of the week.
432 * @dataProvider get_base_weekly_provider
433 * @param int $startwday Day in which the week starts (Sunday = 0)
434 * @param string $timezone Default timezone
435 * @param string $timestart Date and time for which the first day of the week will be obtained
436 * @param string $expected Expected date of the first day of the week
438 public function test_statslib_get_base_weekly($startwday, $timezone, $timestart, $expected) {
439 $this->setTimezone($timezone);
440 $time = strtotime($timestart);
441 $expected = strtotime($expected);
442 set_config('calendar_startwday', $startwday);
443 set_config('statslastweekly', $time);
445 $weekstarttime = stats_get_base_weekly($time);
447 $this->assertEquals($expected, $weekstarttime);
451 * Test the function that gets the action names.
453 * Note: The function results depend on installed modules. The hard coded lists are the
454 * defaults for a new Moodle 2.3 install.
456 public function test_statslib_get_action_names() {
457 $basepostactions = array (
463 5 => 'edit sectionenrol',
471 13 => 'submit for grading',
474 16 => 'choose again',
475 17 => 'record delete',
476 18 => 'add discussion',
478 20 => 'delete discussion',
480 22 => 'move discussion',
483 25 => 'add category',
485 27 => 'approve entry',
486 28 => 'delete category',
487 29 => 'delete entry',
488 30 => 'edit category',
489 31 => 'update entry',
493 35 => 'close attempt',
495 37 => 'editquestions',
496 38 => 'delete attempt',
500 $baseviewactions = array (
504 3 => 'view submission',
505 4 => 'view feedback',
508 7 => 'view discussion',
523 $postactions = stats_get_action_names('post');
525 foreach ($basepostactions as $action) {
526 $this->assertContains($action, $postactions);
529 $viewactions = stats_get_action_names('view');
531 foreach ($baseviewactions as $action) {
532 $this->assertContains($action, $viewactions);
537 * Test the temporary table creation and deletion.
539 public function test_statslib_temp_table_create_and_drop() {
542 foreach ($this->tables
as $table) {
543 $this->assertFalse($DB->get_manager()->table_exists($table));
546 stats_temp_table_create();
548 foreach ($this->tables
as $table) {
549 $this->assertTrue($DB->get_manager()->table_exists($table));
552 stats_temp_table_drop();
554 foreach ($this->tables
as $table) {
555 $this->assertFalse($DB->get_manager()->table_exists($table));
560 * Test the temporary table creation and deletion.
562 * @depends test_statslib_temp_table_create_and_drop
564 public function test_statslib_temp_table_fill() {
565 global $CFG, $DB, $USER;
567 $dataset = $this->load_xml_data_file(__DIR__
."/fixtures/statslib-test09.xml");
568 $this->prepare_db($dataset, array('log'));
570 // This nonsense needs to be rewritten.
571 $date = new \
DateTime('now', \core_date
::get_server_timezone_object());
572 $start = self
::DAY
- $date->getOffset();
573 $end = $start +
(24 * 3600);
575 stats_temp_table_create();
576 stats_temp_table_fill($start, $end);
578 $this->assertEquals(1, $DB->count_records('temp_log1'));
579 $this->assertEquals(1, $DB->count_records('temp_log2'));
581 stats_temp_table_drop();
584 $this->preventResetByRollback();
585 stats_temp_table_create();
587 $course = $this->getDataGenerator()->create_course();
588 $context = \context_course
::instance($course->id
);
589 $fcontext = \context_course
::instance(SITEID
);
590 $user = $this->getDataGenerator()->create_user();
591 $this->setUser($user);
593 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/log/store/standard/version.php");
594 set_config('enabled_stores', 'logstore_standard', 'tool_log');
595 set_config('buffersize', 0, 'logstore_standard');
596 set_config('logguests', 1, 'logstore_standard');
597 get_log_manager(true);
599 $DB->delete_records('logstore_standard_log');
601 \core_tests\event\create_executed
::create(array('context' => $fcontext, 'courseid' => SITEID
))->trigger();
602 \core_tests\event\read_executed
::create(array('context' => $context, 'courseid' => $course->id
))->trigger();
603 \core_tests\event\update_executed
::create(array('context' => \context_system
::instance()))->trigger();
604 \core_tests\event\delete_executed
::create(array('context' => \context_system
::instance()))->trigger();
606 \core\event\user_loggedin
::create(
608 'userid' => $USER->id
,
609 'objectid' => $USER->id
,
610 'other' => array('username' => $USER->username
),
614 $DB->set_field('logstore_standard_log', 'timecreated', 10);
616 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
618 \core_tests\event\delete_executed
::create(array('context' => \context_system
::instance()))->trigger();
619 \core_tests\event\delete_executed
::create(array('context' => \context_system
::instance()))->trigger();
621 // Fake the origin of events.
622 $DB->set_field('logstore_standard_log', 'origin', 'web', array());
624 $this->assertEquals(7, $DB->count_records('logstore_standard_log'));
626 stats_temp_table_fill(9, 11);
628 $logs1 = $DB->get_records('temp_log1');
629 $logs2 = $DB->get_records('temp_log2');
630 $this->assertCount(5, $logs1);
631 $this->assertCount(5, $logs2);
632 // The order of records in the temp tables is not guaranteed...
637 foreach ($logs1 as $log) {
638 if ($log->course
== $course->id
) {
639 $this->assertEquals('view', $log->action
);
642 $this->assertTrue(in_array($log->action
, array('update', 'login')));
643 if ($log->action
=== 'update') {
648 $this->assertEquals(SITEID
, $log->course
);
650 $this->assertEquals($user->id
, $log->userid
);
652 $this->assertEquals(1, $viewcount);
653 $this->assertEquals(3, $updatecount);
654 $this->assertEquals(1, $logincount);
656 set_config('enabled_stores', '', 'tool_log');
657 get_log_manager(true);
658 stats_temp_table_drop();
662 * Test the temporary table creation and deletion.
664 * @depends test_statslib_temp_table_create_and_drop
666 public function test_statslib_temp_table_setup() {
669 $DB->delete_records('log');
671 stats_temp_table_create();
672 stats_temp_table_setup();
674 $this->assertEquals(1, $DB->count_records('temp_enroled'));
676 stats_temp_table_drop();
680 * Test the function that clean out the temporary tables.
682 * @depends test_statslib_temp_table_create_and_drop
684 public function test_statslib_temp_table_clean() {
688 'temp_log1' => array('id' => 1, 'course' => 1),
689 'temp_log2' => array('id' => 1, 'course' => 1),
690 'temp_stats_daily' => array('id' => 1, 'courseid' => 1),
691 'temp_stats_user_daily' => array('id' => 1, 'courseid' => 1),
694 stats_temp_table_create();
696 foreach ($rows as $table => $row) {
697 $DB->insert_record_raw($table, $row);
698 $this->assertEquals(1, $DB->count_records($table));
701 stats_temp_table_clean();
703 foreach ($rows as $table => $row) {
704 $this->assertEquals(0, $DB->count_records($table));
707 $this->assertEquals(1, $DB->count_records('stats_daily'));
708 $this->assertEquals(1, $DB->count_records('stats_user_daily'));
710 stats_temp_table_drop();
714 * Test the daily stats function.
716 * @depends test_statslib_get_base_daily
717 * @depends test_statslib_get_next_day_start
718 * @depends test_statslib_get_start_from
719 * @depends test_statslib_temp_table_create_and_drop
720 * @depends test_statslib_temp_table_setup
721 * @depends test_statslib_temp_table_fill
722 * @dataProvider daily_log_provider
724 public function test_statslib_cron_daily($xmlfile) {
727 $dataset = $this->load_xml_data_file(__DIR__
."/fixtures/{$xmlfile}");
728 $stats = $this->prepare_db($dataset, array('log'));
729 $stats = $dataset->get_rows(['stats_daily', 'stats_user_daily']);
731 // Stats cron daily uses mtrace, turn on buffering to silence output.
734 $output = ob_get_contents();
737 $this->verify_stats($stats, $output);
741 * Test the daily stats function.
743 * @depends test_statslib_get_base_daily
744 * @depends test_statslib_get_next_day_start
746 public function test_statslib_cron_daily_no_default_profile_id() {
748 $CFG->defaultfrontpageroleid
= 0;
750 $course1 = $DB->get_record('course', array('shortname' => 'course1'));
751 $guestid = $CFG->siteguest
;
752 $start = stats_get_base_daily(1272758400);
753 $end = stats_get_next_day_start($start);
754 $fpid = (int) $CFG->defaultfrontpageroleid
;
755 $gr = get_guest_role();
757 $dataset = $this->load_xml_data_file(__DIR__
."/fixtures/statslib-test10.xml");
758 $this->prepare_db($dataset, array('log'));
759 $stats = $dataset->get_rows(['stats_user_daily']);
761 // Stats cron daily uses mtrace, turn on buffering to silence output.
763 stats_cron_daily($maxdays=1);
764 $output = ob_get_contents();
767 $this->verify_stats($dataset, $output);