PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/tests/update_code_manager_test.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 195 lines | 127 code | 34 blank | 34 comment | 7 complexity | a3b524c440d8af2a2750bed73ae0262a MD5 | raw file
  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. * Provides core_update_code_manager_testcase class.
  18. *
  19. * @package core_plugin
  20. * @category test
  21. * @copyright 2015 David Mudrak <david@moodle.com>
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. defined('MOODLE_INTERNAL') || die();
  25. global $CFG;
  26. require_once(__DIR__.'/fixtures/testable_update_code_manager.php');
  27. /**
  28. * Tests for \core\update\code_manager features.
  29. *
  30. * @copyright 2015 David Mudrak <david@moodle.com>
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class core_update_code_manager_testcase extends advanced_testcase {
  34. public function test_get_remote_plugin_zip() {
  35. $codeman = new \core\update\testable_code_manager();
  36. $this->assertFalse($codeman->get_remote_plugin_zip('ftp://not.support.ed/', 'doesnotmatter'));
  37. $this->assertDebuggingCalled('Error fetching plugin ZIP: unsupported transport protocol: ftp://not.support.ed/');
  38. $this->assertEquals(0, $codeman->downloadscounter);
  39. $this->assertFalse($codeman->get_remote_plugin_zip('http://first/', ''));
  40. $this->assertDebuggingCalled('Error fetching plugin ZIP: md5 mismatch.');
  41. $this->assertEquals(1, $codeman->downloadscounter);
  42. $this->assertNotFalse($codeman->get_remote_plugin_zip('http://first/', md5('http://first/')));
  43. $this->assertEquals(2, $codeman->downloadscounter);
  44. $this->assertNotFalse($codeman->get_remote_plugin_zip('http://two/', md5('http://two/')));
  45. $this->assertEquals(3, $codeman->downloadscounter);
  46. $this->assertNotFalse($codeman->get_remote_plugin_zip('http://first/', md5('http://first/')));
  47. $this->assertEquals(3, $codeman->downloadscounter);
  48. }
  49. public function test_get_remote_plugin_zip_corrupted_cache() {
  50. $temproot = make_request_directory();
  51. $codeman = new \core\update\testable_code_manager(null, $temproot);
  52. file_put_contents($temproot.'/distfiles/'.md5('http://valid/').'.zip', 'http://invalid/');
  53. // Even if the cache file is already there, its name does not match its
  54. // actual content. It must be removed and re-downaloaded.
  55. $returned = $codeman->get_remote_plugin_zip('http://valid/', md5('http://valid/'));
  56. $this->assertEquals(basename($returned), md5('http://valid/').'.zip');
  57. $this->assertEquals(file_get_contents($returned), 'http://valid/');
  58. }
  59. public function test_unzip_plugin_file() {
  60. $codeman = new \core\update\testable_code_manager();
  61. $zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
  62. $targetdir = make_request_directory();
  63. mkdir($targetdir.'/aaa_another');
  64. $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir);
  65. $this->assertInternalType('array', $files);
  66. $this->assertCount(4, $files);
  67. $this->assertSame(true, $files['invalid-root/']);
  68. $this->assertSame(true, $files['invalid-root/lang/']);
  69. $this->assertSame(true, $files['invalid-root/lang/en/']);
  70. $this->assertSame(true, $files['invalid-root/lang/en/fixed_root.php']);
  71. foreach ($files as $file => $status) {
  72. if (substr($file, -1) === '/') {
  73. $this->assertTrue(is_dir($targetdir.'/'.$file));
  74. } else {
  75. $this->assertTrue(is_file($targetdir.'/'.$file));
  76. }
  77. }
  78. $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'fixed_root');
  79. $this->assertInternalType('array', $files);
  80. $this->assertCount(4, $files);
  81. $this->assertSame(true, $files['fixed_root/']);
  82. $this->assertSame(true, $files['fixed_root/lang/']);
  83. $this->assertSame(true, $files['fixed_root/lang/en/']);
  84. $this->assertSame(true, $files['fixed_root/lang/en/fixed_root.php']);
  85. foreach ($files as $file => $status) {
  86. if (substr($file, -1) === '/') {
  87. $this->assertTrue(is_dir($targetdir.'/'.$file));
  88. } else {
  89. $this->assertTrue(is_file($targetdir.'/'.$file));
  90. }
  91. }
  92. $zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
  93. $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'bar');
  94. }
  95. /**
  96. * @expectedException moodle_exception
  97. */
  98. public function test_unzip_plugin_file_multidir() {
  99. $codeman = new \core\update\testable_code_manager();
  100. $zipfilepath = __DIR__.'/fixtures/update_validator/zips/multidir.zip';
  101. $targetdir = make_request_directory();
  102. // Attempting to rename the root folder if there are multiple ones should lead to exception.
  103. $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'foo');
  104. }
  105. public function test_get_plugin_zip_root_dir() {
  106. $codeman = new \core\update\testable_code_manager();
  107. $zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
  108. $this->assertEquals('invalid-root', $codeman->get_plugin_zip_root_dir($zipfilepath));
  109. $zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
  110. $this->assertEquals('bar', $codeman->get_plugin_zip_root_dir($zipfilepath));
  111. $zipfilepath = __DIR__.'/fixtures/update_validator/zips/multidir.zip';
  112. $this->assertSame(false, $codeman->get_plugin_zip_root_dir($zipfilepath));
  113. }
  114. public function test_list_plugin_folder_files() {
  115. $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
  116. $codeman = new \core\update\testable_code_manager();
  117. $files = $codeman->list_plugin_folder_files($fixtures.'/foobar');
  118. $this->assertInternalType('array', $files);
  119. $this->assertEquals(6, count($files));
  120. $fixtures = str_replace(DIRECTORY_SEPARATOR, '/', $fixtures);
  121. $this->assertEquals($files['foobar/'], $fixtures.'/foobar');
  122. $this->assertEquals($files['foobar/lang/en/local_foobar.php'], $fixtures.'/foobar/lang/en/local_foobar.php');
  123. }
  124. public function test_zip_plugin_folder() {
  125. $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
  126. $storage = make_request_directory();
  127. $codeman = new \core\update\testable_code_manager();
  128. $codeman->zip_plugin_folder($fixtures.'/foobar', $storage.'/foobar.zip');
  129. $this->assertTrue(file_exists($storage.'/foobar.zip'));
  130. $fp = get_file_packer('application/zip');
  131. $zipfiles = $fp->list_files($storage.'/foobar.zip');
  132. $this->assertNotEmpty($zipfiles);
  133. foreach ($zipfiles as $zipfile) {
  134. if ($zipfile->is_directory) {
  135. $this->assertTrue(is_dir($fixtures.'/'.$zipfile->pathname));
  136. } else {
  137. $this->assertTrue(file_exists($fixtures.'/'.$zipfile->pathname));
  138. }
  139. }
  140. }
  141. public function test_archiving_plugin_version() {
  142. $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
  143. $codeman = new \core\update\testable_code_manager();
  144. $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', 0));
  145. $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', null));
  146. $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', '', 2015100900));
  147. $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar-does-not-exist', 'local_foobar', 2013031900));
  148. $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', 2013031900));
  149. $this->assertFalse($codeman->get_archived_plugin_version('mod_foobar', 2013031900));
  150. $this->assertTrue($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', 2013031900, true));
  151. $this->assertNotFalse($codeman->get_archived_plugin_version('local_foobar', 2013031900));
  152. $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', 2013031900)));
  153. $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', '2013031900')));
  154. $this->assertFalse($codeman->get_archived_plugin_version('mod_foobar', 2013031900));
  155. $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', 2013031901));
  156. $this->assertFalse($codeman->get_archived_plugin_version('', 2013031901));
  157. $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', ''));
  158. $this->assertTrue($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', '2013031900'));
  159. $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', 2013031900)));
  160. }
  161. }