ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
BlocksTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
47 use ilObjUser;
48 use ilSetting;
49 use ilLanguage;
51 use ilCtrl;
52 use stdClass;
55 
56 require_once __DIR__ . '/../ContainerMock.php';
57 
58 class BlocksTest extends TestCase
59 {
60  use ContainerMock;
61 
62  public function testConstruct(): void
63  {
64  $this->assertInstanceOf(Blocks::class, new Blocks('foo', $this->mock(Container::class), $this->mock(Provide::class)));
65  }
66 
67  public function testConstructors(): void
68  {
69  $instance = new Blocks('foo', $this->mock(Container::class), $this->mock(Provide::class));
70 
71  $this->assertInstanceOf(DefaultMappings::class, $instance->defaultMappings());
72  $this->assertInstanceOf(Marshal::class, $instance->marshal());
73  $this->assertInstanceOf(SelectSetting::class, $instance->selectSettingsFrom($this->mock(KeyValueStore::class)));
74  $this->assertInstanceOf(KeyValueStore::class, $instance->readOnlyStore($this->mock(KeyValueStore::class)));
75  $this->assertInstanceOf(KeyValueStore::class, $instance->userStore($this->mock(ilObjUser::class)));
76  $this->assertInstanceOf(KeyValueStore::class, $instance->sessionStore());
77  $this->assertInstanceOf(User::class, $instance->user(
78  $this->mock(Settings::class),
79  $this->mock(UserSettings::class),
80  $this->mock(ilObjUser::class)
81  ));
82  }
83 
84  public function testGlobalStore(): void
85  {
86  $instance = new Blocks(
87  'foo',
88  $this->mockMethod(Container::class, 'settings', [], $this->mock(ilSetting::class)),
89  $this->mock(Provide::class)
90  );
91 
92  $this->assertInstanceOf(KeyValueStore::class, $instance->globalStore());
93 
94  }
95 
96  public function testUi(): void
97  {
98  $container = $this->mock(Container::class);
99  $container->expects(self::once())->method('ui')->willReturn($this->mock(UIServices::class));
100  $container->expects(self::once())->method('language')->willReturn($this->mock(ilLanguage::class));
101 
102  $instance = new Blocks(
103  'foo',
104  $container,
105  $this->mock(Provide::class)
106  );
107 
108  $this->assertInstanceOf(UI::class, $instance->ui());
109  }
110 
111  public function testRouting(): void
112  {
113  $container = $this->mockMethod(Container::class, 'ctrl', [], $this->mock(ilCtrl::class));
114 
115  $instance = new Blocks('foo', $container, $this->mock(Provide::class));
116  $this->assertInstanceOf(Routing::class, $instance->routing());
117  }
118 
119  public function testRetrieveQueryParameter(): void
120  {
121  $selected = new stdClass();
122  $called = false;
123 
124  $refinery = $this->mock(Refinery::class);
125  $transformation = $this->mock(Transformation::class);
126 
127  $container = $this->mockTree(Container::class, [
128  'refinery' => $refinery,
129  'http' => [
130  'wrapper' => [
131  'query' => $this->mockMethod(ArrayBasedRequestWrapper::class, 'retrieve', ['bar', $transformation], $selected)
132  ],
133  ],
134  ]);
135 
136  $instance = new Blocks('foo', $container, $this->mock(Provide::class));
137  $result = $instance->retrieveQueryParameter('bar', function (Refinery $r) use (&$called, $transformation, $refinery): object {
138  $this->assertSame($refinery, $r);
139  $called = true;
140  return $transformation;
141  });
142 
143  $this->assertSame($selected, $result);
144  $this->assertTrue($called);
145  }
146 
147  public function testUserManagementAgreeDateField(): void
148  {
149  $result = $this->mockTree(Result::class, ['value' => 'dummy']);
150  $result->expects(self::once())->method('map')->willReturn($result);
151  $result->expects(self::once())->method('except')->willReturn($result);
152  $user = $this->mock(ilObjUser::class);
153  $date = new DateTimeImmutable();
154  $ldoc_user = $this->mockTree(User::class, [
155  'agreeDate' => ['value' => $date],
156  'acceptedVersion' => $result,
157  ]);
158 
159  $language = $this->mock(ilLanguage::class);
160  $language->expects(self::exactly(2))->method('loadLanguageModule')->withConsecutive(['dummy lang'], ['ldoc']);
161 
162  $instance = new Blocks(
163  'foo',
164  $this->mockTree(Container::class, ['language' => $language]),
165  $this->mock(Provide::class),
166  function (DateTimeImmutable $d) use ($date) {
167  $this->assertSame($date, $d);
168  return 'formatted date';
169  }
170  );
171  $proc = $instance->userManagementAgreeDateField(function (ilObjUser $u) use ($user, $ldoc_user) {
172  $this->assertSame($user, $u);
173  return $ldoc_user;
174  }, 'foo', 'dummy lang');
175 
176  $this->assertSame(['foo' => 'dummy'], $proc($user));
177  }
178 
179  public function testWithRequest(): void
180  {
181  $data = new stdClass();
182  $called = false;
183 
184  $container = $this->mockTree(Container::class, [
185  'http' => ['request' => $this->mockMethod(ServerRequestInterface::class, 'getMethod', [], 'POST')],
186  ]);
187 
188  $form = $this->mock(Form::class);
189  $form->expects(self::once())->method('withRequest')->with($container->http()->request())->willReturn($form);
190  $form->expects(self::once())->method('getData')->willReturn($data);
191 
192  $instance = new Blocks('foo', $container, $this->mock(Provide::class));
193  $this->assertSame($form, $instance->withRequest($form, function ($x) use ($data, &$called) {
194  $this->assertSame($data, $x);
195  $called = true;
196  }));
197  $this->assertTrue($called);
198  }
199 
200  public function testWithoutRequest(): void
201  {
202  $container = $this->mockTree(Container::class, [
203  'http' => ['request' => ['getMethod' => 'GET']],
204  ]);
205 
206  $form = $this->mock(Form::class);
207 
208  $instance = new Blocks('foo', $container, $this->mock(Provide::class));
209  $result = $instance->withRequest($form, $this->fail(...));
210  $this->assertSame($form, $result);
211  }
212 
213  public function testNotAvailable(): void
214  {
215  $called = false;
216  $container = $this->mockTree(Container::class, []);
217 
218  $instance = new Blocks('foo', $container, $this->mock(Provide::class), null, function () use (&$called): string {
219  $called = true;
220  return 'bar';
221  });
222 
223  $result = $instance->notAvailable();
224  $this->assertInstanceOf(PageContent::class, $result);
225  $this->assertTrue($called);
226  }
227 }
$container
Definition: wac.php:14
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
userManagementAgreeDateField(callable $build_user, string $lang_key, ?string $module=null)
Definition: Blocks.php:145
$r
Refinery Factory $refinery