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