ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
CapabilityTest Class Reference
+ Inheritance diagram for CapabilityTest:
+ Collaboration diagram for CapabilityTest:

Public Member Functions

 testCapabilityPriority (bool $wopi_view, bool $wopi_edit, bool $infopage_first, array $permissions, Capabilities $expected_best)
 environmentProvider More...
 

Static Public Member Functions

static tearDownAfterClass ()
 
static environmentProvider ()
 

Data Fields

PHPUnit Framework MockObject MockObject $workspace_access_handler
 
PHPUnit Framework MockObject MockObject TypeResolver $type_resolver
 

Protected Member Functions

 setUp ()
 
 tearDown ()
 

Static Private Member Functions

static updateREADME ()
 
static arrayToMarkdownTable (array $data)
 

Private Attributes

ilObjFileInfoRepository MockObject $file_info_repository
 
ilAccessHandler MockObject $access
 
ilCtrlInterface MockObject $ctrl
 
ActionRepository MockObject $action_repository
 
Services MockObject $http
 
CapabilityBuilder $capability_builder
 

Static Private Attributes

static array $readme_infos = []
 
static bool $update_readme = false
 

Detailed Description

Definition at line 31 of file CapabilityTest.php.

Member Function Documentation

◆ arrayToMarkdownTable()

static CapabilityTest::arrayToMarkdownTable ( array  $data)
staticprivate

Definition at line 307 of file CapabilityTest.php.

307  : string
308  {
309  // Check if the input array is valid
310  if (empty($data) || !is_array($data[0])) {
311  throw new InvalidArgumentException("Input must be a non-empty array of arrays.");
312  }
313 
314  // Calculate the maximum width of each column
315  $col_widths = array_map(
316  static fn($col_index): int => max(
317  array_map(
318  static fn($row): int => isset($row[$col_index]) ? mb_strlen((string) $row[$col_index]) : 0,
319  $data
320  )
321  ),
322  array_keys($data[0])
323  );
324 
325  // Function to pad a row's columns to match the maximum width
326  $pad_row = static fn($row): array => array_map(static function ($value, $index) use ($col_widths): string {
327  $value ??= ''; // Handle missing values
328  return str_pad($value, $col_widths[$index], " ", STR_PAD_RIGHT);
329  }, $row, array_keys($col_widths));
330 
331  // Format the header and rows
332  $header = $pad_row($data[0]);
333  $rows = array_map($pad_row, array_slice($data, 1));
334 
335  // Build the Markdown table
336  $header_row = "| "
337  . implode(" | ", $header)
338  . " |";
339  $sep_row = "| "
340  . implode(" | ", array_map(static fn($width): string => str_repeat("-", $width), $col_widths))
341  . " |";
342  $data_rows = array_map(static fn($row): string => "| " . implode(" | ", $row) . " |", $rows);
343 
344  // Combine all parts
345  return implode("\n", array_merge([$header_row, $sep_row], $data_rows));
346  }

◆ environmentProvider()

static CapabilityTest::environmentProvider ( )
static

Definition at line 90 of file CapabilityTest.php.

90  : array
91  {
92  return [
93  'testerei' => [
94  'wopi_view' => true,
95  'wopi_edit' => true,
96  'infopage_first' => true,
97  'user_permissions' => [
98  Permissions::READ,
99  Permissions::WRITE,
100  Permissions::VISIBLE,
101  Permissions::EDIT_CONTENT,
102  Permissions::VIEW_CONTENT
103  ],
104  'expected_best' => Capabilities::FORCED_INFO_PAGE
105  ],
106  [
107  'wopi_view' => true,
108  'wopi_edit' => true,
109  'infopage_first' => false,
110  'user_permissions' => [
111  Permissions::READ,
112  Permissions::WRITE,
113  Permissions::VISIBLE,
114  Permissions::EDIT_CONTENT,
115  Permissions::VIEW_CONTENT
116  ],
117  'expected_best' => Capabilities::EDIT_EXTERNAL
118  ],
119  [
120  'wopi_view' => true,
121  'wopi_edit' => true,
122  'infopage_first' => false,
123  'user_permissions' => [
124  Permissions::EDIT_CONTENT,
125  Permissions::VIEW_CONTENT
126  ],
127  'expected_best' => Capabilities::EDIT_EXTERNAL
128  ],
129  [
130  'wopi_view' => false,
131  'wopi_edit' => false,
132  'infopage_first' => true,
133  'user_permissions' => [
134  Permissions::READ,
135  Permissions::VISIBLE
136  ],
137  'expected_best' => Capabilities::FORCED_INFO_PAGE
138  ],
139  [
140  'wopi_view' => true,
141  'wopi_edit' => true,
142  'infopage_first' => false,
143  'user_permissions' => [
144  Permissions::EDIT_CONTENT,
145  ],
146  'expected_best' => Capabilities::EDIT_EXTERNAL
147  ],
148  [
149  'wopi_view' => true,
150  'wopi_edit' => true,
151  'infopage_first' => false,
152  'user_permissions' => [
153  Permissions::READ,
154  ],
155  'expected_best' => Capabilities::DOWNLOAD
156  ],
157  [
158  'wopi_view' => true,
159  'wopi_edit' => true,
160  'infopage_first' => false,
161  'user_permissions' => [
162  Permissions::WRITE,
163  Permissions::READ,
164  ],
165  'expected_best' => Capabilities::DOWNLOAD
166  ],
167  [
168  'wopi_view' => true,
169  'wopi_edit' => true,
170  'infopage_first' => false,
171  'user_permissions' => [
172  Permissions::WRITE,
173  ],
174  'expected_best' => Capabilities::MANAGE_VERSIONS
175  ],
176  [
177  'wopi_view' => true,
178  'wopi_edit' => true,
179  'infopage_first' => false,
180  'user_permissions' => [
181  Permissions::VISIBLE,
182  ],
183  'expected_best' => Capabilities::INFO_PAGE
184  ],
185  [
186  'wopi_view' => true,
187  'wopi_edit' => true,
188  'infopage_first' => true,
189  'user_permissions' => [
190  Permissions::WRITE,
191  Permissions::READ,
192  ],
193  'expected_best' => Capabilities::FORCED_INFO_PAGE
194  ],
195  [
196  'wopi_view' => true,
197  'wopi_edit' => true,
198  'infopage_first' => false,
199  'user_permissions' => [
200  Permissions::NONE,
201  ],
202  'expected_best' => Capabilities::NONE
203  ],
204  ];
205  }

◆ setUp()

CapabilityTest::setUp ( )
protected

Definition at line 49 of file CapabilityTest.php.

References ILIAS\Repository\access(), ILIAS\Repository\ctrl(), and ILIAS\FileDelivery\http().

49  : 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  $this->ctrl = $this->createMock(\ilCtrlInterface::class);
58  $this->action_repository = $this->createMock(ActionRepository::class);
59  $this->http = $this->createMock(Services::class);
60  $this->type_resolver = $this->createMock(TypeResolver::class);
61  $this->workspace_access_handler = $this->createMock(ilWorkspaceAccessHandler::class);
62 
63  $this->type_resolver->method('resolveTypeByObjectId')
64  ->withAnyParameters()
65  ->willReturn('file');
66 
67  $this->capability_builder = new CapabilityBuilder(
68  $this->file_info_repository,
69  $this->access,
70  $this->ctrl,
71  $this->action_repository,
72  $this->http,
73  $this->type_resolver,
74  $this->workspace_access_handler
75  );
76  }
static http()
Fetches the global http state from ILIAS.
+ Here is the call graph for this function:

◆ tearDown()

CapabilityTest::tearDown ( )
protected

Definition at line 78 of file CapabilityTest.php.

78  : void
79  {
80  }

◆ tearDownAfterClass()

static CapabilityTest::tearDownAfterClass ( )
static

Definition at line 82 of file CapabilityTest.php.

82  : void
83  {
84  if (!self::$update_readme) {
85  return;
86  }
87  self::updateREADME();
88  }

◆ testCapabilityPriority()

CapabilityTest::testCapabilityPriority ( bool  $wopi_view,
bool  $wopi_edit,
bool  $infopage_first,
array  $permissions,
Capabilities  $expected_best 
)

environmentProvider

Definition at line 210 of file CapabilityTest.php.

References $context, $id, and ILIAS\Repository\access().

216  : void {
217  static $id;
218 
219  $id++;
220 
221  $context = new Context(
222  $id,
223  $id,
224  Context::CONTEXT_REPO
225  );
226 
227  $this->access->method('checkAccess')
228  ->willReturnCallback(
229  function (string $permission) use ($permissions): bool {
230  $checked_permissions = explode(',', $permission);
231  $common_permissions = array_intersect(
232  array_map(static fn(Permissions $p): string => $p->value, $permissions),
233  $checked_permissions
234  );
235  return $common_permissions !== [];
236  }
237  );
238 
239  $file_info = $this->createMock(\ilObjFileInfo::class);
240  $file_info->method('shouldDownloadDirectly')
241  ->willReturn(!$infopage_first);
242 
243  $this->file_info_repository->method('getByObjectId')
244  ->with($context->getObjectId())
245  ->willReturn($file_info);
246 
247  $this->action_repository->method('hasEditActionForSuffix')
248  ->willReturn($wopi_edit);
249 
250  $this->action_repository->method('hasViewActionForSuffix')
251  ->willReturn($wopi_view);
252 
253  $capabilities = $this->capability_builder->get($context);
254  $best = $capabilities->getBest();
255 
256  $this->assertEquals($expected_best, $best->getCapability());
257 
258  self::$readme_infos[] = [
259  implode(', ', array_map(fn(Permissions $p): string => $p->value, $permissions)), // permissions
260  ($wopi_view ? 'Yes' : 'No'),
261  ($wopi_edit ? 'Yes' : 'No'),
262  ($infopage_first ? 'Info-Page' : 'Open'),
263  $best->getCapability()->name
264  ];
265  }
$context
Definition: webdav.php:31
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
+ Here is the call graph for this function:

◆ updateREADME()

static CapabilityTest::updateREADME ( )
staticprivate

Definition at line 267 of file CapabilityTest.php.

References Vendor\Package\$a, and Vendor\Package\$b.

267  : void
268  {
269  // UPDATE README
270  $readme_file = __DIR__ . '/../../docs/README.md';
271  $readme_content = file_get_contents($readme_file);
272 
273  $table = [
274  [
275  'User\'s Permissions',
276  'WOPI View Action av.',
277  'WOPI Edit Action av.',
278  'Click-Setting',
279  'Expected Capability'
280  ]
281  ];
282  $readme_infos = self::$readme_infos;
283  // sort $readme_infos by last column
284  usort($readme_infos, static function ($a, $b): int {
285  $a_string = implode('', array_reverse($a));
286  $b_string = implode('', array_reverse($b));
287 
288  return strcmp((string) $a_string, (string) $b_string);
289  });
290 
291  $table = array_merge($table, $readme_infos);
292 
293  // Define the markers for the block
294  $start_marker = "<!-- START CAPABILITY_TABLE -->";
295  $end_marker = "<!-- END CAPABILITY_TABLE -->";
296 
297  // Prepare the new block content
298  $new_block = $start_marker . "\n\n" . self::arrayToMarkdownTable($table) . "\n\n" . $end_marker;
299 
300  // Replace the content between the markers
301  $pattern = '/' . preg_quote($start_marker, '/') . '.*?' . preg_quote($end_marker, '/') . '/s';
302  $readme_content = preg_replace($pattern, $new_block, $readme_content);
303 
304  file_put_contents($readme_file, $readme_content);
305  }
static array $readme_infos
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples

Field Documentation

◆ $access

ilAccessHandler MockObject CapabilityTest::$access
private

Definition at line 39 of file CapabilityTest.php.

◆ $action_repository

ActionRepository MockObject CapabilityTest::$action_repository
private

Definition at line 41 of file CapabilityTest.php.

◆ $capability_builder

CapabilityBuilder CapabilityTest::$capability_builder
private

Definition at line 43 of file CapabilityTest.php.

◆ $ctrl

ilCtrlInterface MockObject CapabilityTest::$ctrl
private

Definition at line 40 of file CapabilityTest.php.

◆ $file_info_repository

ilObjFileInfoRepository MockObject CapabilityTest::$file_info_repository
private

Definition at line 38 of file CapabilityTest.php.

◆ $http

Services MockObject CapabilityTest::$http
private

Definition at line 42 of file CapabilityTest.php.

◆ $readme_infos

array CapabilityTest::$readme_infos = []
staticprivate

Definition at line 45 of file CapabilityTest.php.

◆ $type_resolver

PHPUnit Framework MockObject MockObject TypeResolver CapabilityTest::$type_resolver

Definition at line 37 of file CapabilityTest.php.

◆ $update_readme

bool CapabilityTest::$update_readme = false
staticprivate

Definition at line 47 of file CapabilityTest.php.

◆ $workspace_access_handler

PHPUnit Framework MockObject MockObject CapabilityTest::$workspace_access_handler

Definition at line 36 of file CapabilityTest.php.


The documentation for this class was generated from the following file: