ILIAS  trunk Revision v11.0_alpha-1715-g7fc467680fb
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
UnzipTest.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\Filesystem\Util;
20 
32 
36 #[BackupGlobals(false)]
37 #[BackupStaticProperties(false)]
38 #[PreserveGlobalState(false)]
39 #[RunTestsInSeparateProcesses]
40 class 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->assertEquals($has_multiple_root_entries, $unzip->hasMultipleRootEntriesInZip());
75  $this->assertEquals($expected_amount_directories, $unzip->getAmountOfDirectories());
76  $this->assertEquals($expected_directories, iterator_to_array($unzip->getDirectories()));
77  $this->assertEquals($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->assertTrue(preg_match('~[^\x20-\x7E\t\r\n]~', $one_file->getContents()) > 0);
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->assertEquals(0, iterator_count($unzip->getFiles()));
95  $this->assertEquals(0, iterator_count($unzip->getDirectories()));
96  $this->assertEquals(0, iterator_count($unzip->getPaths()));
97  $this->assertEquals([], iterator_to_array($unzip->getDirectories()));
98  $this->assertEquals([], 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(): array
218  {
219  return [
220  ['1_folder_mac.zip', false, 10, self::$directories_one, 15, self::$files_one],
221  ['1_folder_win.zip', false, 10, self::$directories_one, 15, self::$files_one],
222  ['3_folders_mac.zip', true, 9, self::$directories_three, 12, self::$files_three],
223  ['3_folders_win.zip', true, 9, self::$directories_three, 12, self::$files_three],
224  ['1_folder_1_file_mac.zip', true, 3, self::$directories_mixed, 5, self::$files_mixed]
225  ];
226  }
227 
228  protected static array $files_mixed = [
229  0 => '03_Test.pdf',
230  1 => 'Ordner A/01_Test.pdf',
231  2 => 'Ordner A/02_Test.pdf',
232  3 => 'Ordner A/Ordner A_2/07_Test.pdf',
233  4 => 'Ordner A/Ordner A_2/08_Test.pdf'
234  ];
235 
236  protected static array $directories_mixed = [
237  0 => 'Ordner A/',
238  1 => 'Ordner A/Ordner A_1/',
239  2 => 'Ordner A/Ordner A_2/'
240  ];
241 
242  protected static array $directories_one = [
243  0 => 'Ordner 0/',
244  1 => 'Ordner 0/Ordner A/',
245  2 => 'Ordner 0/Ordner A/Ordner A_1/',
246  3 => 'Ordner 0/Ordner A/Ordner A_2/',
247  4 => 'Ordner 0/Ordner B/',
248  5 => 'Ordner 0/Ordner B/Ordner B_1/',
249  6 => 'Ordner 0/Ordner B/Ordner B_2/',
250  7 => 'Ordner 0/Ordner C/',
251  8 => 'Ordner 0/Ordner C/Ordner C_1/',
252  9 => 'Ordner 0/Ordner C/Ordner C_2/'
253  ];
254  protected static array $directories_three = [
255  0 => 'Ordner A/',
256  1 => 'Ordner A/Ordner A_1/',
257  2 => 'Ordner A/Ordner A_2/',
258  3 => 'Ordner B/',
259  4 => 'Ordner B/Ordner B_1/',
260  5 => 'Ordner B/Ordner B_2/',
261  6 => 'Ordner C/',
262  7 => 'Ordner C/Ordner C_1/',
263  8 => 'Ordner C/Ordner C_2/'
264  ];
265 
266  protected static array $files_one = [
267  0 => 'Ordner 0/13_Test.pdf',
268  1 => 'Ordner 0/14_Test.pdf',
269  2 => 'Ordner 0/15_Test.pdf',
270  3 => 'Ordner 0/Ordner A/01_Test.pdf',
271  4 => 'Ordner 0/Ordner A/02_Test.pdf',
272  5 => 'Ordner 0/Ordner A/Ordner A_2/07_Test.pdf',
273  6 => 'Ordner 0/Ordner A/Ordner A_2/08_Test.pdf',
274  7 => 'Ordner 0/Ordner B/03_Test.pdf',
275  8 => 'Ordner 0/Ordner B/04_Test.pdf',
276  9 => 'Ordner 0/Ordner B/Ordner B_2/09_Test.pdf',
277  10 => 'Ordner 0/Ordner B/Ordner B_2/10_Test.pdf',
278  11 => 'Ordner 0/Ordner C/05_Test.pdf',
279  12 => 'Ordner 0/Ordner C/06_Test.pdf',
280  13 => 'Ordner 0/Ordner C/Ordner C_2/11_Test.pdf',
281  14 => 'Ordner 0/Ordner C/Ordner C_2/12_Test.pdf'
282  ];
283 
284  protected static array $files_three = [
285  0 => 'Ordner A/01_Test.pdf',
286  1 => 'Ordner A/02_Test.pdf',
287  2 => 'Ordner A/Ordner A_2/07_Test.pdf',
288  3 => 'Ordner A/Ordner A_2/08_Test.pdf',
289  4 => 'Ordner B/03_Test.pdf',
290  5 => 'Ordner B/04_Test.pdf',
291  6 => 'Ordner B/Ordner B_2/09_Test.pdf',
292  7 => 'Ordner B/Ordner B_2/10_Test.pdf',
293  8 => 'Ordner C/05_Test.pdf',
294  9 => 'Ordner C/06_Test.pdf',
295  10 => 'Ordner C/Ordner C_2/11_Test.pdf',
296  11 => 'Ordner C/Ordner C_2/12_Test.pdf',
297  ];
298 
299  protected static array $top_directory_tree = [
300  0 => '3_folders_mac/',
301  1 => '3_folders_mac/Ordner A/',
302  2 => '3_folders_mac/Ordner A/01_Test.pdf',
303  3 => '3_folders_mac/Ordner A/02_Test.pdf',
304  4 => '3_folders_mac/Ordner A/Ordner A_1/',
305  5 => '3_folders_mac/Ordner A/Ordner A_2/',
306  6 => '3_folders_mac/Ordner A/Ordner A_2/07_Test.pdf',
307  7 => '3_folders_mac/Ordner A/Ordner A_2/08_Test.pdf',
308  8 => '3_folders_mac/Ordner B/',
309  9 => '3_folders_mac/Ordner B/03_Test.pdf',
310  10 => '3_folders_mac/Ordner B/04_Test.pdf',
311  11 => '3_folders_mac/Ordner B/Ordner B_1/',
312  12 => '3_folders_mac/Ordner B/Ordner B_2/',
313  13 => '3_folders_mac/Ordner B/Ordner B_2/09_Test.pdf',
314  14 => '3_folders_mac/Ordner B/Ordner B_2/10_Test.pdf',
315  15 => '3_folders_mac/Ordner C/',
316  16 => '3_folders_mac/Ordner C/05_Test.pdf',
317  17 => '3_folders_mac/Ordner C/06_Test.pdf',
318  18 => '3_folders_mac/Ordner C/Ordner C_1/',
319  19 => '3_folders_mac/Ordner C/Ordner C_2/',
320  20 => '3_folders_mac/Ordner C/Ordner C_2/11_Test.pdf',
321  21 => '3_folders_mac/Ordner C/Ordner C_2/12_Test.pdf',
322  ];
323 
324  private static array $expected_flat_files = [
325  0 => '01_Test.pdf',
326  1 => '02_Test.pdf',
327  2 => '03_Test.pdf',
328  3 => '04_Test.pdf',
329  4 => '05_Test.pdf',
330  5 => '06_Test.pdf',
331  6 => '07_Test.pdf',
332  7 => '08_Test.pdf',
333  8 => '09_Test.pdf',
334  9 => '10_Test.pdf',
335  10 => '11_Test.pdf',
336  11 => '12_Test.pdf',
337  ];
338 }
static array static array $directories_mixed
Definition: UnzipTest.php:236
static array static array static array static array static array static array $files_three
Definition: UnzipTest.php:284
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
static array static array static array static array static array static array static array static array $expected_flat_files
Definition: UnzipTest.php:324
static array static array static array static array $directories_three
Definition: UnzipTest.php:254
recurseRmdir(string $path_to_directory)
Definition: UnzipTest.php:183
static array static array static array $directories_one
Definition: UnzipTest.php:242
static array static array static array static array static array static array static array $top_directory_tree
Definition: UnzipTest.php:299
static array static array static array static array static array $files_one
Definition: UnzipTest.php:266
sort()
description: > Example for rendering a Sort Glyph.
Definition: sort.php:41
directoryToArray(string $path_to_directory)
Definition: UnzipTest.php:197
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...