[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 /** 18 * Unit tests for the update deployer. 19 * 20 * @package core 21 * @category phpunit 22 * @copyright 2012 David Mudrak <[email protected]> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 29 /** 30 * Test cases for {@link \core\update\deployer} class. 31 */ 32 class core_update_deployer_testcase extends advanced_testcase { 33 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); 39 } 40 41 public function test_prepare_authorization() { 42 global $CFG; 43 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]); 53 } 54 } 55 56 57 /** 58 * Modified version of {@link \core\update\checker} suitable for testing. 59 */ 60 class testable_available_update_checker extends \core\update\checker { 61 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; 68 69 /** 70 * Factory method for this class. 71 * 72 * @return testable_available_update_checker the singleton instance 73 */ 74 public static function instance() { 75 global $CFG; 76 77 if (is_null(self::$singletoninstance)) { 78 self::$singletoninstance = new self(); 79 } 80 return self::$singletoninstance; 81 } 82 83 protected function validate_response($response) { 84 } 85 86 protected function store_response($response) { 87 $this->fakeresponsestorage = $response; 88 } 89 90 protected function restore_response($forcereload = false) { 91 $this->recentfetch = time(); 92 $this->recentresponse = $this->decode_response($this->get_fake_response()); 93 } 94 95 public function compare_responses(array $old, array $new) { 96 return parent::compare_responses($old, $new); 97 } 98 99 public function is_same_release($remote, $local=null) { 100 return parent::is_same_release($remote, $local); 101 } 102 103 protected function load_current_environment($forcereload=false) { 104 } 105 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; 111 } 112 113 public function get_last_timefetched() { 114 if ($this->fakerecentfetch == -1) { 115 return parent::get_last_timefetched(); 116 } else { 117 return $this->fakerecentfetch; 118 } 119 } 120 121 private function get_fake_response() { 122 $fakeresponse = array( 123 'status' => 'OK', 124 'provider' => 'https://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' => 'https://download.moodle.org/', 137 'download' => 'https://download.moodle.org/download.php/MOODLE_23_STABLE/moodle-2.3.3-latest.zip', 138 ), 139 array( 140 'version' => 2012120100.00, 141 'release' => '2.4dev (Build: 20121201)', 142 'maturity' => 50, 143 'url' => 'https://download.moodle.org/', 144 'download' => 'https://download.moodle.org/download.php/MOODLE_24_STABLE/moodle-2.4.0-latest.zip', 145 ), 146 ), 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', 155 ), 156 array( 157 'version' => 2012030502, 158 'requires' => 2012010100, 159 'maturity' => 100, 160 'release' => '1.2 beta', 161 'url' => 'http://moodle.org/plugins/', 162 ), 163 ), 164 ), 165 ); 166 167 return json_encode($fakeresponse); 168 } 169 170 protected function cron_current_timestamp() { 171 if ($this->fakecurrenttimestamp == -1) { 172 return parent::cron_current_timestamp(); 173 } else { 174 return $this->fakecurrenttimestamp; 175 } 176 } 177 178 protected function cron_mtrace($msg, $eol = PHP_EOL) { 179 } 180 181 protected function cron_autocheck_enabled() { 182 return true; 183 } 184 185 protected function cron_execution_offset() { 186 // Autofetch should run by the first cron after 01:42 AM. 187 return 42 * MINSECS; 188 } 189 190 protected function cron_execute() { 191 throw new testable_available_update_checker_cron_executed('Cron executed!'); 192 } 193 } 194 195 196 /** 197 * Exception used to detect {@link \core\update\checker::cron_execute()} calls. 198 */ 199 class testable_available_update_checker_cron_executed extends Exception { 200 } 201 202 /** 203 * Modified {@link \core\update\deployer} suitable for testing purposes. 204 */ 205 class testable_available_update_deployer extends \core\update\deployer { 206 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |