ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ExpandableTreeTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once("vendor/composer/vendor/autoload.php");
22require_once(__DIR__ . "../../../Base.php");
23
26
28{
29 protected string $label;
30 protected array $children;
31
32 public function __construct(string $label, array $children = [])
33 {
34 $this->label = $label;
35 $this->children = $children;
36 }
37 public function getLabel(): string
38 {
39 return $this->label;
40 }
41 public function getChildren(): array
42 {
43 return $this->children;
44 }
45}
46
48{
49 public function getChildren($record, $environment = null): array
50 {
51 return $record->getChildren();
52 }
53
54 public function build(
55 C\Tree\Node\Factory $factory,
56 $record,
57 $environment = null
58 ): C\Tree\Node\Node {
59 return $factory->simple($record->getLabel());
60 }
61}
62
67{
68 protected C\Tree\Tree $tree;
69
70 public function getUIFactory(): NoUIFactory
71 {
72 return new class () extends NoUIFactory {
73 public function tree(): I\Tree\Factory
74 {
75 return new I\Tree\Factory(
76 new I\Tree\Node\Factory(),
77 );
78 }
79 };
80 }
81
82 public function setUp(): void
83 {
84 $n11 = new DataNode('1.1');
85 $n12 = new DataNode('1.2', array(new DataNode('1.2.1')));
86 $n1 = new DataNode('1', [$n11, $n12]);
87 $n2 = new DataNode('2');
88 $data = [$n1, $n2];
89
90 $label = "label";
91 $recursion = new Recursion();
92 $f = $this->getUIFactory();
93 $this->tree = $f->tree()->expandable($label, $recursion)
94 ->withData($data);
95 }
96
97 public function brutallyTrimHTML(string $html): string
98 {
99 $html = str_replace(["\n", "\r", "\t"], "", $html);
100 $html = preg_replace('# {2,}#', " ", $html);
101 return trim($html);
102 }
103
104 public function testRendering(): void
105 {
106 $r = $this->getDefaultRenderer();
107 $html = $r->render($this->tree);
108
109 $expected = '<ul id="id_1" class="c-tree" role="tree" aria-label="label">' . $this->getInnerTreePart() . '</ul>';
110
111 $this->assertEquals(
112 $this->brutallyTrimHTML($expected),
113 $this->brutallyTrimHTML($html)
114 );
115 }
116
117 public function testRenderingAsSubTree(): void
118 {
119 $r = $this->getDefaultRenderer();
120 $html = $r->render($this->tree->withIsSubTree(true));
121
122 $expected = $this->getInnerTreePart();
123
124 $this->assertEquals(
125 $this->brutallyTrimHTML($expected),
126 $this->brutallyTrimHTML($html)
127 );
128 }
129
130 protected function getInnerTreePart(): string
131 {
132 return '<li class="c-tree__node c-tree__node--simple expandable" role="treeitem" aria-expanded="false">
133 <span class="c-tree__node__line"><span class="c-tree__node__label">1</span></span>
134
135 <ul role="group">
136 <li class="c-tree__node c-tree__node--simple" role="treeitem">
137 <span class="c-tree__node__line"><span class="c-tree__node__label">1.1</span></span>
138 </li>
139 <li class="c-tree__node c-tree__node--simple expandable" role="treeitem" aria-expanded="false">
140 <span class="c-tree__node__line"><span class="c-tree__node__label">1.2</span></span>
141
142 <ul role="group">
143 <li class="c-tree__node c-tree__node--simple" role="treeitem">
144 <span class="c-tree__node__line"><span class="c-tree__node__label">1.2.1</span></span>
145 </li>
146 </ul>
147 </li>
148 </ul>
149 </li>
150 <li class="c-tree__node c-tree__node--simple" role="treeitem">
151 <span class="c-tree__node__line"><span class="c-tree__node__label">2</span></span>
152 </li>';
153 }
154}
__construct(string $label, array $children=[])
Tests for the Expandable Tree.
brutallyTrimHTML(string $html)
Provides common functionality for UI tests.
Definition: Base.php:337
build(C\Tree\Node\Factory $factory, $record, $environment=null)
getChildren($record, $environment=null)
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...