19declare(strict_types=1);
21use PHPUnit\Framework\MockObject\MockObject;
23use PHPUnit\Framework\Attributes\DataProvider;
27use PHPUnit\Framework\TestCase;
49 protected function setUp(): void
51 if (!defined(
'ILIAS_HTTP_PATH')) {
52 define(
'ILIAS_HTTP_PATH',
'https://ilias.unit.test');
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);
61 $this->type_resolver = $this->createMock(TypeResolver::class);
62 $this->workspace_access_handler = $this->createMock(ilWorkspaceAccessHandler::class);
64 $this->type_resolver->method(
'resolveTypeByObjectId')
69 $this->file_info_repository,
72 $this->action_repository,
76 $this->workspace_access_handler
86 if (!self::$update_readme) {
101 Permissions::VISIBLE,
102 Permissions::EDIT_CONTENT,
103 Permissions::VIEW_CONTENT
105 Capabilities::FORCED_INFO_PAGE
114 Permissions::VISIBLE,
115 Permissions::EDIT_CONTENT,
116 Permissions::VIEW_CONTENT
118 Capabilities::VIEW_EXTERNAL
125 Permissions::EDIT_CONTENT,
126 Permissions::VIEW_CONTENT
128 Capabilities::VIEW_EXTERNAL
138 Capabilities::FORCED_INFO_PAGE
145 Permissions::EDIT_CONTENT,
147 Capabilities::EDIT_EXTERNAL
156 Capabilities::DOWNLOAD
166 Capabilities::DOWNLOAD
175 Capabilities::MANAGE_VERSIONS
182 Permissions::VISIBLE,
184 Capabilities::INFO_PAGE
194 Capabilities::FORCED_INFO_PAGE
205 yield
'docu_case' => [
211 Permissions::VISIBLE,
213 Capabilities::FORCED_INFO_PAGE
217 #[DataProvider('environmentProvider')]
221 bool $infopage_first,
222 array $user_permissions,
226 $permissions = $user_permissions;
233 Context::CONTEXT_REPO
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),
244 return $common_permissions !== [];
248 $file_info = $this->createMock(\ilObjFileInfo::class);
249 $file_info->method(
'shouldDownloadDirectly')
250 ->willReturn(!$infopage_first);
252 $this->file_info_repository->method(
'getByObjectId')
254 ->willReturn($file_info);
256 $this->action_repository->method(
'hasEditActionForSuffix')
257 ->willReturn($wopi_edit);
259 $this->action_repository->method(
'hasViewActionForSuffix')
260 ->willReturn($wopi_view);
262 $capabilities = $this->capability_builder->get(
$context);
263 $best = $capabilities->getBest();
265 $this->assertEquals($expected_best, $best->getCapability());
267 self::$readme_infos[] = [
268 implode(
', ', array_map(fn(
Permissions $p):
string => $p->value, $permissions)),
269 ($wopi_view ?
'Yes' :
'No'),
270 ($wopi_edit ?
'Yes' :
'No'),
271 ($infopage_first ?
'Info-Page' :
'Open'),
272 $best->getCapability()->name
279 $readme_file = __DIR__ .
'/../../docs/README.md';
280 $readme_content = file_get_contents($readme_file);
284 'User\'s Permissions',
285 'WOPI View Action av.',
286 'WOPI Edit Action av.',
288 'Expected Capability'
291 $readme_infos = self::$readme_infos;
293 usort($readme_infos,
static function (
$a,
$b):
int {
294 $a_string = implode(
'', array_reverse(
$a));
295 $b_string = implode(
'', array_reverse(
$b));
297 return strcmp($a_string, $b_string);
300 $table = array_merge($table, $readme_infos);
303 $start_marker =
"<!-- START CAPABILITY_TABLE -->";
304 $end_marker =
"<!-- END CAPABILITY_TABLE -->";
307 $new_block = $start_marker .
"\n\n" . self::arrayToMarkdownTable($table) .
"\n\n" . $end_marker;
310 $pattern =
'/' . preg_quote($start_marker,
'/') .
'.*?' . preg_quote($end_marker,
'/') .
'/s';
311 $readme_content = preg_replace($pattern, $new_block, $readme_content);
313 file_put_contents($readme_file, $readme_content);
320 throw new InvalidArgumentException(
"Input must be a non-empty array of arrays.");
324 $col_widths = array_map(
325 static fn(
int|
string $col_index):
int => max(
327 static fn(array $row):
int => isset($row[$col_index]) ? mb_strlen((
string) $row[$col_index]) : 0,
335 $pad_row =
static fn($row): array => array_map(
static function ($value, $index) use ($col_widths):
string {
337 return str_pad($value, $col_widths[$index],
" ", STR_PAD_RIGHT);
338 }, $row, array_keys($col_widths));
341 $header = $pad_row(
$data[0]);
342 $rows = array_map($pad_row, array_slice(
$data, 1));
346 . implode(
" | ", $header)
349 . implode(
" | ", array_map(
static fn(
int $width):
string => str_repeat(
"-", $width), $col_widths))
351 $data_rows = array_map(
static fn($row):
string =>
"| " . implode(
" | ", $row) .
" |", $rows);
354 return implode(
"\n", array_merge([$header_row, $sep_row], $data_rows));
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
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
testCapabilityPriority(bool $wopi_view, bool $wopi_edit, bool $infopage_first, array $user_permissions, Capabilities $expected_best)
static tearDownAfterClass()
MockObject $type_resolver
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples