19 declare(strict_types=1);
41 protected function setUp(): void
44 $this->dic_backup = is_object($DIC) ? clone
$DIC : null;
46 $DIC = new \ILIAS\DI\Container();
47 $DIC[
'ilDB'] = $this->db_mock = $this->createMock(ilDBInterface::class);
58 $settings = $this->createMock(ilFileServicesSettings::class);
59 $settings->expects($this->once())
60 ->method(
'getWhiteListedSuffixes')
61 ->willReturn([
'pdf',
'jpg']);
64 $this->assertTrue($sanitizer->isClean(
'/lib/test.pdf'));
65 $this->assertFalse($sanitizer->isClean(
'/lib/test.xml'));
66 $this->assertEquals(
'/lib/testxml.sec', $sanitizer->sanitize(
'/lib/test.xml'));
71 $settings = $this->createMock(ilFileServicesSettings::class);
72 $settings->expects($this->once())
73 ->method(
'getBlackListedSuffixes')
74 ->willReturn([
'pdf']);
76 $settings->expects($this->once())
77 ->method(
'isByPassAllowedForCurrentUser')
80 $stream = $this->createMock(FileStream::class);
81 $meta =
new Metadata(
'filename.pdf', 42,
'application/pdf');
88 $status = $processor->process($stream, $meta);
89 $this->assertEquals(ProcessingStatus::REJECTED, $status->getCode());
94 $settings = $this->createMock(ilFileServicesSettings::class);
95 $settings->expects($this->once())
96 ->method(
'getBlackListedSuffixes')
97 ->willReturn([
'pdf']);
99 $settings->expects($this->once())
100 ->method(
'isByPassAllowedForCurrentUser')
103 $stream = $this->createMock(FileStream::class);
104 $meta =
new Metadata(
'filename.pdf', 42,
'application/pdf');
111 $status = $processor->process($stream, $meta);
112 $this->assertEquals(ProcessingStatus::OK, $status->getCode());
117 $settings = $this->createMock(ilFileServicesSettings::class);
118 $settings->expects($this->once())
119 ->method(
'getWhiteListedSuffixes')
120 ->willReturn([
'pdf',
'png',
'jpg']);
124 $sane_filename =
'bellerophon.pdf';
125 $this->assertEquals($sane_filename, $sanitizer->sanitize($sane_filename));
127 $insane_filename =
'bellerophon.docx';
128 $this->assertNotEquals($insane_filename, $sanitizer->sanitize($insane_filename));
129 $this->assertEquals(
'bellerophondocx.sec', $sanitizer->sanitize($insane_filename));
134 $settings_mock = $this->createMock(ilSetting::class);
135 $ini_mock = $this->createMock(ilIniFile::class);
139 $this->db_mock->expects($this->once())
140 ->method(
'fetchObject')
143 $this->db_mock->expects($this->once())
144 ->method(
'fetchAssoc')
147 $default_whitelist = include __DIR__ .
"/../defaults/default_whitelist.php";
151 [
'suffix_custom_expl_black',
'bl001,bl002'],
152 [
'suffix_repl_additional',
'docx,doc'],
153 [
'suffix_custom_white_list',
'wl001,wl002']
156 ->expects($this->exactly(3))
158 ->willReturnCallback(
159 function ($k) use (&$consecutive) {
160 list($expected, $return) = array_shift($consecutive);
161 $this->assertEquals($expected, $k);
167 $this->assertEquals([
'bl001',
'bl002'], $settings->getBlackListedSuffixes());
168 $this->assertEquals([
'bl001',
'bl002'], $settings->getProhibited());
169 $this->assertEquals($default_whitelist, $settings->getDefaultWhitelist());
170 $this->assertEquals([
'docx',
'doc'], $settings->getWhiteListNegative());
171 $this->assertEquals([
'wl001',
'wl002'], $settings->getWhiteListPositive());
173 $whitelist = array_merge(
174 array_diff($default_whitelist, [
'docx',
'doc']),
175 [
'wl001',
'wl002',
'']
177 $diff = array_diff($whitelist, $settings->getWhiteListedSuffixes());
179 $this->assertEquals([], $diff);
180 $this->assertEquals(0, count($diff));
187 $settings = $this->createMock(ilFileServicesSettings::class);
189 $settings->expects($this->atLeastOnce())
190 ->method(
'getBlackListedSuffixes')
191 ->willReturn([
'mp3']);
193 $settings->expects($this->atLeastOnce())
194 ->method(
'getWhiteListedSuffixes')
195 ->willReturn([
'pdf',
'png',
'mp3']);
197 $settings->expects($this->atLeastOnce())
198 ->method(
'isASCIIConvertionEnabled')
202 $this->assertEquals(
'testmp3.sec', $policy->prepareFileNameForConsumer(
'test.mp3'));
203 $this->assertEquals(
'test.png', $policy->prepareFileNameForConsumer(
'test.png'));
204 $this->assertEquals(
'test.pdf', $policy->prepareFileNameForConsumer(
'test.pdf'));
205 $this->assertEquals(
'aeaeaeaeaeaeaeaeae.pdf', $policy->prepareFileNameForConsumer(
'äääääääää.pdf'));
206 $this->assertEquals(
'oeoeoeoeoeoeoeoeoe.pdf', $policy->prepareFileNameForConsumer(
'ööööööööö.pdf'));
207 $this->assertEquals(
'ueueueueueueueueue.pdf', $policy->prepareFileNameForConsumer(
'üüüüüüüüü.pdf'));
Class ilFileServicesPolicy.
ILIAS DI Container $dic_backup
Customizing of pimple-DIC for ILIAS.
// this is necessary to avoid side effects with the DIC disabled
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilFileServicesFilenameSanitizer.
testRenamingNonWhitelistedFile()
testBlacklistedUploadWithPermission()
testFileNamePolicyOnDownloading()
Class ilFileServicesPolicy.