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'));
340 \core_tests\event\create_executed::create(array('context' => context_system::instance()))->trigger();
341 \core_tests\event\read_executed::create(array('context' => context_system::instance()))->trigger();
342 \core_tests\event\update_executed::create(array('context' => context_system::instance()))->trigger();
343 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
345 // Fake the origin of events.
346 $DB->set_field('logstore_standard_log', 'origin', 'web', array());
348 $logs = $DB->get_records('logstore_standard_log');
349 $this->assertCount(4, $logs);
351 $firstnew = reset($logs);
352 $this->assertGreaterThan($firstoldtime, $firstnew->timecreated);
353 $DB->set_field('logstore_standard_log', 'timecreated', 10, array('id' => $firstnew->id));
354 $this->assertEquals(10, stats_get_start_from('daily'));
356 $DB->set_field('logstore_standard_log', 'timecreated', $firstnew->timecreated, array('id' => $firstnew->id));
357 $DB->delete_records('log');
358 $this->assertEquals($firstnew->timecreated, stats_get_start_from('daily'));
360 set_config('enabled_stores', '', 'tool_log');
361 get_log_manager(true);
365 * Test the function that calculates the start of the day.
367 * NOTE: I don't think this is the way this function should work.
368 * This test documents the current functionality.
370 public function test_statslib_get_base_daily() {
373 for ($x = 0; $x < 13; $x += 1) {
374 $this->setTimezone($x);
376 $start = 1272672000 - ($x * 3600);
378 $start += (24 * 3600);
381 $this->assertEquals($start, stats_get_base_daily(1272686410), "Timezone $x check");
386 * Test the function that gets the start of the next day.
388 public function test_statslib_get_next_day_start() {
389 $this->setTimezone(0);
390 $this->assertEquals(1272758400, stats_get_next_day_start(1272686410));
392 // Try setting timezone to some place in the US.
393 $this->setTimezone('America/New_York', 'America/New_York');
394 // Then set the time for midnight before daylight savings.
395 // 1425790800 is midnight in New York (2015-03-08) Daylight saving will occur in 2 hours time.
396 // 1425873600 is midnight the next day.
397 $this->assertEquals(1425873600, stats_get_next_day_start(1425790800));
398 $this->assertEquals(23, ((1425873600 - 1425790800) / 60 ) / 60);
399 // Then set the time for midnight before daylight savings ends.
400 // 1446350400 is midnight in New York (2015-11-01) Daylight saving will finish in 2 hours time.
401 // 1446440400 is midnight the next day.
402 $this->assertEquals(1446440400, stats_get_next_day_start(1446350400));
403 $this->assertEquals(25, ((1446440400 - 1446350400) / 60 ) / 60);
404 // The next day should be normal.
405 $this->assertEquals(1446526800, stats_get_next_day_start(1446440400));
406 $this->assertEquals(24, ((1446526800 - 1446440400) / 60 ) / 60);
410 * Test the function that gets the action names.
412 * Note: The function results depend on installed modules. The hard coded lists are the
413 * defaults for a new Moodle 2.3 install.
415 public function test_statslib_get_action_names() {
416 $basepostactions = array (
422 5 => 'edit sectionenrol',
430 13 => 'submit for grading',
433 16 => 'choose again',
434 17 => 'record delete',
435 18 => 'add discussion',
437 20 => 'delete discussion',
439 22 => 'move discussion',
442 25 => 'add category',
444 27 => 'approve entry',
445 28 => 'delete category',
446 29 => 'delete entry',
447 30 => 'edit category',
448 31 => 'update entry',
452 35 => 'close attempt',
454 37 => 'editquestions',
455 38 => 'delete attempt',
459 $baseviewactions = array (
463 3 => 'view submission',
464 4 => 'view feedback',
467 7 => 'view discussion',
482 $postactions = stats_get_action_names('post');
484 foreach ($basepostactions as $action) {
485 $this->assertContains($action, $postactions);
488 $viewactions = stats_get_action_names('view');
490 foreach ($baseviewactions as $action) {
491 $this->assertContains($action, $viewactions);
496 * Test the temporary table creation and deletion.
498 public function test_statslib_temp_table_create_and_drop() {
501 foreach ($this->tables as $table) {
502 $this->assertFalse($DB->get_manager()->table_exists($table));
505 stats_temp_table_create();
507 foreach ($this->tables as $table) {
508 $this->assertTrue($DB->get_manager()->table_exists($table));
511 stats_temp_table_drop();
513 foreach ($this->tables as $table) {
514 $this->assertFalse($DB->get_manager()->table_exists($table));
519 * Test the temporary table creation and deletion.
521 * @depends test_statslib_temp_table_create_and_drop
523 public function test_statslib_temp_table_fill() {
524 global $CFG, $DB, $USER;
526 $dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test09.xml");
528 $this->prepare_db($dataset[0], array('log'));
530 // This nonsense needs to be rewritten.
531 $date = new DateTime('now', core_date::get_server_timezone_object());
532 $start = self::DAY - $date->getOffset();
533 $end = $start + (24 * 3600);
535 stats_temp_table_create();
536 stats_temp_table_fill($start, $end);
538 $this->assertEquals(1, $DB->count_records('temp_log1'));
539 $this->assertEquals(1, $DB->count_records('temp_log2'));
541 stats_temp_table_drop();
544 $this->preventResetByRollback();
545 stats_temp_table_create();
547 $course = $this->getDataGenerator()->create_course();
548 $context = context_course::instance($course->id);
549 $fcontext = context_course::instance(SITEID);
550 $user = $this->getDataGenerator()->create_user();
551 $this->setUser($user);
553 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/log/store/standard/version.php");
554 set_config('enabled_stores', 'logstore_standard', 'tool_log');
555 set_config('buffersize', 0, 'logstore_standard');
556 set_config('logguests', 1, 'logstore_standard');
557 get_log_manager(true);
559 $DB->delete_records('logstore_standard_log');
561 \core_tests\event\create_executed::create(array('context' => $fcontext, 'courseid' => SITEID))->trigger();
562 \core_tests\event\read_executed::create(array('context' => $context, 'courseid' => $course->id))->trigger();
563 \core_tests\event\update_executed::create(array('context' => context_system::instance()))->trigger();
564 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
566 \core\event\user_loggedin::create(
568 'userid' => $USER->id,
569 'objectid' => $USER->id,
570 'other' => array('username' => $USER->username),
574 $DB->set_field('logstore_standard_log', 'timecreated', 10);
576 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
578 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
579 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
581 // Fake the origin of events.
582 $DB->set_field('logstore_standard_log', 'origin', 'web', array());
584 $this->assertEquals(7, $DB->count_records('logstore_standard_log'));
586 stats_temp_table_fill(9, 11);
588 $logs1 = $DB->get_records('temp_log1');
589 $logs2 = $DB->get_records('temp_log2');
590 $this->assertCount(5, $logs1);
591 $this->assertCount(5, $logs2);
592 // The order of records in the temp tables is not guaranteed...
597 foreach ($logs1 as $log) {
598 if ($log->course == $course->id) {
599 $this->assertEquals('view', $log->action);
602 $this->assertTrue(in_array($log->action, array('update', 'login')));
603 if ($log->action === 'update') {
608 $this->assertEquals(SITEID, $log->course);
610 $this->assertEquals($user->id, $log->userid);
612 $this->assertEquals(1, $viewcount);
613 $this->assertEquals(3, $updatecount);
614 $this->assertEquals(1, $logincount);
616 set_config('enabled_stores', '', 'tool_log');
617 get_log_manager(true);
618 stats_temp_table_drop();
622 * Test the temporary table creation and deletion.
624 * @depends test_statslib_temp_table_create_and_drop
626 public function test_statslib_temp_table_setup() {
630 $this->prepare_db($logs, array('log'));
632 stats_temp_table_create();
633 stats_temp_table_setup();
635 $this->assertEquals(1, $DB->count_records('temp_enroled'));
637 stats_temp_table_drop();
641 * Test the function that clean out the temporary tables.
643 * @depends test_statslib_temp_table_create_and_drop
645 public function test_statslib_temp_table_clean() {
649 'temp_log1' => array('id' => 1, 'course' => 1),
650 'temp_log2' => array('id' => 1, 'course' => 1),
651 'temp_stats_daily' => array('id' => 1, 'courseid' => 1),
652 'temp_stats_user_daily' => array('id' => 1, 'courseid' => 1),
655 stats_temp_table_create();
657 foreach ($rows as $table => $row) {
658 $DB->insert_record_raw($table, $row);
659 $this->assertEquals(1, $DB->count_records($table));
662 stats_temp_table_clean();
664 foreach ($rows as $table => $row) {
665 $this->assertEquals(0, $DB->count_records($table));
668 $this->assertEquals(1, $DB->count_records('stats_daily'));
669 $this->assertEquals(1, $DB->count_records('stats_user_daily'));
671 stats_temp_table_drop();
675 * Test the daily stats function.
677 * @depends test_statslib_get_base_daily
678 * @depends test_statslib_get_next_day_start
679 * @depends test_statslib_get_start_from
680 * @depends test_statslib_temp_table_create_and_drop
681 * @depends test_statslib_temp_table_setup
682 * @depends test_statslib_temp_table_fill
683 * @dataProvider daily_log_provider
685 public function test_statslib_cron_daily($xmlfile) {
688 $dataset = $this->load_xml_data_file(__DIR__."/fixtures/{$xmlfile}");
690 list($logs, $stats) = $dataset;
691 $this->prepare_db($logs, array('log'));
693 // Stats cron daily uses mtrace, turn on buffering to silence output.
696 $output = ob_get_contents();
699 $this->verify_stats($stats, $output);
703 * Test the daily stats function.
705 * @depends test_statslib_get_base_daily
706 * @depends test_statslib_get_next_day_start
708 public function test_statslib_cron_daily_no_default_profile_id() {
710 $CFG->defaultfrontpageroleid = 0;
712 $course1 = $DB->get_record('course', array('shortname' => 'course1'));
713 $guestid = $CFG->siteguest;
714 $start = stats_get_base_daily(1272758400);
715 $end = stats_get_next_day_start($start);
716 $fpid = (int) $CFG->defaultfrontpageroleid;
717 $gr = get_guest_role();
719 $dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test10.xml");
721 $this->prepare_db($dataset[0], array('log'));
723 // Stats cron daily uses mtrace, turn on buffering to silence output.
725 stats_cron_daily($maxdays=1);
726 $output = ob_get_contents();
729 $this->verify_stats($dataset[1], $output);