ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
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 
63  $sanitizer = new ilFileServicesFilenameSanitizer($settings);
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__ . "/../defaults/default_whitelist.php";
148 
149  // Blacklist
150  $consecutive = [
151  ['suffix_custom_expl_black', 'bl001,bl002'], // blacklisted
152  ['suffix_repl_additional', 'docx,doc'], // remove from whitelist
153  ['suffix_custom_white_list', 'wl001,wl002'] // add whitelist
154  ];
155  $settings_mock
156  ->expects($this->exactly(3))
157  ->method('get')
158  ->willReturnCallback(
159  function ($k) use (&$consecutive) {
160  list($expected, $return) = array_shift($consecutive);
161  $this->assertEquals($expected, $k);
162  return $return;
163  }
164  );
165 
166  $settings = new ilFileServicesSettings($settings_mock, $ini_mock, $this->db_mock);
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());
172 
173  $whitelist = array_merge(
174  array_diff($default_whitelist, ['docx', 'doc']),
175  ['wl001', 'wl002', '']
176  );
177  $diff = array_diff($whitelist, $settings->getWhiteListedSuffixes());
178 
179  $this->assertEquals([], $diff);
180  $this->assertEquals(0, count($diff));
181  }
182 
183 
184 
185  public function testFileNamePolicyOnDownloading(): void
186  {
187  $settings = $this->createMock(ilFileServicesSettings::class);
188 
189  $settings->expects($this->atLeastOnce())
190  ->method('getBlackListedSuffixes')
191  ->willReturn(['mp3']);
192 
193  $settings->expects($this->atLeastOnce())
194  ->method('getWhiteListedSuffixes')
195  ->willReturn(['pdf', 'png', 'mp3']);
196 
197  $settings->expects($this->atLeastOnce())
198  ->method('isASCIIConvertionEnabled')
199  ->willReturn(true);
200 
201  $policy = new ilFileServicesPolicy($settings);
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'));
208  }
209 }
Class ilFileServicesPolicy.
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
// 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...
global $DIC
Definition: shib_login.php:25
Class ilFileServicesFilenameSanitizer.