ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
UnzipTest.php
Go to the documentation of this file.
1<?php
2
19namespace ILIAS\Filesystem\Util;
20
21use PHPUnit\Framework\Attributes\BackupGlobals;
22use PHPUnit\Framework\Attributes\BackupStaticProperties;
23use PHPUnit\Framework\Attributes\PreserveGlobalState;
24use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
25use PHPUnit\Framework\Attributes\DataProvider;
30use PHPUnit\Framework\TestCase;
32
36#[BackupGlobals(false)]
37#[BackupStaticProperties(false)]
38#[PreserveGlobalState(false)]
39#[RunTestsInSeparateProcesses]
40class UnzipTest extends TestCase
41{
42 protected string $zips_dir = __DIR__ . '/zips/';
43 protected string $unzips_dir = __DIR__ . '/unzips/';
44
45 protected function tearDown(): void
46 {
47 if (file_exists($this->unzips_dir)) {
48 rmdir($this->unzips_dir);
49 }
50 }
51
56 #[DataProvider('getZips')]
57 public function testUnzip(
58 string $zip,
59 bool $has_multiple_root_entries,
60 int $expected_amount_directories,
61 array $expected_directories,
62 int $expected_amount_files,
63 array $expected_files
64 ): void {
65 $this->assertStringContainsString('.zip', $zip);
66 $zip_path = $this->zips_dir . $zip;
67 $this->assertFileExists($zip_path);
68
69 $stream = Streams::ofResource(fopen($zip_path, 'rb'));
70 $options = new UnzipOptions();
71 $unzip = new Unzip($options, $stream);
72
73 $this->assertFalse($unzip->hasZipReadingError());
74 $this->assertSame($has_multiple_root_entries, $unzip->hasMultipleRootEntriesInZip());
75 $this->assertSame($expected_amount_directories, $unzip->getAmountOfDirectories());
76 $this->assertEquals($expected_directories, iterator_to_array($unzip->getDirectories()));
77 $this->assertSame($expected_amount_files, $unzip->getAmountOfFiles());
78 $this->assertEquals($expected_files, iterator_to_array($unzip->getFiles()));
79
81 $one_file = iterator_to_array($unzip->getFileStreams())[0];
82
83 // check if is binary
84 $this->assertGreaterThan(0, preg_match('~[^\x20-\x7E\t\r\n]~', $one_file->getContents()));
85 }
86
87 public function testWrongZip(): void
88 {
89 $stream = Streams::ofResource(fopen(__FILE__, 'rb'));
90 $options = new UnzipOptions();
91 $unzip = new Unzip($options, $stream);
92 $this->assertTrue($unzip->hasZipReadingError());
93 $this->assertFalse($unzip->hasMultipleRootEntriesInZip());
94 $this->assertCount(0, iterator_to_array($unzip->getFiles()));
95 $this->assertCount(0, iterator_to_array($unzip->getDirectories()));
96 $this->assertCount(0, iterator_to_array($unzip->getPaths()));
97 $this->assertSame([], iterator_to_array($unzip->getDirectories()));
98 $this->assertSame([], iterator_to_array($unzip->getFiles()));
99 }
100
105 #[DataProvider('getZips')]
106 public function testLegacyUnzip(
107 string $zip,
108 bool $has_multiple_root_entries,
109 int $expected_amount_directories,
110 array $expected_directories,
111 int $expected_amount_files,
112 array $expected_files
113 ): void {
114 $legacy = new LegacyArchives();
115
116 $this->assertStringContainsString('.zip', $zip);
117 $zip_path = $this->zips_dir . $zip;
118 $this->assertFileExists($zip_path);
119
120 $temp_unzip_path = $this->unzips_dir . uniqid('unzip', true);
121
122 $return = $legacy->unzip(
123 $zip_path,
124 $temp_unzip_path
125 );
126
127 $this->assertTrue($return);
128
129 $unzipped_files = $this->directoryToArray($temp_unzip_path);
130 $expected_paths = array_merge($expected_directories, $expected_files);
131 sort($expected_paths);
132 $this->assertEquals($expected_paths, $unzipped_files);
133 $this->assertTrue($this->recurseRmdir($temp_unzip_path));
134 }
135
136 public function testEnsureTopDirectory(): void
137 {
138 $legacy = new LegacyArchives();
139 $zip_path = $this->zips_dir . '3_folders_mac.zip';
140 $this->assertFileExists($zip_path);
141
142 $temp_unzip_path = $this->unzips_dir . uniqid('unzip', true);
143
144 $return = $legacy->unzip(
145 $zip_path,
146 $temp_unzip_path,
147 false,
148 false,
149 true
150 );
151
152 $this->assertTrue($return);
153
154 $unzipped_files = $this->directoryToArray($temp_unzip_path);
155
156 $this->assertSame(self::$top_directory_tree, $unzipped_files);
157 $this->assertTrue($this->recurseRmdir($temp_unzip_path));
158 }
159
160 public function testFlatLegacyUnzip(): void
161 {
162 $legacy = new LegacyArchives();
163 $zip_path = $this->zips_dir . '3_folders_mac.zip';
164 $this->assertFileExists($zip_path);
165
166 $temp_unzip_path = $this->unzips_dir . uniqid('unzip', true);
167
168 $return = $legacy->unzip(
169 $zip_path,
170 $temp_unzip_path,
171 false,
172 true
173 );
174
175 $this->assertTrue($return);
176
177 $unzipped_files = $this->directoryToArray($temp_unzip_path);
178
179 $this->assertSame(self::$expected_flat_files, $unzipped_files);
180 $this->assertTrue($this->recurseRmdir($temp_unzip_path));
181 }
182
183 private function recurseRmdir(string $path_to_directory): bool
184 {
185 $files = array_diff(scandir($path_to_directory), ['.', '..']);
186 foreach ($files as $file) {
187 (is_dir("$path_to_directory/$file") && !is_link("$path_to_directory/$file")) ? $this->recurseRmdir(
188 "$path_to_directory/$file"
189 ) : unlink("$path_to_directory/$file");
190 }
191 return rmdir($path_to_directory);
192 }
193
197 private function directoryToArray(string $path_to_directory): array
198 {
199 $iterator = new \RecursiveIteratorIterator(
200 new \RecursiveDirectoryIterator($path_to_directory, \RecursiveDirectoryIterator::SKIP_DOTS),
201 \RecursiveIteratorIterator::SELF_FIRST,
202 \RecursiveIteratorIterator::CATCH_GET_CHILD
203 );
204 $paths = [];
205 foreach ($iterator as $item) {
206 $relative_path = str_replace($path_to_directory . '/', '', $item->getPathname());
207 $paths[] = $item->isDir() ? $relative_path . '/' : $relative_path;
208 }
209
210 sort($paths);
211
212 return $paths;
213 }
214
215 // PROVIDERS
216
217 public static function getZips(): \Iterator
218 {
219 yield ['1_folder_mac.zip', false, 10, self::$directories_one, 15, self::$files_one];
220 yield ['1_folder_win.zip', false, 10, self::$directories_one, 15, self::$files_one];
221 yield ['3_folders_mac.zip', true, 9, self::$directories_three, 12, self::$files_three];
222 yield ['3_folders_win.zip', true, 9, self::$directories_three, 12, self::$files_three];
223 yield ['1_folder_1_file_mac.zip', true, 3, self::$directories_mixed, 5, self::$files_mixed];
224 }
225
226 protected static array $files_mixed = [
227 0 => '03_Test.pdf',
228 1 => 'Ordner A/01_Test.pdf',
229 2 => 'Ordner A/02_Test.pdf',
230 3 => 'Ordner A/Ordner A_2/07_Test.pdf',
231 4 => 'Ordner A/Ordner A_2/08_Test.pdf'
232 ];
233
234 protected static array $directories_mixed = [
235 0 => 'Ordner A/',
236 1 => 'Ordner A/Ordner A_1/',
237 2 => 'Ordner A/Ordner A_2/'
238 ];
239
240 protected static array $directories_one = [
241 0 => 'Ordner 0/',
242 1 => 'Ordner 0/Ordner A/',
243 2 => 'Ordner 0/Ordner A/Ordner A_1/',
244 3 => 'Ordner 0/Ordner A/Ordner A_2/',
245 4 => 'Ordner 0/Ordner B/',
246 5 => 'Ordner 0/Ordner B/Ordner B_1/',
247 6 => 'Ordner 0/Ordner B/Ordner B_2/',
248 7 => 'Ordner 0/Ordner C/',
249 8 => 'Ordner 0/Ordner C/Ordner C_1/',
250 9 => 'Ordner 0/Ordner C/Ordner C_2/'
251 ];
252 protected static array $directories_three = [
253 0 => 'Ordner A/',
254 1 => 'Ordner A/Ordner A_1/',
255 2 => 'Ordner A/Ordner A_2/',
256 3 => 'Ordner B/',
257 4 => 'Ordner B/Ordner B_1/',
258 5 => 'Ordner B/Ordner B_2/',
259 6 => 'Ordner C/',
260 7 => 'Ordner C/Ordner C_1/',
261 8 => 'Ordner C/Ordner C_2/'
262 ];
263
264 protected static array $files_one = [
265 0 => 'Ordner 0/13_Test.pdf',
266 1 => 'Ordner 0/14_Test.pdf',
267 2 => 'Ordner 0/15_Test.pdf',
268 3 => 'Ordner 0/Ordner A/01_Test.pdf',
269 4 => 'Ordner 0/Ordner A/02_Test.pdf',
270 5 => 'Ordner 0/Ordner A/Ordner A_2/07_Test.pdf',
271 6 => 'Ordner 0/Ordner A/Ordner A_2/08_Test.pdf',
272 7 => 'Ordner 0/Ordner B/03_Test.pdf',
273 8 => 'Ordner 0/Ordner B/04_Test.pdf',
274 9 => 'Ordner 0/Ordner B/Ordner B_2/09_Test.pdf',
275 10 => 'Ordner 0/Ordner B/Ordner B_2/10_Test.pdf',
276 11 => 'Ordner 0/Ordner C/05_Test.pdf',
277 12 => 'Ordner 0/Ordner C/06_Test.pdf',
278 13 => 'Ordner 0/Ordner C/Ordner C_2/11_Test.pdf',
279 14 => 'Ordner 0/Ordner C/Ordner C_2/12_Test.pdf'
280 ];
281
282 protected static array $files_three = [
283 0 => 'Ordner A/01_Test.pdf',
284 1 => 'Ordner A/02_Test.pdf',
285 2 => 'Ordner A/Ordner A_2/07_Test.pdf',
286 3 => 'Ordner A/Ordner A_2/08_Test.pdf',
287 4 => 'Ordner B/03_Test.pdf',
288 5 => 'Ordner B/04_Test.pdf',
289 6 => 'Ordner B/Ordner B_2/09_Test.pdf',
290 7 => 'Ordner B/Ordner B_2/10_Test.pdf',
291 8 => 'Ordner C/05_Test.pdf',
292 9 => 'Ordner C/06_Test.pdf',
293 10 => 'Ordner C/Ordner C_2/11_Test.pdf',
294 11 => 'Ordner C/Ordner C_2/12_Test.pdf',
295 ];
296
297 protected static array $top_directory_tree = [
298 0 => '3_folders_mac/',
299 1 => '3_folders_mac/Ordner A/',
300 2 => '3_folders_mac/Ordner A/01_Test.pdf',
301 3 => '3_folders_mac/Ordner A/02_Test.pdf',
302 4 => '3_folders_mac/Ordner A/Ordner A_1/',
303 5 => '3_folders_mac/Ordner A/Ordner A_2/',
304 6 => '3_folders_mac/Ordner A/Ordner A_2/07_Test.pdf',
305 7 => '3_folders_mac/Ordner A/Ordner A_2/08_Test.pdf',
306 8 => '3_folders_mac/Ordner B/',
307 9 => '3_folders_mac/Ordner B/03_Test.pdf',
308 10 => '3_folders_mac/Ordner B/04_Test.pdf',
309 11 => '3_folders_mac/Ordner B/Ordner B_1/',
310 12 => '3_folders_mac/Ordner B/Ordner B_2/',
311 13 => '3_folders_mac/Ordner B/Ordner B_2/09_Test.pdf',
312 14 => '3_folders_mac/Ordner B/Ordner B_2/10_Test.pdf',
313 15 => '3_folders_mac/Ordner C/',
314 16 => '3_folders_mac/Ordner C/05_Test.pdf',
315 17 => '3_folders_mac/Ordner C/06_Test.pdf',
316 18 => '3_folders_mac/Ordner C/Ordner C_1/',
317 19 => '3_folders_mac/Ordner C/Ordner C_2/',
318 20 => '3_folders_mac/Ordner C/Ordner C_2/11_Test.pdf',
319 21 => '3_folders_mac/Ordner C/Ordner C_2/12_Test.pdf',
320 ];
321
322 private static array $expected_flat_files = [
323 0 => '01_Test.pdf',
324 1 => '02_Test.pdf',
325 2 => '03_Test.pdf',
326 3 => '04_Test.pdf',
327 4 => '05_Test.pdf',
328 5 => '06_Test.pdf',
329 6 => '07_Test.pdf',
330 7 => '08_Test.pdf',
331 8 => '09_Test.pdf',
332 9 => '10_Test.pdf',
333 10 => '11_Test.pdf',
334 11 => '12_Test.pdf',
335 ];
336}
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
Definition: Streams.php:64
directoryToArray(string $path_to_directory)
Definition: UnzipTest.php:197
recurseRmdir(string $path_to_directory)
Definition: UnzipTest.php:183
testLegacyUnzip(string $zip, bool $has_multiple_root_entries, int $expected_amount_directories, array $expected_directories, int $expected_amount_files, array $expected_files)
Definition: UnzipTest.php:106
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...