MDL-71116 core_badges: Backpack URLs with more than 50 chars
[moodle.git] / lib / filestorage / tests / mbz_packer_test.php
blob2b7ef99556881f09afe6014a5ef2a971aaf0b877
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.
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/>.
17 /**
18 * Unit tests for /lib/filestorage/mbz_packer.php.
20 * @package core_files
21 * @copyright 2013 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
28 require_once($CFG->libdir . '/filestorage/file_progress.php');
30 class core_files_mbz_packer_testcase extends advanced_testcase {
32 public function test_archive_with_both_options() {
33 global $CFG;
34 $this->resetAfterTest();
36 // Get backup packer.
37 $packer = get_file_packer('application/vnd.moodle.backup');
38 require_once($CFG->dirroot . '/lib/filestorage/tgz_packer.php');
40 // Set up basic archive contents.
41 $files = array('1.txt' => array('frog'));
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'));
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'));
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());
63 // Extract files into storage and into filesystem from both formats.
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);
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);
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());
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());
91 public function usezipbackups_provider() {
92 return [
93 'Use zips' => [true],
94 'Use tgz' => [false],
98 /**
99 * @dataProvider usezipbackups_provider
101 public function test_extract_to_pathname_returnvalue_successful($usezipbackups) {
102 global $CFG;
103 $this->resetAfterTest();
105 $packer = get_file_packer('application/vnd.moodle.backup');
107 // Set up basic archive contents.
108 $files = array('1.txt' => array('frog'));
110 // Create 2 archives (each with one file in) in zip mode.
111 $CFG->usezipbackups = $usezipbackups;
113 $mbzfile = make_request_directory() . '/file.mbz';
114 $packer->archive_to_pathname($files, $mbzfile);
116 $target = make_request_directory();
117 $result = $packer->extract_to_pathname($mbzfile, $target, null, null, true);
118 $this->assertTrue($result);
122 * @dataProvider usezipbackups_provider
124 public function test_extract_to_pathname_returnvalue_failure($usezipbackups) {
125 global $CFG;
126 $this->resetAfterTest();
128 $packer = get_file_packer('application/vnd.moodle.backup');
130 // Create 2 archives (each with one file in) in zip mode.
131 $CFG->usezipbackups = $usezipbackups;
133 $mbzfile = make_request_directory() . '/file.mbz';
134 file_put_contents($mbzfile, 'Content');
136 $target = make_request_directory();
137 $result = $packer->extract_to_pathname($mbzfile, $target, null, null, true);
138 $this->assertDebuggingCalledCount(1);
139 $this->assertFalse($result);