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