2b4ed9bcee78b60179d3ee72ab4b1bede10e963f
[moodle.git] / admin / tool / installaddon / tests / installer_test.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * Provides the unit tests class and some helper classes
20  *
21  * @package     tool_installaddon
22  * @category    test
23  * @copyright   2013 David Mudrak <david@moodle.com>
24  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 defined('MOODLE_INTERNAL') || die();
30 /**
31  * Unit tests for the {@link tool_installaddon_installer} class
32  *
33  * @copyright 2013 David Mudrak <david@moodle.com>
34  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class tool_installaddon_installer_testcase extends advanced_testcase {
38     public function test_get_addons_repository_url() {
39         $installer = testable_tool_installaddon_installer::instance();
40         $url = $installer->get_addons_repository_url();
41         $query = parse_url($url, PHP_URL_QUERY);
42         $this->assertEquals(1, preg_match('~^site=(.+)$~', $query, $matches));
43         $site = rawurldecode($matches[1]);
44         $site = json_decode(base64_decode($site), true);
45         $this->assertInternalType('array', $site);
46         $this->assertEquals(3, count($site));
47         $this->assertSame('Nasty site', $site['fullname']);
48         $this->assertSame('file:///etc/passwd', $site['url']);
49         $this->assertSame("2.5'; DROP TABLE mdl_user; --", $site['majorversion']);
50     }
52     public function test_extract_installfromzip_file() {
53         global $CFG;
55         $jobid = md5(rand().uniqid('test_', true));
56         $sourcedir = make_temp_directory('tool_installaddon/'.$jobid.'/source');
57         $contentsdir = make_temp_directory('tool_installaddon/'.$jobid.'/contents');
58         copy($CFG->libdir.'/tests/fixtures/update_validator/zips/invalidroot.zip', $sourcedir.'/testinvalidroot.zip');
60         $installer = tool_installaddon_installer::instance();
61         $files = $installer->extract_installfromzip_file($sourcedir.'/testinvalidroot.zip', $contentsdir, 'fixed_root');
62         $this->assertInternalType('array', $files);
63         $this->assertCount(4, $files);
64         $this->assertSame(true, $files['fixed_root/']);
65         $this->assertSame(true, $files['fixed_root/lang/']);
66         $this->assertSame(true, $files['fixed_root/lang/en/']);
67         $this->assertSame(true, $files['fixed_root/lang/en/fixed_root.php']);
68         foreach ($files as $file => $status) {
69             if (substr($file, -1) === '/') {
70                 $this->assertTrue(is_dir($contentsdir.'/'.$file));
71             } else {
72                 $this->assertTrue(is_file($contentsdir.'/'.$file));
73             }
74         }
75     }
77     public function test_decode_remote_request() {
78         $installer = testable_tool_installaddon_installer::instance();
80         $request = base64_encode(json_encode(array(
81             'name' => '<h1>Stamp collection</h1>"; DELETE FROM mdl_users; --',
82             'component' => 'mod_stampcoll',
83             'version' => 2013032800,
84         )));
85         $request = $installer->testable_decode_remote_request($request);
86         $this->assertTrue(is_object($request));
87         // One, my little hobbit, never trusts the input parameters!
88         $this->assertEquals('Stamp collection&quot;; DELETE FROM mdl_users; --', $request->name);
89         $this->assertEquals('mod_stampcoll', $request->component);
90         $this->assertEquals(2013032800, $request->version);
92         $request = base64_encode(json_encode(array(
93             'name' => 'Theme with invalid version number',
94             'component' => 'theme_invalid',
95             'version' => '1.0',
96         )));
97         $this->assertSame(false, $installer->testable_decode_remote_request($request));
99         $request = base64_encode(json_encode(array(
100             'name' => 'Invalid activity name',
101             'component' => 'mod_invalid_activity',
102             'version' => 2013032800,
103         )));
104         $this->assertSame(false, $installer->testable_decode_remote_request($request));
106         $request = base64_encode(json_encode(array(
107             'name' => 'Moodle 3.0',
108             'component' => 'core',
109             'version' => 2022010100,
110         )));
111         $this->assertSame(false, $installer->testable_decode_remote_request($request));
113         $request = base64_encode(json_encode(array(
114             'name' => 'Invalid core subsystem',
115             'component' => 'core_cache',
116             'version' => 2014123400,
117         )));
118         $this->assertSame(false, $installer->testable_decode_remote_request($request));
120         $request = base64_encode(json_encode(array(
121             'name' => 'Non-existing plugintype',
122             'component' => 'david_mudrak',
123             'version' => 2012123199,
124         )));
125         $this->assertSame(false, $installer->testable_decode_remote_request($request));
127         $request = base64_encode(json_encode(array(
128             'name' => 'Bogus module name',
129             'component' => 'mod_xxx_yyy',
130             'version' => 2012123190,
131         )));
132         $this->assertSame(false, $installer->testable_decode_remote_request($request));
133     }
135     public function test_move_directory() {
136         $jobid = md5(rand().uniqid('test_', true));
137         $jobroot = make_temp_directory('tool_installaddon/'.$jobid);
138         $contentsdir = make_temp_directory('tool_installaddon/'.$jobid.'/contents/sub/folder');
139         file_put_contents($contentsdir.'/readme.txt', 'Hello world!');
141         $installer = tool_installaddon_installer::instance();
142         $installer->move_directory($jobroot.'/contents', $jobroot.'/moved', 0777, 0666);
144         $this->assertFalse(is_dir($jobroot.'/contents'));
145         $this->assertTrue(is_file($jobroot.'/moved/sub/folder/readme.txt'));
146         $this->assertSame('Hello world!', file_get_contents($jobroot.'/moved/sub/folder/readme.txt'));
147     }
149     public function test_detect_plugin_component() {
150         global $CFG;
152         $jobid = md5(rand().uniqid('test_', true));
153         $workdir = make_temp_directory('tool_installaddon/'.$jobid.'/version');
154         $zipfile = $CFG->libdir.'/tests/fixtures/update_validator/zips/bar.zip';
155         $installer = tool_installaddon_installer::instance();
156         $this->assertEquals('foo_bar', $installer->detect_plugin_component($zipfile, $workdir));
157     }
159     public function test_detect_plugin_component_from_versionphp() {
160         $installer = testable_tool_installaddon_installer::instance();
161         $this->assertEquals('bar_bar_conan', $installer->detect_plugin_component_from_versionphp('
162 $plugin->version  = 2014121300;
163   $plugin->component=   "bar_bar_conan"  ; // Go Arnie go!'));
164     }
168 /**
169  * Testable subclass of the tested class
170  *
171  * @copyright 2013 David Mudrak <david@moodle.com>
172  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
173  */
174 class testable_tool_installaddon_installer extends tool_installaddon_installer {
176     public function get_site_fullname() {
177         return strip_tags('<h1 onmouseover="alert(\'Hello Moodle.org!\');">Nasty site</h1>');
178     }
180     public function get_site_url() {
181         return 'file:///etc/passwd';
182     }
184     public function get_site_major_version() {
185         return "2.5'; DROP TABLE mdl_user; --";
186     }
188     public function testable_decode_remote_request($request) {
189         return parent::decode_remote_request($request);
190     }
192     protected function should_send_site_info() {
193         return true;
194     }
196     public function detect_plugin_component_from_versionphp($code) {
197         return parent::detect_plugin_component_from_versionphp($code);
198     }