ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ConductorTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
29 use ILIAS\Refinery\In\Group as InGroup;
47 use ilCtrl;
50 use ilObjUser;
53 use Closure;
54 
55 require_once __DIR__ . '/ContainerMock.php';
56 
57 class ConductorTest extends TestCase
58 {
59  use ContainerMock;
60 
61  public function testConstruct(): void
62  {
63  $this->assertInstanceOf(Conductor::class, new Conductor($this->mock(Container::class), $this->mock(Internal::class), $this->mock(Routing::class)));
64  }
65 
66  public function testProvide(): void
67  {
68  $instance = new Conductor($this->mock(Container::class), $this->mock(Internal::class), $this->mock(Routing::class));
69  $this->assertInstanceOf(Provide::class, $instance->provide('foo'));
70  }
71 
72  public function testOnLogout(): void
73  {
74  $constraint = $this->mock(Constraint::class);
75  $called = false;
76 
77  $ctrl = $this->mock(ilCtrl::class);
78  $ctrl->expects(self::once())->method('setParameterByClass')->with('dummy gui', 'withdraw_from', 'foo');
79 
80  $container = $this->mockTree(Container::class, [
81  'refinery' => ['to' => ['string' => $constraint]],
82  'http' => ['wrapper' => ['query' => $this->mockMethod(ArrayBasedRequestWrapper::class, 'retrieve', ['withdraw_consent', $constraint], 'foo')]],
83  'ctrl' => $ctrl,
84  ]);
85 
86  $internal = $this->mockMethod(Internal::class, 'get', ['logout', 'foo'], static function () use (&$called): void {
87  $called = true;
88  });
89 
90  $instance = new Conductor($container, $internal, $this->mock(Routing::class));
91 
92  $instance->onLogout('dummy gui');
93  $this->assertTrue($called);
94  }
95 
96  public function testLoginPageHTML(): void
97  {
98  $components = [
99  $this->mock(Component::class),
100  $this->mock(Component::class),
101  ];
102 
103  $space = $this->mock(Legacy::class);
104 
105  $container = $this->mockTree(Container::class, [
106  'ui' => [
107  'renderer' => $this->mockMethod(Renderer::class, 'render', [
108  $components,
109  ], 'rendered'),
110  ],
111  ]);
112 
113  $internal = $this->mockMethod(Internal::class, 'get', ['show-on-login-page', 'foo'], fn() => $components);
114 
115  $instance = new Conductor($container, $internal, $this->mock(Routing::class));
116 
117  $this->assertSame('rendered', $instance->loginPageHTML('foo'));
118  }
119 
120  public function testLogoutText(): void
121  {
122  $constraint = $this->mock(Constraint::class);
123  $component = $this->mock(Component::class);
124 
125  $container = $this->mockTree(Container::class, [
126  'refinery' => ['to' => ['string' => $constraint]],
127  'http' => ['wrapper' => ['query' => $this->mockMethod(ArrayBasedRequestWrapper::class, 'retrieve', ['withdraw_from', $constraint], 'foo')]],
128  'ui' => ['renderer' => $this->mockMethod(Renderer::class, 'render', [$component], 'rendered')],
129  ]);
130 
131  $internal = $this->mockMethod(Internal::class, 'get', ['logout-text', 'foo'], static function () use ($component): Component {
132  return $component;
133  });
134 
135  $instance = new Conductor($container, $internal, $this->mock(Routing::class));
136 
137  $this->assertSame('rendered', $instance->logoutText());
138  ;
139  }
140 
141  public function testModifyFooter(): void
142  {
143  $footer = $this->mock(Footer::class);
144  $new_footer = $this->mock(Footer::class);
145 
146  $modify_footer = function ($f) {
147  $this->assertInstanceOf(Closure::class, $f);
148  return $f;
149  };
150 
151  $container = $this->mockTree(Container::class, ['ui' => ['factory' => ['mainControls' => ['footer' => $new_footer]]]]);
152  $instance = new Conductor($container, $this->mockMethod(Internal::class, 'all', ['footer'], [
153  $modify_footer,
154  $modify_footer,
155  ]), $this->mock(Routing::class));
156 
157  $this->assertSame($new_footer, $instance->modifyFooter($footer));
158  }
159 
163  public function testAgree(string $gui, string $key): void
164  {
165  $main_template = $this->mock(ilGlobalTemplateInterface::class);
166  $main_template->expects(self::once())->method('setContent')->with('rendered');
167  $this->agreement('agree', $gui, $key, $main_template);
168  }
169 
173  public function testAgreeContent(string $gui, string $key): void
174  {
175  $this->assertSame('rendered', $this->agreement('agreeContent', $gui, $key));
176  }
177 
178  public function testRedirectAgreeContent(): void
179  {
180  $this->expectExceptionMessage('Not available.');
181  $routing = $this->mock(Routing::class);
182  $routing->expects(self::once())->method('redirectToOriginalTarget');
183  $constraint = $this->mock(Constraint::class);
184  $container = $this->mockTree(Container::class, [
185  'refinery' => ['to' => ['string' => $constraint]],
186  'http' => ['wrapper' => ['query' => $this->mockMethod(ArrayBasedRequestWrapper::class, 'retrieve', ['id', $constraint], 'foo')]],
187  ]);
188  $internal = $this->mock(Internal::class);
189  $internal = $this->mockMethod(Internal::class, 'get', ['agreement-form', 'foo'], function (string $g, string $cmd): Result {
190  return new Error('Not available.');
191  });
192  $instance = new Conductor($container, $internal, $routing);
193  $instance->agreeContent(ilLegalDocumentsAgreementGUI::class, 'foo');
194  }
195 
196  public function testWithdraw(): void
197  {
198  $main_template = $this->mock(ilGlobalTemplateInterface::class);
199  $main_template->expects(self::once())->method('setContent')->with('rendered');
200  $this->agreement('withdraw', 'foo', 'withdraw', $main_template);
201  }
202 
203  public function testUsersWithHiddenOnlineStatus(): void
204  {
205  $internal = $this->mockMethod(Internal::class, 'all', ['filter-online-users'], [
206  fn() => [4, 5, 6],
207  fn() => [5, 6],
208  ]);
209  $instance = new Conductor($this->mock(Container::class), $internal, $this->mock(Routing::class));
210 
211  $this->assertSame(
212  [1, 2, 3, 4, 7, 8, 9],
213  $instance->usersWithHiddenOnlineStatus([1, 2, 3, 4, 5, 6, 7, 8, 9])
214  );
215  }
216 
217  public function testUserCanReadInternalMail(): void
218  {
219  $constraints = [
220  'foo' => $this->mock(Constraint::class),
221  'bar' => $this->mock(Constraint::class),
222  ];
223 
224  $series = $this->mock(Transformation::class);
225 
226  $container = $this->mockTree(Container::class, [
227  'refinery' => ['in' => $this->mockMethod(
228  InGroup::class,
229  'series',
230  [array_values($constraints)],
231  $series
232  )],
233  ]);
234 
235  $internal = $this->mockMethod(Internal::class, 'all', ['constrain-internal-mail'], $constraints);
236 
237  $instance = new Conductor($container, $internal, $this->mock(Routing::class));
238  $this->assertSame($series, $instance->userCanReadInternalMail());
239  }
240 
241  public function testCanUseSoapApi(): void
242  {
243  $constraints = [
244  'foo' => $this->mock(Constraint::class),
245  'bar' => $this->mock(Constraint::class),
246  ];
247 
248  $container = $this->mockTree(Container::class, [
249  'refinery' => ['in' => $this->mockMethod(InGroup::class, 'series', [array_values($constraints)], $this->mock(Transformation::class))]
250  ]);
251 
252  $internal = $this->mockMethod(Internal::class, 'all', ['use-soap-api'], $constraints);
253 
254  $instance = new Conductor($container, $internal, $this->mock(Routing::class));
255  $this->assertInstanceOf(Transformation::class, $instance->canUseSoapApi());
256  }
257 
258  public function testAfterLogin(): void
259  {
260  $called = [false, false];
261 
262  $internal = $this->mockMethod(Internal::class, 'all', ['after-login'], [
263  function () use (&$called): void {
264  $called[0] = true;
265  },
266  function () use (&$called): void {
267  $called[1] = true;
268  }
269  ]);
270 
271  $instance = new Conductor($this->mock(Container::class), $internal, $this->mock(Routing::class));
272 
273  $instance->afterLogin();
274 
275  $this->assertSame([true, true], $called);
276  }
277 
278  public function testFindGotoLink(): void
279  {
280  $foo = $this->mock(Target::class);
281  $internal = $this->mockMethod(Internal::class, 'all', ['goto'], [
282  $this->mockTree(GotoLink::class, ['name' => 'bar']),
283  $this->mockTree(GotoLink::class, ['name' => 'foo', 'target' => $foo]),
284  ]);
285 
286  $instance = new Conductor($this->mock(Container::class), $internal, $this->mock(Routing::class));
287 
288  $target = $instance->findGotoLink('foo');
289  $this->assertTrue($target->isOk());
290  $this->assertSame($foo, $target->value());
291  }
292 
293 
294  public function testIntercepting(): void
295  {
296  $intercepting = [
297  'foo' => $this->mock(Intercept::class),
298  'bar' => $this->mock(Intercept::class)
299  ];
300 
301  $internal = $this->mockMethod(Internal::class, 'all', ['intercept'], $intercepting);
302 
303  $instance = new Conductor($this->mock(Container::class), $internal, $this->mock(Routing::class));
304  $this->assertSame($intercepting, $instance->intercepting());
305  }
306 
307  public function testSelfRegistration(): void
308  {
309  $internal = $this->mockMethod(Internal::class, 'all', ['self-registration'], [
310  $this->mock(SelfRegistration::class),
311  ]);
312 
313  $instance = new Conductor($this->mock(Container::class), $internal, $this->mock(Routing::class));
314 
315  $this->assertInstanceOf(Bundle::class, $instance->selfRegistration());
316  }
317 
318  public function testUserManagementFields(): void
319  {
320  $internal = $this->mockMethod(Internal::class, 'all', ['user-management-fields'], [
321  fn() => ['foo' => 'bar', 'baz' => 'hej'],
322  fn() => ['hoo' => 'har'],
323  ]);
324 
325  $instance = new Conductor($this->mock(Container::class), $internal, $this->mock(Routing::class));
326 
327  $this->assertSame([
328  'foo' => 'bar',
329  'baz' => 'hej',
330  'hoo' => 'har',
331  ], $instance->userManagementFields($this->mock(ilObjUser::class)));
332  }
333 
334  public function agreeTypes(): array
335  {
336  return [
337  'Form type' => [ilLegalDocumentsAgreementGUI::class, 'agreement-form'],
338  'Public type' => ['foo', 'public-page'],
339  ];
340  }
341 
342  private function agreement(string $method, string $gui, string $key, ?ilGlobalTemplateInterface $main_template = null)
343  {
344  $constraint = $this->mock(Constraint::class);
345 
346  $ctrl = $this->mock(ilCtrl::class);
347  $ctrl->expects(self::once())->method('setParameterByClass')->with($gui, 'id', 'foo');
348 
349  $container = $this->mockTree(Container::class, [
350  'refinery' => ['to' => ['string' => $constraint]],
351  'http' => ['wrapper' => ['query' => $this->mockMethod(ArrayBasedRequestWrapper::class, 'retrieve', ['id', $constraint], 'foo')]],
352  'ui' => [
353  'mainTemplate' => $main_template ?? $this->mock(ilGlobalTemplateInterface::class),
354  'renderer' => $this->mock(Renderer::class),
355  ],
356  'ctrl' => $ctrl,
357  ]);
358 
359  $fragment = $this->mockMethod(PageFragment::class, 'render', [$container->ui()->mainTemplate(), $container->ui()->renderer()], 'rendered');
360 
361  $internal = $this->mockMethod(Internal::class, 'get', [$key, 'foo'], function (string $g, string $cmd) use ($fragment, $gui): Result {
362  $this->assertSame($gui, $g);
363  $this->assertSame('some cmd', $cmd);
364  return new Ok($fragment);
365  });
366 
367  $instance = new Conductor($container, $internal, $this->mock(Routing::class));
368 
369  return $instance->$method($gui, 'some cmd');
370  }
371 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:14
$space
Definition: Sanitizer.php:43
$container
Definition: wac.php:14
agreement(string $method, string $gui, string $key, ?ilGlobalTemplateInterface $main_template=null)
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:16
string $key
Consumer key/client ID value.
Definition: System.php:193
testAgree(string $gui, string $key)
agreeTypes
testAgreeContent(string $gui, string $key)
agreeTypes