Merge branch 'MDL-47975-master' of git://github.com/damyon/moodle
[moodle.git] / lib / tests / statslib_test.php
CommitLineData
c0f00c5f
TB
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Tests for ../statslib.php
19 *
d53111c0
PS
20 * @package core_stats
21 * @category phpunit
c0f00c5f
TB
22 * @copyright 2012 Tyler Bannister
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/adminlib.php');
30require_once($CFG->libdir . '/statslib.php');
613eebfc 31require_once($CFG->libdir . '/cronlib.php');
92927fba 32require_once(__DIR__ . '/fixtures/stats_events.php');
c0f00c5f
TB
33
34/**
d53111c0 35 * Test functions that affect daily stats.
c0f00c5f 36 */
d53111c0 37class core_statslib_testcase extends advanced_testcase {
c0f00c5f
TB
38 /** The student role ID **/
39 const STID = 5;
40
41 /** The day to use for testing **/
67690cfa
TB
42 const DAY = 1272672000;
43
44 /** The timezone to use for testing **/
45 const TIMEZONE = 0;
c0f00c5f
TB
46
47 /** @var array The list of temporary tables created for the statistic calculations **/
48 protected $tables = array('temp_log1', 'temp_log2', 'temp_stats_daily', 'temp_stats_user_daily');
49
50 /** @var array The replacements to be used when loading XML files **/
51 protected $replacements = null;
52
53 /**
d53111c0 54 * Set up the database for tests.
c0f00c5f
TB
55 *
56 * This function is needed so that daily_log_provider has the before-test set up from setUp()
57 */
d53111c0 58 protected function set_up_db() {
c0f00c5f
TB
59 global $DB;
60
61 if ($DB->record_exists('user', array('username' => 'user1'))) {
62 return;
63 }
64
65 $datagen = self::getDataGenerator();
66
67 $user1 = $datagen->create_user(array('username'=>'user1'));
68 $user2 = $datagen->create_user(array('username'=>'user2'));
69
70 $course1 = $datagen->create_course(array('shortname'=>'course1'));
71
72 $success = enrol_try_internal_enrol($course1->id, $user1->id, 5);
73
d53111c0 74 if (!$success) {
c0f00c5f
TB
75 trigger_error('User enrollment failed', E_USER_ERROR);
76 }
77
78 $context = context_system::instance();
79 role_assign(self::STID, $user2->id, $context->id);
80
81 $this->generate_replacement_list();
82 }
83
84 /**
85 * Setup function
86 * - Allow changes to CFG->debug for testing purposes.
87 */
88 protected function setUp() {
89 global $CFG;
90 parent::setUp();
91
d53111c0 92 // Settings to force statistic to run during testing.
67690cfa 93 $CFG->timezone = self::TIMEZONE;
c0f00c5f
TB
94 $CFG->statsfirstrun = 'all';
95 $CFG->statslastdaily = 0;
96 $CFG->statslastexecution = 0;
67690cfa 97
d53111c0 98 // Figure out the broken day start so I can figure out when to the start time should be.
67690cfa
TB
99 $time = time();
100 $offset = get_timezone_offset($CFG->timezone);
101 $stime = $time + $offset;
102 $stime = intval($stime / (60*60*24)) * 60*60*24;
103 $stime -= $offset;
104
105 $shour = intval(($time - $stime) / (60*60));
106
107 $CFG->statsruntimestarthour = $shour;
c0f00c5f
TB
108 $CFG->statsruntimestartminute = 0;
109
d53111c0 110 $this->set_up_db();
c0f00c5f 111
d53111c0 112 $this->resetAfterTest();
c0f00c5f
TB
113 }
114
115 /**
116 * Function to setup database.
117 *
118 * @param array $dataset An array of tables including the log table.
d53111c0 119 * @param array $tables
c0f00c5f
TB
120 */
121 protected function prepare_db($dataset, $tables) {
122 global $DB;
123
124 foreach ($tables as $tablename) {
125 $DB->delete_records($tablename);
126
127 foreach ($dataset as $name => $table) {
128
129 if ($tablename == $name) {
130
131 $rows = $table->getRowCount();
132
133 for ($i = 0; $i < $rows; $i++) {
134 $row = $table->getRow($i);
135
136 $DB->insert_record($tablename, $row, false, true);
137 }
138 }
139 }
140 }
141 }
142
143 /**
d53111c0 144 * Load dataset from XML file.
c0f00c5f
TB
145 */
146 protected function generate_replacement_list() {
147 global $CFG, $DB;
148
149 if ($this->replacements !== null) {
150 return;
151 }
152
67690cfa
TB
153 $CFG->timezone = self::TIMEZONE;
154
c0f00c5f
TB
155 $guest = $DB->get_record('user', array('id' => $CFG->siteguest));
156 $user1 = $DB->get_record('user', array('username' => 'user1'));
157 $user2 = $DB->get_record('user', array('username' => 'user2'));
158
159 if (($guest === false) || ($user1 === false) || ($user2 === false)) {
160 trigger_error('User setup incomplete', E_USER_ERROR);
161 }
162
163 $site = $DB->get_record('course', array('id' => SITEID));
164 $course1 = $DB->get_record('course', array('shortname' => 'course1'));
165
166 if (($site === false) || ($course1 === false)) {
167 trigger_error('Course setup incomplete', E_USER_ERROR);
168 }
169
67690cfa
TB
170 $offset = get_timezone_offset($CFG->timezone);
171
c0f00c5f
TB
172 $start = stats_get_base_daily(self::DAY + 3600);
173 $startnolog = stats_get_base_daily(stats_get_start_from('daily'));
174 $gr = get_guest_role();
175
176 $this->replacements = array(
d53111c0
PS
177 // Start and end times.
178 '[start_0]' => $start - 14410, // 4 hours before.
179 '[start_1]' => $start + 14410, // 4 hours after.
c0f00c5f
TB
180 '[start_2]' => $start + 14420,
181 '[start_3]' => $start + 14430,
d53111c0 182 '[start_4]' => $start + 100800, // 28 hours after.
c0f00c5f
TB
183 '[end]' => stats_get_next_day_start($start),
184 '[end_no_logs]' => stats_get_next_day_start($startnolog),
185
d53111c0 186 // User ids.
c0f00c5f
TB
187 '[guest_id]' => $guest->id,
188 '[user1_id]' => $user1->id,
189 '[user2_id]' => $user2->id,
190
d53111c0 191 // Course ids.
c0f00c5f
TB
192 '[course1_id]' => $course1->id,
193 '[site_id]' => SITEID,
194
d53111c0 195 // Role ids.
c0f00c5f
TB
196 '[frontpage_roleid]' => (int) $CFG->defaultfrontpageroleid,
197 '[guest_roleid]' => $gr->id,
198 '[student_roleid]' => self::STID,
199 );
200 }
201
202 /**
d53111c0 203 * Load dataset from XML file.
c0f00c5f
TB
204 *
205 * @param string $file The name of the file to load
d53111c0 206 * @return array
c0f00c5f
TB
207 */
208 protected function load_xml_data_file($file) {
209 static $replacements = null;
210
211 $raw = $this->createXMLDataSet($file);
212 $clean = new PHPUnit_Extensions_Database_DataSet_ReplacementDataSet($raw);
213
214 foreach ($this->replacements as $placeholder => $value) {
215 $clean->addFullReplacement($placeholder, $value);
216 }
217
218 $logs = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($clean);
219 $logs->addIncludeTables(array('log'));
220
221 $stats = new PHPUnit_Extensions_Database_DataSet_DataSetFilter($clean);
222 $stats->addIncludeTables(array('stats_daily', 'stats_user_daily'));
223
224 return array($logs, $stats);
225 }
226
227 /**
d53111c0 228 * Provides the log data for test_statslib_cron_daily.
c0f00c5f
TB
229 */
230 public function daily_log_provider() {
231 global $CFG, $DB;
232
d53111c0 233 $this->set_up_db();
c0f00c5f
TB
234
235 $tests = array('00', '01', '02', '03', '04', '05', '06', '07', '08');
236
237 $dataset = array();
238
239 foreach ($tests as $test) {
240 $dataset[] = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test{$test}.xml");
241 }
242
243 return $dataset;
244 }
245
246 /**
247 * Compare the expected stats to those in the database.
248 *
d53111c0
PS
249 * @param array $expected
250 * @param string $output
c0f00c5f 251 */
12ccfc12 252 protected function verify_stats($expected, $output = '') {
c0f00c5f
TB
253 global $DB;
254
255 // Note: We can not use $this->assertDataSetEqual($expected, $actual) because there's no
256 // $this->getConnection() in advanced_testcase.
257
258 foreach ($expected as $type => $table) {
259 $records = $DB->get_records($type);
260
261 $rows = $table->getRowCount();
262
12ccfc12
TB
263 $message = 'Incorrect number of results returned for '. $type;
264
265 if ($output != '') {
266 $message .= "\nCron output:\n$output";
267 }
268
d53111c0 269 $this->assertCount($rows, $records, $message);
c0f00c5f
TB
270
271 for ($i = 0; $i < $rows; $i++) {
272 $row = $table->getRow($i);
273 $found = 0;
274
275 foreach ($records as $key => $record) {
276 $record = (array) $record;
277 unset($record['id']);
278 $diff = array_merge(array_diff_assoc($row, $record),
279 array_diff_assoc($record, $row));
280
281 if (empty($diff)) {
282 $found = $key;
283 break;
284 }
285 }
12ccfc12 286
c0f00c5f
TB
287 $this->assertGreaterThan(0, $found, 'Expected log '. var_export($row, true)
288 ." was not found in $type ". var_export($records, true));
289 unset($records[$found]);
290 }
291 }
292 }
293
294 /**
d53111c0 295 * Test progress output when debug is on.
c0f00c5f
TB
296 */
297 public function test_statslib_progress_debug() {
96f81ea3 298 set_debugging(DEBUG_ALL);
c0f00c5f
TB
299 $this->expectOutputString('1:0 ');
300 stats_progress('init');
301 stats_progress('1');
96f81ea3 302 $this->resetDebugging();
c0f00c5f
TB
303 }
304
305 /**
d53111c0 306 * Test progress output when debug is off.
c0f00c5f
TB
307 */
308 public function test_statslib_progress_no_debug() {
96f81ea3 309 set_debugging(DEBUG_NONE);
c0f00c5f
TB
310 $this->expectOutputString('.');
311 stats_progress('init');
312 stats_progress('1');
96f81ea3 313 $this->resetDebugging();
c0f00c5f
TB
314 }
315
316 /**
d53111c0 317 * Test the function that gets the start date from the config.
c0f00c5f
TB
318 */
319 public function test_statslib_get_start_from() {
320 global $CFG, $DB;
321
322 $dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test01.xml");
c0f00c5f
TB
323 $time = time();
324 $DB->delete_records('log');
325
67690cfa
TB
326 // Don't ask. I don't think get_timezone_offset works correctly.
327 $day = self::DAY - get_timezone_offset($CFG->timezone);
328
c0f00c5f
TB
329 $CFG->statsfirstrun = 'all';
330 // Allow 1 second difference in case we cross a second boundary.
594d66c2
DW
331 // Note: within 3 days of a DST change - -3 days != 3 * 24 hours (it may be more or less).
332 $this->assertLessThanOrEqual(1, stats_get_start_from('daily') - strtotime('-3 days', $time), 'All start time');
c0f00c5f
TB
333
334 $this->prepare_db($dataset[0], array('log'));
335 $records = $DB->get_records('log');
336
67690cfa 337 $this->assertEquals($day + 14410, stats_get_start_from('daily'), 'Log entry start');
c0f00c5f
TB
338
339 $CFG->statsfirstrun = 'none';
594d66c2 340 $this->assertLessThanOrEqual(1, stats_get_start_from('daily') - strtotime('-3 days', $time), 'None start time');
c0f00c5f
TB
341
342 $CFG->statsfirstrun = 14515200;
343 $this->assertLessThanOrEqual(1, stats_get_start_from('daily') - ($time - (14515200)), 'Specified start time');
344
345 $this->prepare_db($dataset[1], array('stats_daily'));
67690cfa 346 $this->assertEquals($day + (24 * 3600), stats_get_start_from('daily'), 'Daily stats start time');
92927fba
347
348 // New log stores.
349 $this->preventResetByRollback();
350
351 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/log/store/standard/version.php");
352 set_config('enabled_stores', 'logstore_standard', 'tool_log');
353 set_config('buffersize', 0, 'logstore_standard');
354 set_config('logguests', 1, 'logstore_standard');
355 get_log_manager(true);
356
357 $this->assertEquals(0, $DB->count_records('logstore_standard_log'));
358
359 $DB->delete_records('stats_daily');
360 $CFG->statsfirstrun = 'all';
361 $firstoldtime = $DB->get_field_sql('SELECT MIN(time) FROM {log}');
362
363 $this->assertEquals($firstoldtime, stats_get_start_from('daily'));
364
365 \core_tests\event\create_executed::create(array('context' => context_system::instance()))->trigger();
366 \core_tests\event\read_executed::create(array('context' => context_system::instance()))->trigger();
367 \core_tests\event\update_executed::create(array('context' => context_system::instance()))->trigger();
368 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
369
8dcb7ae6
PS
370 // Fake the origin of events.
371 $DB->set_field('logstore_standard_log', 'origin', 'web', array());
372
92927fba
373 $logs = $DB->get_records('logstore_standard_log');
374 $this->assertCount(4, $logs);
375
376 $firstnew = reset($logs);
377 $this->assertGreaterThan($firstoldtime, $firstnew->timecreated);
378 $DB->set_field('logstore_standard_log', 'timecreated', 10, array('id' => $firstnew->id));
379 $this->assertEquals(10, stats_get_start_from('daily'));
380
381 $DB->set_field('logstore_standard_log', 'timecreated', $firstnew->timecreated, array('id' => $firstnew->id));
382 $DB->delete_records('log');
383 $this->assertEquals($firstnew->timecreated, stats_get_start_from('daily'));
384
385 set_config('enabled_stores', '', 'tool_log');
386 get_log_manager(true);
c0f00c5f
TB
387 }
388
389 /**
d53111c0 390 * Test the function that calculates the start of the day.
67690cfa
TB
391 *
392 * NOTE: I don't think this is the way this function should work.
393 * This test documents the current functionality.
c0f00c5f
TB
394 */
395 public function test_statslib_get_base_daily() {
396 global $CFG;
397
67690cfa
TB
398 for ($x = 0; $x < 24; $x += 1) {
399 $CFG->timezone = $x;
400
401 $start = 1272672000 - ($x * 3600);
402 if ($x >= 20) {
403 $start += (24 * 3600);
404 }
405
406 $this->assertEquals($start, stats_get_base_daily(1272686410), "Timezone $x check");
407 }
c0f00c5f
TB
408 }
409
410 /**
d53111c0 411 * Test the function that gets the start of the next day.
c0f00c5f
TB
412 */
413 public function test_statslib_get_next_day_start() {
414 global $CFG;
415
416 $CFG->timezone = 0;
417 $this->assertEquals(1272758400, stats_get_next_day_start(1272686410));
418 }
419
420 /**
d53111c0 421 * Test the function that gets the action names.
c0f00c5f
TB
422 *
423 * Note: The function results depend on installed modules. The hard coded lists are the
424 * defaults for a new Moodle 2.3 install.
425 */
426 public function test_statslib_get_action_names() {
427 $basepostactions = array (
428 0 => 'add',
429 1 => 'delete',
430 2 => 'edit',
431 3 => 'add mod',
432 4 => 'delete mod',
433 5 => 'edit sectionenrol',
434 6 => 'loginas',
435 7 => 'new',
436 8 => 'unenrol',
437 9 => 'update',
438 10 => 'update mod',
439 11 => 'upload',
440 12 => 'submit',
441 13 => 'submit for grading',
442 14 => 'talk',
443 15 => 'choose',
444 16 => 'choose again',
445 17 => 'record delete',
446 18 => 'add discussion',
447 19 => 'add post',
448 20 => 'delete discussion',
449 21 => 'delete post',
450 22 => 'move discussion',
451 23 => 'prune post',
452 24 => 'update post',
453 25 => 'add category',
454 26 => 'add entry',
455 27 => 'approve entry',
456 28 => 'delete category',
457 29 => 'delete entry',
458 30 => 'edit category',
459 31 => 'update entry',
460 32 => 'end',
461 33 => 'start',
462 34 => 'attempt',
463 35 => 'close attempt',
464 36 => 'preview',
465 37 => 'editquestions',
466 38 => 'delete attempt',
467 39 => 'manualgrade',
468 );
469
470 $baseviewactions = array (
471 0 => 'view',
472 1 => 'view all',
473 2 => 'history',
474 3 => 'view submission',
475 4 => 'view feedback',
476 5 => 'print',
477 6 => 'report',
478 7 => 'view discussion',
479 8 => 'search',
480 9 => 'forum',
481 10 => 'forums',
482 11 => 'subscribers',
483 12 => 'view forum',
484 13 => 'view entry',
485 14 => 'review',
486 15 => 'pre-view',
487 16 => 'download',
488 17 => 'view form',
489 18 => 'view graph',
490 19 => 'view report',
491 );
492
493 $postactions = stats_get_action_names('post');
494
495 foreach ($basepostactions as $action) {
496 $this->assertContains($action, $postactions);
497 }
498
499 $viewactions = stats_get_action_names('view');
500
501 foreach ($baseviewactions as $action) {
502 $this->assertContains($action, $viewactions);
503 }
504 }
505
506 /**
507 * Test the temporary table creation and deletion.
508 */
509 public function test_statslib_temp_table_create_and_drop() {
510 global $DB;
511
512 foreach ($this->tables as $table) {
513 $this->assertFalse($DB->get_manager()->table_exists($table));
514 }
515
516 stats_temp_table_create();
517
518 foreach ($this->tables as $table) {
519 $this->assertTrue($DB->get_manager()->table_exists($table));
520 }
521
522 stats_temp_table_drop();
523
524 foreach ($this->tables as $table) {
525 $this->assertFalse($DB->get_manager()->table_exists($table));
526 }
527 }
528
529 /**
530 * Test the temporary table creation and deletion.
531 *
532 * @depends test_statslib_temp_table_create_and_drop
533 */
534 public function test_statslib_temp_table_fill() {
92927fba 535 global $CFG, $DB, $USER;
c0f00c5f
TB
536
537 $dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test09.xml");
538
539 $this->prepare_db($dataset[0], array('log'));
540
67690cfa
TB
541 $start = self::DAY - get_timezone_offset($CFG->timezone);
542 $end = $start + (24 * 3600);
543
c0f00c5f 544 stats_temp_table_create();
67690cfa 545 stats_temp_table_fill($start, $end);
c0f00c5f
TB
546
547 $this->assertEquals(1, $DB->count_records('temp_log1'));
548 $this->assertEquals(1, $DB->count_records('temp_log2'));
549
550 stats_temp_table_drop();
92927fba
551
552 // New log stores.
553 $this->preventResetByRollback();
554 stats_temp_table_create();
555
556 $course = $this->getDataGenerator()->create_course();
557 $context = context_course::instance($course->id);
558 $fcontext = context_course::instance(SITEID);
559 $user = $this->getDataGenerator()->create_user();
560 $this->setUser($user);
561
562 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/log/store/standard/version.php");
563 set_config('enabled_stores', 'logstore_standard', 'tool_log');
564 set_config('buffersize', 0, 'logstore_standard');
565 set_config('logguests', 1, 'logstore_standard');
566 get_log_manager(true);
567
568 $DB->delete_records('logstore_standard_log');
569
570 \core_tests\event\create_executed::create(array('context' => $fcontext, 'courseid' => SITEID))->trigger();
571 \core_tests\event\read_executed::create(array('context' => $context, 'courseid' => $course->id))->trigger();
572 \core_tests\event\update_executed::create(array('context' => context_system::instance()))->trigger();
573 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
574
575 \core\event\user_loggedin::create(
576 array(
577 'userid' => $USER->id,
578 'objectid' => $USER->id,
579 'other' => array('username' => $USER->username),
580 )
581 )->trigger();
582
583 $DB->set_field('logstore_standard_log', 'timecreated', 10);
584
585 $this->assertEquals(5, $DB->count_records('logstore_standard_log'));
586
587 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
588 \core_tests\event\delete_executed::create(array('context' => context_system::instance()))->trigger();
589
8dcb7ae6
PS
590 // Fake the origin of events.
591 $DB->set_field('logstore_standard_log', 'origin', 'web', array());
592
92927fba
593 $this->assertEquals(7, $DB->count_records('logstore_standard_log'));
594
595 stats_temp_table_fill(9, 11);
596
597 $logs1 = $DB->get_records('temp_log1');
598 $logs2 = $DB->get_records('temp_log2');
599 $this->assertCount(5, $logs1);
600 $this->assertCount(5, $logs2);
601 // The order of records in the temp tables is not guaranteed...
602
603 $viewcount = 0;
604 $updatecount = 0;
605 $logincount = 0;
606 foreach ($logs1 as $log) {
607 if ($log->course == $course->id) {
608 $this->assertEquals('view', $log->action);
609 $viewcount++;
610 } else {
611 $this->assertTrue(in_array($log->action, array('update', 'login')));
612 if ($log->action === 'update') {
613 $updatecount++;
614 } else {
615 $logincount++;
616 }
617 $this->assertEquals(SITEID, $log->course);
618 }
619 $this->assertEquals($user->id, $log->userid);
620 }
621 $this->assertEquals(1, $viewcount);
622 $this->assertEquals(3, $updatecount);
623 $this->assertEquals(1, $logincount);
624
625 set_config('enabled_stores', '', 'tool_log');
626 get_log_manager(true);
627 stats_temp_table_drop();
c0f00c5f
TB
628 }
629
630 /**
631 * Test the temporary table creation and deletion.
632 *
633 * @depends test_statslib_temp_table_create_and_drop
634 */
635 public function test_statslib_temp_table_setup() {
636 global $DB;
637
638 $logs = array();
639 $this->prepare_db($logs, array('log'));
640
641 stats_temp_table_create();
642 stats_temp_table_setup();
643
644 $this->assertEquals(1, $DB->count_records('temp_enroled'));
645
646 stats_temp_table_drop();
647 }
648
649 /**
650 * Test the function that clean out the temporary tables.
651 *
652 * @depends test_statslib_temp_table_create_and_drop
653 */
654 public function test_statslib_temp_table_clean() {
655 global $DB;
656
657 $rows = array(
658 'temp_log1' => array('id' => 1, 'course' => 1),
659 'temp_log2' => array('id' => 1, 'course' => 1),
660 'temp_stats_daily' => array('id' => 1, 'courseid' => 1),
661 'temp_stats_user_daily' => array('id' => 1, 'courseid' => 1),
662 );
663
664 stats_temp_table_create();
665
666 foreach ($rows as $table => $row) {
667 $DB->insert_record_raw($table, $row);
668 $this->assertEquals(1, $DB->count_records($table));
669 }
670
671 stats_temp_table_clean();
672
673 foreach ($rows as $table => $row) {
674 $this->assertEquals(0, $DB->count_records($table));
675 }
676
677 $this->assertEquals(1, $DB->count_records('stats_daily'));
678 $this->assertEquals(1, $DB->count_records('stats_user_daily'));
679
680 stats_temp_table_drop();
681 }
682
683 /**
d53111c0 684 * Test the daily stats function.
c0f00c5f
TB
685 *
686 * @depends test_statslib_get_base_daily
687 * @depends test_statslib_get_next_day_start
67690cfa 688 * @depends test_statslib_get_start_from
c0f00c5f
TB
689 * @depends test_statslib_temp_table_create_and_drop
690 * @depends test_statslib_temp_table_setup
691 * @depends test_statslib_temp_table_fill
692 * @dataProvider daily_log_provider
693 */
694 public function test_statslib_cron_daily($logs, $stats) {
67690cfa 695 global $CFG, $DB;
c0f00c5f 696
c0f00c5f
TB
697 $this->prepare_db($logs, array('log'));
698
699 // Stats cron daily uses mtrace, turn on buffering to silence output.
700 ob_start();
701 stats_cron_daily(1);
12ccfc12 702 $output = ob_get_contents();
c0f00c5f
TB
703 ob_end_clean();
704
12ccfc12 705 $this->verify_stats($stats, $output);
c0f00c5f
TB
706 }
707
708 /**
d53111c0
PS
709 * Test the daily stats function.
710 *
c0f00c5f
TB
711 * @depends test_statslib_get_base_daily
712 * @depends test_statslib_get_next_day_start
713 */
714 public function test_statslib_cron_daily_no_default_profile_id() {
715 global $CFG, $DB;
716 $CFG->defaultfrontpageroleid = 0;
717
718 $course1 = $DB->get_record('course', array('shortname' => 'course1'));
719 $guestid = $CFG->siteguest;
720 $start = stats_get_base_daily(1272758400);
721 $end = stats_get_next_day_start($start);
722 $fpid = (int) $CFG->defaultfrontpageroleid;
723 $gr = get_guest_role();
724
725 $dataset = $this->load_xml_data_file(__DIR__."/fixtures/statslib-test10.xml");
726
c0f00c5f
TB
727 $this->prepare_db($dataset[0], array('log'));
728
729 // Stats cron daily uses mtrace, turn on buffering to silence output.
730 ob_start();
731 stats_cron_daily($maxdays=1);
12ccfc12 732 $output = ob_get_contents();
c0f00c5f
TB
733 ob_end_clean();
734
12ccfc12 735 $this->verify_stats($dataset[1], $output);
c0f00c5f
TB
736 }
737}