MDL-49329 admin: Archive plugin code before removing it from dirroot
[moodle.git] / lib / tests / update_code_manager_test.php
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/>.
17 /**
18  * Provides core_update_code_manager_testcase class.
19  *
20  * @package     core_plugin
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once(__DIR__.'/fixtures/testable_update_code_manager.php');
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  */
37 class core_update_code_manager_testcase extends advanced_testcase {
39     public function test_get_remote_plugin_zip() {
40         $codeman = new \core\update\testable_code_manager();
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/');
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     }
57     public function test_get_remote_plugin_zip_corrupted_cache() {
59         $temproot = make_request_directory();
60         $codeman = new \core\update\testable_code_manager(null, $temproot);
62         file_put_contents($temproot.'/distfiles/'.md5('http://valid/').'.zip', 'http://invalid/');
64         // Even if the cache file is already there, its name does not match its
65         // actual content. It must be removed and re-downaloaded.
66         $returned = $codeman->get_remote_plugin_zip('http://valid/', md5('http://valid/'));
68         $this->assertEquals(basename($returned), md5('http://valid/').'.zip');
69         $this->assertEquals(file_get_contents($returned), 'http://valid/');
70     }
72     public function test_move_plugin_directory() {
73         $codeman = new \core\update\testable_code_manager();
75         $tmp = make_request_directory();
76         $dir = make_writable_directory($tmp.'/mod/foo/lang/en');
77         file_put_contents($dir.'/foo.txt', 'Hello world!');
79         $codeman->move_plugin_directory($tmp.'/mod/foo', $tmp.'/mod/.foo.2015100200');
81         $this->assertTrue(is_dir($tmp.'/mod'));
82         $this->assertFalse(is_dir($tmp.'/mod/foo'));
83         $this->assertTrue(is_file($tmp.'/mod/.foo.2015100200/lang/en/foo.txt'));
84         $this->assertSame('Hello world!', file_get_contents($tmp.'/mod/.foo.2015100200/lang/en/foo.txt'));
85     }
87     /**
88      * @expectedException moodle_exception
89      */
90     public function test_move_plugin_directory_invalid_target() {
91         $codeman = new \core\update\testable_code_manager();
92         $codeman->move_plugin_directory(make_request_directory(), 'this_is_not_valid_path');
93     }
95     /**
96      * @expectedException moodle_exception
97      */
98     public function test_move_plugin_directory_nonwritable_target() {
99         $codeman = new \core\update\testable_code_manager();
100         // If this does not throw exception for you, please send me your IP address.
101         $codeman->move_plugin_directory(make_request_directory(), '/');
102     }
104     /**
105      * @expectedException moodle_exception
106      */
107     public function test_move_plugin_directory_existing_target() {
108         $codeman = new \core\update\testable_code_manager();
109         $dir1 = make_request_directory();
110         $dir2 = make_request_directory();
111         $codeman->move_plugin_directory($dir1, $dir2);
112     }
114     /**
115      * @expectedException moodle_exception
116      */
117     public function test_move_plugin_directory_nonexisting_source() {
118         $codeman = new \core\update\testable_code_manager();
119         $codeman->move_plugin_directory(make_request_directory().'/source', make_request_directory().'/target');
120     }
122     public function test_unzip_plugin_file() {
123         $codeman = new \core\update\testable_code_manager();
124         $zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
125         $targetdir = make_request_directory();
127         $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir);
129         $this->assertInternalType('array', $files);
130         $this->assertCount(4, $files);
131         $this->assertSame(true, $files['invalid-root/']);
132         $this->assertSame(true, $files['invalid-root/lang/']);
133         $this->assertSame(true, $files['invalid-root/lang/en/']);
134         $this->assertSame(true, $files['invalid-root/lang/en/fixed_root.php']);
135         foreach ($files as $file => $status) {
136             if (substr($file, -1) === '/') {
137                 $this->assertTrue(is_dir($targetdir.'/'.$file));
138             } else {
139                 $this->assertTrue(is_file($targetdir.'/'.$file));
140             }
141         }
143         $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'fixed_root');
145         $this->assertInternalType('array', $files);
146         $this->assertCount(4, $files);
147         $this->assertSame(true, $files['fixed_root/']);
148         $this->assertSame(true, $files['fixed_root/lang/']);
149         $this->assertSame(true, $files['fixed_root/lang/en/']);
150         $this->assertSame(true, $files['fixed_root/lang/en/fixed_root.php']);
151         foreach ($files as $file => $status) {
152             if (substr($file, -1) === '/') {
153                 $this->assertTrue(is_dir($targetdir.'/'.$file));
154             } else {
155                 $this->assertTrue(is_file($targetdir.'/'.$file));
156             }
157         }
159         $zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
160         $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'bar');
161     }
163     public function test_get_plugin_zip_root_dir() {
164         $codeman = new \core\update\testable_code_manager();
166         $zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
167         $this->assertEquals('invalid-root', $codeman->get_plugin_zip_root_dir($zipfilepath));
169         $zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
170         $this->assertEquals('bar', $codeman->get_plugin_zip_root_dir($zipfilepath));
171     }
173     public function test_list_plugin_folder_files() {
174         $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
175         $codeman = new \core\update\testable_code_manager();
176         $files = $codeman->list_plugin_folder_files($fixtures.'/foobar');
177         $this->assertInternalType('array', $files);
178         $this->assertEquals(6, count($files));
179         $this->assertEquals($files['foobar/'], $fixtures.'/foobar');
180         $this->assertEquals($files['foobar/lang/en/local_foobar.php'], $fixtures.'/foobar/lang/en/local_foobar.php');
181     }
183     public function test_zip_plugin_folder() {
184         $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
185         $storage = make_request_directory();
186         $codeman = new \core\update\testable_code_manager();
187         $codeman->zip_plugin_folder($fixtures.'/foobar', $storage.'/foobar.zip');
188         $this->assertTrue(file_exists($storage.'/foobar.zip'));
190         $fp = get_file_packer('application/zip');
191         $zipfiles = $fp->list_files($storage.'/foobar.zip');
192         $this->assertNotEmpty($zipfiles);
193         foreach ($zipfiles as $zipfile) {
194             if ($zipfile->is_directory) {
195                 $this->assertTrue(is_dir($fixtures.'/'.$zipfile->pathname));
196             } else {
197                 $this->assertTrue(file_exists($fixtures.'/'.$zipfile->pathname));
198             }
199         }
200     }
202     public function test_archiving_plugin_version() {
203         $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
204         $codeman = new \core\update\testable_code_manager();
206         $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', 0));
207         $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', null));
208         $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', '', 2015100900));
209         $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar-does-not-exist', 'local_foobar', 2013031900));
211         $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', 2013031900));
212         $this->assertFalse($codeman->get_archived_plugin_version('mod_foobar', 2013031900));
214         $this->assertTrue($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', 2013031900, true));
216         $this->assertNotFalse($codeman->get_archived_plugin_version('local_foobar', 2013031900));
217         $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', 2013031900)));
218         $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', '2013031900')));
220         $this->assertFalse($codeman->get_archived_plugin_version('mod_foobar', 2013031900));
221         $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', 2013031901));
222         $this->assertFalse($codeman->get_archived_plugin_version('', 2013031901));
223         $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', ''));
225         $this->assertTrue($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', '2013031900'));
226         $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', 2013031900)));
228     }