Merge branch 'MDL-42592_master' of https://github.com/markn86/moodle
[moodle.git] / lib / tests / update_deployer_test.php
blob6b8f977b4382d74bd369af0a7228c568b26df3c6
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 the update deployer.
20 * @package core
21 * @category phpunit
22 * @copyright 2012 David Mudrak <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Test cases for {@link \core\update\deployer} class.
32 class core_update_deployer_testcase extends advanced_testcase {
34 public function test_magic_setters() {
35 $deployer = testable_available_update_deployer::instance();
36 $value = new moodle_url('/');
37 $deployer->set_returnurl($value);
38 $this->assertSame($deployer->get_returnurl(), $value);
41 public function test_prepare_authorization() {
42 global $CFG;
44 $deployer = testable_available_update_deployer::instance();
45 list($passfile, $password) = $deployer->prepare_authorization();
46 $filename = $CFG->phpunit_dataroot.'/mdeploy/auth/'.$passfile;
47 $this->assertFileExists($filename);
48 $stored = file($filename, FILE_IGNORE_NEW_LINES);
49 $this->assertCount(2, $stored);
50 $this->assertGreaterThan(23, strlen($stored[0]));
51 $this->assertSame($stored[0], $password);
52 $this->assertLessThan(60, time() - (int)$stored[1]);
57 /**
58 * Modified version of {@link \core\update\checker} suitable for testing.
60 class testable_available_update_checker extends \core\update\checker {
62 /** @var replaces the default DB table storage for the fetched response */
63 protected $fakeresponsestorage;
64 /** @var int stores the fake recentfetch value */
65 public $fakerecentfetch = -1;
66 /** @var int stores the fake value of time() */
67 public $fakecurrenttimestamp = -1;
69 /**
70 * Factory method for this class.
72 * @return testable_available_update_checker the singleton instance
74 public static function instance() {
75 global $CFG;
77 if (is_null(self::$singletoninstance)) {
78 self::$singletoninstance = new self();
80 return self::$singletoninstance;
83 protected function validate_response($response) {
86 protected function store_response($response) {
87 $this->fakeresponsestorage = $response;
90 protected function restore_response($forcereload = false) {
91 $this->recentfetch = time();
92 $this->recentresponse = $this->decode_response($this->get_fake_response());
95 public function compare_responses(array $old, array $new) {
96 return parent::compare_responses($old, $new);
99 public function is_same_release($remote, $local=null) {
100 return parent::is_same_release($remote, $local);
103 protected function load_current_environment($forcereload=false) {
106 public function fake_current_environment($version, $release, $branch, array $plugins) {
107 $this->currentversion = $version;
108 $this->currentrelease = $release;
109 $this->currentbranch = $branch;
110 $this->currentplugins = $plugins;
113 public function get_last_timefetched() {
114 if ($this->fakerecentfetch == -1) {
115 return parent::get_last_timefetched();
116 } else {
117 return $this->fakerecentfetch;
121 private function get_fake_response() {
122 $fakeresponse = array(
123 'status' => 'OK',
124 'provider' => 'http://download.moodle.org/api/1.0/updates.php',
125 'apiver' => '1.0',
126 'timegenerated' => time(),
127 'forversion' => '2012010100.00',
128 'forbranch' => '2.3',
129 'ticket' => sha1('No, I am not going to mention the word "frog" here. Oh crap. I just did.'),
130 'updates' => array(
131 'core' => array(
132 array(
133 'version' => 2012060103.00,
134 'release' => '2.3.3 (Build: 20121201)',
135 'maturity' => 200,
136 'url' => 'http://download.moodle.org/',
137 'download' => 'http://download.moodle.org/download.php/MOODLE_23_STABLE/moodle-2.3.3-latest.zip',
139 array(
140 'version' => 2012120100.00,
141 'release' => '2.4dev (Build: 20121201)',
142 'maturity' => 50,
143 'url' => 'http://download.moodle.org/',
144 'download' => 'http://download.moodle.org/download.php/MOODLE_24_STABLE/moodle-2.4.0-latest.zip',
147 'mod_foo' => array(
148 array(
149 'version' => 2012030501,
150 'requires' => 2012010100,
151 'maturity' => 200,
152 'release' => '1.1',
153 'url' => 'http://moodle.org/plugins/blahblahblah/',
154 'download' => 'http://moodle.org/plugins/download.php/blahblahblah',
156 array(
157 'version' => 2012030502,
158 'requires' => 2012010100,
159 'maturity' => 100,
160 'release' => '1.2 beta',
161 'url' => 'http://moodle.org/plugins/',
167 return json_encode($fakeresponse);
170 protected function cron_current_timestamp() {
171 if ($this->fakecurrenttimestamp == -1) {
172 return parent::cron_current_timestamp();
173 } else {
174 return $this->fakecurrenttimestamp;
178 protected function cron_mtrace($msg, $eol = PHP_EOL) {
181 protected function cron_autocheck_enabled() {
182 return true;
185 protected function cron_execution_offset() {
186 // Autofetch should run by the first cron after 01:42 AM.
187 return 42 * MINSECS;
190 protected function cron_execute() {
191 throw new testable_available_update_checker_cron_executed('Cron executed!');
197 * Exception used to detect {@link \core\update\checker::cron_execute()} calls.
199 class testable_available_update_checker_cron_executed extends Exception {
203 * Modified {@link \core\update\deployer} suitable for testing purposes.
205 class testable_available_update_deployer extends \core\update\deployer {