mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
installer_test.php
195 lines (166 loc) · 7.83 KB
/
installer_test.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
<?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/>.
/**
* Provides the unit tests class and some helper classes
*
* @package tool_installaddon
* @category test
* @copyright 2013 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Unit tests for the {@link tool_installaddon_installer} class
*
* @copyright 2013 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_installaddon_installer_testcase extends advanced_testcase {
public function test_get_addons_repository_url() {
$installer = testable_tool_installaddon_installer::instance();
$url = $installer->get_addons_repository_url();
$query = parse_url($url, PHP_URL_QUERY);
$this->assertEquals(1, preg_match('~^site=(.+)$~', $query, $matches));
$site = rawurldecode($matches[1]);
$site = json_decode(base64_decode($site), true);
$this->assertInternalType('array', $site);
$this->assertEquals(3, count($site));
$this->assertSame('Nasty site', $site['fullname']);
$this->assertSame('file:///etc/passwd', $site['url']);
$this->assertSame("2.5'; DROP TABLE mdl_user; --", $site['majorversion']);
}
public function test_extract_installfromzip_file() {
$jobid = md5(rand().uniqid('test_', true));
$sourcedir = make_temp_directory('tool_installaddon/'.$jobid.'/source');
$contentsdir = make_temp_directory('tool_installaddon/'.$jobid.'/contents');
copy(dirname(__FILE__).'/fixtures/zips/invalidroot.zip', $sourcedir.'/testinvalidroot.zip');
$installer = tool_installaddon_installer::instance();
$files = $installer->extract_installfromzip_file($sourcedir.'/testinvalidroot.zip', $contentsdir, 'fixed_root');
$this->assertInternalType('array', $files);
$this->assertCount(4, $files);
$this->assertSame(true, $files['fixed_root/']);
$this->assertSame(true, $files['fixed_root/lang/']);
$this->assertSame(true, $files['fixed_root/lang/en/']);
$this->assertSame(true, $files['fixed_root/lang/en/fixed_root.php']);
foreach ($files as $file => $status) {
if (substr($file, -1) === '/') {
$this->assertTrue(is_dir($contentsdir.'/'.$file));
} else {
$this->assertTrue(is_file($contentsdir.'/'.$file));
}
}
}
public function test_decode_remote_request() {
$installer = testable_tool_installaddon_installer::instance();
$request = base64_encode(json_encode(array(
'name' => '<h1>Stamp collection</h1>"; DELETE FROM mdl_users; --',
'component' => 'mod_stampcoll',
'version' => 2013032800,
)));
$request = $installer->testable_decode_remote_request($request);
$this->assertTrue(is_object($request));
// One, my little hobbit, never trusts the input parameters!
$this->assertEquals('Stamp collection"; DELETE FROM mdl_users; --', $request->name);
$this->assertEquals('mod_stampcoll', $request->component);
$this->assertEquals(2013032800, $request->version);
$request = base64_encode(json_encode(array(
'name' => 'Theme with invalid version number',
'component' => 'theme_invalid',
'version' => '1.0',
)));
$this->assertSame(false, $installer->testable_decode_remote_request($request));
$request = base64_encode(json_encode(array(
'name' => 'Invalid activity name',
'component' => 'mod_invalid_activity',
'version' => 2013032800,
)));
$this->assertSame(false, $installer->testable_decode_remote_request($request));
$request = base64_encode(json_encode(array(
'name' => 'Moodle 3.0',
'component' => 'core',
'version' => 2022010100,
)));
$this->assertSame(false, $installer->testable_decode_remote_request($request));
$request = base64_encode(json_encode(array(
'name' => 'Invalid core subsystem',
'component' => 'core_cache',
'version' => 2014123400,
)));
$this->assertSame(false, $installer->testable_decode_remote_request($request));
$request = base64_encode(json_encode(array(
'name' => 'Non-existing plugintype',
'component' => 'david_mudrak',
'version' => 2012123199,
)));
$this->assertSame(false, $installer->testable_decode_remote_request($request));
$request = base64_encode(json_encode(array(
'name' => 'Bogus module name',
'component' => 'mod_xxx_yyy',
'version' => 2012123190,
)));
$this->assertSame(false, $installer->testable_decode_remote_request($request));
}
public function test_move_directory() {
$jobid = md5(rand().uniqid('test_', true));
$jobroot = make_temp_directory('tool_installaddon/'.$jobid);
$contentsdir = make_temp_directory('tool_installaddon/'.$jobid.'/contents/sub/folder');
file_put_contents($contentsdir.'/readme.txt', 'Hello world!');
$installer = tool_installaddon_installer::instance();
$installer->move_directory($jobroot.'/contents', $jobroot.'/moved', 0777, 0666);
$this->assertFalse(is_dir($jobroot.'/contents'));
$this->assertTrue(is_file($jobroot.'/moved/sub/folder/readme.txt'));
$this->assertSame('Hello world!', file_get_contents($jobroot.'/moved/sub/folder/readme.txt'));
}
public function test_detect_plugin_component() {
$jobid = md5(rand().uniqid('test_', true));
$workdir = make_temp_directory('tool_installaddon/'.$jobid.'/version');
$zipfile = __DIR__.'/fixtures/zips/bar.zip';
$installer = tool_installaddon_installer::instance();
$this->assertEquals('foo_bar', $installer->detect_plugin_component($zipfile, $workdir));
}
public function test_detect_plugin_component_from_versionphp() {
$installer = testable_tool_installaddon_installer::instance();
$this->assertEquals('bar_bar_conan', $installer->detect_plugin_component_from_versionphp('
$plugin->version = 2014121300;
$plugin->component= "bar_bar_conan" ; // Go Arnie go!'));
}
}
/**
* Testable subclass of the tested class
*
* @copyright 2013 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class testable_tool_installaddon_installer extends tool_installaddon_installer {
public function get_site_fullname() {
return strip_tags('<h1 onmouseover="alert(\'Hello Moodle.org!\');">Nasty site</h1>');
}
public function get_site_url() {
return 'file:///etc/passwd';
}
public function get_site_major_version() {
return "2.5'; DROP TABLE mdl_user; --";
}
public function testable_decode_remote_request($request) {
return parent::decode_remote_request($request);
}
protected function should_send_site_info() {
return true;
}
public function detect_plugin_component_from_versionphp($code) {
return parent::detect_plugin_component_from_versionphp($code);
}
}