ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
5require_once(__DIR__ . "/../../../../libs/composer/vendor/autoload.php");
6require_once(__DIR__ . "/../../Base.php");
7
8use \ILIAS\UI\Component as C;
9use \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 <div class="il-toggle-switch"></div>
96</button>
97EOT;
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 return $button;
114 }
115
116 public function test_render_with_signals()
117 {
118 $r = $this->getDefaultRenderer();
119 $signal_on = $this->createMock(Signal::class);
120 $signal_on->method("__toString")
121 ->willReturn("MOCK_SIGNAL");
122 $signal_off = $this->createMock(Signal::class);
123 $signal_off->method("__toString")
124 ->willReturn("MOCK_SIGNAL");
125 $button = $this->getFactory()->toggle("label", $signal_on, $signal_off);
126
127 $expected = <<<EOT
128 <label>label</label>
129<button class="il-toggle-button off" id="id_1" aria-pressed="false">
130 <div class="il-toggle-switch"></div>
131</button>
132EOT;
133
134 $this->assertHTMLEquals("<div>" . $expected . "</div>", "<div>" . $r->render($button) . "</div>");
135 }
136
140 public function test_append_UnavailAction($button)
141 {
142 $r = $this->getDefaultRenderer();
143 $button = $button->withUnavailableAction();
144
145 $html = $r->render($button);
146
147 $expected = ''
148 . '<button class="il-toggle-button unavailable" aria-pressed="false" disabled="disabled">'
149 . ' <div class="il-toggle-switch"></div>'
150 . '</button>';
151
152 $this->assertHTMLEquals(
153 $expected,
154 $html
155 );
156 }
157}
An exception for terminatinating execution or to throw for unit testing.
Provides common functionality for UI tests.
Definition: Base.php:225
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:326
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:268
Test Toggle Button.
test_construction_action_on_type_wrong()
test_construction_action_off_type_wrong()
test_append_UnavailAction($button)
@depends test_render_setOn_on_default