mirrored from git://git.moodle.org/moodle.git
/
testpluginlib.php
259 lines (217 loc) · 8.61 KB
/
testpluginlib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for the lib/pluginlib.php library
*
* @package core
* @category test
* @copyright 2012 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
if (empty($CFG->unittestprefix)) {
die('You must define $CFG->unittestprefix to run these unit tests.');
}
require_once($CFG->libdir.'/pluginlib.php');
/**
* Modified {@link plugininfo_mod} suitable for testing purposes
*/
class testable_plugininfo_mod extends plugininfo_mod {
public function init_display_name() {
$this->displayname = ucfirst($this->name);
}
public function load_disk_version() {
$this->versiondisk = 2012030500;
}
protected function load_version_php() {
return (object)array(
'version' => 2012030500,
'requires' => 2012010100,
'component' => $this->type.'_'.$this->name);
}
public function load_db_version() {
$this->versiondb = 2012022900;
}
}
/**
* Modified {@link plugin_manager} suitable for testing purposes
*/
class testable_plugin_manager extends plugin_manager {
/**
* Factory method for this class
*
* @return plugin_manager the singleton instance
*/
public static function instance() {
global $CFG;
if (is_null(self::$singletoninstance)) {
self::$singletoninstance = new self();
}
return self::$singletoninstance;
}
/**
* A version of {@link plugin_manager::get_plugins()} that prepares some faked
* testable instances.
*
* @param bool $disablecache ignored in this class
* @return array
*/
public function get_plugins($disablecache=false) {
global $CFG;
$this->pluginsinfo = array(
'mod' => array(
'foo' => plugininfo_default_factory::make('mod', $CFG->dirroot.'/mod', 'foo',
$CFG->dirroot.'/mod/foo', 'testable_plugininfo_mod'),
'bar' => plugininfo_default_factory::make('mod', $CFG->dirroot.'/bar', 'bar',
$CFG->dirroot.'/mod/bar', 'testable_plugininfo_mod'),
)
);
$checker = testable_available_update_checker::instance();
$this->pluginsinfo['mod']['foo']->check_available_updates($checker);
$this->pluginsinfo['mod']['bar']->check_available_updates($checker);
return $this->pluginsinfo;
}
}
/**
* Modified version of {@link available_update_checker} suitable for testing
*/
class testable_available_update_checker extends available_update_checker {
/** @var replaces the default DB table storage for the fetched response */
protected $responsestorage;
/**
* Factory method for this class
*
* @return testable_available_update_checker the singleton instance
*/
public static function instance() {
global $CFG;
if (is_null(self::$singletoninstance)) {
self::$singletoninstance = new self();
}
return self::$singletoninstance;
}
protected function validate_response() {
}
protected function store_response($response) {
$this->responsestorage = $response;
}
protected function restore_response($forcereload = false) {
$this->recentfetch = time();
$this->recentresponse = $this->decode_response($this->get_fake_response());
}
protected function load_current_environment($forcereload=false) {
}
public function fake_current_environment($version, $branch, array $plugins) {
$this->currentversion = $version;
$this->currentbranch = $branch;
$this->currentplugins = $plugins;
}
private function get_fake_response() {
$fakeresponse = array(
'status' => 'OK',
'provider' => 'http://download.moodle.org/api/1.0/updates.php',
'apiver' => '1.0',
'timegenerated' => time(),
'forversion' => '2012010100.00',
'forbranch' => '2.3',
'ticket' => sha1('No, I am not going to mention the word "frog" here. Oh crap. I just did.'),
'updates' => array(
'core' => array(
array(
'version' => 2012060103.00,
'release' => '2.3.3 (Build: 20121201)',
'maturity' => 200,
'url' => 'http://download.moodle.org/',
'download' => 'http://download.moodle.org/download.php/MOODLE_23_STABLE/moodle-2.3.3-latest.zip',
),
array(
'version' => 2012120100.00,
'release' => '2.4dev (Build: 20121201)',
'maturity' => 50,
'url' => 'http://download.moodle.org/',
'download' => 'http://download.moodle.org/download.php/MOODLE_24_STABLE/moodle-2.4.0-latest.zip',
),
),
'mod_foo' => array(
array(
'version' => 2012030501,
'requires' => 2012010100,
'maturity' => 200,
'release' => '1.1',
'url' => 'http://moodle.org/plugins/blahblahblah/',
'download' => 'http://moodle.org/plugins/download.php/blahblahblah',
),
array(
'version' => 2012030502,
'requires' => 2012010100,
'maturity' => 100,
'release' => '1.2 beta',
'url' => 'http://moodle.org/plugins/',
),
),
),
);
return json_encode($fakeresponse);
}
}
/**
* Tests of the basic API of the plugin manager
*/
class plugin_manager_test extends UnitTestCase {
public function test_plugin_manager_instance() {
$pluginman = testable_plugin_manager::instance();
$this->assertTrue($pluginman instanceof testable_plugin_manager);
}
public function test_get_plugins() {
$pluginman = testable_plugin_manager::instance();
$plugins = $pluginman->get_plugins();
$this->assertTrue(isset($plugins['mod']['foo']));
$this->assertTrue($plugins['mod']['foo'] instanceof testable_plugininfo_mod);
}
public function test_get_status() {
$pluginman = testable_plugin_manager::instance();
$plugins = $pluginman->get_plugins();
$modfoo = $plugins['mod']['foo'];
$this->assertEqual($modfoo->get_status(), plugin_manager::PLUGIN_STATUS_UPGRADE);
}
public function test_available_update() {
$pluginman = testable_plugin_manager::instance();
$plugins = $pluginman->get_plugins();
$this->assertNull($plugins['mod']['bar']->available_updates());
$this->assertIsA($plugins['mod']['foo']->available_updates(), 'array');
foreach ($plugins['mod']['foo']->available_updates() as $availableupdate) {
$this->assertIsA($availableupdate, 'available_update_info');
}
}
}
/**
* Tests of the basic API of the available update checker
*/
class available_update_checker_test extends UnitTestCase {
public function test_core_available_update() {
$provider = testable_available_update_checker::instance();
$this->assertTrue($provider instanceof available_update_checker);
$provider->fake_current_environment(2012060102.00, '2.3', array());
$updates = $provider->get_core_update_info();
$this->assertEqual(count($updates), 2);
$provider->fake_current_environment(2012060103.00, '2.3', array());
$updates = $provider->get_core_update_info();
$this->assertEqual(count($updates), 1);
$provider->fake_current_environment(2012060103.00, '2.3', array());
$updates = $provider->get_core_update_info(MATURITY_STABLE);
$this->assertNull($updates);
}
}