[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/filestorage/tests/ -> mbz_packer_test.php (source)

   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 default mode.
  44          $CFG->enabletgzbackups = false;
  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->enabletgzbackups = true;
  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, '/', 'false.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          // (Note: the setting does not matter, but set to false just to check.)
  65          $CFG->enabletgzbackups = false;
  66  
  67          // Extract to path (zip).
  68          $packer->extract_to_pathname($filefalse, $CFG->tempdir);
  69          $onefile = $CFG->tempdir . '/1.txt';
  70          $this->assertEquals('frog', file_get_contents($onefile));
  71          unlink($onefile);
  72  
  73          // Extract to path (tgz).
  74          $packer->extract_to_pathname($filetrue, $CFG->tempdir);
  75          $onefile = $CFG->tempdir . '/1.txt';
  76          $this->assertEquals('frog', file_get_contents($onefile));
  77          unlink($onefile);
  78  
  79          // Extract to storage (zip).
  80          $packer->extract_to_storage($storagefalse, $context->id, 'phpunit', 'data', 1, '/');
  81          $fs = get_file_storage();
  82          $out = $fs->get_file($context->id, 'phpunit', 'data', 1, '/', '1.txt');
  83          $this->assertNotEmpty($out);
  84          $this->assertEquals('frog', $out->get_content());
  85  
  86          // Extract to storage (tgz).
  87          $packer->extract_to_storage($storagetrue, $context->id, 'phpunit', 'data', 2, '/');
  88          $out = $fs->get_file($context->id, 'phpunit', 'data', 2, '/', '1.txt');
  89          $this->assertNotEmpty($out);
  90          $this->assertEquals('frog', $out->get_content());
  91      }
  92  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1