ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
UnzipTest.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\Filesystem\Util;
20 
29 
38 class UnzipTest extends TestCase
39 {
40  protected string $zips_dir = __DIR__ . '/zips/';
41  protected string $unzips_dir = __DIR__ . '/unzips/';
42 
43  protected function tearDown(): void
44  {
45  if (file_exists($this->unzips_dir)) {
46  rmdir($this->unzips_dir);
47  }
48  }
49 
55  public function testUnzip(
56  string $zip,
57  bool $has_multiple_root_entries,
58  int $expected_amount_directories,
59  array $expected_directories,
60  int $expected_amount_files,
61  array $expected_files
62  ): void {
63  $this->assertStringContainsString('.zip', $zip);
64  $zip_path = $this->zips_dir . $zip;
65  $this->assertFileExists($zip_path);
66 
67  $stream = Streams::ofResource(fopen($zip_path, 'rb'));
68  $options = new UnzipOptions();
69  $unzip = new Unzip($options, $stream);
70 
71  $this->assertFalse($unzip->hasZipReadingError());
72  $this->assertEquals($has_multiple_root_entries, $unzip->hasMultipleRootEntriesInZip());
73  $this->assertEquals($expected_amount_directories, $unzip->getAmountOfDirectories());
74  $this->assertEquals($expected_directories, iterator_to_array($unzip->getDirectories()));
75  $this->assertEquals($expected_amount_files, $unzip->getAmountOfFiles());
76  $this->assertEquals($expected_files, iterator_to_array($unzip->getFiles()));
77 
79  $one_file = iterator_to_array($unzip->getFileStreams())[0];
80 
81  // check if is binary
82  $this->assertTrue(preg_match('~[^\x20-\x7E\t\r\n]~', $one_file->getContents()) > 0);
83  }
84 
85  public function testWrongZip(): void
86  {
87  $stream = Streams::ofResource(fopen(__FILE__, 'rb'));
88  $options = new UnzipOptions();
89  $unzip = new Unzip($options, $stream);
90  $this->assertTrue($unzip->hasZipReadingError());
91  $this->assertFalse($unzip->hasMultipleRootEntriesInZip());
92  $this->assertEquals(0, iterator_count($unzip->getFiles()));
93  $this->assertEquals(0, iterator_count($unzip->getDirectories()));
94  $this->assertEquals(0, iterator_count($unzip->getPaths()));
95  $this->assertEquals([], iterator_to_array($unzip->getDirectories()));
96  $this->assertEquals([], iterator_to_array($unzip->getFiles()));
97  }
98 
104  public function testLegacyUnzip(
105  string $zip,
106  bool $has_multiple_root_entries,
107  int $expected_amount_directories,
108  array $expected_directories,
109  int $expected_amount_files,
110  array $expected_files
111  ): void {
112  $legacy = new LegacyArchives();
113 
114  $this->assertStringContainsString('.zip', $zip);
115  $zip_path = $this->zips_dir . $zip;
116  $this->assertFileExists($zip_path);
117 
118  $temp_unzip_path = $this->unzips_dir . uniqid('unzip', true);
119 
120  $return = $legacy->unzip(
121  $zip_path,
122  $temp_unzip_path
123  );
124 
125  $this->assertTrue($return);
126 
127  $unzipped_files = $this->directoryToArray($temp_unzip_path);
128  $expected_paths = array_merge($expected_directories, $expected_files);
129  sort($expected_paths);
130  $this->assertEquals($expected_paths, $unzipped_files);
131  $this->assertTrue($this->recurseRmdir($temp_unzip_path));
132  }
133 
134  public function testEnsureTopDirectory(): void
135  {
136  $legacy = new LegacyArchives();
137  $zip_path = $this->zips_dir . '3_folders_mac.zip';
138  $this->assertFileExists($zip_path);
139 
140  $temp_unzip_path = $this->unzips_dir . uniqid('unzip', true);
141 
142  $return = $legacy->unzip(
143  $zip_path,
144  $temp_unzip_path,
145  false,
146  false,
147  true
148  );
149 
150  $this->assertTrue($return);
151 
152  $unzipped_files = $this->directoryToArray($temp_unzip_path);
153 
154  $this->assertSame(self::$top_directory_tree, $unzipped_files);
155  $this->assertTrue($this->recurseRmdir($temp_unzip_path));
156  }
157 
158  public function testFlatLegacyUnzip(): void
159  {
160  $legacy = new LegacyArchives();
161  $zip_path = $this->zips_dir . '3_folders_mac.zip';
162  $this->assertFileExists($zip_path);
163 
164  $temp_unzip_path = $this->unzips_dir . uniqid('unzip', true);
165 
166  $return = $legacy->unzip(
167  $zip_path,
168  $temp_unzip_path,
169  false,
170  true
171  );
172 
173  $this->assertTrue($return);
174 
175  $unzipped_files = $this->directoryToArray($temp_unzip_path);
176 
177  $this->assertSame(self::$expected_flat_files, $unzipped_files);
178  $this->assertTrue($this->recurseRmdir($temp_unzip_path));
179  }
180 
181  private function recurseRmdir(string $path_to_directory): bool
182  {
183  $files = array_diff(scandir($path_to_directory), ['.', '..']);
184  foreach ($files as $file) {
185  (is_dir("$path_to_directory/$file") && !is_link("$path_to_directory/$file")) ? $this->recurseRmdir(
186  "$path_to_directory/$file"
187  ) : unlink("$path_to_directory/$file");
188  }
189  return rmdir($path_to_directory);
190  }
191 
195  private function directoryToArray(string $path_to_directory): array
196  {
197  $iterator = new \RecursiveIteratorIterator(
198  new \RecursiveDirectoryIterator($path_to_directory, \RecursiveDirectoryIterator::SKIP_DOTS),
199  \RecursiveIteratorIterator::SELF_FIRST,
200  \RecursiveIteratorIterator::CATCH_GET_CHILD
201  );
202  $paths = [];
203  foreach ($iterator as $item) {
204  $relative_path = str_replace($path_to_directory . '/', '', $item->getPathname());
205  $paths[] = $item->isDir() ? $relative_path . '/' : $relative_path;
206  }
207 
208  sort($paths);
209 
210  return $paths;
211  }
212 
213  // PROVIDERS
214 
215  public static function getZips(): array
216  {
217  return [
218  ['1_folder_mac.zip', false, 10, self::$directories_one, 15, self::$files_one],
219  ['1_folder_win.zip', false, 10, self::$directories_one, 15, self::$files_one],
220  ['3_folders_mac.zip', true, 9, self::$directories_three, 12, self::$files_three],
221  ['3_folders_win.zip', true, 9, self::$directories_three, 12, self::$files_three],
222  ['1_folder_1_file_mac.zip', true, 3, self::$directories_mixed, 5, self::$files_mixed]
223  ];
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 }
static array static array $directories_mixed
Definition: UnzipTest.php:234
static array static array static array static array static array static array $files_three
Definition: UnzipTest.php:282
testLegacyUnzip(string $zip, bool $has_multiple_root_entries, int $expected_amount_directories, array $expected_directories, int $expected_amount_files, array $expected_files)
getZips
Definition: UnzipTest.php:104
static array static array static array static array static array static array static array static array $expected_flat_files
Definition: UnzipTest.php:322
static array static array static array static array $directories_three
Definition: UnzipTest.php:252
recurseRmdir(string $path_to_directory)
Definition: UnzipTest.php:181
static array static array static array $directories_one
Definition: UnzipTest.php:240
static array static array static array static array static array static array static array $top_directory_tree
Definition: UnzipTest.php:297
static array static array static array static array static array $files_one
Definition: UnzipTest.php:264
directoryToArray(string $path_to_directory)
Definition: UnzipTest.php:195
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...