3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * Provides the unit tests class and some helper classes
21 * @package tool_installaddon
23 * @copyright 2013 David Mudrak <david@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/installaddon/classes/installer.php');
34 * Unit tests for the {@link tool_installaddon_installer} class
36 * @copyright 2013 David Mudrak <david@moodle.com>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class tool_installaddon_installer_test extends advanced_testcase {
41 public function test_get_addons_repository_url() {
42 $installer = testable_tool_installaddon_installer::instance();
43 $url = $installer->get_addons_repository_url();
44 $query = parse_url($url, PHP_URL_QUERY);
45 $this->assertEquals(1, preg_match('~^site=(.+)$~', $query, $matches));
46 $site = rawurldecode($matches[1]);
47 $site = json_decode(base64_decode($site), true);
48 $this->assertEquals('array', gettype($site));
49 $this->assertEquals(3, count($site));
50 $this->assertSame('Nasty site', $site['fullname']);
51 $this->assertSame('file:///etc/passwd', $site['url']);
52 $this->assertSame("2.5'; DROP TABLE mdl_user; --", $site['majorversion']);
55 public function test_extract_installfromzip_file() {
56 $jobid = md5(rand().uniqid('test_', true));
57 $sourcedir = make_temp_directory('tool_installaddon/'.$jobid.'/source');
58 $contentsdir = make_temp_directory('tool_installaddon/'.$jobid.'/contents');
59 copy(dirname(__FILE__).'/fixtures/zips/invalidroot.zip', $sourcedir.'/testinvalidroot.zip');
61 $installer = tool_installaddon_installer::instance();
62 $files = $installer->extract_installfromzip_file($sourcedir.'/testinvalidroot.zip', $contentsdir, 'fixed_root');
63 $this->assertEquals('array', gettype($files));
64 $this->assertEquals(4, count($files));
65 $this->assertSame(true, $files['fixed_root/']);
66 $this->assertSame(true, $files['fixed_root/lang/']);
67 $this->assertSame(true, $files['fixed_root/lang/en/']);
68 $this->assertSame(true, $files['fixed_root/lang/en/fixed_root.php']);
69 foreach ($files as $file => $status) {
70 if (substr($file, -1) === '/') {
71 $this->assertTrue(is_dir($contentsdir.'/'.$file));
73 $this->assertTrue(is_file($contentsdir.'/'.$file));
81 * Testable subclass of the tested class
83 * @copyright 2013 David Mudrak <david@moodle.com>
84 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
86 class testable_tool_installaddon_installer extends tool_installaddon_installer {
88 public function get_site_fullname() {
89 return strip_tags('<h1 onmouseover="alert(\'Hello Moodle.org!\');">Nasty site</h1>');
92 public function get_site_url() {
93 return 'file:///etc/passwd';
96 public function get_site_major_version() {
97 return "2.5'; DROP TABLE mdl_user; --";
100 protected function should_send_site_info() {