Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core_analytics;
18
 
19
/**
20
 * Unit tests for the calculation info cache.
21
 *
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
25
 */
1441 ariadna 26
final class calculation_info_test extends \advanced_testcase {
1 efrain 27
    /**
28
     * test_calculation_info description
29
     *
30
     * @dataProvider provider_test_calculation_info_add_pull
31
     * @param mixed $info1
32
     * @param mixed $info2
33
     * @param mixed $info3
34
     * @param mixed $info4
35
     * @return null
36
     */
11 efrain 37
    public function test_calculation_info_add_pull($info1, $info2, $info3, $info4): void {
1 efrain 38
        require_once(__DIR__ . '/fixtures/test_indicator_max.php');
39
        require_once(__DIR__ . '/fixtures/test_indicator_min.php');
40
        $this->resetAfterTest();
41
 
42
        $atimesplitting = new \core\analytics\time_splitting\quarters();
43
 
44
        $indicator1 = new \test_indicator_min();
45
        $indicator2 = new \test_indicator_max();
46
 
47
        $calculationinfo = new \core_analytics\calculation_info();
48
        $calculationinfo->add_shared(111, [111 => $info1]);
49
        $calculationinfo->add_shared(222, [222 => 'should-get-overwritten-in-next-line']);
50
        $calculationinfo->add_shared(222, [222 => $info2]);
51
        $calculationinfo->save($indicator1, $atimesplitting, 0);
52
 
53
        // We also check that the eheheh does not overwrite the value previously stored in the cache
54
        // during the previous save call.
55
        $calculationinfo->add_shared(222, [222 => 'eheheh']);
56
        $calculationinfo->save($indicator1, $atimesplitting, 0);
57
 
58
        // The method save() should clear the internal attrs in \core_analytics\calculation_info
59
        // so it is fine to reuse the same calculation_info instance.
60
        $calculationinfo->add_shared(111, [111 => $info3]);
61
        $calculationinfo->add_shared(333, [333 => $info4]);
62
        $calculationinfo->save($indicator2, $atimesplitting, 0);
63
 
64
        // We pull data in rangeindex '0' for samples 111, 222 and 333.
65
        $predictionrecords = [
66
            '111-0' => (object)['sampleid' => '111'],
67
            '222-0' => (object)['sampleid' => '222'],
68
            '333-0' => (object)['sampleid' => '333'],
69
        ];
70
        $info = \core_analytics\calculation_info::pull_info($predictionrecords);
71
 
72
        $this->assertCount(3, $info);
73
        $this->assertCount(2, $info[111]);
74
        $this->assertCount(1, $info[222]);
75
        $this->assertCount(1, $info[333]);
76
        $this->assertEquals($info1, $info[111]['test_indicator_min:extradata'][111]);
77
        $this->assertEquals($info2, $info[222]['test_indicator_min:extradata'][222]);
78
        $this->assertEquals($info3, $info[111]['test_indicator_max:extradata'][111]);
79
        $this->assertEquals($info4, $info[333]['test_indicator_max:extradata'][333]);
80
 
81
        // The calculationinfo cache gets emptied.
82
        $this->assertFalse(\core_analytics\calculation_info::pull_info($predictionrecords));
83
    }
84
 
85
    /**
86
     * provider_test_calculation_info_add_pull
87
     *
88
     * @return mixed[]
89
     */
1441 ariadna 90
    public static function provider_test_calculation_info_add_pull(): array {
1 efrain 91
        return [
92
            'mixed-types' => ['asd', true, [123, 123, 123], (object)['asd' => 'fgfg']],
93
        ];
94
    }
95
}