ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
LinkTransferTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once(__DIR__ . "/../../Base.php");
22
25use PHPUnit\Framework\MockObject\Stub;
26use PHPUnit\Framework\MockObject\MockObject;
27
32{
33 protected string $uri_stub_value;
34 protected \ILIAS\Data\URI&Stub $uri_stub;
35 protected string $glyph_stub_html;
36 protected I\Symbol\Glyph\Glyph&Stub $glyph_stub;
37
38 protected string $button_mock_html;
39 protected I\Button\Standard&MockObject $button_mock;
40 protected string $modal_mock_html;
41 protected I\Modal\RoundTrip&MockObject $modal_mock;
42 protected string $image_mock_html;
43 protected I\Image\Image&MockObject $image_mock;
44
45 protected I\Button\Factory&MockObject $button_factory_mock;
46 protected I\Modal\Factory&MockObject $modal_factory_mock;
47 protected I\Image\Factory&MockObject $image_factory_mock;
48
49 protected \ILIAS\Refinery\Transformation&MockObject $to_svg_qr_code_mock;
50 protected \ILIAS\Refinery\Transformation&MockObject $from_svg_mock;
51
52 protected function setUp(): void
53 {
54 $this->uri_stub_value = sha1(\ILIAS\Data\URI::class);
55 $this->uri_stub = $this->createStub(\ILIAS\Data\URI::class);
56 $this->uri_stub->method('__toString')->willReturn($this->uri_stub_value);
57
58 $this->glyph_stub_html = sha1(I\Symbol\Glyph\Glyph::class);
59 $this->glyph_stub = $this->createStub(I\Symbol\Glyph\Glyph::class);
60 $this->glyph_stub->method('getCanonicalName')->willReturn($this->glyph_stub_html);
61 $this->glyph_stub->method('withLabel')->willReturnSelf();
62
63 $this->button_mock_html = sha1(I\Button\Standard::class);
64 $this->button_mock = $this->createMock(I\Button\Standard::class);
65 $this->button_mock->method('getCanonicalName')->willReturn($this->button_mock_html);
66
67 $this->modal_mock_html = sha1(I\Modal\RoundTrip::class);
68 $this->modal_mock = $this->createMock(I\Modal\RoundTrip::class);
69 $this->modal_mock->method('getCanonicalName')->willReturn($this->modal_mock_html);
70
71 $this->image_mock_html = sha1(I\Image\Image::class);
72 $this->image_mock = $this->createMock(I\Image\Image::class);
73 $this->image_mock->method('getCanonicalName')->willReturn($this->image_mock_html);
74
75 $this->button_factory_mock = $this->createMock(I\Button\Factory::class);
76 $this->button_factory_mock->method('standard')->willReturn($this->button_mock);
77
78 $this->modal_factory_mock = $this->createMock(I\Modal\Factory::class);
79 $this->modal_factory_mock->method('roundtrip')->willReturn($this->modal_mock);
80
81 $this->image_factory_mock = $this->createMock(I\Image\Factory::class);
82 $this->image_factory_mock->method('responsive')->willReturn($this->image_mock);
83
84 $this->to_svg_qr_code_mock = $this->createMock(\ILIAS\Refinery\Transformation::class);
85 $this->from_svg_mock = $this->createMock(\ILIAS\Refinery\Transformation::class);
86
88 }
89
90 public function getUIFactory(): \NoUIFactory
91 {
92 $glyph_factory_stub = $this->createStub(I\Symbol\Glyph\Factory::class);
93 $glyph_factory_stub->method($this->anything())->willReturn($this->glyph_stub);
94 $symbol_factory_stub = $this->createStub(I\Symbol\Factory::class);
95 $symbol_factory_stub->method('glyph')->willReturn($glyph_factory_stub);
96
97 $real_transfer_factory = new I\Transfer\Factory();
98
99 return new class (
100 $symbol_factory_stub,
104 $real_transfer_factory,
105 ) extends \NoUIFactory {
106 public function __construct(
107 protected I\Symbol\Factory $symbol_factory,
108 protected I\Button\Factory $button_factory,
109 protected I\Modal\Factory $modal_factory,
110 protected I\Image\Factory $image_factory,
111 protected I\Transfer\Factory $transfer_factory,
112 ) {
113 }
114 public function symbol(): I\Symbol\Factory
115 {
116 return $this->symbol_factory;
117 }
118 public function button(): I\Button\Factory
119 {
120 return $this->button_factory;
121 }
122 public function modal(): I\Modal\Factory
123 {
124 return $this->modal_factory;
125 }
126 public function image(): I\Image\Factory
127 {
128 return $this->image_factory;
129 }
130 public function transfer(): I\Transfer\Factory
131 {
132 return $this->transfer_factory;
133 }
134 };
135 }
136
137 public function getRefinery(): \ILIAS\Refinery\Factory & Stub
138 {
139 $uri_group_stub = $this->createStub(\ILIAS\Refinery\URI\Group::class);
140 $uri_group_stub->method('toSvgQrCode')->willReturn($this->to_svg_qr_code_mock);
141 $uri_group_stub->method('fromSvg')->willReturn($this->from_svg_mock);
142
143 $refinery_stub = $this->createStub(\ILIAS\Refinery\Factory::class);
144 $refinery_stub->method('uri')->willReturn($uri_group_stub);
145
146 return $refinery_stub;
147 }
148
150 {
151 $link_label = sha1('testRenderWithClipboardTransferMechanism');
152 $link_transfer = $this->getUIFactory()->transfer()->link(
153 C\Transfer\TransferMechanism::CLIPBOARD,
154 $this->uri_stub,
155 $link_label,
156 );
157
158 $renderer = $this->getDefaultRenderer(null, [$this->glyph_stub]);
159
160 $expected_transfer_type = C\Transfer\TransferMechanism::CLIPBOARD->value;
161 $expected = <<<HTML
162<div id="id_2" class="c-transfer c-transfer-link">
163 <label for="id_2_value">$link_label</label>
164 <div class="c-transfer__contents">
165 <input id="id_2_value" class="c-transfer__payload visible" type="url" value="$this->uri_stub_value" readonly="readonly">
166 <div class="c-tooltip__container">
167 <button data-transfer-type="$expected_transfer_type" class="btn btn-default" aria-label="copy_to_clipboard_aria">
168 <span data-transfer-status="default" class="visible">$this->glyph_stub_html</span>
169 <span data-transfer-status="success" data-transfer-message="copy_to_clipboard_success" class="hidden">$this->glyph_stub_html</span>
170 <span data-transfer-status="failure" data-transfer-message="copy_to_clipboard_failure" class="hidden">$this->glyph_stub_html</span>
171 <span>copy_to_clipboard</span>
172 </button>
173 <div id="id_1" role="status" class="c-tooltip c-tooltip--hidden">
174 <p></p>
175 </div>
176 </div>
177 </div>
178</div>
179HTML;
180
181 $actual = $renderer->render($link_transfer);
182
183 $this->assertSame($this->brutallyTrimHTML($expected), $this->brutallyTrimHTML($actual));
184 }
185
187 {
188 $link_label = sha1('testRenderWithWebShreTransferMechanism');
189 $link_transfer = $this->getUIFactory()->transfer()->link(
190 C\Transfer\TransferMechanism::WEB_SHARE,
191 $this->uri_stub,
192 $link_label,
193 );
194
195 $renderer = $this->getDefaultRenderer(null, [$this->glyph_stub]);
196
197 $expected_transfer_type = C\Transfer\TransferMechanism::WEB_SHARE->value;
198 $expected = <<<HTML
199<div id="id_2" class="c-transfer c-transfer-link">
200 <label for="id_2_value">$link_label</label>
201 <div class="c-transfer__contents">
202 <input id="id_2_value" class="c-transfer__payload visible" type="url" value="$this->uri_stub_value" readonly="readonly">
203 <div class="c-tooltip__container">
204 <button data-transfer-type="$expected_transfer_type" class="btn btn-default" aria-label="open_web_share_api_aria">
205 <span data-transfer-status="default" class="visible">$this->glyph_stub_html</span>
206 <span data-transfer-status="success" data-transfer-message="open_web_share_api_success" class="hidden">$this->glyph_stub_html</span>
207 <span data-transfer-status="failure" data-transfer-message="open_web_share_api_failure" class="hidden">$this->glyph_stub_html</span>
208 <span>open_web_share_api</span>
209 </button>
210 <div id="id_1" role="status" class="c-tooltip c-tooltip--hidden">
211 <p></p>
212 </div>
213 </div>
214 </div>
215</div>
216HTML;
217
218 $actual = $renderer->render($link_transfer);
219
220 $this->assertSame($this->brutallyTrimHTML($expected), $this->brutallyTrimHTML($actual));
221 }
222
224 {
225 $link_label = sha1('testRenderWithQrCodeTransferMechanism');
226
227 // values passed along the transformation chain
228 $svg_stub = $this->createStub(\ILIAS\Data\SVG::class);
229 $svg_data_uri = 'data:image/svg+xml;base64,' . base64_encode($link_label);
230 $show_signal = $this->createStub(C\Signal::class);
231
232 // test url transformations
233 $this->to_svg_qr_code_mock
234 ->expects($this->once())
235 ->method('transform')
236 ->with($this->identicalTo($this->uri_stub))
237 ->willReturn($svg_stub);
238 $this->from_svg_mock
239 ->expects($this->once())
240 ->method('transform')
241 ->with($this->identicalTo($svg_stub))
242 ->willReturn($svg_data_uri);
243
244 // test image compisition
245 $this->image_factory_mock
246 ->expects($this->once())
247 ->method('responsive')
248 ->with($svg_data_uri, $link_label);
249 $this->image_mock
250 ->expects($this->once())
251 ->method('withAction')
252 ->with($this->uri_stub_value)
253 ->willReturnSelf();
254
255 // test modal compisition
256 $this->modal_factory_mock
257 ->expects($this->once())
258 ->method('roundtrip')
259 ->with('use_qr_code', [$this->image_mock]);
260 $this->modal_mock
261 ->expects($this->once())
262 ->method('withCancelButtonLabel')
263 ->with('close')
264 ->willReturnSelf();
265 $this->modal_mock
266 ->expects($this->once())
267 ->method('withCloseWithKeyboard')
268 ->with(true)
269 ->willReturnSelf();
270 $this->modal_mock
271 ->expects($this->once())
272 ->method('getShowSignal')
273 ->willReturn($show_signal);
274
275 // test button compisition
276 $this->button_factory_mock
277 ->expects($this->once())
278 ->method('standard')
279 ->with('show_qr_code', '');
280 $this->button_mock
281 ->expects($this->once())
282 ->method('withAriaLabel')
283 ->with('show_qr_code_aria')
284 ->willReturnSelf();
285 $this->button_mock
286 ->expects($this->once())
287 ->method('withSymbol')
288 ->with($this->identicalTo($this->glyph_stub))
289 ->willReturnSelf();
290 $this->button_mock
291 ->expects($this->once())
292 ->method('withOnClick')
293 ->with($this->identicalTo($show_signal))
294 ->willReturnSelf();
295
296 $link_transfer = $this->getUIFactory()->transfer()->link(
298 $this->uri_stub,
299 $link_label,
300 );
301
302 // only the button and modal are rendered directly; the image and glyph
303 // are consumed by mocked components and never reach the renderer.
304 $renderer = $this->getDefaultRenderer(null, [$this->button_mock, $this->modal_mock]);
305
306 $expected = <<<HTML
307<div id="id_1" class="c-transfer c-transfer-link">
308 <label for="id_1_value">$link_label</label>
309 <div class="c-transfer__contents">
310 <input id="id_1_value" class="c-transfer__payload visible" type="url" value="$this->uri_stub_value" readonly="readonly">
311 $this->button_mock_html{$this->modal_mock_html}
312 </div>
313</div>
314HTML;
315
316 $actual = $renderer->render($link_transfer);
317
318 $this->assertSame($this->brutallyTrimHTML($expected), $this->brutallyTrimHTML($actual));
319 }
320
322 {
323 $initial = C\Transfer\TransferMechanism::CLIPBOARD;
324 $first = C\Transfer\TransferMechanism::WEB_SHARE;
326
327 $link_transfer = $this->getUIFactory()->transfer()->link($initial, $this->uri_stub, '');
328 $this->assertSame([$initial], array_values($link_transfer->getTransferMechanisms()));
329
330 $link_transfer = $link_transfer->withAdditionalTransferMechanism($first);
331 $this->assertSame([$initial, $first], array_values($link_transfer->getTransferMechanisms()));
332
333 $link_transfer = $link_transfer->withAdditionalTransferMechanism($second);
334 $this->assertSame([$initial, $first, $second], array_values($link_transfer->getTransferMechanisms()));
335
336 // should not alter the previous ordering
337 $link_transfer = $link_transfer->withAdditionalTransferMechanism($second);
338 $link_transfer = $link_transfer->withAdditionalTransferMechanism($first);
339
340 $this->assertSame([$initial, $first, $second], array_values($link_transfer->getTransferMechanisms()));
341 }
342}
$renderer
Provides common functionality for UI tests.
Definition: Base.php:346
testRenderWithClipboardTransferMechanism()
I Modal RoundTrip &MockObject $modal_mock
I Image Factory &MockObject $image_factory_mock
I Symbol Glyph Glyph &Stub $glyph_stub
I Modal Factory &MockObject $modal_factory_mock
ILIAS Refinery Transformation &MockObject $from_svg_mock
ILIAS Data URI &Stub $uri_stub
testWithAdditionalTransferMechanismOrderPreservation()
ILIAS Refinery Transformation &MockObject $to_svg_qr_code_mock
I Button Factory &MockObject $button_factory_mock
I Button Standard &MockObject $button_mock
I Image Image &MockObject $image_mock
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
modal(string $title="", string $cancel_label="")
button(string $caption, string $cmd)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Bulky.php:21
@ QR_CODE
Transfer information by copying its contents to the computers clipboard.
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.