ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
FieldLeafNodeTest.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 testRender(): void
90 {
91 $node_id = 'some-existing-node-id';
92 $node_name = 'some existing node name';
93
94 $component = $this->getNodeFactory()->leaf([$node_id], $node_name);
95 $renderer = $this->getDefaultRenderer();
96
97 $expected = <<<HTML
98<li data-node-id="$node_id" class="c-input-node c-input-node__leaf c-drilldown__leaf">
99 <p class="il-link link-bulky c-input-node__name" tabindex="0">
100 $this->icon_html<span data-node-name>$node_name</span>
101 </p>
102 <button type="button" class="c-input-node__select" aria-label="select_node">
103 <span data-action="select">$this->glyph_html</span>
104 <span class="hidden" data-action="remove">$this->glyph_html</span>
105 </button>
106</li>
107HTML;
108
109 $actual = $renderer->render($component);
110
111 $this->assertEquals(
112 $this->brutallyTrimHTML($expected),
113 $this->brutallyTrimHTML($actual),
114 );
115 }
116
117 protected function testRenderWithIcon(): void
118 {
119 [$icon_stub, $icon_html] = $this->createSimpleRenderingStub(Component\Symbol\Icon\Custom::class);
120
121 $component = $this->getNodeFactory()->leaf([''], '');
122 $renderer = $this->getDefaultRenderer(null, [$icon_stub]);
123
124 $actual = $renderer->render($component);
125
126 $this->assertTrue(str_contains($actual, $icon_html));
127 }
128
129 public function testRenderWithoutIcon(): void
130 {
131 $first_node_name_letter = 's';
132 $node_name = "{$first_node_name_letter}ome existing node name";
133
134 $this->icon_factory->expects($this->once())->method('standard');
135 $this->icon_stub->expects($this->once())->method('withAbbreviation')->with($first_node_name_letter);
136
137 $component = $this->getNodeFactory()->leaf([''], $node_name);
138 $renderer = $this->getDefaultRenderer();
139
140 $actual = $renderer->render($component);
141
142 $this->assertTrue(str_contains($actual, $this->icon_html));
143 }
144
145 protected function testConstructorWithValidNodePath(): void
146 {
147 $root_node_id = 0;
148 $level_one_node_id = 1;
149 $level_two_node_id = 2;
150 $parent_ids = [$root_node_id, $level_one_node_id];
151 $full_node_path = [...$parent_ids, $level_two_node_id];
152
153 $node = $this->getNodeFactory()->leaf($full_node_path, "");
154
155 $this->assertEquals($full_node_path, $node->getFullPath());
156 $this->assertEquals($parent_ids, $node->getParentIds());
157 $this->assertEquals($level_two_node_id, $node->getId());
158 }
159
160 #[DataProvider('provideInvalidNodePaths')]
161 protected function testConstructorWithInvalidNodePath(array $path): void
162 {
163 $this->expectException(\InvalidArgumentException::class);
164 $this->getNodeFactory()->leaf($path, "");
165 }
166
167 public static function provideInvalidNodePaths(): array
168 {
169 return [
170 [1.0],
171 [false],
172 [null],
173 [],
174 ];
175 }
176
177 protected function getNodeFactory(): Field\Node\Factory
178 {
179 return new Field\Node\Factory();
180 }
181
183 protected function getGlyphStub(): array
184 {
185 return $this->createSimpleRenderingStub(Component\Symbol\Glyph\Glyph::class);
186 }
187
189 protected function getIconStub(): array
190 {
191 return $this->createSimpleRenderingStub(Component\Symbol\Icon\Standard::class);
192 }
193
195 protected function createSimpleRenderingStub(string $class_name): array
196 {
197 $stub = $this->createMock($class_name);
198 $html = sha1($class_name);
199 $stub->method('getCanonicalName')->willReturn($html);
200 $stub->method($this->anything())->willReturnSelf();
201 return [$stub, $html];
202 }
203}
$renderer
Component Symbol Icon Factory &MockObject $icon_factory
createSimpleRenderingStub(string $class_name)
Component Symbol Icon Icon &MockObject $icon_stub
testConstructorWithInvalidNodePath(array $path)
Component Symbol Factory &MockObject $symbol_factory
Component Symbol Glyph Factory &MockObject $glyph_factory
Component Symbol Glyph Glyph &MockObject $glyph_stub
static provideInvalidNodePaths()
getDefaultRenderer(?JavaScriptBinding $js_binding=null, array $with_stub_renderings=[], array $with_additional_contexts=[],)
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