MDL-49329 admin: Clean up code manager methods
[moodle.git] / lib / tests / update_code_manager_test.php
index c811c2f..a2ef45c 100644 (file)
@@ -17,7 +17,7 @@
 /**
  * Provides core_update_code_manager_testcase class.
  *
- * @package     core
+ * @package     core_plugin
  * @category    test
  * @copyright   2015 David Mudrak <david@moodle.com>
  * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -54,59 +54,24 @@ class core_update_code_manager_testcase extends advanced_testcase {
         $this->assertEquals(3, $codeman->downloadscounter);
     }
 
-    public function test_move_plugin_directory() {
-        $codeman = new \core\update\testable_code_manager();
-
-        $tmp = make_request_directory();
-        $dir = make_writable_directory($tmp.'/mod/foo/lang/en');
-        file_put_contents($dir.'/foo.txt', 'Hello world!');
+    public function test_get_remote_plugin_zip_corrupted_cache() {
 
-        $codeman->move_plugin_directory($tmp.'/mod/foo', $tmp.'/mod/.foo.2015100200');
+        $temproot = make_request_directory();
+        $codeman = new \core\update\testable_code_manager(null, $temproot);
 
-        $this->assertTrue(is_dir($tmp.'/mod'));
-        $this->assertFalse(is_dir($tmp.'/mod/foo'));
-        $this->assertTrue(is_file($tmp.'/mod/.foo.2015100200/lang/en/foo.txt'));
-        $this->assertSame('Hello world!', file_get_contents($tmp.'/mod/.foo.2015100200/lang/en/foo.txt'));
-    }
-
-    /**
-     * @expectedException moodle_exception
-     */
-    public function test_move_plugin_directory_invalid_target() {
-        $codeman = new \core\update\testable_code_manager();
-        $codeman->move_plugin_directory(make_request_directory(), 'this_is_not_valid_path');
-    }
+        file_put_contents($temproot.'/distfiles/'.md5('http://valid/').'.zip', 'http://invalid/');
 
-    /**
-     * @expectedException moodle_exception
-     */
-    public function test_move_plugin_directory_nonwritable_target() {
-        $codeman = new \core\update\testable_code_manager();
-        // If this does not throw exception for you, please send me your IP address.
-        $codeman->move_plugin_directory(make_request_directory(), '/');
-    }
+        // Even if the cache file is already there, its name does not match its
+        // actual content. It must be removed and re-downaloaded.
+        $returned = $codeman->get_remote_plugin_zip('http://valid/', md5('http://valid/'));
 
-    /**
-     * @expectedException moodle_exception
-     */
-    public function test_move_plugin_directory_existing_target() {
-        $codeman = new \core\update\testable_code_manager();
-        $dir1 = make_request_directory();
-        $dir2 = make_request_directory();
-        $codeman->move_plugin_directory($dir1, $dir2);
-    }
-
-    /**
-     * @expectedException moodle_exception
-     */
-    public function test_move_plugin_directory_nonexisting_source() {
-        $codeman = new \core\update\testable_code_manager();
-        $codeman->move_plugin_directory(make_request_directory().'/source', make_request_directory().'/target');
+        $this->assertEquals(basename($returned), md5('http://valid/').'.zip');
+        $this->assertEquals(file_get_contents($returned), 'http://valid/');
     }
 
     public function test_unzip_plugin_file() {
         $codeman = new \core\update\testable_code_manager();
-        $zipfilepath = __DIR__.'/fixtures/invalidroot.zip';
+        $zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
         $targetdir = make_request_directory();
 
         $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir);
@@ -141,7 +106,74 @@ class core_update_code_manager_testcase extends advanced_testcase {
             }
         }
 
-        $zipfilepath = __DIR__.'/fixtures/bar.zip';
+        $zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
         $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'bar');
     }
+
+    public function test_get_plugin_zip_root_dir() {
+        $codeman = new \core\update\testable_code_manager();
+
+        $zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
+        $this->assertEquals('invalid-root', $codeman->get_plugin_zip_root_dir($zipfilepath));
+
+        $zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
+        $this->assertEquals('bar', $codeman->get_plugin_zip_root_dir($zipfilepath));
+    }
+
+    public function test_list_plugin_folder_files() {
+        $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
+        $codeman = new \core\update\testable_code_manager();
+        $files = $codeman->list_plugin_folder_files($fixtures.'/foobar');
+        $this->assertInternalType('array', $files);
+        $this->assertEquals(6, count($files));
+        $this->assertEquals($files['foobar/'], $fixtures.'/foobar');
+        $this->assertEquals($files['foobar/lang/en/local_foobar.php'], $fixtures.'/foobar/lang/en/local_foobar.php');
+    }
+
+    public function test_zip_plugin_folder() {
+        $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
+        $storage = make_request_directory();
+        $codeman = new \core\update\testable_code_manager();
+        $codeman->zip_plugin_folder($fixtures.'/foobar', $storage.'/foobar.zip');
+        $this->assertTrue(file_exists($storage.'/foobar.zip'));
+
+        $fp = get_file_packer('application/zip');
+        $zipfiles = $fp->list_files($storage.'/foobar.zip');
+        $this->assertNotEmpty($zipfiles);
+        foreach ($zipfiles as $zipfile) {
+            if ($zipfile->is_directory) {
+                $this->assertTrue(is_dir($fixtures.'/'.$zipfile->pathname));
+            } else {
+                $this->assertTrue(file_exists($fixtures.'/'.$zipfile->pathname));
+            }
+        }
+    }
+
+    public function test_archiving_plugin_version() {
+        $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
+        $codeman = new \core\update\testable_code_manager();
+
+        $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', 0));
+        $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', null));
+        $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', '', 2015100900));
+        $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar-does-not-exist', 'local_foobar', 2013031900));
+
+        $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', 2013031900));
+        $this->assertFalse($codeman->get_archived_plugin_version('mod_foobar', 2013031900));
+
+        $this->assertTrue($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', 2013031900, true));
+
+        $this->assertNotFalse($codeman->get_archived_plugin_version('local_foobar', 2013031900));
+        $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', 2013031900)));
+        $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', '2013031900')));
+
+        $this->assertFalse($codeman->get_archived_plugin_version('mod_foobar', 2013031900));
+        $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', 2013031901));
+        $this->assertFalse($codeman->get_archived_plugin_version('', 2013031901));
+        $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', ''));
+
+        $this->assertTrue($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', '2013031900'));
+        $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', 2013031900)));
+
+    }
 }