ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
FieldBranchNodeTest.php
Go to the documentation of this file.
1<?php
2
18declare(strict_types=1);
19
24use PHPUnit\Framework\MockObject\MockObject;
25use PHPUnit\Framework\Attributes\DataProvider;
26
27require_once(__DIR__ . "/../../../../../../../../vendor/composer/vendor/autoload.php");
28require_once(__DIR__ . "/../../../../Base.php");
29
34{
36 protected string $glyph_html;
37 protected Component\Symbol\Icon\Icon & MockObject $icon_stub;
38 protected string $icon_html;
39
43
44 protected function setUp(): void
45 {
48
49 $this->glyph_factory = $this->createMock(Component\Symbol\Glyph\Factory::class);
50 $this->glyph_factory->method($this->anything())->willReturn($this->glyph_stub);
51
52 $this->icon_factory = $this->createMock(Component\Symbol\Icon\Factory::class);
53 $this->icon_factory->method('standard')->willReturn($this->icon_stub);
54
55 $this->symbol_factory = $this->createMock(Component\Symbol\Factory::class);
56 $this->symbol_factory->method('glyph')->willReturn($this->glyph_factory);
57 $this->symbol_factory->method('icon')->willReturn($this->icon_factory);
58
59 parent::setUp();
60 }
61
62 public function getDefaultRenderer(
63 ?JavaScriptBinding $js_binding = null,
64 array $with_stub_renderings = [],
65 array $with_additional_contexts = [],
67 $with_stub_renderings = array_merge($with_stub_renderings, [
68 $this->glyph_stub,
69 $this->icon_stub,
70 ]);
71
72 return parent::getDefaultRenderer($js_binding, $with_stub_renderings, $with_additional_contexts);
73 }
74
75 public function getUIFactory(): \NoUIFactory
76 {
77 return new class ($this->symbol_factory) extends \NoUIFactory {
78 public function __construct(
79 protected Component\Symbol\Factory $symbol_factory,
80 ) {
81 }
82 public function symbol(): Component\Symbol\Factory
83 {
84 return $this->symbol_factory;
85 }
86 };
87 }
88
89 public function testRenderWithChildren(): void
90 {
91 $node_id = 'some-existing-node-id';
92 $node_name = 'some existing node name';
93
94 [$child_node_stub, $child_node_html] = $this->createSimpleRenderingStub(Field\Node\Leaf::class);
95
96 $component = $this->getNodeFactory()->branch([$node_id], $node_name, null, $child_node_stub);
97 $renderer = $this->getDefaultRenderer(null, [$child_node_stub]);
98
99 $expected = <<<HTML
100<li data-node-id="$node_id" class="c-input-node c-input-node__branch c-drilldown__branch">
101 <button type="button" class="c-drilldown__menulevel--trigger" aria-expanded="false">
102 <span> $this->icon_html<span data-node-name>$node_name</span></span>
103 <span> $this->glyph_html</span>
104 </button>
105 <button type="button" class="c-input-node__select" aria-label="select_node">
106 <span data-action="select">$this->glyph_html</span>
107 <span class="hidden" data-action="remove">$this->glyph_html</span>
108 </button>
109 <ul> $child_node_html</ul>
110</li>
111HTML;
112
113 $actual = $renderer->render($component);
114
115 $this->assertEquals(
116 $this->brutallyTrimHTML($expected),
117 $this->brutallyTrimHTML($actual),
118 );
119 }
120
121 public function testRenderWithoutChildren(): void
122 {
123 $component = $this->getNodeFactory()->branch([''], '');
124 $renderer = $this->getDefaultRenderer();
125
126 $actual = $renderer->render($component);
127
128 $this->assertTrue(str_contains($this->brutallyTrimHTML($actual), "<ul></ul>"));
129 }
130
131 protected function testRenderWithIcon(): void
132 {
133 [$icon_stub, $icon_html] = $this->createSimpleRenderingStub(Component\Symbol\Icon\Custom::class);
134
135 $component = $this->getNodeFactory()->branch([''], '');
136 $renderer = $this->getDefaultRenderer(null, [$icon_stub]);
137
138 $actual = $renderer->render($component);
139
140 $this->assertTrue(str_contains($actual, $icon_html));
141 }
142
143 public function testRenderWithoutIcon(): void
144 {
145 $first_node_name_letter = 's';
146 $node_name = "{$first_node_name_letter}ome existing node name";
147
148 $this->icon_factory->expects($this->once())->method('standard');
149 $this->icon_stub->expects($this->once())->method('withAbbreviation')->with($first_node_name_letter);
150
151 $component = $this->getNodeFactory()->branch([''], $node_name);
152 $renderer = $this->getDefaultRenderer();
153
154 $actual = $renderer->render($component);
155
156 $this->assertTrue(str_contains($actual, $this->icon_html));
157 }
158
159 protected function testConstructorWithValidNodePath(): void
160 {
161 $root_node_id = 0;
162 $level_one_node_id = 1;
163 $level_two_node_id = 2;
164 $parent_ids = [$root_node_id, $level_one_node_id];
165 $full_node_path = [...$parent_ids, $level_two_node_id];
166
167 $node = $this->getNodeFactory()->branch($full_node_path, "");
168
169 $this->assertEquals($full_node_path, $node->getFullPath());
170 $this->assertEquals($parent_ids, $node->getParentIds());
171 $this->assertEquals($level_two_node_id, $node->getId());
172 }
173
174 #[DataProvider('provideInvalidNodePaths')]
175 protected function testConstructorWithInvalidNodePath(array $path): void
176 {
177 $this->expectException(\InvalidArgumentException::class);
178 $this->getNodeFactory()->branch($path, "");
179 }
180
181 public static function provideInvalidNodePaths(): array
182 {
183 return [
184 [1.0],
185 [false],
186 [null],
187 [],
188 ];
189 }
190
191 protected function getNodeFactory(): Field\Node\Factory
192 {
193 return new Field\Node\Factory();
194 }
195
197 protected function getGlyphStub(): array
198 {
199 return $this->createSimpleRenderingStub(Component\Symbol\Glyph\Glyph::class);
200 }
201
203 protected function getIconStub(): array
204 {
205 return $this->createSimpleRenderingStub(Component\Symbol\Icon\Standard::class);
206 }
207
209 protected function createSimpleRenderingStub(string $class_name): array
210 {
211 $stub = $this->createMock($class_name);
212 $html = sha1($class_name);
213 $stub->method('getCanonicalName')->willReturn($html);
214 $stub->method($this->anything())->willReturnSelf();
215 return [$stub, $html];
216 }
217}
$renderer
getDefaultRenderer(?JavaScriptBinding $js_binding=null, array $with_stub_renderings=[], array $with_additional_contexts=[],)
testConstructorWithInvalidNodePath(array $path)
createSimpleRenderingStub(string $class_name)
Component Symbol Icon Factory &MockObject $icon_factory
Component Symbol Icon Icon &MockObject $icon_stub
Component Symbol Glyph Factory &MockObject $glyph_factory
Component Symbol Factory &MockObject $symbol_factory
Component Symbol Glyph Glyph &MockObject $glyph_stub
Factory for Date Formats.
Definition: Factory.php:27
Provides common functionality for UI tests.
Definition: Base.php:337
This is what a factory for input fields looks like.
Definition: Factory.php:31
This describes a symbol.
Definition: Symbol.php:30
Provides methods to interface with javascript.
$path
Definition: ltiservices.php:30
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
button(string $caption, string $cmd)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21