ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
FieldBranchNodeTest.php
Go to the documentation of this file.
1 <?php
2 
18 declare(strict_types=1);
19 
25 
26 require_once(__DIR__ . "/../../../../../../../../vendor/composer/vendor/autoload.php");
27 require_once(__DIR__ . "/../../../../Base.php");
28 
33 {
34  protected Component\Symbol\Glyph\Glyph & MockObject $glyph_stub;
35  protected string $glyph_html;
36  protected Component\Symbol\Icon\Icon & MockObject $icon_stub;
37  protected string $icon_html;
38 
39  protected Component\Symbol\Glyph\Factory & MockObject $glyph_factory;
40  protected Component\Symbol\Icon\Factory & MockObject $icon_factory;
41  protected Component\Symbol\Factory & MockObject $symbol_factory;
42 
43  protected function setUp(): void
44  {
47 
48  $this->glyph_factory = $this->createMock(Component\Symbol\Glyph\Factory::class);
49  $this->glyph_factory->method($this->anything())->willReturn($this->glyph_stub);
50 
51  $this->icon_factory = $this->createMock(Component\Symbol\Icon\Factory::class);
52  $this->icon_factory->method('standard')->willReturn($this->icon_stub);
53 
54  $this->symbol_factory = $this->createMock(Component\Symbol\Factory::class);
55  $this->symbol_factory->method('glyph')->willReturn($this->glyph_factory);
56  $this->symbol_factory->method('icon')->willReturn($this->icon_factory);
57 
58  parent::setUp();
59  }
60 
61  public function getDefaultRenderer(
62  ?JavaScriptBinding $js_binding = null,
63  array $with_stub_renderings = [],
64  array $with_additional_contexts = [],
66  $with_stub_renderings = array_merge($with_stub_renderings, [
67  $this->glyph_stub,
68  $this->icon_stub,
69  ]);
70 
71  return parent::getDefaultRenderer($js_binding, $with_stub_renderings, $with_additional_contexts);
72  }
73 
74  public function getUIFactory(): \NoUIFactory
75  {
76  return new class ($this->symbol_factory) extends \NoUIFactory {
77  public function __construct(
78  protected Component\Symbol\Factory $symbol_factory,
79  ) {
80  }
81  public function symbol(): Component\Symbol\Factory
82  {
83  return $this->symbol_factory;
84  }
85  };
86  }
87 
88  public function testRenderWithChildren(): void
89  {
90  $node_id = 'some-existing-node-id';
91  $node_name = 'some existing node name';
92 
93  [$child_node_stub, $child_node_html] = $this->createSimpleRenderingStub(Field\Node\Leaf::class);
94 
95  $component = $this->getNodeFactory()->branch($node_id, $node_name, null, $child_node_stub);
96  $renderer = $this->getDefaultRenderer(null, [$child_node_stub]);
97 
98  $expected = <<<HTML
99 <li data-node-id="$node_id" class="c-input-node c-input-node__branch c-drilldown__branch">
100  <button type="button" class="c-drilldown__menulevel--trigger" aria-expanded="false">
101  <span> $this->icon_html<span data-node-name>$node_name</span></span>
102  <span> $this->glyph_html</span>
103  </button>
104  <button type="button" class="c-input-node__select" aria-label="select_node">
105  <span data-action="select">$this->glyph_html</span>
106  <span class="hidden" data-action="remove">$this->glyph_html</span>
107  </button>
108  <ul> $child_node_html</ul>
109 </li>
110 HTML;
111 
112  $actual = $renderer->render($component);
113 
114  $this->assertEquals(
115  $this->brutallyTrimHTML($expected),
116  $this->brutallyTrimHTML($actual),
117  );
118  }
119 
120  public function testRenderWithoutChildren(): void
121  {
122  $component = $this->getNodeFactory()->branch('', '');
123  $renderer = $this->getDefaultRenderer();
124 
125  $actual = $renderer->render($component);
126 
127  $this->assertTrue(str_contains($this->brutallyTrimHTML($actual), "<ul></ul>"));
128  }
129 
130  protected function testRenderWithIcon(): void
131  {
132  [$icon_stub, $icon_html] = $this->createSimpleRenderingStub(Component\Symbol\Icon\Custom::class);
133 
134  $component = $this->getNodeFactory()->branch('', '');
135  $renderer = $this->getDefaultRenderer(null, [$icon_stub]);
136 
137  $actual = $renderer->render($component);
138 
139  $this->assertTrue(str_contains($actual, $icon_html));
140  }
141 
142  public function testRenderWithoutIcon(): void
143  {
144  $first_node_name_letter = 's';
145  $node_name = "{$first_node_name_letter}ome existing node name";
146 
147  $this->icon_factory->expects($this->once())->method('standard');
148  $this->icon_stub->expects($this->once())->method('withAbbreviation')->with($first_node_name_letter);
149 
150  $component = $this->getNodeFactory()->branch('', $node_name);
151  $renderer = $this->getDefaultRenderer();
152 
153  $actual = $renderer->render($component);
154 
155  $this->assertTrue(str_contains($actual, $this->icon_html));
156  }
157 
158  protected function getNodeFactory(): Field\Node\Factory
159  {
160  return new Field\Node\Factory();
161  }
162 
164  protected function getGlyphStub(): array
165  {
166  return $this->createSimpleRenderingStub(Component\Symbol\Glyph\Glyph::class);
167  }
168 
170  protected function getIconStub(): array
171  {
172  return $this->createSimpleRenderingStub(Component\Symbol\Icon\Standard::class);
173  }
174 
176  protected function createSimpleRenderingStub(string $class_name): array
177  {
178  $stub = $this->createMock($class_name);
179  $html = sha1($class_name);
180  $stub->method('getCanonicalName')->willReturn($html);
181  $stub->method($this->anything())->willReturnSelf();
182  return [$stub, $html];
183  }
184 }
button(string $caption, string $cmd)
Component Symbol Icon Factory &MockObject $icon_factory
$renderer
Component Symbol Factory &MockObject $symbol_factory
getDefaultRenderer(?JavaScriptBinding $js_binding=null, array $with_stub_renderings=[], array $with_additional_contexts=[],)
Factory for Date Formats.
Definition: Factory.php:26
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Component Symbol Icon Icon &MockObject $icon_stub
createSimpleRenderingStub(string $class_name)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
Component Symbol Glyph Glyph &MockObject $glyph_stub
Provides common functionality for UI tests.
Definition: Base.php:333
Component Symbol Glyph Factory &MockObject $glyph_factory
Provides methods to interface with javascript.
__construct(Container $dic, ilPlugin $plugin)