ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ButtonTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.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;
9
14 public function getButtonFactory() {
15 return new \ILIAS\UI\Implementation\Component\Button\Factory();
16 }
17
19 ( "standard" => "btn btn-default"
20 , "primary" => "btn btn-default btn-primary"
21 );
22
24 $f = $this->getButtonFactory();
25
26 $this->assertInstanceOf("ILIAS\\UI\\Component\\Button\\Factory", $f);
27 $this->assertInstanceOf
28 ( "ILIAS\\UI\\Component\\Button\\Standard"
29 , $f->standard("label", "http://www.ilias.de")
30 );
31 $this->assertInstanceOf
32 ( "ILIAS\\UI\\Component\\Button\\Primary"
33 , $f->primary("label", "http://www.ilias.de")
34 );
35 $this->assertInstanceOf
36 ( "ILIAS\\UI\\Component\\Button\\Close"
37 , $f->close()
38 );
39 }
40
44 public function test_button_label_or_glyph_only($factory_method) {
45 $f = $this->getButtonFactory();
46 try {
47 $f->$factory_method($this, "http://www.ilias.de");
48 $this->assertFalse("This should not happen");
49 }
50 catch (\InvalidArgumentException $e) {}
51 }
52
56 public function test_button_string_action_only($factory_method) {
57 $f = $this->getButtonFactory();
58 try {
59 $f->$factory_method("label", $this);
60 $this->assertFalse("This should not happen");
61 }
62 catch (\InvalidArgumentException $e) {}
63 }
64
68 public function test_button_label($factory_method) {
69 $f = $this->getButtonFactory();
70 $b = $f->$factory_method("label", "http://www.ilias.de");
71
72 $this->assertEquals("label", $b->getLabel());
73 }
74
78 public function test_button_with_label($factory_method) {
79 $f = $this->getButtonFactory();
80 $b = $f->$factory_method("label", "http://www.ilias.de");
81
82 $b2 = $b->withLabel("label2");
83
84 $this->assertEquals("label", $b->getLabel());
85 $this->assertEquals("label2", $b2->getLabel());
86 }
87
91 public function test_button_action($factory_method) {
92 $f = $this->getButtonFactory();
93 $b = $f->$factory_method("label", "http://www.ilias.de");
94
95 $this->assertEquals("http://www.ilias.de", $b->getAction());
96 }
97
101 public function test_button_activated_on_default($factory_method) {
102 $f = $this->getButtonFactory();
103 $b = $f->$factory_method("label", "http://www.ilias.de");
104
105 $this->assertTrue($b->isActive());
106 }
107
111 public function test_button_deactivation($factory_method) {
112 $f = $this->getButtonFactory();
113 $b = $f->$factory_method("label", "http://www.ilias.de")
114 ->withUnavailableAction();
115
116 $this->assertFalse($b->isActive());
117 $this->assertEquals("http://www.ilias.de", $b->getAction());
118 }
119
123 public function test_render_button_label($factory_method) {
124 $ln = "http://www.ilias.de";
125 $f = $this->getButtonFactory();
126 $b = $f->$factory_method("label", $ln);
127 $r = $this->getDefaultRenderer();
128
129 $html = $this->normalizeHTML($r->render($b));
130
131 $css_classes = self::$canonical_css_classes[$factory_method];
132 $expected = "<a class=\"$css_classes\" href=\"$ln\" data-action=\"$ln\">".
133 "label".
134 "</a>";
135 $this->assertEquals($expected, $html);
136 }
137
141 public function test_render_button_disabled($factory_method) {
142 $ln = "http://www.ilias.de";
143 $f = $this->getButtonFactory();
144 $b = $f->$factory_method("label", $ln)
145 ->withUnavailableAction();
146 $r = $this->getDefaultRenderer();
147
148 $html = $this->normalizeHTML($r->render($b));
149
150 $css_classes = self::$canonical_css_classes[$factory_method];
151 $expected = "<a class=\"$css_classes ilSubmitInactive\" data-action=\"$ln\">".
152 "label".
153 "</a>";
154 $this->assertEquals($expected, $html);
155 }
156
157 public function test_render_close_button() {
158 $f = $this->getButtonFactory();
159 $r = $this->getDefaultRenderer();
160 $b = $f->close();
161
162 $html = $this->normalizeHTML($r->render($b));
163
164 $expected = "<button type=\"button\" class=\"close\" data-dismiss=\"modal\">".
165 " <span aria-hidden=\"true\">&times;</span>".
166 " <span class=\"sr-only\">Close</span>".
167 "</button>";
168 $this->assertEquals($expected, $html);
169 }
170
174 public function test_render_button_with_on_load_code($factory_method) {
175 $ln = "http://www.ilias.de";
176 $f = $this->getButtonFactory();
177 $r = $this->getDefaultRenderer();
178 $ids = array();
179 $b = $f->$factory_method("label", $ln)
180 ->withOnLoadCode(function($id) use (&$ids) {
181 $ids[] = $id;
182 return "";
183 });
184
185 $html = $this->normalizeHTML($r->render($b));
186
187 $this->assertCount(1, $ids);
188
189 $id = $ids[0];
190 $css_classes = self::$canonical_css_classes[$factory_method];
191 $expected = "<a class=\"$css_classes\" href=\"$ln\" data-action=\"$ln\" id=\"$id\">".
192 "label".
193 "</a>";
194 $this->assertEquals($expected, $html);
195 }
196
198 $f = $this->getButtonFactory();
199 $r = $this->getDefaultRenderer();
200 $ids = array();
201 $b = $f->close()
202 ->withOnLoadCode(function($id) use (&$ids) {
203 $ids[] = $id;
204 return "";
205 });
206
207 $html = $this->normalizeHTML($r->render($b));
208
209 $this->assertCount(1, $ids);
210
211 $id = $ids[0];
212 $expected = "<button type=\"button\" class=\"close\" data-dismiss=\"modal\" id=\"$id\">".
213 " <span aria-hidden=\"true\">&times;</span>".
214 " <span class=\"sr-only\">Close</span>".
215 "</button>";
216 $this->assertEquals($expected, $html);
217 }
218
219 public function button_type_provider() {
220 return array
221 ( array("standard")
222 , array("primary")
223 );
224 }
225
226}
Test on button implementation.
Definition: ButtonTest.php:13
test_button_label_or_glyph_only($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:44
test_button_with_label($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:78
test_____render_close_button_with_on_load_code()
Definition: ButtonTest.php:197
test_render_button_label($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:123
button_type_provider()
Definition: ButtonTest.php:219
test_render_button_disabled($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:141
getButtonFactory()
Definition: ButtonTest.php:14
test_implements_factory_interface()
Definition: ButtonTest.php:23
test_button_deactivation($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:111
test_button_string_action_only($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:56
test_render_button_with_on_load_code($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:174
test_render_close_button()
Definition: ButtonTest.php:157
static $canonical_css_classes
Definition: ButtonTest.php:18
test_button_activated_on_default($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:101
test_button_label($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:68
test_button_action($factory_method)
@dataProvider button_type_provider
Definition: ButtonTest.php:91
An exception for terminatinating execution or to throw for unit testing.
Provides common functionality for UI tests.
Definition: Base.php:69
normalizeHTML($html)
Definition: Base.php:110
getDefaultRenderer()
Definition: Base.php:100
$html
Definition: example_001.php:87
$r
Definition: example_031.php:79