MDL-49329 admin: Introduce new \core\update\remote_info class
[moodle.git] / lib / tests / update_code_manager_test.php
CommitLineData
0e442ee7
DM
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 * Provides core_update_code_manager_testcase class.
19 *
d2283521 20 * @package core_plugin
0e442ee7
DM
21 * @category test
22 * @copyright 2015 David Mudrak <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once(__DIR__.'/fixtures/testable_update_code_manager.php');
30
31/**
32 * Tests for \core\update\code_manager features.
33 *
34 * @copyright 2015 David Mudrak <david@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class core_update_code_manager_testcase extends advanced_testcase {
38
39 public function test_get_remote_plugin_zip() {
40 $codeman = new \core\update\testable_code_manager();
41
42 $this->assertFalse($codeman->get_remote_plugin_zip('ftp://not.support.ed/', 'doesnotmatter'));
43 $this->assertDebuggingCalled('Error fetching plugin ZIP: unsupported transport protocol: ftp://not.support.ed/');
44
45 $this->assertEquals(0, $codeman->downloadscounter);
46 $this->assertFalse($codeman->get_remote_plugin_zip('http://first/', ''));
47 $this->assertDebuggingCalled('Error fetching plugin ZIP: md5 mismatch.');
48 $this->assertEquals(1, $codeman->downloadscounter);
49 $this->assertNotFalse($codeman->get_remote_plugin_zip('http://first/', md5('http://first/')));
50 $this->assertEquals(2, $codeman->downloadscounter);
51 $this->assertNotFalse($codeman->get_remote_plugin_zip('http://two/', md5('http://two/')));
52 $this->assertEquals(3, $codeman->downloadscounter);
53 $this->assertNotFalse($codeman->get_remote_plugin_zip('http://first/', md5('http://first/')));
54 $this->assertEquals(3, $codeman->downloadscounter);
55 }
56
57 public function test_move_plugin_directory() {
58 $codeman = new \core\update\testable_code_manager();
59
60 $tmp = make_request_directory();
61 $dir = make_writable_directory($tmp.'/mod/foo/lang/en');
62 file_put_contents($dir.'/foo.txt', 'Hello world!');
63
64 $codeman->move_plugin_directory($tmp.'/mod/foo', $tmp.'/mod/.foo.2015100200');
65
66 $this->assertTrue(is_dir($tmp.'/mod'));
67 $this->assertFalse(is_dir($tmp.'/mod/foo'));
68 $this->assertTrue(is_file($tmp.'/mod/.foo.2015100200/lang/en/foo.txt'));
69 $this->assertSame('Hello world!', file_get_contents($tmp.'/mod/.foo.2015100200/lang/en/foo.txt'));
70 }
71
72 /**
73 * @expectedException moodle_exception
74 */
75 public function test_move_plugin_directory_invalid_target() {
76 $codeman = new \core\update\testable_code_manager();
77 $codeman->move_plugin_directory(make_request_directory(), 'this_is_not_valid_path');
78 }
79
80 /**
81 * @expectedException moodle_exception
82 */
83 public function test_move_plugin_directory_nonwritable_target() {
84 $codeman = new \core\update\testable_code_manager();
85 // If this does not throw exception for you, please send me your IP address.
86 $codeman->move_plugin_directory(make_request_directory(), '/');
87 }
88
89 /**
90 * @expectedException moodle_exception
91 */
92 public function test_move_plugin_directory_existing_target() {
93 $codeman = new \core\update\testable_code_manager();
94 $dir1 = make_request_directory();
95 $dir2 = make_request_directory();
96 $codeman->move_plugin_directory($dir1, $dir2);
97 }
98
99 /**
100 * @expectedException moodle_exception
101 */
102 public function test_move_plugin_directory_nonexisting_source() {
103 $codeman = new \core\update\testable_code_manager();
104 $codeman->move_plugin_directory(make_request_directory().'/source', make_request_directory().'/target');
105 }
106
107 public function test_unzip_plugin_file() {
108 $codeman = new \core\update\testable_code_manager();
f2d8ed45 109 $zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
0e442ee7
DM
110 $targetdir = make_request_directory();
111
112 $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir);
113
114 $this->assertInternalType('array', $files);
115 $this->assertCount(4, $files);
116 $this->assertSame(true, $files['invalid-root/']);
117 $this->assertSame(true, $files['invalid-root/lang/']);
118 $this->assertSame(true, $files['invalid-root/lang/en/']);
119 $this->assertSame(true, $files['invalid-root/lang/en/fixed_root.php']);
120 foreach ($files as $file => $status) {
121 if (substr($file, -1) === '/') {
122 $this->assertTrue(is_dir($targetdir.'/'.$file));
123 } else {
124 $this->assertTrue(is_file($targetdir.'/'.$file));
125 }
126 }
127
128 $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'fixed_root');
129
130 $this->assertInternalType('array', $files);
131 $this->assertCount(4, $files);
132 $this->assertSame(true, $files['fixed_root/']);
133 $this->assertSame(true, $files['fixed_root/lang/']);
134 $this->assertSame(true, $files['fixed_root/lang/en/']);
135 $this->assertSame(true, $files['fixed_root/lang/en/fixed_root.php']);
136 foreach ($files as $file => $status) {
137 if (substr($file, -1) === '/') {
138 $this->assertTrue(is_dir($targetdir.'/'.$file));
139 } else {
140 $this->assertTrue(is_file($targetdir.'/'.$file));
141 }
142 }
143
f2d8ed45 144 $zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
0e442ee7
DM
145 $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'bar');
146 }
147}