ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
DataViewControlsTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once("vendor/composer/vendor/autoload.php");
22require_once(__DIR__ . "/TableTestBase.php");
23
29use Psr\Http\Message\ServerRequestInterface;
31
36{
37 protected function getDataRetrieval(int $total_count): I\Table\DataRetrieval
38 {
39 return new class ($total_count) implements I\Table\DataRetrieval {
40 public function __construct(
41 protected int $total_count
42 ) {
43 }
44 public function getRows(
45 I\Table\DataRowBuilder $row_builder,
46 array $visible_column_ids,
48 Order $order,
49 mixed $additional_viewcontrol_data,
50 mixed $filter_data,
51 mixed $additional_parameters
52 ): \Generator {
53 yield $row_builder->buildStandardRow('', []);
54 }
55 public function getTotalRowCount(
56 mixed $additional_viewcontrol_data,
57 mixed $filter_data,
58 mixed $additional_parameters
59 ): ?int {
60 return $this->total_count;
61 }
62 };
63 }
64
65 protected function getTable(int $total_count, array $columns): array
66 {
67 $factory = $this->getTableFactory();
68 $table = $factory->data($this->getDataRetrieval($total_count), 'Table', $columns);
69 return $table
70 ->withRequest($this->getRequestMock([]))
71 ->applyViewControls([], []);
72 }
73
74 public function testDataTableHasViewControls(): void
75 {
76 $factory = $this->getTableFactory();
77 $columns = [
78 'f1' => $factory->column()->text('f1'),
79 'f2' => $factory->column()->text('f2')->withIsOptional(true),
80 ];
81 $total_count = 12;
82 list($table, $view_controls) = $this->getTable($total_count, $columns);
83
84 $this->assertInstanceOf(I\Input\Container\ViewControl\ViewControl::class, $view_controls);
85 $this->assertEquals(
86 [
87 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
88 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
89 C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
90 ],
91 array_keys($view_controls->getInputs())
92 );
93 foreach (array_values($view_controls->getInputs()) as $vc) {
94 $this->assertInstanceOf(I\Input\Container\ViewControl\ViewControlInput::class, $vc);
95 }
96 }
97
99 {
100 $factory = $this->getTableFactory();
101 $columns = [
102 'f1' => $factory->column()->text('f1'),
103 'f2' => $factory->column()->text('f2')->withIsOptional(true),
104 ];
105 $total_count = current(C\Input\ViewControl\Pagination::DEFAULT_LIMITS) - 1;
106 list($table, $view_controls) = $this->getTable($total_count, $columns);
107
108 $this->assertEquals(
109 [
110 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
111 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
112 C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
113 ],
114 array_keys($view_controls->getInputs())
115 );
116 $this->assertInstanceOf(
117 I\Input\ViewControl\Group::class,
118 $view_controls->getInputs()[C\Table\Data::VIEWCONTROL_KEY_PAGINATION]
119 );
120
121 $group_contents = $view_controls->getInputs()[C\Table\Data::VIEWCONTROL_KEY_PAGINATION]->getInputs();
122 array_walk(
123 $group_contents,
124 fn($vc) => $this->assertInstanceOf(I\Input\ViewControl\NullControl::class, $vc)
125 );
126 }
127
129 {
130 $factory = $this->getTableFactory();
131 $columns = [
132 'f1' => $factory->column()->text('f1')
133 ->withIsSortable(false),
134 'f2' => $factory->column()->text('f2')
135 ->withIsSortable(false)
136 ->withIsOptional(true),
137 ];
138 $total_count = 200;
139 list($table, $view_controls) = $this->getTable($total_count, $columns);
140
141
142 $inputs = $view_controls->getInputs();
143 $this->assertEquals(
144 [
145 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
146 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
147 C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
148 ],
149 array_keys($inputs)
150 );
151 $this->assertInstanceOf(
152 I\Input\ViewControl\Group::class,
153 $inputs[C\Table\Data::VIEWCONTROL_KEY_ORDERING]
154 );
155 }
156
158 {
159 $factory = $this->getTableFactory();
160 $columns = [
161 'f1' => $factory->column()->text('f1')
162 ->withIsSortable(true),
163 'f2' => $factory->column()->text('f2')
164 ->withIsSortable(true)
165 ->withIsOptional(true),
166 ];
167 $total_count = 1;
168 list($table, $view_controls) = $this->getTable($total_count, $columns);
169
170 $inputs = $view_controls->getInputs();
171 $this->assertEquals(
172 [
173 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
174 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
175 C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
176 ],
177 array_keys($inputs)
178 );
179 $this->assertInstanceOf(
180 I\Input\ViewControl\Group::class,
181 $inputs[C\Table\Data::VIEWCONTROL_KEY_ORDERING]
182 );
183 }
184
186 {
187 $factory = $this->getTableFactory();
188 $columns = [
189 'f1' => $factory->column()->text('f1'),
190 'f2' => $factory->column()->text('f2'),
191 ];
192 $total_count = 200;
193 list($table, $view_controls) = $this->getTable($total_count, $columns);
194
195 $this->assertEquals(
196 [
197 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
198 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
199 ],
200 array_keys($view_controls->getInputs())
201 );
202 }
203
204
205 private function getRequestMock(array $returns): ServerRequestInterface
206 {
207 $request = $this->createMock(ServerRequestInterface::class);
208 $request
209 ->method("getUri")
210 ->willReturn(new \GuzzleHttp\Psr7\Uri('http://localhost:80'));
211 $request
212 ->method("getQueryParams")
213 ->willReturn($returns);
214 return $request;
215 }
216
217 public function testDataTableViewControlStorage(): void
218 {
219 $factory = $this->getTableFactory();
220 $columns = [
221 'f1' => $factory->column()->text('f1')->withIsOptional(true),
222 'f2' => $factory->column()->text('f2')->withIsOptional(true),
223 'f3' => $factory->column()->text('f3')->withIsOptional(true),
224 ];
225 $total_count = 12;
226 list($base_table, $view_controls) = $this->getTable($total_count, $columns);
227
228 $table_id = 'testing_data_table';
229 $table = $base_table
230 ->withId($table_id)
231 ->withRequest(
232 $this->getRequestMock([
233 'vctesting_data_table/input_7/input_8' => 0,
234 'vctesting_data_table/input_7/input_9' => 10,
235 'vctesting_data_table/input_10/input_11' => 'f2',
236 'vctesting_data_table/input_10/input_12' => 'DESC',
237 'vctesting_data_table/input_13' => ['f2']
238 ])
239 );
240 list($table, $view_controls) = $table->applyViewControls([], []);
241
242 //applied values from viewcontrols
243 $this->assertEquals(new Range(0, 10), $table->getRange());
244 $this->assertEquals(new Order('f2', Order::DESC), $table->getOrder());
245 $this->assertEquals(1, count($table->getSelectedOptionalColumns()));
246
247 //default_values for different id
248 $table = $base_table
249 ->withId('other id')
250 ->withRequest($this->getRequestMock([]));
251 list($table, $view_controls) = $table->applyViewControls([], []);
252 $this->assertEquals(new Range(0, 12), $table->getRange());
253 $this->assertEquals(new Order('f1', Order::ASC), $table->getOrder());
254 $this->assertEquals(3, count($table->getSelectedOptionalColumns()));
255
256 //applied values from session with empty request
257 $table = $base_table
258 ->withId($table_id)
259 ->withRequest($this->getRequestMock([]));
260 list($table, $view_controls) = $table->applyViewControls([], []);
261 $this->assertEquals(new Range(0, 10), $table->getRange());
262 $this->assertEquals(new Order('f2', Order::DESC), $table->getOrder());
263 $this->assertEquals(1, count($table->getSelectedOptionalColumns()));
264 }
265
267 {
268 $factory = $this->getTableFactory();
269 $columns = [
270 'f1' => $factory->column()->text('f1'),
271 'f2' => $factory->column()->text('f2'),
272 ];
273 $total_count = 200;
274 $table = $factory->data($this->getDataRetrieval($total_count), 'Table', $columns)
275 ->withAdditionalViewControl(
276 $this->getViewControlFactory()->mode([
277 'mode1' => 'a mode',
278 'mode2' => 'another mode'
279 ])
280 )
281 ->withId('testing_data_table_id')
282 ->withRequest(
283 $this->getRequestMock([
284 'vctesting_data_table_id/additional_12' => 'mode2'
285 ])
286 );
287 list($table, $view_controls) = $table->applyViewControls([], []);
288
289 $this->assertEquals(
290 [
291 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
292 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
293 0
294 ],
295 array_keys($view_controls->getInputs())
296 );
297
298 $this->assertEquals(
299 ['mode2'],
300 $table->getAdditionalViewControlData()
301 );
302 }
303}
Tests for the Data Table.
getRequestMock(array $returns)
testDataTableHasNoFieldSelectionButAdditionalViewControl()
getDataRetrieval(int $total_count)
getTable(int $total_count, array $columns)
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
Basic Tests for all Tables.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc