ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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->isOn());
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" id="id_1" aria-pressed="false">
95  <div class="il-toggle-switch"></div>
96 </button>
97 EOT;
98 
99  $this->assertHTMLEquals("<div>" . $expected . "</div>", "<div>" . $r->render($button) . "</div>");
100  }
101 
103  {
104  $r = $this->getDefaultRenderer();
105  $button = $this->getFactory()->toggle("", "action_on_string", "action_off_string", true);
106 
107  $expected = ''
108  . '<button class="il-toggle-button on" id="id_1" aria-pressed="false">' //aria-pressed is set to "true" by JS
109  . ' <div class="il-toggle-switch"></div>'
110  . '</button>';
111 
112  $this->assertHTMLEquals($expected, $r->render($button));
113  }
114 
115  public function test_render_with_signals()
116  {
117  $r = $this->getDefaultRenderer();
118  $signal_on = $this->createMock(Signal::class);
119  $signal_on->method("__toString")
120  ->willReturn("MOCK_SIGNAL");
121  $signal_off = $this->createMock(Signal::class);
122  $signal_off->method("__toString")
123  ->willReturn("MOCK_SIGNAL");
124  $button = $this->getFactory()->toggle("label", $signal_on, $signal_off);
125 
126  $expected = <<<EOT
127  <label>label</label>
128 <button class="il-toggle-button" id="id_1" aria-pressed="false">
129  <div class="il-toggle-switch"></div>
130 </button>
131 EOT;
132 
133  $this->assertHTMLEquals("<div>" . $expected . "</div>", "<div>" . $r->render($button) . "</div>");
134  }
135 }
Test Toggle Button.
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:228
$r
Definition: example_031.php:79
test_construction_action_off_type_wrong()
Provides common functionality for UI tests.
Definition: Base.php:191
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:270
test_construction_action_on_type_wrong()