ILIAS  trunk Revision v11.0_alpha-2662-g519ff7d528f
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 $user_permissions, Capabilities $expected_best)
 

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
 
ActionRepository MockObject $action_repository
 
CapabilityBuilder $capability_builder
 

Static Private Attributes

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

Detailed Description

Definition at line 32 of file CapabilityTest.php.

Member Function Documentation

◆ arrayToMarkdownTable()

static CapabilityTest::arrayToMarkdownTable ( array  $data)
staticprivate

Definition at line 315 of file CapabilityTest.php.

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

◆ environmentProvider()

static CapabilityTest::environmentProvider ( )
static

Definition at line 91 of file CapabilityTest.php.

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

◆ setUp()

CapabilityTest::setUp ( )
protected

Definition at line 48 of file CapabilityTest.php.

References $http, $static_url, and ILIAS\Repository\access().

48  : void
49  {
50  if (!defined('ILIAS_HTTP_PATH')) {
51  define('ILIAS_HTTP_PATH', 'https://ilias.unit.test');
52  }
53 
54  $this->file_info_repository = $this->createMock(\ilObjFileInfoRepository::class);
55  $this->access = $this->createMock(\ilAccessHandler::class);
56  $ctrl = $this->createMock(\ilCtrlInterface::class);
57  $this->action_repository = $this->createMock(ActionRepository::class);
58  $http = $this->createMock(Services::class);
59  $static_url = $this->createMock(URIBuilder::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  $ctrl,
71  $this->action_repository,
72  $http,
74  $this->type_resolver,
75  $this->workspace_access_handler
76  );
77  }
$http
Definition: deliver.php:30
$static_url
Definition: goto.php:29
+ Here is the call graph for this function:

◆ tearDown()

CapabilityTest::tearDown ( )
protected

Definition at line 79 of file CapabilityTest.php.

79  : void
80  {
81  }

◆ tearDownAfterClass()

static CapabilityTest::tearDownAfterClass ( )
static

Definition at line 83 of file CapabilityTest.php.

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

◆ testCapabilityPriority()

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

Definition at line 217 of file CapabilityTest.php.

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

223  : void {
224  static $id;
225  $permissions = $user_permissions;
226 
227  $id++;
228 
229  $context = new Context(
230  $id,
231  $id,
232  Context::CONTEXT_REPO
233  );
234 
235  $this->access->method('checkAccess')
236  ->willReturnCallback(
237  function (string $permission) use ($permissions): bool {
238  $checked_permissions = explode(',', $permission);
239  $common_permissions = array_intersect(
240  array_map(static fn(Permissions $p): string => $p->value, $permissions),
241  $checked_permissions
242  );
243  return $common_permissions !== [];
244  }
245  );
246 
247  $file_info = $this->createMock(\ilObjFileInfo::class);
248  $file_info->method('shouldDownloadDirectly')
249  ->willReturn(!$infopage_first);
250 
251  $this->file_info_repository->method('getByObjectId')
252  ->with($context->getObjectId())
253  ->willReturn($file_info);
254 
255  $this->action_repository->method('hasEditActionForSuffix')
256  ->willReturn($wopi_edit);
257 
258  $this->action_repository->method('hasViewActionForSuffix')
259  ->willReturn($wopi_view);
260 
261  $capabilities = $this->capability_builder->get($context);
262  $best = $capabilities->getBest();
263 
264  $this->assertEquals($expected_best, $best->getCapability());
265 
266  self::$readme_infos[] = [
267  implode(', ', array_map(fn(Permissions $p): string => $p->value, $permissions)), // permissions
268  ($wopi_view ? 'Yes' : 'No'),
269  ($wopi_edit ? 'Yes' : 'No'),
270  ($infopage_first ? 'Info-Page' : 'Open'),
271  $best->getCapability()->name
272  ];
273  }
$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 275 of file CapabilityTest.php.

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

275  : void
276  {
277  // UPDATE README
278  $readme_file = __DIR__ . '/../../docs/README.md';
279  $readme_content = file_get_contents($readme_file);
280 
281  $table = [
282  [
283  'User\'s Permissions',
284  'WOPI View Action av.',
285  'WOPI Edit Action av.',
286  'Click-Setting',
287  'Expected Capability'
288  ]
289  ];
290  $readme_infos = self::$readme_infos;
291  // sort $readme_infos by last column
292  usort($readme_infos, static function ($a, $b): int {
293  $a_string = implode('', array_reverse($a));
294  $b_string = implode('', array_reverse($b));
295 
296  return strcmp((string) $a_string, (string) $b_string);
297  });
298 
299  $table = array_merge($table, $readme_infos);
300 
301  // Define the markers for the block
302  $start_marker = "<!-- START CAPABILITY_TABLE -->";
303  $end_marker = "<!-- END CAPABILITY_TABLE -->";
304 
305  // Prepare the new block content
306  $new_block = $start_marker . "\n\n" . self::arrayToMarkdownTable($table) . "\n\n" . $end_marker;
307 
308  // Replace the content between the markers
309  $pattern = '/' . preg_quote($start_marker, '/') . '.*?' . preg_quote($end_marker, '/') . '/s';
310  $readme_content = preg_replace($pattern, $new_block, $readme_content);
311 
312  file_put_contents($readme_file, $readme_content);
313  }
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 40 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 42 of file CapabilityTest.php.

◆ $file_info_repository

ilObjFileInfoRepository MockObject CapabilityTest::$file_info_repository
private

Definition at line 39 of file CapabilityTest.php.

◆ $readme_infos

array CapabilityTest::$readme_infos = []
staticprivate

Definition at line 44 of file CapabilityTest.php.

◆ $type_resolver

PHPUnit Framework MockObject MockObject TypeResolver CapabilityTest::$type_resolver

Definition at line 38 of file CapabilityTest.php.

◆ $update_readme

bool CapabilityTest::$update_readme = false
staticprivate

Definition at line 46 of file CapabilityTest.php.

◆ $workspace_access_handler

PHPUnit Framework MockObject MockObject CapabilityTest::$workspace_access_handler

Definition at line 37 of file CapabilityTest.php.


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