ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CheckboxInputTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once(__DIR__ . "/../../../../../../../vendor/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../../Base.php");
23require_once(__DIR__ . "/InputTest.php");
24require_once(__DIR__ . "/CommonFieldRendering.php");
25
28use ILIAS\Refinery\Factory as Refinery;
29use ILIAS\Data;
30
32{
33 use CommonFieldRendering;
34
37
38 public function setUp(): void
39 {
40 $this->name_source = new DefNamesource();
41 $this->refinery = new Refinery($this->createMock(Data\Factory::class), $this->createMock(ILIAS\Language\Language::class));
42 }
43
44 public function testImplementsFactoryInterface(): void
45 {
46 $f = $this->getFieldFactory();
47
48 $checkbox = $f->checkbox("label", "byline");
49
50 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $checkbox);
51 $this->assertInstanceOf(Field\Checkbox::class, $checkbox);
52 }
53
54 public function testRender(): void
55 {
56 $f = $this->getFieldFactory();
57 $label = "label";
58 $byline = "byline";
59 $checkbox = $f->checkbox($label, $byline)->withNameFrom($this->name_source);
60 $expected = $this->getFormWrappedHtml(
61 'checkbox-field-input',
62 $label,
63 '<input type="checkbox" id="id_1" value="checked" name="name_0" class="c-field-checkbox" />',
64 $byline,
65 'id_1'
66 );
67 $this->assertEquals($expected, $this->render($checkbox));
68 }
69
70 public function testRenderValue(): void
71 {
72 $f = $this->getFieldFactory();
73 $label = "label";
74 $value = true;
75 $checkbox = $f->checkbox($label)->withValue($value)->withNameFrom($this->name_source);
76
77 $expected = '<input type="checkbox" id="id_1" value="checked" checked="checked" name="name_0" class="c-field-checkbox" />';
78 $this->assertStringContainsString($expected, $this->render($checkbox));
79 }
80
81 public function testHandleInvalidValue(): void
82 {
83 $f = $this->getFieldFactory();
84 $label = "label";
85 $value = "invalid";
86 try {
87 $f->checkbox($label)->withValue($value);
88 $this->fail();
89 } catch (InvalidArgumentException $e) {
90 $this->assertTrue(true);
91 }
92 }
93
94 public function testCommonRendering(): void
95 {
96 $f = $this->getFieldFactory();
97 $label = "label";
98 $checkbox = $f->checkbox($label, null)->withNameFrom($this->name_source);
99
100 $this->testWithError($checkbox);
101 $this->testWithNoByline($checkbox);
102 $this->testWithRequired($checkbox);
103 $this->testWithDisabled($checkbox);
104 $this->testWithAdditionalOnloadCodeRendersId($checkbox);
105 }
106
107 public function testTrueContent(): void
108 {
109 $f = $this->getFieldFactory();
110 $label = "label";
111 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
112
113 $input_data = $this->createMock(InputData::class);
114 $input_data
115 ->expects($this->atLeastOnce())
116 ->method("getOr")
117 ->with("name_0", "")
118 ->willReturn("checked");
119
120 $checkbox_true = $checkbox->withInput($input_data);
121
122 $this->assertIsBool($checkbox_true->getContent()->value());
123 $this->assertTrue($checkbox_true->getContent()->value());
124 }
125
126 public function testFalseContent(): void
127 {
128 $f = $this->getFieldFactory();
129 $label = "label";
130 $checkbox = $f->checkbox($label)->withNameFrom($this->name_source);
131
132 $input_data = $this->createMock(InputData::class);
133 $input_data
134 ->expects($this->atLeastOnce())
135 ->method("getOr")
136 ->with("name_0", "")
137 ->willReturn("");
138
139 $checkbox_false = $checkbox->withInput($input_data);
140
141 $this->assertIsBool($checkbox_false->getContent()->value());
142 $this->assertFalse($checkbox_false->getContent()->value());
143 }
144
145 public function testDisabledContent(): void
146 {
147 $f = $this->getFieldFactory();
148 $label = "label";
149 $checkbox = $f->checkbox($label)
150 ->withNameFrom($this->name_source)
151 ->withDisabled(true)
152 ->withValue(true)
153 ->withInput($this->createMock(InputData::class))
154 ;
155
156 $this->assertIsBool($checkbox->getContent()->value());
157 $this->assertTrue($checkbox->getContent()->value());
158 }
159
160 public function testTransformation(): void
161 {
162 $f = $this->getFieldFactory();
163 $label = "label";
164 $new_value = "NEW_VALUE";
165 $checkbox = $f->checkbox($label)
166 ->withNameFrom($this->name_source)
167 ->withDisabled(true)
168 ->withValue(true)
169 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called, $new_value): string {
170 $called = $v;
171 return $new_value;
172 }))
173 ->withInput($this->createMock(InputData::class))
174 ;
175
176 $this->assertIsString($checkbox->getContent()->value());
177 $this->assertEquals($new_value, $checkbox->getContent()->value());
178 }
179
180 public function testNullValue(): void
181 {
182 $f = $this->getFieldFactory();
183 $checkbox = $f->checkbox("label");
184 $checkbox->withValue(null);
185 $this->assertEquals(false, $checkbox->getValue());
186 }
187}
DefNamesource $name_source
Builds data types.
Definition: Factory.php:36
Definition: UI.php:24
Provides common functionality for UI tests.
Definition: Base.php:337
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
This describes commonalities between all inputs.
Definition: Input.php:47
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.