ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
ChartBarTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23
28{
29 protected function getFactory(): C\Chart\Bar\Factory
30 {
31 return new I\Component\Chart\Bar\Factory();
32 }
33
34 public function getDataFactory(): ILIAS\Data\Factory
35 {
36 return new ILIAS\Data\Factory();
37 }
38
39 public function getUIFactory(): NoUIFactory
40 {
41 return new class () extends NoUIFactory {
42 public function listing(): I\Component\Listing\Factory
43 {
44 return new I\Component\Listing\Factory(
45 new I\Component\Listing\Workflow\Factory(),
46 new I\Component\Listing\CharacteristicValue\Factory(),
47 new I\Component\Listing\Entity\Factory(),
48 );
49 }
50 };
51 }
52
53 protected function getSimpleDataset(): \ILIAS\Data\Chart\Dataset
54 {
55 $df = $this->getDataFactory();
56
57 $c_dimension = $df->dimension()->cardinal();
58
59 $dataset = $df->dataset(["Dataset" => $c_dimension]);
60 $dataset = $dataset->withPoint("Item", ["Dataset" => 0]);
61
62 return $dataset;
63 }
64
65 protected function getExtendedDataset(): \ILIAS\Data\Chart\Dataset
66 {
67 $df = $this->getDataFactory();
68
69 $c_dimension = $df->dimension()->cardinal();
70 $t_dimension = $df->dimension()->range($c_dimension);
71
72 $dataset = $df->dataset(["Dataset 1" => $c_dimension, "Dataset 2" => $t_dimension]);
73 $dataset = $dataset->withPoint("Item 1", ["Dataset 1" => -1.25, "Dataset 2" => [-2, -1]]);
74 $dataset = $dataset->withPoint("Item 2", ["Dataset 1" => null, "Dataset 2" => [0, 0.5]]);
75
76 return $dataset;
77 }
78
79 public function testImplementsFactoryInterface(): void
80 {
81 $f = $this->getFactory();
82
83 $this->assertInstanceOf("ILIAS\\UI\\Component\\Chart\\Bar\\Factory", $f);
84 }
85
86 public function testGetInstances(): void
87 {
88 $f = $this->getFactory();
89
90 $dataset = $this->getSimpleDataset();
91
92 $horizontal = $f->horizontal(
93 "Horizontal Bar",
94 $dataset
95 );
96
97 $this->assertInstanceOf("ILIAS\\UI\\Component\\Chart\\Bar\\Bar", $horizontal);
98 $this->assertInstanceOf("ILIAS\\UI\\Component\\Chart\\Bar\\Horizontal", $horizontal);
99
100 $vertical = $f->vertical(
101 "Vertical Bar",
102 $dataset
103 );
104 $this->assertInstanceOf("ILIAS\\UI\\Component\\Chart\\Bar\\Bar", $vertical);
105 $this->assertInstanceOf("ILIAS\\UI\\Component\\Chart\\Bar\\Vertical", $vertical);
106 }
107
108 public function testEmptyDataset(): void
109 {
110 $f = $this->getFactory();
111 $df = $this->getDataFactory();
112
113 $c_dimension = $df->dimension()->cardinal();
114
115 $dataset = $df->dataset(["Dataset" => $c_dimension]);
116
117 $this->expectException(LogicException::class);
118 $this->expectExceptionMessage("Dataset must not be empty.");
119 $horizontal = $f->horizontal(
120 "Horizontal Bar",
121 $dataset
122 );
123 }
124
125 // This test can only be performed when there will be a Dimension in the future, which is not compatible with Bar Charts
126 /*
127 public function testInvalidDatasetDimension() : void
128 {
129 $f = $this->getFactory();
130 $df = $this->getDataFactory();
131
132 $o_dimension = $df->dimension()->ordinal();
133
134 $dataset = $df->dataset(["Dataset" => $o_dimension]);
135
136 $this->expectException(InvalidArgumentException::class);
137 $this->expectExceptionMessage("Expected parameter to be a CardinalDimension or RangeDimension.");
138 $horizontal = $f->horizontal(
139 "Horizontal Bar",
140 $dataset
141 );
142 }
143 */
144
145 public function testWithTitle(): void
146 {
147 $f = $this->getFactory();
148
149 $dataset = $this->getSimpleDataset();
150
151 $horizontal = $f->horizontal(
152 "Horizontal Bar",
153 $dataset
154 );
155 $horizontal1 = $horizontal->withTitle("Alternative title for Horizontal Bar");
156
157 $this->assertEquals("Horizontal Bar", $horizontal->getTitle());
158 $this->assertEquals("Alternative title for Horizontal Bar", $horizontal1->getTitle());
159 }
160
161 public function testWithTitleInvisible(): void
162 {
163 $f = $this->getFactory();
164
165 $dataset = $this->getSimpleDataset();
166
167 $horizontal = $f->horizontal(
168 "Horizontal Bar",
169 $dataset
170 );
171 $horizontal1 = $horizontal->withTitleVisible(false);
172
173 $this->assertEquals(true, $horizontal->isTitleVisible());
174 $this->assertEquals(false, $horizontal1->isTitleVisible());
175 }
176
177 public function testWithTooltipsInvisible(): void
178 {
179 $f = $this->getFactory();
180
181 $dataset = $this->getSimpleDataset();
182
183 $horizontal = $f->horizontal(
184 "Horizontal Bar",
185 $dataset
186 );
187 $horizontal1 = $horizontal->withTooltipsVisible(false);
188
189 $this->assertEquals(true, $horizontal->isTooltipsVisible());
190 $this->assertEquals(false, $horizontal1->isTooltipsVisible());
191 }
192
193 public function testWithLegendInvisible(): void
194 {
195 $f = $this->getFactory();
196
197 $dataset = $this->getSimpleDataset();
198
199 $horizontal = $f->horizontal(
200 "Horizontal Bar",
201 $dataset
202 );
203 $horizontal1 = $horizontal->withLegendVisible(false);
204
205 $this->assertEquals(true, $horizontal->isLegendVisible());
206 $this->assertEquals(false, $horizontal1->isLegendVisible());
207 }
208
209 public function testWithLegendPosition(): void
210 {
211 $f = $this->getFactory();
212
213 $dataset = $this->getSimpleDataset();
214
215 $horizontal = $f->horizontal(
216 "Horizontal Bar",
217 $dataset
218 );
219 $horizontal1 = $horizontal->withLegendPosition("left");
220
221 $this->assertEquals("top", $horizontal->getLegendPosition());
222 $this->assertEquals("left", $horizontal1->getLegendPosition());
223 }
224
225 public function testWithInvalidLegendPosition(): void
226 {
227 $f = $this->getFactory();
228
229 $dataset = $this->getSimpleDataset();
230
231 $horizontal = $f->horizontal(
232 "Horizontal Bar",
233 $dataset
234 );
235
236 $this->expectException(InvalidArgumentException::class);
237 $this->expectExceptionMessage("Position must be 'bottom', 'top', 'left' or 'right'.");
238
239 $horizontal = $horizontal->withLegendPosition("middle");
240 }
241
242 public function testWithDataset(): void
243 {
244 $f = $this->getFactory();
245
246 $s_dataset = $this->getSimpleDataset();
247 $e_dataset = $this->getExtendedDataset();
248
249 $horizontal = $f->horizontal(
250 "Horizontal Bar",
251 $s_dataset
252 );
253 $horizontal1 = $horizontal->withDataset($e_dataset);
254
255 $this->assertEquals($s_dataset, $horizontal->getDataset());
256 $this->assertEquals($e_dataset, $horizontal1->getDataset());
257 }
258
259 public function testWithBarConfigs(): void
260 {
261 $f = $this->getFactory();
262 $df = $this->getDataFactory();
263
264 $dataset = $this->getSimpleDataset();
265
266 $bc = new C\Chart\Bar\BarConfig();
267 $bc = $bc->withColor($df->color("#d38000"));
268
269 $bars = [
270 "Dataset" => $bc,
271 ];
272
273 $horizontal = $f->horizontal(
274 "Horizontal Bar",
275 $dataset
276 );
277 $horizontal1 = $horizontal->withBarConfigs($bars);
278
279 $this->assertEquals([], $horizontal->getBarConfigs());
280 $this->assertEquals($bars, $horizontal1->getBarConfigs());
281 }
282
283 public function testWithGroupConfigs(): void
284 {
285 $f = $this->getFactory();
286 $df = $this->getDataFactory();
287
288 $dataset = $this->getSimpleDataset();
289
290 $gc = new C\Chart\Bar\GroupConfig();
291 $gc = $gc->withStacked();
292
293 $groups = [
294 "Group" => $gc,
295 ];
296
297 $horizontal = $f->horizontal(
298 "Horizontal Bar",
299 $dataset
300 );
301 $horizontal1 = $horizontal->withGroupConfigs($groups);
302
303 $this->assertEquals([], $horizontal->getGroupConfigs());
304 $this->assertEquals($groups, $horizontal1->getGroupConfigs());
305 }
306
307 public function testIndexAxis(): void
308 {
309 $f = $this->getFactory();
310 $df = $this->getDataFactory();
311
312 $dataset = $this->getSimpleDataset();
313
314 $horizontal = $f->horizontal(
315 "Horizontal Bar",
316 $dataset
317 );
318
319 $this->assertEquals("y", $horizontal->getIndexAxis());
320
321 $vertical = $f->vertical(
322 "Vertical Bar",
323 $dataset
324 );
325
326 $this->assertEquals("x", $vertical->getIndexAxis());
327 }
328
329 public function testRenderHorizontal(): void
330 {
331 $r = $this->getDefaultRenderer();
332 $f = $this->getFactory();
333
334 $dataset = $this->getSimpleDataset();
335
336 $horizontal = $f->horizontal(
337 "bar123",
338 $dataset
339 );
340
341 $html = $r->render($horizontal);
342
343 $expected_html = <<<EOT
344<div class="il-chart-bar-horizontal">
345 <canvas id="id_1" height="150" aria-label="bar123" role="img"></canvas>
346</div>
347<div class="sr-only">
348 <dl>
349 <dt>Dataset</dt>
350 <dd>
351 <ul>
352 <li>Item: 0</li>
353 </ul>
354 </dd>
355 </dl>
356</div>
357EOT;
358
359 $this->assertHTMLEquals("<div>" . $expected_html . "</div>", "<div>" . $html . "</div>");
360 }
361
362 public function testRenderVertical(): void
363 {
364 $r = $this->getDefaultRenderer();
365 $f = $this->getFactory();
366
367 $dataset = $this->getExtendedDataset();
368
369 $vertical = $f->vertical(
370 "bar123",
371 $dataset
372 );
373
374 $html = $r->render($vertical);
375
376 $expected_html = <<<EOT
377<div class="il-chart-bar-vertical">
378 <canvas id="id_1" height="165" aria-label="bar123" role="img"></canvas>
379</div>
380<div class="sr-only">
381 <dl>
382 <dt>Dataset 1</dt>
383 <dd>
384 <ul>
385 <li>Item 1: -1.25</li>
386 <li>Item 2: -</li>
387 </ul>
388 </dd>
389 <dt>Dataset 2</dt>
390 <dd>
391 <ul>
392 <li>Item 1: -2 - -1</li>
393 <li>Item 2: 0 - 0.5</li>
394 </ul>
395 </dd>
396 </dl>
397</div>
398EOT;
399
400 $this->assertHTMLEquals("<div>" . $expected_html . "</div>", "<div>" . $html . "</div>");
401 }
402
403 public static function provideRiskyData(): array
404 {
405 return [
406 "ampersand" => ["this&that", "this&amp;that"],
407 "single quote" => ["it's a kind of magic", "it&#039;s a kind of magic"],
408 "double quote" => ['Dwayne "The Rock" Johnson', 'Dwayne &quot;The Rock&quot; Johnson'],
409 "tags" => ['<script>alert("XSS")</script>', '&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt;'],
410 ];
411 }
412
417 string $risky_datum,
418 string $expected_in_html
419 ): void {
420 $r = $this->getDefaultRenderer();
421 $f = $this->getFactory();
422 $df = $this->getDataFactory();
423
424 $c_dimension = $df->dimension()->cardinal();
425
426 $dataset = $df->dataset([$risky_datum => $c_dimension]);
427 $dataset = $dataset->withPoint("Item", [$risky_datum => 123]);
428
429 $vertical = $f->vertical(
430 "bar123",
431 $dataset
432 );
433
434 $html = $r->render($vertical);
435
436 $expected_html = <<<EOT
437<div class="il-chart-bar-vertical">
438 <canvas id="id_1" height="150" aria-label="bar123" role="img"></canvas>
439</div>
440<div class="sr-only">
441 <dl>
442 <dt>$expected_in_html</dt>
443 <dd>
444 <ul>
445 <li>Item: 123</li>
446 </ul>
447 </dd>
448 </dl>
449</div>
450EOT;
451
452 $this->assertHTMLEquals("<div>" . $expected_html . "</div>", "<div>" . $html . "</div>");
453 }
454
459 string $risky_datum,
460 string $expected_in_html
461 ): void {
462 $r = $this->getDefaultRenderer();
463 $f = $this->getFactory();
464 $df = $this->getDataFactory();
465
466 $c_dimension = $df->dimension()->cardinal();
467
468 $dataset = $df->dataset(["Dataset" => $c_dimension]);
469 $dataset = $dataset->withPoint($risky_datum, ["Dataset" => 123]);
470
471 $vertical = $f->vertical(
472 "bar123",
473 $dataset
474 );
475
476 $html = $r->render($vertical);
477
478 $expected_html = <<<EOT
479<div class="il-chart-bar-vertical">
480 <canvas id="id_1" height="150" aria-label="bar123" role="img"></canvas>
481</div>
482<div class="sr-only">
483 <dl>
484 <dt>Dataset</dt>
485 <dd>
486 <ul>
487 <li>$expected_in_html: 123</li>
488 </ul>
489 </dd>
490 </dl>
491</div>
492EOT;
493
494 $this->assertHTMLEquals("<div>" . $expected_html . "</div>", "<div>" . $html . "</div>");
495 }
496}
Test on Bar Chart implementation.
static provideRiskyData()
testRenderConvertSpecialCharactersInItemLabel(string $risky_datum, string $expected_in_html)
@dataProvider provideRiskyData
testRenderConvertSpecialCharactersInDatasetLabel(string $risky_datum, string $expected_in_html)
@dataProvider provideRiskyData
testWithTooltipsInvisible()
testWithInvalidLegendPosition()
testImplementsFactoryInterface()
Builds data types.
Definition: Factory.php:36
Provides common functionality for UI tests.
Definition: Base.php:337
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.