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 * Unit tests for the analysis class.
20 * @package core_analytics
21 * @copyright 2019 David MonllaĆ³ {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once(__DIR__
. '/fixtures/test_timesplitting_upcoming_seconds.php');
30 * Unit tests for the analysis class.
32 * @package core_analytics
33 * @copyright 2019 David MonllaĆ³ {@link http://www.davidmonllao.com}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class analytics_analysis_testcase
extends advanced_testcase
{
39 * Test fill_firstanalyses_cache.
42 public function test_fill_firstanalyses_cache() {
43 $this->resetAfterTest();
47 $params = ['startdate' => (new DateTimeImmutable('-5 seconds'))->getTimestamp()];
48 $course1 = $this->getDataGenerator()->create_course($params);
49 $course2 = $this->getDataGenerator()->create_course($params);
50 $analysable1 = new \core_analytics\
course($course1);
52 $afewsecsago = time() - 5;
53 $earliest = $afewsecsago - 1;
55 $this->insert_used($modelid, $course1->id
, 'training', $afewsecsago);
57 // Course2 processed after course1.
58 $this->insert_used($modelid, $course2->id
, 'training', $afewsecsago +
1);
60 // After the first process involving course1.
61 $this->insert_used($modelid, $course1->id
, 'prediction', $afewsecsago +
5);
63 $firstanalyses = \core_analytics\analysis
::fill_firstanalyses_cache($modelid);
64 $this->assertCount(2, $firstanalyses);
65 $this->assertEquals($afewsecsago, $firstanalyses[$modelid . '_' . $course1->id
]);
66 $this->assertEquals($afewsecsago +
1, $firstanalyses[$modelid . '_' . $course2->id
]);
68 // The cached elements get refreshed.
69 $this->insert_used($modelid, $course1->id
, 'prediction', $earliest);
70 $firstanalyses = \core_analytics\analysis
::fill_firstanalyses_cache($modelid, $course1->id
);
71 $this->assertCount(1, $firstanalyses);
72 $this->assertEquals($earliest, $firstanalyses[$modelid . '_' . $course1->id
]);
74 // Upcoming periodic time-splitting methods can read and process the cached data.
75 $seconds = new test_timesplitting_upcoming_seconds();
76 $seconds->set_modelid($modelid);
77 $seconds->set_analysable($analysable1);
79 // The generated ranges should start from the cached firstanalysis value, which is $earliest.
80 $ranges = $seconds->get_all_ranges();
81 $this->assertGreaterThanOrEqual(7, count($ranges));
82 $firstrange = reset($ranges);
83 $this->assertEquals($earliest, $firstrange['time']);
86 private function insert_used($modelid, $analysableid, $action, $timestamp) {
89 $obj = new \
stdClass();
90 $obj->modelid
= $modelid;
91 $obj->action
= $action;
92 $obj->analysableid
= $analysableid;
93 $obj->firstanalysis
= $timestamp;
94 $obj->timeanalysed
= $timestamp;
95 $obj->id
= $DB->insert_record('analytics_used_analysables', $obj);