ILIAS  release_7 Revision v7.30-3-g800a261c036
MainBarTest.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
3 /* Copyright (c) 2018 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
5 require_once("libs/composer/vendor/autoload.php");
6 require_once(__DIR__ . "/../../Base.php");
7 
8 use ILIAS\UI\Component as C;
12 use ILIAS\Data;
14 
19 {
23  protected $button_factory;
27  protected $link_factory;
31  protected $icon_factory;
35  protected $factory;
39  protected $mainbar;
40 
41  public function setUp() : void
42  {
43  $sig_gen = new I\SignalGenerator();
44  $this->button_factory = new I\Button\Factory();
45  $this->link_factory = new I\Link\Factory();
46  $this->icon_factory = new I\Symbol\Icon\Factory();
47  $counter_factory = new I\Counter\Factory();
48  $slate_factory = new I\MainControls\Slate\Factory(
49  $sig_gen,
50  $counter_factory,
51  new I\Symbol\Factory(
52  new I\Symbol\Icon\Factory(),
53  new I\Symbol\Glyph\Factory(),
54  new I\Symbol\Avatar\Factory()
55  )
56  );
57  $this->factory = new I\MainControls\Factory($sig_gen, $slate_factory);
58 
59  $this->mainbar = $this->factory->mainBar();
60  }
61 
62  public function testConstruction() : void
63  {
64  $this->assertInstanceOf(
65  "ILIAS\\UI\\Component\\MainControls\\MainBar",
66  $this->mainbar
67  );
68  }
69 
70  protected function getButton() : C\Button\Bulky
71  {
72  $symbol = $this->icon_factory->custom('', '');
73  return $this->button_factory->bulky($symbol, 'TestEntry', '#');
74  }
75 
76  protected function getLink() : C\Link\Bulky
77  {
78  $symbol = $this->icon_factory->custom('', '');
79  $target = new Data\URI("http://www.ilias.de");
80  return $this->link_factory->bulky($symbol, 'TestEntry', $target);
81  }
82 
86  protected function getSlate()
87  {
88  $mock = $this->getMockBuilder(Legacy::class)
89  ->disableOriginalConstructor()
90  ->getMock();
91 
92  return $mock;
93  }
94 
95  public function testAddEntry() : C\MainControls\MainBar
96  {
97  $btn = $this->getButton();
98  $lnk = $this->getLink();
99  $mb = $this->mainbar
100  ->withAdditionalEntry('testbtn', $btn)
101  ->withAdditionalEntry('testlnk', $lnk);
102 
103  $entries = $mb->getEntries();
104  $expected = [
105  'testbtn' => $btn,
106  'testlnk' => $lnk,
107  ];
108  $this->assertEquals($expected, $entries);
109  return $mb;
110  }
111 
112  public function testDisallowedEntry() : void
113  {
114  $this->expectException(InvalidArgumentException::class);
115  $this->mainbar->withAdditionalEntry('test', 'wrong_param');
116  }
117 
118  public function testDouplicateIdEntry() : void
119  {
120  $this->expectException(InvalidArgumentException::class);
121  $btn = $this->getButton();
122  $this->mainbar
123  ->withAdditionalEntry('test', $btn)
124  ->withAdditionalEntry('test', $btn);
125  }
126 
127  public function testDisallowedToolEntry() : void
128  {
129  $this->expectException(\TypeError::class);
130  $this->mainbar->withAdditionalToolEntry('test', 'wrong_param');
131  }
132 
133  public function testAddToolEntryWithoutToolsButton() : void
134  {
135  $this->expectException(LogicException::class);
136  $this->mainbar->withAdditionalToolEntry('test', $this->getSlate());
137  }
138 
139  public function testAddToolEntry() : void
140  {
141  $slate = $this->getSlate();
142  $mb = $this->mainbar
143  ->withToolsButton($this->getButton())
144  ->withAdditionalToolEntry('test', $slate);
145  $entries = $mb->getToolEntries();
146  $this->assertEquals($slate, $entries['test']);
147  }
148 
149  public function testDouplicateIdToolEntry() : void
150  {
151  $this->expectException(InvalidArgumentException::class);
152  $btn = $this->getButton();
153  $slate = $this->getSlate();
154  $this->mainbar
155  ->withToolsButton($btn)
156  ->withAdditionalToolEntry('test', $slate)
157  ->withAdditionalToolEntry('test', $slate);
158  }
159 
163  public function testActive(C\MainControls\MainBar $mb) : void
164  {
165  $mb = $mb->withActive('testbtn');
166  $this->assertEquals('testbtn', $mb->getActive());
167  }
168 
169  public function testWithInvalidActive() : void
170  {
171  $this->expectException(InvalidArgumentException::class);
172  $this->mainbar->withActive('this-is-not-a-valid-entry');
173  }
174 
175  public function testSignalsPresent() : void
176  {
177  $this->assertInstanceOf(Signal::class, $this->mainbar->getEntryClickSignal());
178  $this->assertInstanceOf(Signal::class, $this->mainbar->getToolsClickSignal());
179  $this->assertInstanceOf(Signal::class, $this->mainbar->getToolsRemovalSignal());
180  $this->assertInstanceOf(Signal::class, $this->mainbar->getDisengageAllSignal());
181  }
182 
183  public function getUIFactory() : NoUIFactory
184  {
185  $factory = new class extends NoUIFactory {
189  public $button_factory;
190 
191  public function button() : C\Button\Factory
192  {
193  return $this->button_factory;
194  }
195  public function symbol() : C\Symbol\Factory
196  {
197  $f_icon = new I\Symbol\Icon\Factory();
198  $f_glyph = new I\Symbol\Glyph\Factory();
199  $f_avatar = new I\Symbol\Avatar\Factory();
200 
201  return new I\Symbol\Factory($f_icon, $f_glyph, $f_avatar);
202  }
203  public function mainControls() : C\MainControls\Factory
204  {
205  $sig_gen = new I\SignalGenerator();
206  $counter_factory = new I\Counter\Factory();
207  $symbol_factory = new I\Symbol\Factory(
208  new I\Symbol\Icon\Factory(),
209  new I\Symbol\Glyph\Factory(),
210  new I\Symbol\Avatar\Factory()
211  );
212  $slate_factory = new I\MainControls\Slate\Factory($sig_gen, $counter_factory, $symbol_factory);
213  return new I\MainControls\Factory($sig_gen, $slate_factory);
214  }
215  public function legacy($content) : C\Legacy\Legacy
216  {
217  $sig_gen = new I\SignalGenerator();
218  return new I\Legacy\Legacy($content, $sig_gen);
219  }
220  };
221  $factory->button_factory = $this->button_factory;
222  return $factory;
223  }
224 
225  public function testRendering() : void
226  {
227  $r = $this->getDefaultRenderer();
228  $icon = $this->icon_factory->custom('', '');
229 
230  $sf = $this->factory->slate();
231  $slate = $sf->combined('1', $icon)
232  ->withAdditionalEntry(
233  $sf->combined('1.1', $icon)
234  ->withAdditionalEntry(
235  $sf->combined('1.1.1', $icon)
236  )
237  );
238 
239  $toolslate = $sf->legacy('Help', $icon, new I\Legacy\Legacy('Help', new I\SignalGenerator()));
240 
241  $mb = $this->factory->mainBar()
242  ->withAdditionalEntry('test1', $this->getButton())
243  ->withAdditionalEntry('test2', $this->getButton())
244  ->withAdditionalEntry('slate', $slate)
245  ->withToolsButton($this->getButton())
246  ->withAdditionalToolEntry('tool1', $toolslate);
247 
248  $html = $r->render($mb);
249 
250  $expected = <<<EOT
251  <div class="il-maincontrols-mainbar" id="id_16">
252  <nav class="il-mainbar" aria-label="mainbar_aria_label">
253 
254  <div class="il-mainbar-tools-button">
255  <button class="btn btn-bulky" id="id_14"><img class="icon custom small" src="" alt=""/><span class="bulky-label">TestEntry</span></button>
256  </div>
257 
258  <div class="il-mainbar-triggers">
259  <ul class="il-mainbar-entries" role="menubar" style="visibility: hidden">
260  <li role="none"><button class="btn btn-bulky" data-action="#" id="id_1" role="menuitem" ><img class="icon custom small" src="" alt=""/><span class="bulky-label">TestEntry</span></button></li>
261  <li role="none"><button class="btn btn-bulky" data-action="#" id="id_2" role="menuitem" ><img class="icon custom small" src="" alt=""/><span class="bulky-label">TestEntry</span></button></li>
262  <li role="none"><button class="btn btn-bulky" id="id_3" role="menuitem" ><img class="icon custom small" src="" alt=""/><span class="bulky-label">1</span></button></li>
263  <li role="none"><button class="btn btn-bulky" id="id_9" role="menuitem" ><span class="glyph" aria-label="show_more" role="img"><span class="glyphicon glyphicon-option-horizontal" aria-hidden="true"></span></span><span class="bulky-label">mainbar_more_label</span></button></li>
264  </ul>
265  </div>
266  </nav>
267 
268  <div class="il-mainbar-slates">
269  <div class="il-mainbar-tools-entries">
270  <ul class="il-mainbar-tools-entries-bg" role="menubar">
271  <li class="il-mainbar-tool-trigger-item" role="none">
272  <button class="btn btn-bulky" id="id_11" role="menuitem"><img class="icon custom small" src="" alt=""/><span class="bulky-label">Help</span></button>
273  </li>
274  </ul>
275  </div>
276  <div class="il-maincontrols-slate disengaged" id="id_8" data-depth-level="1" role="menu">
277  <div class="il-maincontrols-slate-content" data-replace-marker="content">
278  <button class="btn btn-bulky" id="id_4" ><img class="icon custom small" src="" alt=""/><span class="bulky-label">1.1</span></button>
279 
280  <div class="il-maincontrols-slate disengaged" id="id_7" data-depth-level="2">
281  <div class="il-maincontrols-slate-content" data-replace-marker="content">
282  <button class="btn btn-bulky" id="id_5" ><img class="icon custom small" src="" alt=""/><span class="bulky-label">1.1.1</span></button>
283 
284  <div class="il-maincontrols-slate disengaged" id="id_6" data-depth-level="3">
285  <div class="il-maincontrols-slate-content" data-replace-marker="content"></div>
286  </div>
287  </div>
288  </div>
289 
290  </div>
291  </div>
292 
293  <div class="il-maincontrols-slate disengaged" id="id_10" data-depth-level="1" role="menu">
294  <div class="il-maincontrols-slate-content" data-replace-marker="content"></div>
295  </div>
296 
297  <div class="il-maincontrols-slate disengaged" id="id_13" data-depth-level="1" role="menu">
298  <div class="il-maincontrols-slate-content" data-replace-marker="content">Help</div>
299  </div>
300 
301 
302  <div class="il-mainbar-close-slates">
303  <button class="btn btn-bulky" id="id_15" >
304  <span class="glyph" aria-label="back" role="img"><span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span></span>
305  <span class="bulky-label">close</span></button>
306  </div>
307  </div>
308  </div>
309 EOT;
310 
311  $this->assertEquals(
312  $this->brutallyTrimHTML($expected),
313  $this->brutallyTrimHTML($html)
314  );
315  }
316 }
close()
Definition: close.php:2
testWithInvalidActive()
Provides common functionality for UI tests.
Definition: Base.php:262
Builds data types.
Definition: Factory.php:19
testAddToolEntryWithoutToolsButton()
testDisallowedToolEntry()
brutallyTrimHTML($html)
A more radical version of normalizeHTML.
Definition: Base.php:392
legacy()
Definition: legacy.php:3
Tests for the Main Bar.
Definition: MainBarTest.php:18
mainbar()
Definition: mainbar.php:2
testDouplicateIdToolEntry()
testActive(C\MainControls\MainBar $mb)
testAddEntry
testDouplicateIdEntry()
getDefaultRenderer(JavaScriptBinding $js_binding=null, $with_stub_renderings=[])
Definition: Base.php:311