ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
BlocksTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24use Psr\Http\Message\ServerRequestInterface;
30use ILIAS\UI\Factory as UIFactory;
40use ILIAS\LegalDocuments\test\ContainerMock;
43use PHPUnit\Framework\TestCase;
45use ilObjUser;
46use ilSetting;
47use ilLanguage;
49use ilCtrl;
50use stdClass;
51use DateTimeImmutable;
53
54require_once __DIR__ . '/../ContainerMock.php';
55
56class BlocksTest extends TestCase
57{
58 use ContainerMock;
59
60 public function testConstruct(): void
61 {
62 $this->assertInstanceOf(Blocks::class, new Blocks('foo', $this->mock(Container::class), $this->mock(Provide::class)));
63 }
64
65 public function testConstructors(): void
66 {
67 $instance = new Blocks('foo', $this->mock(Container::class), $this->mock(Provide::class));
68
69 $this->assertInstanceOf(DefaultMappings::class, $instance->defaultMappings());
70 $this->assertInstanceOf(Marshal::class, $instance->marshal());
71 $this->assertInstanceOf(SelectSetting::class, $instance->selectSettingsFrom($this->mock(KeyValueStore::class)));
72 $this->assertInstanceOf(KeyValueStore::class, $instance->readOnlyStore($this->mock(KeyValueStore::class)));
73 $this->assertInstanceOf(KeyValueStore::class, $instance->userStore($this->mock(ilObjUser::class)));
74 $this->assertInstanceOf(KeyValueStore::class, $instance->sessionStore());
75 $this->assertInstanceOf(User::class, $instance->user(
76 $this->mock(Settings::class),
77 $this->mock(UserSettings::class),
78 $this->mock(ilObjUser::class)
79 ));
80 }
81
82 public function testGlobalStore(): void
83 {
84 $instance = new Blocks(
85 'foo',
86 $this->mockMethod(Container::class, 'settings', [], $this->mock(ilSetting::class)),
87 $this->mock(Provide::class)
88 );
89
90 $this->assertInstanceOf(KeyValueStore::class, $instance->globalStore());
91
92 }
93
94 public function testUi(): void
95 {
96 $container = $this->mock(Container::class);
97 $container->expects(self::once())->method('ui')->willReturn($this->mock(UIServices::class));
98 $container->expects(self::once())->method('language')->willReturn($this->mock(ilLanguage::class));
99
100 $instance = new Blocks(
101 'foo',
103 $this->mock(Provide::class)
104 );
105
106 $this->assertInstanceOf(UI::class, $instance->ui());
107 }
108
109 public function testRouting(): void
110 {
111 $container = $this->mockMethod(Container::class, 'ctrl', [], $this->mock(ilCtrl::class));
112
113 $instance = new Blocks('foo', $container, $this->mock(Provide::class));
114 $this->assertInstanceOf(Routing::class, $instance->routing());
115 }
116
117 public function testRetrieveQueryParameter(): void
118 {
119 $selected = new stdClass();
120 $called = false;
121
122 $refinery = $this->mock(Refinery::class);
123 $transformation = $this->mock(Transformation::class);
124
125 $container = $this->mockTree(Container::class, [
126 'refinery' => $refinery,
127 'http' => [
128 'wrapper' => [
129 'query' => $this->mockMethod(ArrayBasedRequestWrapper::class, 'retrieve', ['bar', $transformation], $selected)
130 ],
131 ],
132 ]);
133
134 $instance = new Blocks('foo', $container, $this->mock(Provide::class));
135 $result = $instance->retrieveQueryParameter('bar', function (Refinery $r) use (&$called, $transformation, $refinery): object {
136 $this->assertSame($refinery, $r);
137 $called = true;
138 return $transformation;
139 });
140
141 $this->assertSame($selected, $result);
142 $this->assertTrue($called);
143 }
144
145 public function testUserManagementAgreeDateField(): void
146 {
147 $result = $this->mockTree(Result::class, ['value' => 'dummy']);
148 $result->expects(self::once())->method('map')->willReturn($result);
149 $result->expects(self::once())->method('except')->willReturn($result);
150 $user = $this->mock(ilObjUser::class);
151 $date = new DateTimeImmutable();
152 $ldoc_user = $this->mockTree(User::class, [
153 'agreeDate' => ['value' => $date],
154 'acceptedVersion' => $result,
155 ]);
156
157 $language = $this->mock(ilLanguage::class);
158 $consecutive = ['dummy lang', 'ldoc'];
159 $language->expects(self::exactly(2))->method('loadLanguageModule')->with(
160 $this->callback(function ($value) use (&$consecutive) {
161 $this->assertSame(array_shift($consecutive), $value);
162 return true;
163 })
164 );
165
166 $instance = new Blocks(
167 'foo',
168 $this->mockTree(Container::class, ['language' => $language]),
169 $this->mock(Provide::class),
170 function (DateTimeImmutable $d) use ($date) {
171 $this->assertSame($date, $d);
172 return 'formatted date';
173 }
174 );
175 $proc = $instance->userManagementAgreeDateField(function (ilObjUser $u) use ($user, $ldoc_user) {
176 $this->assertSame($user, $u);
177 return $ldoc_user;
178 }, 'foo', 'dummy lang');
179
180 $this->assertSame(['foo' => 'dummy'], $proc($user));
181 }
182
183 public function testWithRequest(): void
184 {
185 $data = new stdClass();
186 $called = false;
187
188 $container = $this->mockTree(Container::class, [
189 'http' => ['request' => $this->mockMethod(ServerRequestInterface::class, 'getMethod', [], 'POST')],
190 ]);
191
192 $form = $this->mock(Form::class);
193 $form->expects(self::once())->method('withRequest')->with($container->http()->request())->willReturn($form);
194 $form->expects(self::once())->method('getData')->willReturn($data);
195
196 $instance = new Blocks('foo', $container, $this->mock(Provide::class));
197 $this->assertSame($form, $instance->withRequest($form, function ($x) use ($data, &$called) {
198 $this->assertSame($data, $x);
199 $called = true;
200 }));
201 $this->assertTrue($called);
202 }
203
204 public function testWithoutRequest(): void
205 {
206 $container = $this->mockTree(Container::class, [
207 'http' => ['request' => ['getMethod' => 'GET']],
208 ]);
209
210 $form = $this->mock(Form::class);
211
212 $instance = new Blocks('foo', $container, $this->mock(Provide::class));
213 $result = $instance->withRequest($form, $this->fail(...));
214 $this->assertSame($form, $result);
215 }
216
217 public function testNotAvailable(): void
218 {
219 $called = false;
220 $container = $this->mockTree(Container::class, []);
221
222 $instance = new Blocks('foo', $container, $this->mock(Provide::class), null, function () use (&$called): string {
223 $called = true;
224 return 'bar';
225 });
226
227 $result = $instance->notAvailable();
228 $this->assertInstanceOf(PageContent::class, $result);
229 $this->assertTrue($called);
230 }
231}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Provides fluid interface to RBAC services.
Definition: UIServices.php:25
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
userManagementAgreeDateField(callable $build_user, string $lang_key, ?string $module=null)
Definition: Blocks.php:145
Class ilCtrl provides processing control methods.
language handling
User class.
ILIAS Setting Class.
A transformation is a function from one datatype to another.
This describes commonalities between all forms.
Definition: Form.php:33
$container
@noRector
Definition: wac.php:37