MDL-55020 admin: Fix renaming of the plugin package root folder

There was a problem with core\update\code_manager::unzip_plugin_file()
if it was used to extract a plugin package into a non-empty target
directory and the plugin package root folder was being renamed at the
same time.

The problem was caused by the underlying helper method
rename_extracted_rootdir() that worked only for ZIPs extracted to an
empty temporary location. When the plugin was extracted to the actual
dirroot with other existing plugin folders present, the method failed
badly.

The solution in the patch is to always extract the ZIP into a temporary
empty location, perform the eventual root renaming there, and only then
move the extracted contents to the final destination. Additionally we
are changing the behaviour of the rename_extracted_rootdir() method so
that now it throws exception if the plugin package contains multiple
root folders (it should not happen in normal situations as such a plugin
would not pass the pre-install validation).

Unit tests did not catch this bug before because in the tests, the
target directory had been empty. Now we are adding a new directory
"aaa_another" to the target location to test in more realistic
environment. Tests for the new behaviour of the renaming method are
added, too.

p.s. I noticed that moodle_exception class was not imported into the
namespace and this is fixed now too (and covered with unit tests).
This commit is contained in:
David Mudrák
2016-07-01 01:11:36 +02:00
parent eb36cfcbb6
commit 9ba44fa3fd
3 changed files with 60 additions and 9 deletions
+13
View File
@@ -73,6 +73,7 @@ class core_update_code_manager_testcase extends advanced_testcase {
$codeman = new \core\update\testable_code_manager();
$zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
$targetdir = make_request_directory();
mkdir($targetdir.'/aaa_another');
$files = $codeman->unzip_plugin_file($zipfilepath, $targetdir);
@@ -110,6 +111,15 @@ class core_update_code_manager_testcase extends advanced_testcase {
$files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'bar');
}
public function test_unzip_plugin_file_multidir() {
$codeman = new \core\update\testable_code_manager();
$zipfilepath = __DIR__.'/fixtures/update_validator/zips/multidir.zip';
$targetdir = make_request_directory();
// Attempting to rename the root folder if there are multiple ones should lead to exception.
$this->setExpectedException('moodle_exception');
$files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'foo');
}
public function test_get_plugin_zip_root_dir() {
$codeman = new \core\update\testable_code_manager();
@@ -118,6 +128,9 @@ class core_update_code_manager_testcase extends advanced_testcase {
$zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
$this->assertEquals('bar', $codeman->get_plugin_zip_root_dir($zipfilepath));
$zipfilepath = __DIR__.'/fixtures/update_validator/zips/multidir.zip';
$this->assertSame(false, $codeman->get_plugin_zip_root_dir($zipfilepath));
}
public function test_list_plugin_folder_files() {