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/>.
17 namespace core_analytics
;
20 * Unit tests for the calculation info cache.
22 * @package core_analytics
23 * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 class calculation_info_test
extends \advanced_testcase
{
29 * test_calculation_info description
31 * @dataProvider provider_test_calculation_info_add_pull
38 public function test_calculation_info_add_pull($info1, $info2, $info3, $info4) {
39 require_once(__DIR__
. '/fixtures/test_indicator_max.php');
40 require_once(__DIR__
. '/fixtures/test_indicator_min.php');
41 $this->resetAfterTest();
43 $atimesplitting = new \core\analytics\time_splitting\
quarters();
45 $indicator1 = new \test_indicator_min
();
46 $indicator2 = new \test_indicator_max
();
48 $calculationinfo = new \core_analytics\
calculation_info();
49 $calculationinfo->add_shared(111, [111 => $info1]);
50 $calculationinfo->add_shared(222, [222 => 'should-get-overwritten-in-next-line']);
51 $calculationinfo->add_shared(222, [222 => $info2]);
52 $calculationinfo->save($indicator1, $atimesplitting, 0);
54 // We also check that the eheheh does not overwrite the value previously stored in the cache
55 // during the previous save call.
56 $calculationinfo->add_shared(222, [222 => 'eheheh']);
57 $calculationinfo->save($indicator1, $atimesplitting, 0);
59 // The method save() should clear the internal attrs in \core_analytics\calculation_info
60 // so it is fine to reuse the same calculation_info instance.
61 $calculationinfo->add_shared(111, [111 => $info3]);
62 $calculationinfo->add_shared(333, [333 => $info4]);
63 $calculationinfo->save($indicator2, $atimesplitting, 0);
65 // We pull data in rangeindex '0' for samples 111, 222 and 333.
66 $predictionrecords = [
67 '111-0' => (object)['sampleid' => '111'],
68 '222-0' => (object)['sampleid' => '222'],
69 '333-0' => (object)['sampleid' => '333'],
71 $info = \core_analytics\calculation_info
::pull_info($predictionrecords);
73 $this->assertCount(3, $info);
74 $this->assertCount(2, $info[111]);
75 $this->assertCount(1, $info[222]);
76 $this->assertCount(1, $info[333]);
77 $this->assertEquals($info1, $info[111]['test_indicator_min:extradata'][111]);
78 $this->assertEquals($info2, $info[222]['test_indicator_min:extradata'][222]);
79 $this->assertEquals($info3, $info[111]['test_indicator_max:extradata'][111]);
80 $this->assertEquals($info4, $info[333]['test_indicator_max:extradata'][333]);
82 // The calculationinfo cache gets emptied.
83 $this->assertFalse(\core_analytics\calculation_info
::pull_info($predictionrecords));
87 * provider_test_calculation_info_add_pull
91 public function provider_test_calculation_info_add_pull() {
93 'mixed-types' => ['asd', true, [123, 123, 123], (object)['asd' => 'fgfg']],