Search moodle.org's
Developer Documentation

See Release Notes

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