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 update checker.
20 * @package core_plugin
22 * @copyright 2012, 2015 David Mudrak <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
28 use core\update\testable_checker
;
29 use core\update\testable_checker_cron_executed
;
32 require_once(__DIR__
.'/fixtures/testable_update_checker.php');
35 * Tests of the basic API of the available update checker.
37 class core_update_checker_testcase
extends advanced_testcase
{
39 public function test_core_available_update() {
40 $provider = testable_checker
::instance();
41 $this->assertInstanceOf('\core\update\checker', $provider);
43 $provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array());
44 $updates = $provider->get_update_info('core');
45 $this->assertCount(2, $updates);
47 $provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array());
48 $updates = $provider->get_update_info('core');
49 $this->assertCount(1, $updates);
51 $provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array());
52 $updates = $provider->get_update_info('core', array('minmaturity' => MATURITY_STABLE
));
53 $this->assertNull($updates);
57 * If there are no fetched data yet, the first cron should fetch them.
59 public function test_cron_initial_fetch() {
60 $provider = testable_checker
::instance();
61 $provider->fakerecentfetch
= null;
62 $provider->fakecurrenttimestamp
= -1;
63 $this->setExpectedException('\core\update\testable_checker_cron_executed');
68 * If there is a fresh fetch available, no cron execution is expected.
70 public function test_cron_has_fresh_fetch() {
71 $provider = testable_checker
::instance();
72 $provider->fakerecentfetch
= time() - 23 * HOURSECS
; // Fetched 23 hours ago.
73 $provider->fakecurrenttimestamp
= -1;
75 $this->assertTrue(true); // We should get here with no exception thrown.
79 * If there is an outdated fetch, the cron execution is expected.
81 public function test_cron_has_outdated_fetch() {
82 $provider = testable_checker
::instance();
83 $provider->fakerecentfetch
= time() - 49 * HOURSECS
; // Fetched 49 hours ago.
84 $provider->fakecurrenttimestamp
= -1;
85 $this->setExpectedException('\core\update\testable_checker_cron_executed');
90 * The first cron after 01:42 AM today should fetch the data.
92 * @see testable_checker::cron_execution_offset()
94 public function test_cron_offset_execution_not_yet() {
95 $provider = testable_checker
::instance();
96 $provider->fakecurrenttimestamp
= mktime(1, 40, 02); // 01:40:02 AM today
97 $provider->fakerecentfetch
= $provider->fakecurrenttimestamp
- 24 * HOURSECS
;
99 $this->assertTrue(true); // We should get here with no exception thrown.
103 * The first cron after 01:42 AM today should fetch the data and then
104 * it is supposed to wait next 24 hours.
106 * @see testable_checker::cron_execution_offset()
108 public function test_cron_offset_execution() {
109 $provider = testable_checker
::instance();
111 // The cron at 01:45 should fetch the data.
112 $provider->fakecurrenttimestamp
= mktime(1, 45, 02); // 01:45:02 AM today
113 $provider->fakerecentfetch
= $provider->fakecurrenttimestamp
- 24 * HOURSECS
- 1;
117 } catch (testable_checker_cron_executed
$e) {
120 $this->assertTrue($executed, 'Cron should be executed at 01:45:02 but it was not.');
122 // Another cron at 06:45 should still consider data as fresh enough.
123 $provider->fakerecentfetch
= $provider->fakecurrenttimestamp
;
124 $provider->fakecurrenttimestamp
= mktime(6, 45, 03); // 06:45:03 AM
128 } catch (testable_checker_cron_executed
$e) {
131 $this->assertFalse($executed, 'Cron should not be executed at 06:45:03 but it was.');
133 // The next scheduled execution should happen the next day.
134 $provider->fakecurrenttimestamp
= $provider->fakerecentfetch +
24 * HOURSECS +
1;
138 } catch (testable_checker_cron_executed
$e) {
141 $this->assertTrue($executed, 'Cron should be executed the next night but it was not.');
144 public function test_compare_responses_both_empty() {
145 $provider = testable_checker
::instance();
148 $cmp = $provider->compare_responses($old, $new);
149 $this->assertInternalType('array', $cmp);
150 $this->assertEmpty($cmp);
153 public function test_compare_responses_old_empty() {
154 $provider = testable_checker
::instance();
160 'version' => 2012060103
165 $cmp = $provider->compare_responses($old, $new);
166 $this->assertInternalType('array', $cmp);
167 $this->assertNotEmpty($cmp);
168 $this->assertTrue(isset($cmp['core'][0]['version']));
169 $this->assertEquals(2012060103, $cmp['core'][0]['version']);
172 public function test_compare_responses_no_change() {
173 $provider = testable_checker
::instance();
178 'version' => 2012060104
181 'version' => 2012120100
186 'version' => 2011010101
191 $cmp = $provider->compare_responses($old, $new);
192 $this->assertInternalType('array', $cmp);
193 $this->assertEmpty($cmp);
196 public function test_compare_responses_new_and_missing_update() {
197 $provider = testable_checker
::instance();
202 'version' => 2012060104
207 'version' => 2011010101
216 'version' => 2012060104
219 'version' => 2012120100
224 $cmp = $provider->compare_responses($old, $new);
225 $this->assertInternalType('array', $cmp);
226 $this->assertNotEmpty($cmp);
227 $this->assertCount(1, $cmp);
228 $this->assertCount(1, $cmp['core']);
229 $this->assertEquals(2012120100, $cmp['core'][0]['version']);
232 public function test_compare_responses_modified_update() {
233 $provider = testable_checker
::instance();
238 'version' => 2011010101
247 'version' => 2011010102
252 $cmp = $provider->compare_responses($old, $new);
253 $this->assertInternalType('array', $cmp);
254 $this->assertNotEmpty($cmp);
255 $this->assertCount(1, $cmp);
256 $this->assertCount(1, $cmp['mod_foo']);
257 $this->assertEquals(2011010102, $cmp['mod_foo'][0]['version']);
260 public function test_compare_responses_invalid_format() {
261 $provider = testable_checker
::instance();
263 'status' => 'ERROR' // No 'updates' key here.
265 $this->setExpectedException('\core\update\checker_exception');
266 $cmp = $provider->compare_responses($broken, $broken);
269 public function test_is_same_release_explicit() {
270 $provider = testable_checker
::instance();
271 $this->assertTrue($provider->is_same_release('2.3dev (Build: 20120323)', '2.3dev (Build: 20120323)'));
272 $this->assertTrue($provider->is_same_release('2.3dev (Build: 20120323)', '2.3dev (Build: 20120330)'));
273 $this->assertFalse($provider->is_same_release('2.3dev (Build: 20120529)', '2.3 (Build: 20120601)'));
274 $this->assertFalse($provider->is_same_release('2.3dev', '2.3 dev'));
275 $this->assertFalse($provider->is_same_release('2.3.1', '2.3'));
276 $this->assertFalse($provider->is_same_release('2.3.1', '2.3.2'));
277 $this->assertTrue($provider->is_same_release('2.3.2+', '2.3.2')); // Yes, really!
278 $this->assertTrue($provider->is_same_release('2.3.2 (Build: 123456)', '2.3.2+ (Build: 123457)'));
279 $this->assertFalse($provider->is_same_release('3.0 Community Edition', '3.0 Enterprise Edition'));
280 $this->assertTrue($provider->is_same_release('3.0 Community Edition', '3.0 Community Edition (Build: 20290101)'));
283 public function test_is_same_release_implicit() {
284 $provider = testable_checker
::instance();
285 $provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array());
286 $this->assertTrue($provider->is_same_release('2.3.2'));
287 $this->assertTrue($provider->is_same_release('2.3.2+'));
288 $this->assertTrue($provider->is_same_release('2.3.2+ (Build: 20121013)'));
289 $this->assertFalse($provider->is_same_release('2.4dev (Build: 20121012)'));