ILIAS  trunk Revision v12.0_alpha-1338-g8f7e531aa3c
EnsurePathInZipTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\Attributes\DataProvider;
24use PHPUnit\Framework\TestCase;
25use ZipArchive;
26
34final class EnsurePathInZipTest extends TestCase
35{
36 private string $zip_file;
37 private ZipArchive $zip;
38
39 protected function setUp(): void
40 {
41 parent::setUp();
42 $this->zip_file = tempnam(sys_get_temp_dir(), 'irss_zip_test_');
43 $this->zip = new ZipArchive();
44 $this->zip->open($this->zip_file, ZipArchive::OVERWRITE);
45 }
46
47 protected function tearDown(): void
48 {
49 parent::tearDown();
50 if ($this->zip->filename !== '') {
51 @$this->zip->close();
52 }
53 @unlink($this->zip_file);
54 }
55
56 #[DataProvider('filePathProvider')]
57 public function testFilePathHasNoLeadingSlash(string $input, string $expected): void
58 {
59 $method = new \ReflectionMethod(ResourceBuilder::class, 'ensurePathInZIP');
60 $builder = (new \ReflectionClass(ResourceBuilder::class))->newInstanceWithoutConstructor();
61
62 $result = $method->invoke($builder, $this->zip, $input, true);
63
64 $this->assertSame($expected, $result);
65 $this->assertStringStartsNotWith('/', $result);
66 }
67
68 public static function filePathProvider(): \Iterator
69 {
70 yield 'root file, no slash' => ['index.html', 'index.html'];
71 yield 'root file, leading slash' => ['/index.html', 'index.html'];
72 yield 'subdir file, no slash' => ['assets/style.css', 'assets/style.css'];
73 yield 'subdir file, leading slash' => ['/assets/style.css', 'assets/style.css'];
74 yield 'nested file, no slash' => ['assets/css/osd.css', 'assets/css/osd.css'];
75 yield 'nested file, leading slash' => ['/assets/css/osd.css', 'assets/css/osd.css'];
76 }
77
78 public function testDirectoryPathHasNoLeadingSlash(): void
79 {
80 $method = new \ReflectionMethod(ResourceBuilder::class, 'ensurePathInZIP');
81 $builder = (new \ReflectionClass(ResourceBuilder::class))->newInstanceWithoutConstructor();
82
83 $result = $method->invoke($builder, $this->zip, '/dir/sub', false);
84
85 $this->assertStringStartsNotWith('/', $result);
86 $this->assertSame('dir/sub/', $result);
87 }
88
90 {
91 $method = new \ReflectionMethod(ResourceBuilder::class, 'ensurePathInZIP');
92 $builder = (new \ReflectionClass(ResourceBuilder::class))->newInstanceWithoutConstructor();
93
94 $entry = $method->invoke($builder, $this->zip, '/index.html', true);
95 $this->zip->addFromString($entry, '<html></html>');
96 $this->zip->close();
97
98 $verify = new ZipArchive();
99 $verify->open($this->zip_file);
100 $names = [];
101 for ($i = 0; $i < $verify->numFiles; $i++) {
102 $names[] = $verify->getNameIndex($i);
103 }
104 $verify->close();
105
106 foreach ($names as $name) {
107 $this->assertStringStartsNotWith('/', $name, "ZIP entry must not start with '/': {$name}");
108 }
109 $this->assertContains('index.html', $names);
110 }
111}
Regression tests for Mantis 0045580 / 0047237: entries written to a ZIP container must not start with...
testFilePathHasNoLeadingSlash(string $input, string $expected)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...