MDL-59212 analytics: Expand unit test coverage
[moodle.git] / analytics / tests / time_splittings_test.php
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/>.
17 /**
18  * Unit tests for core time splitting methods.
19  *
20  * @package   core_analytics
21  * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 require_once(__DIR__ . '/fixtures/test_target_shortname.php');
28 require_once(__DIR__ . '/../../lib/enrollib.php');
30 /**
31  * Unit tests for core time splitting methods.
32  *
33  * @package   core_analytics
34  * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
35  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class analytics_time_splittings_testcase extends advanced_testcase {
39     /**
40      * setUp
41      *
42      * @return void
43      */
44     public function setUp() {
46         $this->resetAfterTest(true);
48         // Generate training data.
49         $params = array(
50             'startdate' => mktime(8, 15, 32, 10, 24, 2015),
51             'enddate' => mktime(12, 12, 31, 10, 24, 2016),
52         );
53         $course = $this->getDataGenerator()->create_course($params);
54         $this->analysable = new \core_analytics\course($course);
55     }
57     /**
58      * test_ranges
59      *
60      * @return void
61      */
62     public function test_valid_ranges() {
64         // All core_analytics time splitting methods.
65         $timesplittings = array(
66             '\\core_analytics\\local\\time_splitting\\deciles',
67             '\\core_analytics\\local\\time_splitting\\deciles_accum',
68             '\\core_analytics\\local\\time_splitting\\no_splitting',
69             '\\core_analytics\\local\\time_splitting\\quarters',
70             '\\core_analytics\\local\\time_splitting\\quarters_accum',
71             '\\core_analytics\\local\\time_splitting\\single_range'
72         );
74         // Check that defined ranges are valid (tested through validate_ranges).
75         foreach ($timesplittings as $timesplitting) {
76             $instance = new $timesplitting();
77             $instance->set_analysable($this->analysable);
78         }
79     }
81     /**
82      * test_range_dates
83      *
84      * @return void
85      */
86     public function test_range_dates() {
88         $nov2015 = mktime(0, 0, 0, 11, 24, 2015);
89         $aug2016 = mktime(0, 0, 0, 8, 29, 2016);
91         // Equal parts.
92         $quarters = new \core_analytics\local\time_splitting\quarters();
93         $quarters->set_analysable($this->analysable);
94         $ranges = $quarters->get_all_ranges();
95         $this->assertCount(4, $ranges);
97         $this->assertGreaterThan($ranges[0]['start'], $ranges[1]['start']);
98         $this->assertGreaterThan($ranges[0]['end'], $ranges[1]['start']);
99         $this->assertGreaterThan($ranges[0]['end'], $ranges[1]['end']);
101         $this->assertGreaterThan($ranges[1]['start'], $ranges[2]['start']);
102         $this->assertGreaterThan($ranges[1]['end'], $ranges[2]['start']);
103         $this->assertGreaterThan($ranges[1]['end'], $ranges[2]['end']);
105         $this->assertGreaterThan($ranges[2]['start'], $ranges[3]['start']);
106         $this->assertGreaterThan($ranges[2]['end'], $ranges[3]['end']);
107         $this->assertGreaterThan($ranges[2]['end'], $ranges[3]['start']);
109         // First range.
110         $this->assertLessThan($nov2015, $ranges[0]['start']);
111         $this->assertGreaterThan($nov2015, $ranges[0]['end']);
113         // Last range.
114         $this->assertLessThan($aug2016, $ranges[3]['start']);
115         $this->assertGreaterThan($aug2016, $ranges[3]['end']);
117         // Accumulative.
118         $accum = new \core_analytics\local\time_splitting\quarters_accum();
119         $accum->set_analysable($this->analysable);
120         $ranges = $accum->get_all_ranges();
121         $this->assertCount(4, $ranges);
123         $this->assertEquals($ranges[0]['start'], $ranges[1]['start']);
124         $this->assertEquals($ranges[1]['start'], $ranges[2]['start']);
125         $this->assertEquals($ranges[2]['start'], $ranges[3]['start']);
127         $this->assertGreaterThan($ranges[0]['end'], $ranges[1]['end']);
128         $this->assertGreaterThan($ranges[1]['end'], $ranges[2]['end']);
129         $this->assertGreaterThan($ranges[2]['end'], $ranges[3]['end']);
131         // Present in all ranges.
132         $this->assertLessThan($nov2015, $ranges[0]['start']);
133         $this->assertGreaterThan($nov2015, $ranges[0]['end']);
134         $this->assertGreaterThan($nov2015, $ranges[1]['end']);
135         $this->assertGreaterThan($nov2015, $ranges[2]['end']);
136         $this->assertGreaterThan($nov2015, $ranges[3]['end']);
138         // Only in the last range.
139         $this->assertLessThan($aug2016, $ranges[0]['end']);
140         $this->assertLessThan($aug2016, $ranges[1]['end']);
141         $this->assertLessThan($aug2016, $ranges[2]['end']);
142         $this->assertLessThan($aug2016, $ranges[3]['start']);
143         $this->assertGreaterThan($aug2016, $ranges[3]['end']);
144     }
146     /**
147      * test_ready_predict
148      *
149      * @return void
150      */
151     public function test_ready_predict() {
153         $quarters = new \core_analytics\local\time_splitting\quarters();
154         $nosplitting = new \core_analytics\local\time_splitting\no_splitting();
155         $singlerange = new \core_analytics\local\time_splitting\single_range();
157         $range = array(
158             'start' => time() - 100,
159             'end' => time() - 20,
160         );
161         $range['time'] = $range['end'];
162         $this->assertTrue($quarters->ready_to_predict($range));
163         $this->assertTrue($nosplitting->ready_to_predict($range));
165         // Single range time is 0.
166         $range['time'] = 0;
167         $this->assertTrue($singlerange->ready_to_predict($range));
169         $range = array(
170             'start' => time() + 20,
171             'end' => time() + 100,
172         );
173         $range['time'] = $range['end'];
174         $this->assertFalse($quarters->ready_to_predict($range));
175         $this->assertTrue($nosplitting->ready_to_predict($range));
177         // Single range time is 0.
178         $range['time'] = 0;
179         $this->assertTrue($singlerange->ready_to_predict($range));
180     }