Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [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  namespace core;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->libdir . '/filestorage/file_progress.php');
  23  
  24  /**
  25   * Unit tests for /lib/filestorage/mbz_packer.php.
  26   *
  27   * @package core
  28   * @copyright 2013 The Open University
  29   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class mbz_packer_test extends \advanced_testcase {
  32  
  33      public function test_archive_with_both_options() {
  34          global $CFG;
  35          $this->resetAfterTest();
  36  
  37          // Get backup packer.
  38          $packer = get_file_packer('application/vnd.moodle.backup');
  39          require_once($CFG->dirroot . '/lib/filestorage/tgz_packer.php');
  40  
  41          // Set up basic archive contents.
  42          $files = array('1.txt' => array('frog'));
  43  
  44          // Create 2 archives (each with one file in) in zip mode.
  45          $CFG->usezipbackups = true;
  46          $filefalse = $CFG->tempdir . '/false.mbz';
  47          $this->assertNotEmpty($packer->archive_to_pathname($files, $filefalse));
  48          $context = \context_system::instance();
  49          $this->assertNotEmpty($storagefalse = $packer->archive_to_storage(
  50                  $files, $context->id, 'phpunit', 'data', 0, '/', 'false.mbz'));
  51  
  52          // Create 2 archives in tgz mode.
  53          $CFG->usezipbackups = false;
  54          $filetrue = $CFG->tempdir . '/true.mbz';
  55          $this->assertNotEmpty($packer->archive_to_pathname($files, $filetrue));
  56          $context = \context_system::instance();
  57          $this->assertNotEmpty($storagetrue = $packer->archive_to_storage(
  58                  $files, $context->id, 'phpunit', 'data', 0, '/', 'true.mbz'));
  59  
  60          // Check the sizes are different (indicating different formats).
  61          $this->assertNotEquals(filesize($filefalse), filesize($filetrue));
  62          $this->assertNotEquals($storagefalse->get_filesize(), $storagetrue->get_filesize());
  63  
  64          // Extract files into storage and into filesystem from both formats.
  65  
  66          // Extract to path (zip).
  67          $packer->extract_to_pathname($filefalse, $CFG->tempdir);
  68          $onefile = $CFG->tempdir . '/1.txt';
  69          $this->assertEquals('frog', file_get_contents($onefile));
  70          unlink($onefile);
  71  
  72          // Extract to path (tgz).
  73          $packer->extract_to_pathname($filetrue, $CFG->tempdir);
  74          $onefile = $CFG->tempdir . '/1.txt';
  75          $this->assertEquals('frog', file_get_contents($onefile));
  76          unlink($onefile);
  77  
  78          // Extract to storage (zip).
  79          $packer->extract_to_storage($storagefalse, $context->id, 'phpunit', 'data', 1, '/');
  80          $fs = get_file_storage();
  81          $out = $fs->get_file($context->id, 'phpunit', 'data', 1, '/', '1.txt');
  82          $this->assertNotEmpty($out);
  83          $this->assertEquals('frog', $out->get_content());
  84  
  85          // Extract to storage (tgz).
  86          $packer->extract_to_storage($storagetrue, $context->id, 'phpunit', 'data', 2, '/');
  87          $out = $fs->get_file($context->id, 'phpunit', 'data', 2, '/', '1.txt');
  88          $this->assertNotEmpty($out);
  89          $this->assertEquals('frog', $out->get_content());
  90      }
  91  
  92      public function usezipbackups_provider() {
  93          return [
  94              'Use zips'  => [true],
  95              'Use tgz'   => [false],
  96          ];
  97      }
  98  
  99      /**
 100       * @dataProvider usezipbackups_provider
 101       */
 102      public function test_extract_to_pathname_returnvalue_successful($usezipbackups) {
 103          global $CFG;
 104          $this->resetAfterTest();
 105  
 106          $packer = get_file_packer('application/vnd.moodle.backup');
 107  
 108          // Set up basic archive contents.
 109          $files = array('1.txt' => array('frog'));
 110  
 111          // Create 2 archives (each with one file in) in zip mode.
 112          $CFG->usezipbackups = $usezipbackups;
 113  
 114          $mbzfile = make_request_directory() . '/file.mbz';
 115          $packer->archive_to_pathname($files, $mbzfile);
 116  
 117          $target = make_request_directory();
 118          $result = $packer->extract_to_pathname($mbzfile, $target, null, null, true);
 119          $this->assertTrue($result);
 120      }
 121  
 122      /**
 123       * @dataProvider usezipbackups_provider
 124       */
 125      public function test_extract_to_pathname_returnvalue_failure($usezipbackups) {
 126          global $CFG;
 127          $this->resetAfterTest();
 128  
 129          $packer = get_file_packer('application/vnd.moodle.backup');
 130  
 131          // Create 2 archives (each with one file in) in zip mode.
 132          $CFG->usezipbackups = $usezipbackups;
 133  
 134          $mbzfile = make_request_directory() . '/file.mbz';
 135          file_put_contents($mbzfile, 'Content');
 136  
 137          $target = make_request_directory();
 138          $result = $packer->extract_to_pathname($mbzfile, $target, null, null, true);
 139          $this->assertDebuggingCalledCount(1);
 140          $this->assertFalse($result);
 141      }
 142  }