ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CapabilityTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\MockObject\MockObject;
23use PHPUnit\Framework\Attributes\DataProvider;
27use PHPUnit\Framework\TestCase;
32
33class CapabilityTest extends TestCase
34{
38 public MockObject $workspace_access_handler;
39 public MockObject $type_resolver;
40 private MockObject $file_info_repository;
41 private MockObject $access;
42 private MockObject $action_repository;
44
45 private static array $readme_infos = [];
46
47 private static bool $update_readme = false;
48
49 protected function setUp(): void
50 {
51 if (!defined('ILIAS_HTTP_PATH')) {
52 define('ILIAS_HTTP_PATH', 'https://ilias.unit.test');
53 }
54
55 $this->file_info_repository = $this->createMock(\ilObjFileInfoRepository::class);
56 $this->access = $this->createMock(\ilAccessHandler::class);
57 $ctrl = $this->createMock(\ilCtrlInterface::class);
58 $this->action_repository = $this->createMock(ActionRepository::class);
59 $http = $this->createMock(Services::class);
60 $static_url = $this->createMock(URIBuilder::class);
61 $this->type_resolver = $this->createMock(TypeResolver::class);
62 $this->workspace_access_handler = $this->createMock(ilWorkspaceAccessHandler::class);
63
64 $this->type_resolver->method('resolveTypeByObjectId')
65 ->withAnyParameters()
66 ->willReturn('file');
67
68 $this->capability_builder = new CapabilityBuilder(
69 $this->file_info_repository,
70 $this->access,
71 $ctrl,
72 $this->action_repository,
73 $http,
75 $this->type_resolver,
76 $this->workspace_access_handler
77 );
78 }
79
80 protected function tearDown(): void
81 {
82 }
83
84 public static function tearDownAfterClass(): void
85 {
86 if (!self::$update_readme) {
87 return;
88 }
90 }
91
92 public static function environmentProvider(): \Iterator
93 {
94 yield 'testerei' => [
95 true,
96 true,
97 true,
98 [
99 Permissions::READ,
100 Permissions::WRITE,
101 Permissions::VISIBLE,
102 Permissions::EDIT_CONTENT,
103 Permissions::VIEW_CONTENT
104 ],
105 Capabilities::FORCED_INFO_PAGE
106 ];
107 yield [
108 true,
109 true,
110 false,
111 [
112 Permissions::READ,
113 Permissions::WRITE,
114 Permissions::VISIBLE,
115 Permissions::EDIT_CONTENT,
116 Permissions::VIEW_CONTENT
117 ],
118 Capabilities::VIEW_EXTERNAL
119 ];
120 yield [
121 true,
122 true,
123 false,
124 [
125 Permissions::EDIT_CONTENT,
126 Permissions::VIEW_CONTENT
127 ],
128 Capabilities::VIEW_EXTERNAL
129 ];
130 yield [
131 false,
132 false,
133 true,
134 [
135 Permissions::READ,
136 Permissions::VISIBLE
137 ],
138 Capabilities::FORCED_INFO_PAGE
139 ];
140 yield [
141 true,
142 true,
143 false,
144 [
145 Permissions::EDIT_CONTENT,
146 ],
147 Capabilities::EDIT_EXTERNAL
148 ];
149 yield [
150 true,
151 true,
152 false,
153 [
154 Permissions::READ,
155 ],
156 Capabilities::DOWNLOAD
157 ];
158 yield [
159 true,
160 true,
161 false,
162 [
163 Permissions::WRITE,
164 Permissions::READ,
165 ],
166 Capabilities::DOWNLOAD
167 ];
168 yield [
169 true,
170 true,
171 false,
172 [
173 Permissions::WRITE,
174 ],
175 Capabilities::MANAGE_VERSIONS
176 ];
177 yield [
178 true,
179 true,
180 false,
181 [
182 Permissions::VISIBLE,
183 ],
184 Capabilities::INFO_PAGE
185 ];
186 yield [
187 true,
188 true,
189 true,
190 [
191 Permissions::WRITE,
192 Permissions::READ,
193 ],
194 Capabilities::FORCED_INFO_PAGE
195 ];
196 yield [
197 true,
198 true,
199 false,
200 [
201 Permissions::NONE,
202 ],
203 Capabilities::NONE
204 ];
205 yield 'docu_case' => [
206 true,
207 true,
208 true,
209 [
210 Permissions::READ,
211 Permissions::VISIBLE,
212 ],
213 Capabilities::FORCED_INFO_PAGE
214 ];
215 }
216
217 #[DataProvider('environmentProvider')]
218 public function testCapabilityPriority(
219 bool $wopi_view,
220 bool $wopi_edit,
221 bool $infopage_first,
222 array $user_permissions,
223 Capabilities $expected_best
224 ): void {
225 static $id;
226 $permissions = $user_permissions;
227
228 $id++;
229
230 $context = new Context(
231 $id,
232 $id,
233 Context::CONTEXT_REPO
234 );
235
236 $this->access->method('checkAccess')
237 ->willReturnCallback(
238 function (string $permission) use ($permissions): bool {
239 $checked_permissions = explode(',', $permission);
240 $common_permissions = array_intersect(
241 array_map(static fn(Permissions $p): string => $p->value, $permissions),
242 $checked_permissions
243 );
244 return $common_permissions !== [];
245 }
246 );
247
248 $file_info = $this->createMock(\ilObjFileInfo::class);
249 $file_info->method('shouldDownloadDirectly')
250 ->willReturn(!$infopage_first);
251
252 $this->file_info_repository->method('getByObjectId')
253 ->with($context->getObjectId())
254 ->willReturn($file_info);
255
256 $this->action_repository->method('hasEditActionForSuffix')
257 ->willReturn($wopi_edit);
258
259 $this->action_repository->method('hasViewActionForSuffix')
260 ->willReturn($wopi_view);
261
262 $capabilities = $this->capability_builder->get($context);
263 $best = $capabilities->getBest();
264
265 $this->assertEquals($expected_best, $best->getCapability());
266
267 self::$readme_infos[] = [
268 implode(', ', array_map(fn(Permissions $p): string => $p->value, $permissions)), // permissions
269 ($wopi_view ? 'Yes' : 'No'),
270 ($wopi_edit ? 'Yes' : 'No'),
271 ($infopage_first ? 'Info-Page' : 'Open'),
272 $best->getCapability()->name
273 ];
274 }
275
276 private static function updateREADME(): void
277 {
278 // UPDATE README
279 $readme_file = __DIR__ . '/../../docs/README.md';
280 $readme_content = file_get_contents($readme_file);
281
282 $table = [
283 [
284 'User\'s Permissions',
285 'WOPI View Action av.',
286 'WOPI Edit Action av.',
287 'Click-Setting',
288 'Expected Capability'
289 ]
290 ];
291 $readme_infos = self::$readme_infos;
292 // sort $readme_infos by last column
293 usort($readme_infos, static function ($a, $b): int {
294 $a_string = implode('', array_reverse($a));
295 $b_string = implode('', array_reverse($b));
296
297 return strcmp($a_string, $b_string);
298 });
299
300 $table = array_merge($table, $readme_infos);
301
302 // Define the markers for the block
303 $start_marker = "<!-- START CAPABILITY_TABLE -->";
304 $end_marker = "<!-- END CAPABILITY_TABLE -->";
305
306 // Prepare the new block content
307 $new_block = $start_marker . "\n\n" . self::arrayToMarkdownTable($table) . "\n\n" . $end_marker;
308
309 // Replace the content between the markers
310 $pattern = '/' . preg_quote($start_marker, '/') . '.*?' . preg_quote($end_marker, '/') . '/s';
311 $readme_content = preg_replace($pattern, $new_block, $readme_content);
312
313 file_put_contents($readme_file, $readme_content);
314 }
315
316 private static function arrayToMarkdownTable(array $data): string
317 {
318 // Check if the input array is valid
319 if (empty($data) || !is_array($data[0])) {
320 throw new InvalidArgumentException("Input must be a non-empty array of arrays.");
321 }
322
323 // Calculate the maximum width of each column
324 $col_widths = array_map(
325 static fn(int|string $col_index): int => max(
326 array_map(
327 static fn(array $row): int => isset($row[$col_index]) ? mb_strlen((string) $row[$col_index]) : 0,
328 $data
329 )
330 ),
331 array_keys($data[0])
332 );
333
334 // Function to pad a row's columns to match the maximum width
335 $pad_row = static fn($row): array => array_map(static function ($value, $index) use ($col_widths): string {
336 $value ??= ''; // Handle missing values
337 return str_pad($value, $col_widths[$index], " ", STR_PAD_RIGHT);
338 }, $row, array_keys($col_widths));
339
340 // Format the header and rows
341 $header = $pad_row($data[0]);
342 $rows = array_map($pad_row, array_slice($data, 1));
343
344 // Build the Markdown table
345 $header_row = "| "
346 . implode(" | ", $header)
347 . " |";
348 $sep_row = "| "
349 . implode(" | ", array_map(static fn(int $width): string => str_repeat("-", $width), $col_widths))
350 . " |";
351 $data_rows = array_map(static fn($row): string => "| " . implode(" | ", $row) . " |", $rows);
352
353 // Combine all parts
354 return implode("\n", array_merge([$header_row, $sep_row], $data_rows));
355 }
356
357}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
MockObject $access
static environmentProvider()
static bool $update_readme
MockObject $workspace_access_handler
MockObject $action_repository
static array $readme_infos
MockObject $file_info_repository
static arrayToMarkdownTable(array $data)
CapabilityBuilder $capability_builder
static updateREADME()
testCapabilityPriority(bool $wopi_view, bool $wopi_edit, bool $infopage_first, array $user_permissions, Capabilities $expected_best)
static tearDownAfterClass()
MockObject $type_resolver
Class Services.
Definition: Services.php:38
$http
Definition: deliver.php:30
$static_url
Definition: goto.php:29
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$context
Definition: webdav.php:31