ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ilServicesFileServicesTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 
33 class ilServicesFileServicesTest extends TestCase
34 {
39  private ?ilDBInterface $db_mock = null;
40 
41  protected function setUp(): void
42  {
43  global $DIC;
44  $this->dic_backup = is_object($DIC) ? clone $DIC : null;
45 
46  $DIC = new \ILIAS\DI\Container();
47  $DIC['ilDB'] = $this->db_mock = $this->createMock(ilDBInterface::class);
48  }
49 
50  protected function tearDown(): void
51  {
52  global $DIC;
53  $DIC = $this->dic_backup;
54  }
55 
56  public function testSanitizing(): void
57  {
58  $settings = $this->createMock(ilFileServicesSettings::class);
59  $settings->expects($this->once())
60  ->method('getWhiteListedSuffixes')
61  ->willReturn(['pdf', 'jpg']);
62 
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'));
67  }
68 
69  public function testBlacklistedUpload(): void
70  {
71  $settings = $this->createMock(ilFileServicesSettings::class);
72  $settings->expects($this->once())
73  ->method('getBlackListedSuffixes')
74  ->willReturn(['pdf']);
75 
76  $settings->expects($this->once())
77  ->method('isByPassAllowedForCurrentUser')
78  ->willReturn(false);
79 
80  $stream = $this->createMock(FileStream::class);
81  $meta = new Metadata('filename.pdf', 42, 'application/pdf');
82 
83  $processor = new ilFileServicesPreProcessor(
84  $settings,
85  'the reason'
86  );
87  // is ok since user has permission
88  $status = $processor->process($stream, $meta);
89  $this->assertEquals(ProcessingStatus::REJECTED, $status->getCode());
90  }
91 
92  public function testBlacklistedUploadWithPermission(): void
93  {
94  $settings = $this->createMock(ilFileServicesSettings::class);
95  $settings->expects($this->once())
96  ->method('getBlackListedSuffixes')
97  ->willReturn(['pdf']);
98 
99  $settings->expects($this->once())
100  ->method('isByPassAllowedForCurrentUser')
101  ->willReturn(true);
102 
103  $stream = $this->createMock(FileStream::class);
104  $meta = new Metadata('filename.pdf', 42, 'application/pdf');
105 
106  $processor = new ilFileServicesPreProcessor(
107  $settings,
108  'the reason'
109  );
110  // is ok since user has permission
111  $status = $processor->process($stream, $meta);
112  $this->assertEquals(ProcessingStatus::OK, $status->getCode());
113  }
114 
115  public function testRenamingNonWhitelistedFile(): void
116  {
117  $settings = $this->createMock(ilFileServicesSettings::class);
118  $settings->expects($this->once())
119  ->method('getWhiteListedSuffixes')
120  ->willReturn(['pdf', 'png', 'jpg']);
121 
122  $sanitizer = new ilFileServicesFilenameSanitizer($settings);
123 
124  $sane_filename = 'bellerophon.pdf';
125  $this->assertEquals($sane_filename, $sanitizer->sanitize($sane_filename));
126 
127  $insane_filename = 'bellerophon.docx';
128  $this->assertNotEquals($insane_filename, $sanitizer->sanitize($insane_filename));
129  $this->assertEquals('bellerophondocx.sec', $sanitizer->sanitize($insane_filename));
130  }
131 
132  public function testActualWhitelist(): void
133  {
134  $settings_mock = $this->createMock(ilSetting::class);
135  $ini_mock = $this->createMock(ilIniFile::class);
136 
137  $ref = new stdClass();
138  $ref->ref_id = 32;
139  $this->db_mock->expects($this->once())
140  ->method('fetchObject')
141  ->willReturn($ref);
142 
143  $this->db_mock->expects($this->once())
144  ->method('fetchAssoc')
145  ->willReturn([]);
146 
147  $default_whitelist = include __DIR__ . "/../../../Services/FileServices/defaults/default_whitelist.php";
148 
149  // Blacklist
150  $settings_mock->expects($this->exactly(3))
151  ->method('get')
152  ->withConsecutive(
153  ['suffix_custom_expl_black'],
154  ['suffix_repl_additional'],
155  ['suffix_custom_white_list']
156  )
157  ->willReturnOnConsecutiveCalls(
158  'bl001,bl002', // blacklisted
159  'docx,doc', // remove from whitelist
160  'wl001,wl002' // add whitelist
161  );
162 
163  $settings = new ilFileServicesSettings($settings_mock, $ini_mock, $this->db_mock);
164  $this->assertEquals(['bl001', 'bl002'], $settings->getBlackListedSuffixes());
165  $this->assertEquals(['bl001', 'bl002'], $settings->getProhibited());
166  $this->assertEquals($default_whitelist, $settings->getDefaultWhitelist());
167  $this->assertEquals(['docx', 'doc'], $settings->getWhiteListNegative());
168  $this->assertEquals(['wl001', 'wl002'], $settings->getWhiteListPositive());
169 
170  $whitelist = array_merge(
171  array_diff($default_whitelist, ['docx', 'doc']),
172  ['wl001', 'wl002', '']
173  );
174  $diff = array_diff($whitelist, $settings->getWhiteListedSuffixes());
175 
176  $this->assertEquals([], $diff);
177  $this->assertEquals(0, count($diff));
178  }
179 
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 }
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
Class ilFileServicesPolicy.
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
global $DIC
Definition: feed.php:28
// 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.