Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   *
  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   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_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   */
  37  class 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_get_remote_plugin_zip_corrupted_cache() {
  58  
  59          $temproot = make_request_directory();
  60          $codeman = new \core\update\testable_code_manager(null, $temproot);
  61  
  62          file_put_contents($temproot.'/distfiles/'.md5('http://valid/').'.zip', 'http://invalid/');
  63  
  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/'));
  67  
  68          $this->assertEquals(basename($returned), md5('http://valid/').'.zip');
  69          $this->assertEquals(file_get_contents($returned), 'http://valid/');
  70      }
  71  
  72      public function test_unzip_plugin_file() {
  73          $codeman = new \core\update\testable_code_manager();
  74          $zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
  75          $targetdir = make_request_directory();
  76          mkdir($targetdir.'/aaa_another');
  77  
  78          $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir);
  79  
  80          $this->assertInternalType('array', $files);
  81          $this->assertCount(4, $files);
  82          $this->assertSame(true, $files['invalid-root/']);
  83          $this->assertSame(true, $files['invalid-root/lang/']);
  84          $this->assertSame(true, $files['invalid-root/lang/en/']);
  85          $this->assertSame(true, $files['invalid-root/lang/en/fixed_root.php']);
  86          foreach ($files as $file => $status) {
  87              if (substr($file, -1) === '/') {
  88                  $this->assertTrue(is_dir($targetdir.'/'.$file));
  89              } else {
  90                  $this->assertTrue(is_file($targetdir.'/'.$file));
  91              }
  92          }
  93  
  94          $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'fixed_root');
  95  
  96          $this->assertInternalType('array', $files);
  97          $this->assertCount(4, $files);
  98          $this->assertSame(true, $files['fixed_root/']);
  99          $this->assertSame(true, $files['fixed_root/lang/']);
 100          $this->assertSame(true, $files['fixed_root/lang/en/']);
 101          $this->assertSame(true, $files['fixed_root/lang/en/fixed_root.php']);
 102          foreach ($files as $file => $status) {
 103              if (substr($file, -1) === '/') {
 104                  $this->assertTrue(is_dir($targetdir.'/'.$file));
 105              } else {
 106                  $this->assertTrue(is_file($targetdir.'/'.$file));
 107              }
 108          }
 109  
 110          $zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
 111          $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'bar');
 112      }
 113  
 114      /**
 115       * @expectedException moodle_exception
 116       */
 117      public function test_unzip_plugin_file_multidir() {
 118          $codeman = new \core\update\testable_code_manager();
 119          $zipfilepath = __DIR__.'/fixtures/update_validator/zips/multidir.zip';
 120          $targetdir = make_request_directory();
 121          // Attempting to rename the root folder if there are multiple ones should lead to exception.
 122          $files = $codeman->unzip_plugin_file($zipfilepath, $targetdir, 'foo');
 123      }
 124  
 125      public function test_get_plugin_zip_root_dir() {
 126          $codeman = new \core\update\testable_code_manager();
 127  
 128          $zipfilepath = __DIR__.'/fixtures/update_validator/zips/invalidroot.zip';
 129          $this->assertEquals('invalid-root', $codeman->get_plugin_zip_root_dir($zipfilepath));
 130  
 131          $zipfilepath = __DIR__.'/fixtures/update_validator/zips/bar.zip';
 132          $this->assertEquals('bar', $codeman->get_plugin_zip_root_dir($zipfilepath));
 133  
 134          $zipfilepath = __DIR__.'/fixtures/update_validator/zips/multidir.zip';
 135          $this->assertSame(false, $codeman->get_plugin_zip_root_dir($zipfilepath));
 136      }
 137  
 138      public function test_list_plugin_folder_files() {
 139          $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
 140          $codeman = new \core\update\testable_code_manager();
 141          $files = $codeman->list_plugin_folder_files($fixtures.'/foobar');
 142          $this->assertInternalType('array', $files);
 143          $this->assertEquals(6, count($files));
 144          $fixtures = str_replace(DIRECTORY_SEPARATOR, '/', $fixtures);
 145          $this->assertEquals($files['foobar/'], $fixtures.'/foobar');
 146          $this->assertEquals($files['foobar/lang/en/local_foobar.php'], $fixtures.'/foobar/lang/en/local_foobar.php');
 147      }
 148  
 149      public function test_zip_plugin_folder() {
 150          $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
 151          $storage = make_request_directory();
 152          $codeman = new \core\update\testable_code_manager();
 153          $codeman->zip_plugin_folder($fixtures.'/foobar', $storage.'/foobar.zip');
 154          $this->assertTrue(file_exists($storage.'/foobar.zip'));
 155  
 156          $fp = get_file_packer('application/zip');
 157          $zipfiles = $fp->list_files($storage.'/foobar.zip');
 158          $this->assertNotEmpty($zipfiles);
 159          foreach ($zipfiles as $zipfile) {
 160              if ($zipfile->is_directory) {
 161                  $this->assertTrue(is_dir($fixtures.'/'.$zipfile->pathname));
 162              } else {
 163                  $this->assertTrue(file_exists($fixtures.'/'.$zipfile->pathname));
 164              }
 165          }
 166      }
 167  
 168      public function test_archiving_plugin_version() {
 169          $fixtures = __DIR__.'/fixtures/update_validator/plugindir';
 170          $codeman = new \core\update\testable_code_manager();
 171  
 172          $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', 0));
 173          $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', null));
 174          $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar', '', 2015100900));
 175          $this->assertFalse($codeman->archive_plugin_version($fixtures.'/foobar-does-not-exist', 'local_foobar', 2013031900));
 176  
 177          $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', 2013031900));
 178          $this->assertFalse($codeman->get_archived_plugin_version('mod_foobar', 2013031900));
 179  
 180          $this->assertTrue($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', 2013031900, true));
 181  
 182          $this->assertNotFalse($codeman->get_archived_plugin_version('local_foobar', 2013031900));
 183          $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', 2013031900)));
 184          $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', '2013031900')));
 185  
 186          $this->assertFalse($codeman->get_archived_plugin_version('mod_foobar', 2013031900));
 187          $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', 2013031901));
 188          $this->assertFalse($codeman->get_archived_plugin_version('', 2013031901));
 189          $this->assertFalse($codeman->get_archived_plugin_version('local_foobar', ''));
 190  
 191          $this->assertTrue($codeman->archive_plugin_version($fixtures.'/foobar', 'local_foobar', '2013031900'));
 192          $this->assertTrue(file_exists($codeman->get_archived_plugin_version('local_foobar', 2013031900)));
 193  
 194      }
 195  }