ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
RadioInputTest.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
5require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
6require_once(__DIR__ . "/../../../Base.php");
7
10use \ILIAS\UI\Component\Input\Field;
11use \ILIAS\Data;
12use \ILIAS\Validation;
13use \ILIAS\Transformation;
14
16{
17 public function setUp()
18 {
19 $this->name_source = new DefNamesource();
20 }
21
22 protected function buildFactory()
23 {
24 $df = new Data\Factory();
26 new SignalGenerator(),
27 $df,
28 new Validation\Factory($df, $this->createMock(\ilLanguage::class)),
30 );
31 }
32
33 protected function buildRadio() : Radio
34 {
35 $f = $this->buildFactory();
36 $label = "label";
37 $byline = "byline";
38 $radio = $f->radio($label, $byline)
39 ->withOption('value0', 'label0', 'byline0')
40 ->withOption('value1', 'label1', 'byline1')
41 ->withNameFrom($this->name_source);
42 return $radio;
43 }
44
45
47 {
48 $f = $this->buildFactory();
49 $radio = $f->radio("label", "byline");
50 $this->assertInstanceOf(Field\Input::class, $radio);
51 $this->assertInstanceOf(Field\Radio::class, $radio);
52 }
53
54
55 public function test_render()
56 {
57 $r = $this->getDefaultRenderer();
58 $radio = $this->buildRadio();
59 $name = $radio->getName();
60 $label = $radio->getLabel();
61 $byline = $radio->getByline();
62 $options = $radio->getOptions();
63
64 $expected = ""
65 . "<div class=\"form-group row\">"
66 . "<label for=\"\" class=\"control-label col-sm-3\">$label</label>"
67 . "<div class=\"col-sm-9\">"
68 . "<div id=\"id_1\" class=\"il-input-radio\">";
69
70 foreach ($options as $opt_value => $opt_label) {
71 $expected .= ""
72 . "<div class=\"form-control form-control-sm il-input-radiooption\">"
73 . "<input type=\"radio\" id=\"id_1_" . $opt_value . "_opt\" name=\"$name\" value=\"$opt_value\" />"
74 . "<label for=\"id_1_" . $opt_value . "_opt\">$opt_label</label>"
75 . "<div class=\"help-block\">{$radio->getBylineFor($opt_value)}</div>"
76 . "</div>";
77 }
78
79 $expected .= ""
80 . "</div>"
81 . "<div class=\"help-block\">$byline</div>"
82 . "</div>"
83 . "</div>";
84 $this->assertHTMLEquals($expected, $r->render($radio));
85 }
86
87
88 public function test_render_value()
89 {
90 $r = $this->getDefaultRenderer();
91 $radio = $this->buildRadio();
92 $name = $radio->getName();
93 $label = $radio->getLabel();
94 $byline = $radio->getByline();
95 $options = $radio->getOptions();
96 $value = array_keys($options)[0];
97 $radio = $radio->withValue($value);
98 $expected = ""
99 . "<div class=\"form-group row\">"
100 . "<label for=\"\" class=\"control-label col-sm-3\">$label</label>"
101 . "<div class=\"col-sm-9\">"
102 . "<div id=\"id_1\" class=\"il-input-radio\">";
103
104 foreach ($options as $opt_value => $opt_label) {
105 $expected .= "<div class=\"form-control form-control-sm il-input-radiooption\">";
106 if ($opt_value === $value) {
107 $expected .= "<input type=\"radio\" id=\"id_1_" . $opt_value . "_opt\" name=\"$name\" value=\"$opt_value\" checked=\"checked\"/>";
108 } else {
109 $expected .= "<input type=\"radio\" id=\"id_1_" . $opt_value . "_opt\" name=\"$name\" value=\"$opt_value\" />";
110 }
111 $expected .= ""
112 . "<label for=\"id_1_" . $opt_value . "_opt\">$opt_label</label>"
113 . "<div class=\"help-block\">{$radio->getBylineFor($opt_value)}</div>"
114 . "</div>";
115 }
116
117 $expected .= ""
118 . "</div>"
119 . "<div class=\"help-block\">$byline</div>"
120 . "</div>"
121 . "</div>";
122
123 $this->assertHTMLEquals($expected, $r->render($radio));
124 }
125
126
127 public function test_with_dependant()
128 {
129 $r = $this->getDefaultRenderer();
130 $f = $this->buildFactory();
131 $dep_field = $f->text('text', 'text');
132 $radio = $f->radio('label', 'byline')
133 ->withOption('value0', 'label0', 'byline0');
134
135 $this->assertNull($radio->getDependantFieldsFor('value0'));
136
137 $dep = ['dep1' => $dep_field];
138 $radio = $radio->withOption('value1', 'label1', 'byline1', $dep);
139 $this->assertEquals(
140 $dep,
141 $radio->getDependantFieldsFor('value1')
142 );
143 }
144}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:15
Provides common functionality for UI tests.
Definition: Base.php:192
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:270
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:228
test_implements_factory_interface()
$r
Definition: example_031.php:79
A transformation is a function from one datatype to another.
This is what a radio-input looks like.
Definition: Radio.php:11