ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
LegacyZipTest.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;
26use PHPUnit\Framework\TestCase;
27
31#[BackupGlobals(false)]
32#[BackupStaticProperties(false)]
33#[PreserveGlobalState(false)]
34#[RunTestsInSeparateProcesses]
35class LegacyZipTest extends TestCase
36{
40 private const DIR_TO_ZIP = __DIR__ . '/dir_zip/testing_001';
41 public const ZIPPED_ZIP = 'zipped.zip';
42 protected string $zips_dir = __DIR__ . '/zips/';
43 protected string $unzips_dir = __DIR__ . '/unzips/';
44 private string $extracting_dir = '';
45
46 private string $zip_output_path = '';
47
48 protected function setUp(): void
49 {
50 if (file_exists($this->unzips_dir . self::ZIPPED_ZIP)) {
51 unlink($this->unzips_dir . self::ZIPPED_ZIP);
52 }
53 if (!defined('CLIENT_WEB_DIR')) {
54 define('CLIENT_WEB_DIR', __DIR__);
55 }
56 if (!defined('ILIAS_WEB_DIR')) {
57 define('ILIAS_WEB_DIR', __DIR__);
58 }
59 if (!defined('CLIENT_DATA_DIR')) {
60 define('CLIENT_DATA_DIR', __DIR__);
61 }
62 if (!defined('ILIAS_ABSOLUTE_PATH')) {
63 define('ILIAS_ABSOLUTE_PATH', __DIR__);
64 }
65 if (!defined('CLIENT_ID')) {
66 define('CLIENT_ID', 'client_id');
67 }
68 }
69
70 protected function tearDown(): void
71 {
72 if (!empty($this->extracting_dir) && file_exists($this->extracting_dir)) {
73 $this->recurseRmdir($this->extracting_dir);
74 }
75 if (!empty($this->zip_output_path) && file_exists($this->zip_output_path)) {
76 unlink($this->zip_output_path);
77 }
78 }
79
80 public function testZipAndUnzipWithTop(): void
81 {
82 $legacy = new LegacyArchives();
83 $directory_to_zip = self::DIR_TO_ZIP;
84 $this->zip_output_path = $zip_output_path = $this->zips_dir . self::ZIPPED_ZIP;
85
86 $legacy->zip(
87 $directory_to_zip,
89 true
90 );
91
92 $this->assertFileExists($zip_output_path);
93
94 // unzip
95 $this->extracting_dir = $extracting_dir = $this->unzips_dir . 'extracted';
96 $legacy->unzip(
99 true,
100 false,
101 false
102 );
103
104 $this->assertEquals(
105 $this->pathToArray($directory_to_zip),
106 $this->pathToArray($extracting_dir . '/' . basename($directory_to_zip))
107 );
108
109 // remove zip file
110 unlink($zip_output_path);
111 $this->assertFileDoesNotExist($zip_output_path);
112
113 // remove extracted dir
114 $this->recurseRmdir($extracting_dir);
115 }
116
117 private function pathToArray(string $path): array
118 {
119 $ignore = ['.', '..', '.DS_Store'];
120 $files = new \RecursiveIteratorIterator(
121 new \RecursiveDirectoryIterator($path),
122 \RecursiveIteratorIterator::SELF_FIRST
123 );
124 return array_map(
125 static function ($file) use ($path): string {
126 $real_path = $file->getRealPath();
127
128 return str_replace($path, '', $real_path);
129 },
130 array_values(
131 array_filter(
132 iterator_to_array($files),
133 static fn(\SplFileInfo $file): bool => !in_array($file->getFilename(), $ignore, true)
134 )
135 )
136 );
137 }
138
139 private function recurseRmdir(string $path_to_directory): bool
140 {
141 $files = array_diff(scandir($path_to_directory), ['.', '..']);
142 foreach ($files as $file) {
143 (is_dir("$path_to_directory/$file") && !is_link("$path_to_directory/$file")) ? $this->recurseRmdir(
144 "$path_to_directory/$file"
145 ) : unlink("$path_to_directory/$file");
146 }
147 return rmdir($path_to_directory);
148 }
149
150}
recurseRmdir(string $path_to_directory)
$path
Definition: ltiservices.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...