Merge branch 'MDL-63702-master' of git://github.com/mihailges/moodle
[moodle.git] / lib / tests / time_splittings_test.php
blobfe99b830b0ebe37acaf1341d143419233b0dae7f
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.
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.
20 * @package core
21 * @category analytics
22 * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 require_once(__DIR__ . '/../../analytics/tests/fixtures/test_target_shortname.php');
29 require_once(__DIR__ . '/../../lib/enrollib.php');
31 /**
32 * Unit tests for core time splitting methods.
34 * @package core
35 * @category analytics
36 * @copyright 2017 David MonllaĆ³ {@link http://www.davidmonllao.com}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class core_analytics_time_splittings_testcase extends advanced_testcase {
41 /**
42 * setUp
44 * @return void
46 public function setUp() {
48 $this->resetAfterTest(true);
50 // Generate training data.
51 $params = array(
52 'startdate' => mktime(8, 15, 32, 10, 24, 2015),
53 'enddate' => mktime(12, 12, 31, 10, 24, 2016),
55 $course = $this->getDataGenerator()->create_course($params);
56 $this->analysable = new \core_analytics\course($course);
59 /**
60 * test_ranges
62 * @return void
64 public function test_valid_ranges() {
66 // All core time splitting methods.
67 $timesplittings = array(
68 '\core\analytics\time_splitting\deciles',
69 '\core\analytics\time_splitting\deciles_accum',
70 '\core\analytics\time_splitting\no_splitting',
71 '\core\analytics\time_splitting\quarters',
72 '\core\analytics\time_splitting\quarters_accum',
73 '\core\analytics\time_splitting\single_range'
76 // Check that defined ranges are valid (tested through validate_ranges).
77 foreach ($timesplittings as $timesplitting) {
78 $instance = new $timesplitting();
79 $instance->set_analysable($this->analysable);
83 /**
84 * test_range_dates
86 * @return void
88 public function test_range_dates() {
90 $nov2015 = mktime(0, 0, 0, 11, 24, 2015);
91 $aug2016 = mktime(0, 0, 0, 8, 29, 2016);
93 // Equal parts.
94 $quarters = new \core\analytics\time_splitting\quarters();
95 $quarters->set_analysable($this->analysable);
96 $ranges = $quarters->get_all_ranges();
97 $this->assertCount(4, $ranges);
99 $this->assertGreaterThan($ranges[0]['start'], $ranges[1]['start']);
100 $this->assertGreaterThan($ranges[0]['end'], $ranges[1]['start']);
101 $this->assertGreaterThan($ranges[0]['end'], $ranges[1]['end']);
103 $this->assertGreaterThan($ranges[1]['start'], $ranges[2]['start']);
104 $this->assertGreaterThan($ranges[1]['end'], $ranges[2]['start']);
105 $this->assertGreaterThan($ranges[1]['end'], $ranges[2]['end']);
107 $this->assertGreaterThan($ranges[2]['start'], $ranges[3]['start']);
108 $this->assertGreaterThan($ranges[2]['end'], $ranges[3]['end']);
109 $this->assertGreaterThan($ranges[2]['end'], $ranges[3]['start']);
111 // First range.
112 $this->assertLessThan($nov2015, $ranges[0]['start']);
113 $this->assertGreaterThan($nov2015, $ranges[0]['end']);
115 // Last range.
116 $this->assertLessThan($aug2016, $ranges[3]['start']);
117 $this->assertGreaterThan($aug2016, $ranges[3]['end']);
119 // Accumulative.
120 $accum = new \core\analytics\time_splitting\quarters_accum();
121 $accum->set_analysable($this->analysable);
122 $ranges = $accum->get_all_ranges();
123 $this->assertCount(4, $ranges);
125 $this->assertEquals($ranges[0]['start'], $ranges[1]['start']);
126 $this->assertEquals($ranges[1]['start'], $ranges[2]['start']);
127 $this->assertEquals($ranges[2]['start'], $ranges[3]['start']);
129 $this->assertGreaterThan($ranges[0]['end'], $ranges[1]['end']);
130 $this->assertGreaterThan($ranges[1]['end'], $ranges[2]['end']);
131 $this->assertGreaterThan($ranges[2]['end'], $ranges[3]['end']);
133 // Present in all ranges.
134 $this->assertLessThan($nov2015, $ranges[0]['start']);
135 $this->assertGreaterThan($nov2015, $ranges[0]['end']);
136 $this->assertGreaterThan($nov2015, $ranges[1]['end']);
137 $this->assertGreaterThan($nov2015, $ranges[2]['end']);
138 $this->assertGreaterThan($nov2015, $ranges[3]['end']);
140 // Only in the last range.
141 $this->assertLessThan($aug2016, $ranges[0]['end']);
142 $this->assertLessThan($aug2016, $ranges[1]['end']);
143 $this->assertLessThan($aug2016, $ranges[2]['end']);
144 $this->assertLessThan($aug2016, $ranges[3]['start']);
145 $this->assertGreaterThan($aug2016, $ranges[3]['end']);
149 * test_ready_predict
151 * @return void
153 public function test_ready_predict() {
155 $quarters = new \core\analytics\time_splitting\quarters();
156 $nosplitting = new \core\analytics\time_splitting\no_splitting();
157 $singlerange = new \core\analytics\time_splitting\single_range();
159 $range = array(
160 'start' => time() - 100,
161 'end' => time() - 20,
163 $range['time'] = $range['end'];
164 $this->assertTrue($quarters->ready_to_predict($range));
165 $this->assertTrue($nosplitting->ready_to_predict($range));
167 // Single range time is 0.
168 $range['time'] = 0;
169 $this->assertTrue($singlerange->ready_to_predict($range));
171 $range = array(
172 'start' => time() + 20,
173 'end' => time() + 100,
175 $range['time'] = $range['end'];
176 $this->assertFalse($quarters->ready_to_predict($range));
177 $this->assertTrue($nosplitting->ready_to_predict($range));
179 // Single range time is 0.
180 $range['time'] = 0;
181 $this->assertTrue($singlerange->ready_to_predict($range));