1 |
efrain |
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_files\local\archive_writer;
|
|
|
18 |
|
|
|
19 |
use advanced_testcase;
|
|
|
20 |
use context_module;
|
|
|
21 |
use core_files\archive_writer;
|
|
|
22 |
use ZipArchive;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Unit tests for \core_files\local\archive_writer\zip_writer.
|
|
|
26 |
*
|
|
|
27 |
* @package core_files
|
|
|
28 |
* @category test
|
|
|
29 |
* @copyright 2020 Mark Nelson <mdjnelson@gmail.com>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
31 |
* @covers \core_files\local\archive_writer\zip_writer
|
|
|
32 |
*/
|
|
|
33 |
class zip_writer_test extends advanced_testcase {
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Test add_file_from_filepath().
|
|
|
37 |
*/
|
|
|
38 |
public function test_add_file_from_filepath(): void {
|
|
|
39 |
global $CFG;
|
|
|
40 |
|
|
|
41 |
$pathtofileinzip = '/some/made/up/name.txt';
|
|
|
42 |
$filetoadd = $CFG->dirroot . '/files/tests/fixtures/awesome_file.txt';
|
|
|
43 |
|
|
|
44 |
$zipwriter = archive_writer::get_file_writer('test.zip', archive_writer::ZIP_WRITER);
|
|
|
45 |
$zipwriter->add_file_from_filepath($pathtofileinzip, $filetoadd);
|
|
|
46 |
$zipwriter->finish();
|
|
|
47 |
|
|
|
48 |
$pathtozip = $zipwriter->get_path_to_zip();
|
|
|
49 |
$zip = new ZipArchive();
|
|
|
50 |
$opened = $zip->open($pathtozip);
|
|
|
51 |
$this->assertTrue($opened);
|
|
|
52 |
|
|
|
53 |
// Filename that has been sanitized by Zipstream.
|
|
|
54 |
$pathtofileinzip = ltrim($pathtofileinzip, '/');
|
|
|
55 |
|
|
|
56 |
$this->assertEquals("Hey, this is an awesome text file. Hello! :)", $zip->getFromName($pathtofileinzip));
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Test add_file_from_string().
|
|
|
61 |
*/
|
|
|
62 |
public function test_add_file_from_string(): void {
|
|
|
63 |
$pathtofileinzip = "/path/to/my/awesome/file.txt";
|
|
|
64 |
$mycontent = "This is some real awesome content, ya dig?";
|
|
|
65 |
|
|
|
66 |
$zipwriter = archive_writer::get_file_writer('test.zip', archive_writer::ZIP_WRITER);
|
|
|
67 |
$zipwriter->add_file_from_string($pathtofileinzip, $mycontent);
|
|
|
68 |
$zipwriter->finish();
|
|
|
69 |
|
|
|
70 |
$pathtozip = $zipwriter->get_path_to_zip();
|
|
|
71 |
$zip = new ZipArchive();
|
|
|
72 |
$opened = $zip->open($pathtozip);
|
|
|
73 |
$this->assertTrue($opened);
|
|
|
74 |
|
|
|
75 |
// Filename that has been sanitized by Zipstream.
|
|
|
76 |
$pathtofileinzip = ltrim($pathtofileinzip, '/');
|
|
|
77 |
|
|
|
78 |
$this->assertEquals($mycontent, $zip->getFromName($pathtofileinzip));
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
/**
|
|
|
82 |
* Test add_file_from_stream().
|
|
|
83 |
*/
|
|
|
84 |
public function test_add_file_from_stream(): void {
|
|
|
85 |
$this->resetAfterTest(true);
|
|
|
86 |
$this->setAdminUser();
|
|
|
87 |
|
|
|
88 |
$course = $this->getDataGenerator()->create_course();
|
|
|
89 |
$assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
|
|
|
90 |
|
|
|
91 |
// Add a file to the intro.
|
|
|
92 |
$filerecord = [
|
|
|
93 |
'contextid' => context_module::instance($assign->cmid)->id,
|
|
|
94 |
'component' => 'mod_assign',
|
|
|
95 |
'filearea' => 'intro',
|
|
|
96 |
'itemid' => 0,
|
|
|
97 |
'filepath' => '/',
|
|
|
98 |
'filename' => 'fileintro.txt',
|
|
|
99 |
];
|
|
|
100 |
$fs = get_file_storage();
|
|
|
101 |
$storedfile = $fs->create_file_from_string($filerecord, 'Contents for the assignment, yeow!');
|
|
|
102 |
|
|
|
103 |
$pathtofileinzip = $storedfile->get_filepath() . $storedfile->get_filename();
|
|
|
104 |
|
|
|
105 |
$zipwriter = archive_writer::get_file_writer('test.zip', archive_writer::ZIP_WRITER);
|
|
|
106 |
$zipwriter->add_file_from_stream($pathtofileinzip, $storedfile->get_content_file_handle());
|
|
|
107 |
$zipwriter->finish();
|
|
|
108 |
|
|
|
109 |
$pathtozip = $zipwriter->get_path_to_zip();
|
|
|
110 |
$zip = new ZipArchive();
|
|
|
111 |
$opened = $zip->open($pathtozip);
|
|
|
112 |
$this->assertTrue($opened);
|
|
|
113 |
|
|
|
114 |
// Filename that has been sanitized by Zipstream.
|
|
|
115 |
$pathtofileinzip = ltrim($pathtofileinzip, '/');
|
|
|
116 |
|
|
|
117 |
$this->assertEquals($storedfile->get_content(), $zip->getFromName($pathtofileinzip));
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Test add_file_from_stored_file().
|
|
|
122 |
*/
|
|
|
123 |
public function test_add_file_from_stored_file(): void {
|
|
|
124 |
$this->resetAfterTest(true);
|
|
|
125 |
$this->setAdminUser();
|
|
|
126 |
|
|
|
127 |
$course = $this->getDataGenerator()->create_course();
|
|
|
128 |
$assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
|
|
|
129 |
|
|
|
130 |
// Add a file to the intro.
|
|
|
131 |
$filerecord = [
|
|
|
132 |
'contextid' => context_module::instance($assign->cmid)->id,
|
|
|
133 |
'component' => 'mod_assign',
|
|
|
134 |
'filearea' => 'intro',
|
|
|
135 |
'itemid' => 0,
|
|
|
136 |
'filepath' => '/',
|
|
|
137 |
'filename' => 'fileintro.txt',
|
|
|
138 |
];
|
|
|
139 |
$fs = get_file_storage();
|
|
|
140 |
$storedfile = $fs->create_file_from_string($filerecord, 'Contents for the assignment, yeow!');
|
|
|
141 |
|
|
|
142 |
$pathtofileinzip = $storedfile->get_filepath() . $storedfile->get_filename();
|
|
|
143 |
|
|
|
144 |
$zipwriter = archive_writer::get_file_writer('test.zip', archive_writer::ZIP_WRITER);
|
|
|
145 |
$zipwriter->add_file_from_stored_file($pathtofileinzip, $storedfile);
|
|
|
146 |
$zipwriter->finish();
|
|
|
147 |
|
|
|
148 |
$pathtozip = $zipwriter->get_path_to_zip();
|
|
|
149 |
$zip = new ZipArchive();
|
|
|
150 |
$opened = $zip->open($pathtozip);
|
|
|
151 |
$this->assertTrue($opened);
|
|
|
152 |
|
|
|
153 |
// Filename that has been sanitized by Zipstream.
|
|
|
154 |
$pathtofileinzip = ltrim($pathtofileinzip, '/');
|
|
|
155 |
|
|
|
156 |
$this->assertEquals($storedfile->get_content(), $zip->getFromName($pathtofileinzip));
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
}
|