ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
MainBarTest.php
Go to the documentation of this file.
1 <?php
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;
9 use \ILIAS\UI\Implementation\Component as I;
10 use \ILIAS\UI\Implementation\Component\MainControls\Slate\Legacy;
11 use \ILIAS\UI\Component\Signal;
12 
17 {
18  public function setUp() : void
19  {
20  $sig_gen = new I\SignalGenerator();
21  $this->button_factory = new I\Button\Factory($sig_gen);
22  $this->link_factory = new I\Link\Factory();
23  $this->icon_factory = new I\Symbol\Icon\Factory();
24  $counter_factory = new I\Counter\Factory();
25  $slate_factory = new I\MainControls\Slate\Factory(
26  $sig_gen,
27  $counter_factory,
28  new I\Symbol\Factory(
29  new I\Symbol\Icon\Factory(),
30  new I\Symbol\Glyph\Factory(),
31  new I\Symbol\Avatar\Factory()
32  )
33  );
34  $this->factory = new I\MainControls\Factory($sig_gen, $slate_factory);
35 
36  $this->mainbar = $this->factory->mainBar();
37  }
38 
39  public function testConstruction()
40  {
41  $this->assertInstanceOf(
42  "ILIAS\\UI\\Component\\MainControls\\MainBar",
43  $this->mainbar
44  );
45  }
46 
47  protected function getButton()
48  {
49  $symbol = $this->icon_factory->custom('', '');
50  return $this->button_factory->bulky($symbol, 'TestEntry', '#');
51  }
52 
53  protected function getLink()
54  {
55  $symbol = $this->icon_factory->custom('', '');
56  $target = new \ILIAS\Data\URI("http://www.ilias.de");
57  return $this->link_factory->bulky($symbol, 'TestEntry', $target);
58  }
59 
60  protected function getSlate()
61  {
62  $mock = $this->getMockBuilder(Legacy::class)
63  ->disableOriginalConstructor()
64  ->setMethods(["transformToLegacyComponent"])
65  ->getMock();
66 
67  $mock->method('transformToLegacyComponent')->willReturn('content');
68  return $mock;
69  }
70 
71  public function testAddEntry()
72  {
73  $btn = $this->getButton();
74  $lnk = $this->getLink();
75  $mb = $this->mainbar
76  ->withAdditionalEntry('testbtn', $btn)
77  ->withAdditionalEntry('testlnk', $lnk);
78 
79  $entries = $mb->getEntries();
80  $expected = [
81  'testbtn' => $btn,
82  'testlnk' => $lnk,
83  ];
84  $this->assertEquals($expected, $entries);
85  return $mb;
86  }
87 
88  public function testDisallowedEntry()
89  {
90  $this->expectException(\InvalidArgumentException::class);
91  $mb = $this->mainbar->withAdditionalEntry('test', 'wrong_param');
92  }
93 
94  public function testDouplicateIdEntry()
95  {
96  $this->expectException(\InvalidArgumentException::class);
97  $btn = $this->getButton();
98  $mb = $this->mainbar
99  ->withAdditionalEntry('test', $btn)
100  ->withAdditionalEntry('test', $btn);
101  }
102 
103  public function testDisallowedToolEntry()
104  {
105  $this->expectException(\TypeError::class);
106  $mb = $this->mainbar->withAdditionalToolEntry('test', 'wrong_param');
107  }
108 
110  {
111  $this->expectException(\LogicException::class);
112  $mb = $this->mainbar->withAdditionalToolEntry('test', $this->getSlate());
113  }
114 
115  public function testAddToolEntry()
116  {
117  $slate = $this->getSlate();
118  $mb = $this->mainbar
119  ->withToolsButton($this->getButton())
120  ->withAdditionalToolEntry('test', $slate);
121  $entries = $mb->getToolEntries();
122  $this->assertEquals($slate, $entries['test']);
123  }
124 
125  public function testDouplicateIdToolEntry()
126  {
127  $this->expectException(\InvalidArgumentException::class);
128  $btn = $this->getButton();
129  $slate = $this->getSlate();
130  $mb = $this->mainbar->withToolsButton($btn)
131  ->withAdditionalToolEntry('test', $slate)
132  ->withAdditionalToolEntry('test', $slate);
133  }
134 
138  public function testActive($mb)
139  {
140  $mb = $mb->withActive('testbtn');
141  $this->assertEquals('testbtn', $mb->getActive());
142  }
143 
144  public function testWithInvalidActive()
145  {
146  $this->expectException(\InvalidArgumentException::class);
147  $mb = $this->mainbar
148  ->withActive('this-is-not-a-valid-entry');
149  }
150 
151  public function testSignalsPresent()
152  {
153  $this->assertInstanceOf(Signal::class, $this->mainbar->getEntryClickSignal());
154  $this->assertInstanceOf(Signal::class, $this->mainbar->getToolsClickSignal());
155  $this->assertInstanceOf(Signal::class, $this->mainbar->getToolsRemovalSignal());
156  $this->assertInstanceOf(Signal::class, $this->mainbar->getDisengageAllSignal());
157  }
158 
159  public function getUIFactory()
160  {
161  $factory = new class extends NoUIFactory {
162  public function button()
163  {
164  return $this->button_factory;
165  }
166  public function symbol() : C\Symbol\Factory
167  {
168  $f_icon = new I\Symbol\Icon\Factory();
169  $f_glyph = new I\Symbol\Glyph\Factory();
170  $f_avatar = new I\Symbol\Avatar\Factory();
171 
172  return new I\Symbol\Factory($f_icon, $f_glyph, $f_avatar);
173  }
174  public function mainControls() : C\MainControls\Factory
175  {
176  $sig_gen = new I\SignalGenerator();
177  $counter_factory = new I\Counter\Factory();
178  $symbol_factory = new I\Symbol\Factory(
179  new I\Symbol\Icon\Factory(),
180  new I\Symbol\Glyph\Factory(),
181  new I\Symbol\Avatar\Factory()
182  );
183  $slate_factory = new I\MainControls\Slate\Factory($sig_gen, $counter_factory, $symbol_factory);
184  return new I\MainControls\Factory($sig_gen, $slate_factory);
185  }
186  public function legacy($legacy)
187  {
188  $sig_gen = new I\SignalGenerator();
189  return new I\Legacy\Legacy($legacy, $sig_gen);
190  }
191  };
192  $factory->button_factory = $this->button_factory;
193  return $factory;
194  }
195 
196  public function brutallyTrimHTML($html)
197  {
198  $html = str_replace(["\n", "\r", "\t"], "", $html);
199  $html = preg_replace('# {2,}#', " ", $html);
200  $html = preg_replace('/<!--(.|\s)*?-->/', '', $html);
201  return trim($html);
202  }
203 
204  public function testRendering()
205  {
206  $r = $this->getDefaultRenderer();
207  $icon = $this->icon_factory->custom('', '');
208 
209  $sf = $this->factory->slate();
210  $slate = $sf->combined('1', $icon, '')
211  ->withAdditionalEntry(
212  $sf->combined('1.1', $icon, '')
213  ->withAdditionalEntry(
214  $sf->combined('1.1.1', $icon, '')
215  )
216  );
217 
218  $mb = $this->factory->mainBar()
219  ->withMoreButton(
220  $this->button_factory->bulky($icon, 'more', '')
221  )
222  ->withAdditionalEntry('test1', $this->getButton())
223  ->withAdditionalEntry('test2', $this->getButton())
224  ->withAdditionalEntry('slate', $slate);
225 
226  $html = $r->render($mb);
227 
228  $expected = <<<EOT
229  <div class="il-maincontrols-mainbar" id="id_12">
230  <nav class="il-mainbar" aria-label="mainbar_aria_label">
231  <div class="il-mainbar-triggers">
232  <div class="il-mainbar-entries" role="menubar" style="visibility: hidden">
233  <button class="btn btn-bulky" data-action="#" id="id_1" ><div class="icon custom small" aria-label=""><img src="" /></div><span class="bulky-label">TestEntry</span></button>
234  <button class="btn btn-bulky" data-action="#" id="id_2" ><div class="icon custom small" aria-label=""><img src="" /></div><span class="bulky-label">TestEntry</span></button>
235  <button class="btn btn-bulky" id="id_3" role="menuitem" aria-haspopup="true" ><div class="icon custom small" aria-label=""><img src="" /></div><span class="bulky-label">1</span></button>
236  <button class="btn btn-bulky" id="id_9" role="menuitem" aria-haspopup="true" ><span class="glyph" aria-label="show_more"><span class="glyphicon glyphicon-option-horizontal" aria-hidden="true"></span></span><span class="bulky-label">more</span></button>
237  </div>
238  </div>
239  </nav>
240 
241  <div class="il-mainbar-slates">
242  <div class="il-mainbar-tools-entries">
243  <div class="il-mainbar-tools-entries-bg"></div>
244  </div>
245  <div class="il-maincontrols-slate disengaged" id="id_8" data-depth-level="1" role="menu">
246  <div class="il-maincontrols-slate-content" data-replace-marker="content">
247  <button class="btn btn-bulky" id="id_4" ><div class="icon custom small" aria-label=""><img src="" /></div><span class="bulky-label">1.1</span></button>
248 
249  <div class="il-maincontrols-slate disengaged" id="id_7" data-depth-level="2">
250  <div class="il-maincontrols-slate-content" data-replace-marker="content">
251  <button class="btn btn-bulky" id="id_5" ><div class="icon custom small" aria-label=""><img src="" /></div><span class="bulky-label">1.1.1</span></button>
252 
253  <div class="il-maincontrols-slate disengaged" id="id_6" data-depth-level="3">
254  <div class="il-maincontrols-slate-content" data-replace-marker="content"></div>
255  </div>
256  </div>
257  </div>
258 
259  </div>
260  </div>
261 
262  <div class="il-maincontrols-slate disengaged" id="id_10" data-depth-level="1" role="menu">
263  <div class="il-maincontrols-slate-content" data-replace-marker="content"></div>
264  </div>
265 
266  <div class="il-mainbar-close-slates">
267  <button class="btn btn-bulky" id="id_11" ><span class="glyph" href="#" aria-label="back"><span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span></span><span class="bulky-label">close</span></button>
268  </div>
269  </div>
270  </div>
271 EOT;
272 
273  $this->assertEquals(
274  $this->brutallyTrimHTML($expected),
275  $this->brutallyTrimHTML($html)
276  );
277  }
278 }
close()
Definition: close.php:2
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:268
more()
Definition: more.php:2
testDisallowedEntry()
Definition: MainBarTest.php:88
testWithInvalidActive()
Provides common functionality for UI tests.
Definition: Base.php:224
testAddToolEntryWithoutToolsButton()
testDisallowedToolEntry()
legacy()
Definition: legacy.php:3
Tests for the Main Bar.
Definition: MainBarTest.php:16
mainbar()
Definition: mainbar.php:2
testActive($mb)
testAddEntry
testDouplicateIdToolEntry()
testDouplicateIdEntry()
Definition: MainBarTest.php:94
$factory
Definition: metadata.php:58
brutallyTrimHTML($html)