Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

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