ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
ToggleButtonTest.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2018 Thomas Famula <famula@leifos.de> Extended GPL, see docs/LICENSE */
4 
5 require_once(__DIR__ . "/../../../../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\Signal;
10 
15 {
16  public function getFactory()
17  {
18  return new \ILIAS\UI\Implementation\Component\Button\Factory();
19  }
20 
22  {
23  $f = $this->getFactory();
24 
25  $this->assertInstanceOf(
26  "ILIAS\\UI\\Component\\Button\\Toggle",
27  $f->toggle("label", "action_on_string", "action_off_string")
28  );
29  }
30 
32  {
33  $f = $this->getFactory();
34  try {
35  $f->toggle("label", 1, "action_off_string");
36  $this->assertFalse("This should not happen");
37  } catch (\InvalidArgumentException $e) {
38  $this->assertTrue(true);
39  }
40  }
41 
43  {
44  $f = $this->getFactory();
45  try {
46  $f->toggle("label", "action_on_string", 2);
47  $this->assertFalse("This should not happen");
48  } catch (\InvalidArgumentException $e) {
49  $this->assertTrue(true);
50  }
51  }
52 
53  public function test_setOn_on_default()
54  {
55  $f = $this->getFactory();
56  $button = $f->toggle("label", "action_on_string", "action_off_string", true);
57 
58  $this->assertTrue($button->isEngaged());
59  }
60 
61  public function test_append_OnAction()
62  {
63  $f = $this->getFactory();
64  $signal_on1 = $this->createMock(Signal::class);
65  $signal_on2 = $this->createMock(Signal::class);
66  $signal_off = $this->createMock(Signal::class);
67  $button = $f->toggle("label", $signal_on1, $signal_off);
68  $this->assertEquals([$signal_on1], $button->getActionOn());
69 
70  $button = $button->withAdditionalToggleOnSignal($signal_on2);
71  $this->assertEquals([$signal_on1, $signal_on2], $button->getActionOn());
72  }
73 
74  public function test_append_OffAction()
75  {
76  $f = $this->getFactory();
77  $signal_off1 = $this->createMock(Signal::class);
78  $signal_off2 = $this->createMock(Signal::class);
79  //$signal_on = $this->createMock(Signal::class);
80  $button = $f->toggle("label", "action_on", $signal_off1);
81  $this->assertEquals([$signal_off1], $button->getActionOff());
82 
83  $button = $button->withAdditionalToggleOffSignal($signal_off2);
84  $this->assertEquals([$signal_off1, $signal_off2], $button->getActionOff());
85  }
86 
87  public function test_render_with_label()
88  {
89  $r = $this->getDefaultRenderer();
90  $button = $this->getFactory()->toggle("label", "action_on_string", "action_off_string");
91 
92  $expected = <<<EOT
93  <label>label</label>
94 <button class="il-toggle-button off" id="id_1" aria-pressed="false">
95  <span class="il-toggle-label-on">toggle_on</span>
96  <span class="il-toggle-label-off">toggle_off</span>
97  <span class="il-toggle-switch"></span>
98 </button>
99 EOT;
100 
101  $this->assertHTMLEquals("<div>" . $expected . "</div>", "<div>" . $r->render($button) . "</div>");
102  }
103 
105  {
106  $r = $this->getDefaultRenderer();
107  $button = $this->getFactory()->toggle("", "action_on_string", "action_off_string", true);
108 
109  $expected = ''
110  . '<button class="il-toggle-button on" id="id_1" aria-pressed="false">' //aria-pressed is set to "true" by JS
111  . ' <span class="il-toggle-label-on">toggle_on</span>'
112  . ' <span class="il-toggle-label-off">toggle_off</span>'
113  . ' <span class="il-toggle-switch"></span>'
114  . '</button>';
115 
116  $this->assertHTMLEquals($expected, $r->render($button));
117  return $button;
118  }
119 
120  public function test_render_with_signals()
121  {
122  $r = $this->getDefaultRenderer();
123  $signal_on = $this->createMock(Signal::class);
124  $signal_on->method("__toString")
125  ->willReturn("MOCK_SIGNAL");
126  $signal_off = $this->createMock(Signal::class);
127  $signal_off->method("__toString")
128  ->willReturn("MOCK_SIGNAL");
129  $button = $this->getFactory()->toggle("label", $signal_on, $signal_off);
130 
131  $expected = <<<EOT
132  <label>label</label>
133 <button class="il-toggle-button off" id="id_1" aria-pressed="false">
134  <span class="il-toggle-label-on">toggle_on</span>
135  <span class="il-toggle-label-off">toggle_off</span>
136  <span class="il-toggle-switch"></span>
137 </button>
138 EOT;
139 
140  $this->assertHTMLEquals("<div>" . $expected . "</div>", "<div>" . $r->render($button) . "</div>");
141  }
142 
146  public function test_append_UnavailAction($button)
147  {
148  $r = $this->getDefaultRenderer();
149  $button = $button->withUnavailableAction();
150 
151  $html = $r->render($button);
152 
153  $expected = ''
154  . '<button class="il-toggle-button unavailable" aria-pressed="false" disabled="disabled">'
155  . ' <span class="il-toggle-switch"></span>'
156  . '</button>';
157 
158  $this->assertHTMLEquals(
159  $expected,
160  $html
161  );
162  }
163 }
Test Toggle Button.
test_append_UnavailAction($button)
test_render_setOn_on_default
test_construction_action_off_type_wrong()
Provides common functionality for UI tests.
Definition: Base.php:262
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:372
test_construction_action_on_type_wrong()
getDefaultRenderer(JavaScriptBinding $js_binding=null, $with_stub_renderings=[])
Definition: Base.php:311