ILIAS  release_8 Revision v8.24
ilServicesFileServicesTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21use PHPUnit\Framework\TestCase;
22use Symfony\Component\DependencyInjection\Container;
26
27class ilServicesFileServicesTest extends TestCase
28{
30
31 public function testSanitizing(): void
32 {
33 $settings = $this->createMock(ilFileServicesSettings::class);
34 $settings->expects($this->once())
35 ->method('getWhiteListedSuffixes')
36 ->willReturn(['pdf', 'jpg']);
37
39 $this->assertTrue($sanitizer->isClean('/lib/test.pdf'));
40 $this->assertFalse($sanitizer->isClean('/lib/test.xml'));
41 $this->assertEquals('/lib/testxml.sec', $sanitizer->sanitize('/lib/test.xml'));
42 }
43
44 public function testBlacklistedUpload(): void
45 {
46 $settings = $this->createMock(ilFileServicesSettings::class);
47 $settings->expects($this->once())
48 ->method('getBlackListedSuffixes')
49 ->willReturn(['pdf']);
50
51 $settings->expects($this->once())
52 ->method('isByPassAllowedForCurrentUser')
53 ->willReturn(false);
54
55 $stream = $this->createMock(FileStream::class);
56 $meta = new Metadata('filename.pdf', 42, 'application/pdf');
57
58 $processor = new ilFileServicesPreProcessor(
60 'the reason'
61 );
62 // is ok since user has permission
63 $status = $processor->process($stream, $meta);
64 $this->assertEquals(ProcessingStatus::REJECTED, $status->getCode());
65 }
66
68 {
69 $settings = $this->createMock(ilFileServicesSettings::class);
70 $settings->expects($this->once())
71 ->method('getBlackListedSuffixes')
72 ->willReturn(['pdf']);
73
74 $settings->expects($this->once())
75 ->method('isByPassAllowedForCurrentUser')
76 ->willReturn(true);
77
78 $stream = $this->createMock(FileStream::class);
79 $meta = new Metadata('filename.pdf', 42, 'application/pdf');
80
81 $processor = new ilFileServicesPreProcessor(
83 'the reason'
84 );
85 // is ok since user has permission
86 $status = $processor->process($stream, $meta);
87 $this->assertEquals(ProcessingStatus::OK, $status->getCode());
88 }
89
90 public function testRenamingNonWhitelistedFile(): void
91 {
92 $settings = $this->createMock(ilFileServicesSettings::class);
93 $settings->expects($this->once())
94 ->method('getWhiteListedSuffixes')
95 ->willReturn(['pdf', 'png', 'jpg']);
96
98
99 $sane_filename = 'bellerophon.pdf';
100 $this->assertEquals($sane_filename, $sanitizer->sanitize($sane_filename));
101
102 $insane_filename = 'bellerophon.docx';
103 $this->assertNotEquals($insane_filename, $sanitizer->sanitize($insane_filename));
104 $this->assertEquals('bellerophondocx.sec', $sanitizer->sanitize($insane_filename));
105 }
106
107 public function testActualWhitelist(): void
108 {
109 $db_mock = $this->createMock(ilDBInterface::class);
110 $settings_mock = $this->createMock(ilSetting::class);
111 $ini_mock = $this->createMock(ilIniFile::class);
112
113 $ref = new stdClass();
114 $ref->ref_id = 32;
115 $db_mock->expects($this->once())
116 ->method('fetchObject')
117 ->willReturn($ref);
118
119 $default_whitelist = include "./Services/FileServices/defaults/default_whitelist.php";
120
121 // Blacklist
122 $settings_mock->expects($this->exactly(3))
123 ->method('get')
124 ->withConsecutive(
125 ['suffix_custom_expl_black'],
126 ['suffix_repl_additional'],
127 ['suffix_custom_white_list']
128 )
129 ->willReturnOnConsecutiveCalls(
130 'bl001,bl002', // blacklisted
131 'docx,doc', // remove from whitelist
132 'wl001,wl002' // add whitelist
133 );
134
135 $settings = new ilFileServicesSettings($settings_mock, $ini_mock, $db_mock);
136 $this->assertEquals(['bl001', 'bl002'], $settings->getBlackListedSuffixes());
137 $this->assertEquals(['bl001', 'bl002'], $settings->getProhibited());
138 $this->assertEquals($default_whitelist, $settings->getDefaultWhitelist());
139 $this->assertEquals(['docx', 'doc'], $settings->getWhiteListNegative());
140 $this->assertEquals(['wl001', 'wl002'], $settings->getWhiteListPositive());
141
142 $whitelist = array_merge(
143 array_diff($default_whitelist, ['docx', 'doc']),
144 ['wl001', 'wl002', '']
145 );
146 $diff = array_diff($whitelist, $settings->getWhiteListedSuffixes());
147
148 $this->assertEquals([], $diff);
149 $this->assertEquals(0, count($diff));
150 }
151
152 public function testDisabledASCIISetting(): void
153 {
154 $db_mock = $this->createMock(ilDBInterface::class);
155 $settings_mock = $this->createMock(ilSetting::class);
156 $ini_mock = $this->createMock(ilIniFile::class);
157
158 $ini_mock->expects($this->once())
159 ->method('readVariable')
160 ->with('file_access', 'disable_ascii')
161 ->willReturn('1');
162
163 $settings = new ilFileServicesSettings($settings_mock, $ini_mock, $db_mock);
164 $this->assertFalse($settings->isASCIIConvertionEnabled());
165 }
166
167 public function testNoASCIISetting(): void
168 {
169 $db_mock = $this->createMock(ilDBInterface::class);
170 $settings_mock = $this->createMock(ilSetting::class);
171 $ini_mock = $this->createMock(ilIniFile::class);
172
173 $ini_mock->expects($this->once())
174 ->method('readVariable')
175 ->with('file_access', 'disable_ascii')
176 ->willReturn('');
177
178 $settings = new ilFileServicesSettings($settings_mock, $ini_mock, $db_mock);
179 $this->assertTrue($settings->isASCIIConvertionEnabled());
180 }
181
182 public function testFileNamePolicyOnDownloading(): void
183 {
184 $settings = $this->createMock(ilFileServicesSettings::class);
185
186 $settings->expects($this->atLeastOnce())
187 ->method('getBlackListedSuffixes')
188 ->willReturn(['mp3']);
189
190 $settings->expects($this->atLeastOnce())
191 ->method('getWhiteListedSuffixes')
192 ->willReturn(['pdf', 'png', 'mp3']);
193
194 $settings->expects($this->atLeastOnce())
195 ->method('isASCIIConvertionEnabled')
196 ->willReturn(true);
197
198 $policy = new ilFileServicesPolicy($settings);
199 $this->assertEquals('testmp3.sec', $policy->prepareFileNameForConsumer('test.mp3'));
200 $this->assertEquals('test.png', $policy->prepareFileNameForConsumer('test.png'));
201 $this->assertEquals('test.pdf', $policy->prepareFileNameForConsumer('test.pdf'));
202 $this->assertEquals('aeaeaeaeaeaeaeaeae.pdf', $policy->prepareFileNameForConsumer('äääääääää.pdf'));
203 $this->assertEquals('oeoeoeoeoeoeoeoeoe.pdf', $policy->prepareFileNameForConsumer('ööööööööö.pdf'));
204 $this->assertEquals('ueueueueueueueueue.pdf', $policy->prepareFileNameForConsumer('üüüüüüüüü.pdf'));
205 }
206}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
Class ilFileServicesFilenameSanitizer.
Class ilFileServicesPolicy.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200