ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
CustomIconTempUploadPathTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use org\bovigo\vfs;
23use PHPUnit\Framework\TestCase;
24use PHPUnit\Framework\Attributes\DataProvider;
25
26class CustomIconTempUploadPathTest extends TestCase
27{
28 private function vfsStreamIsAvailable(): bool
29 {
30 return class_exists(vfs\vfsStreamWrapper::class);
31 }
32
33 private function skipIfVfsStreamNotAvailable(): void
34 {
35 if (!$this->vfsStreamIsAvailable()) {
36 $this->markTestSkipped(
37 'vfsStream (https://github.com/bovigo/vfsStream) is required for virtual filesystem tests.'
38 );
39 }
40 }
41
42 private function buildTestingObject(
43 string $temp_file_name,
44 string $ilias_data_dir
46 return new class (
47 $temp_file_name,
48 $ilias_data_dir
49 ) extends CustomIconTempUploadPath {
50 #[\Override]
51 protected function getRealPath(
52 string $path
53 ): string|false {
54 $normalized = str_replace('\\', '/', $path);
55 if (is_dir($path)) {
56 return rtrim($normalized, '/');
57 }
58
59 if (is_file($path)) {
60 return $normalized;
61 }
62
63 return false;
64 }
65 };
66 }
67
69 {
70 $this->skipIfVfsStreamNotAvailable();
71
72 vfs\vfsStream::setup();
73 vfs\vfsStream::create([
74 'data' => [
75 'temp' => [
76 'icon_upload.svg' => '<svg xmlns="http://www.w3.org/2000/svg"/>',
77 ],
78 ],
79 ]);
80
81 $data_dir = vfs\vfsStream::url('root/data');
82 $expected_file = vfs\vfsStream::url('root/data/temp/icon_upload.svg');
83
84 $path = $this->buildTestingObject(
85 'icon_upload.svg',
87 );
88
89 self::assertSame($expected_file, $path->getAbsolutePath());
90 }
91
93 {
94 $this->skipIfVfsStreamNotAvailable();
95
96 vfs\vfsStream::setup();
97 vfs\vfsStream::create([
98 'data' => [
99 'secret.txt' => 'sensitive',
100 'temp' => [],
101 ],
102 ]);
103
104 $data_dir = vfs\vfsStream::url('root/data');
105
106 $this->expectException(InvalidArgumentException::class);
107 $this->expectExceptionMessage('Temporary upload file not found.');
108
109 $this->buildTestingObject(
110 '../secret.txt',
112 );
113 }
114
116 {
117 $this->skipIfVfsStreamNotAvailable();
118
119 vfs\vfsStream::setup();
120 vfs\vfsStream::create([
121 'data' => [
122 'temp' => [],
123 ],
124 ]);
125
126 $data_dir = vfs\vfsStream::url('root/data');
127
128 $this->expectException(InvalidArgumentException::class);
129 $this->expectExceptionMessage('Temporary upload file not found.');
130
131 $this->buildTestingObject(
132 'missing.svg',
134 );
135 }
136
137 #[DataProvider('invalidTemporaryFileNameProvider')]
139 string $malicious_input
140 ): void {
141 $this->expectException(InvalidArgumentException::class);
142 $this->expectExceptionMessage('Invalid temporary upload file name.');
143
144 $this->buildTestingObject(
145 $malicious_input,
146 '/does/not/matter'
147 );
148 }
149
153 public static function invalidTemporaryFileNameProvider(): Generator
154 {
155 yield 'parent directory segment' => ['..'];
156 yield 'current directory segment' => ['.'];
157 }
158}
testInvalidTemporaryFileNameIsRejected(string $malicious_input)
buildTestingObject(string $temp_file_name, string $ilias_data_dir)
Resolves a user-supplied temp file identifier to an absolute path that is guaranteed to refer to a re...
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$path
Definition: ltiservices.php:30