ILIAS  release_8 Revision v8.24
DataViewControlsTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once("libs/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 protected int $total_count;
41 public function __construct(
42 int $total_count
43 ) {
44 $this->total_count = $total_count;
45 }
46 public function getRows(
47 I\Table\DataRowBuilder $row_builder,
48 array $visible_column_ids,
49 Range $range,
50 Order $order,
51 ?array $filter_data,
52 ?array $additional_parameters
53 ): \Generator {
54 yield $row_builder->buildStandardRow('', []);
55 }
56 public function getTotalRowCount(
57 ?array $filter_data,
58 ?array $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('Table', $columns, $this->getDataRetrieval($total_count));
69 return $table->applyViewControls([], []);
70 }
71
72 public function testDataTableHasViewControls(): void
73 {
74 $factory = $this->getTableFactory();
75 $columns = [
76 'f1' => $factory->column()->text('f1'),
77 'f2' => $factory->column()->text('f2')->withIsOptional(true),
78 ];
79 $total_count = 12;
80 list($table, $view_controls) = $this->getTable($total_count, $columns);
81
82 $this->assertInstanceOf(I\Input\Container\ViewControl\ViewControl::class, $view_controls);
83 $this->assertEquals(
84 [
85 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
86 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
87 C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
88 ],
89 array_keys($view_controls->getInputs())
90 );
91 foreach (array_values($view_controls->getInputs()) as $vc) {
92 $this->assertInstanceOf(I\Input\Container\ViewControl\ViewControlInput::class, $vc);
93 }
94 }
95
97 {
98 $factory = $this->getTableFactory();
99 $columns = [
100 'f1' => $factory->column()->text('f1'),
101 'f2' => $factory->column()->text('f2')->withIsOptional(true),
102 ];
103 $total_count = current(C\Input\ViewControl\Pagination::DEFAULT_LIMITS) - 1;
104 list($table, $view_controls) = $this->getTable($total_count, $columns);
105
106 $this->assertEquals(
107 [
108 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
109 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
110 C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
111 ],
112 array_keys($view_controls->getInputs())
113 );
114 $this->assertInstanceOf(
115 I\Input\ViewControl\Group::class,
116 $view_controls->getInputs()[C\Table\Data::VIEWCONTROL_KEY_PAGINATION]
117 );
118
119 $group_contents = $view_controls->getInputs()[C\Table\Data::VIEWCONTROL_KEY_PAGINATION]->getInputs();
120 array_walk(
121 $group_contents,
122 fn($vc) => $this->assertInstanceOf(I\Input\ViewControl\NullControl::class, $vc)
123 );
124 }
125
127 {
128 $factory = $this->getTableFactory();
129 $columns = [
130 'f1' => $factory->column()->text('f1')
131 ->withIsSortable(false),
132 'f2' => $factory->column()->text('f2')
133 ->withIsSortable(false)
134 ->withIsOptional(true),
135 ];
136 $total_count = 200;
137 list($table, $view_controls) = $this->getTable($total_count, $columns);
138
139 $this->assertEquals(
140 [
141 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
142 C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
143 ],
144 array_keys($view_controls->getInputs())
145 );
146 }
147
149 {
150 $factory = $this->getTableFactory();
151 $columns = [
152 'f1' => $factory->column()->text('f1'),
153 'f2' => $factory->column()->text('f2'),
154 ];
155 $total_count = 200;
156 list($table, $view_controls) = $this->getTable($total_count, $columns);
157
158 $this->assertEquals(
159 [
160 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
161 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
162 ],
163 array_keys($view_controls->getInputs())
164 );
165 }
166
167
168 private function getRequestMock(array $returns): ServerRequestInterface
169 {
170 $request = $this->createMock(ServerRequestInterface::class);
171 $request
172 ->method("getUri")
173 ->willReturn(new class () {
174 public function __toString()
175 {
176 return 'http://localhost:80';
177 }
178 });
179 $request
180 ->method("getQueryParams")
181 ->willReturn($returns);
182 return $request;
183 }
184
185 public function testDataTableViewControlStorage(): void
186 {
187 $factory = $this->getTableFactory();
188 $columns = [
189 'f1' => $factory->column()->text('f1')->withIsOptional(true),
190 'f2' => $factory->column()->text('f2')->withIsOptional(true),
191 'f3' => $factory->column()->text('f3')->withIsOptional(true),
192 ];
193 $total_count = 12;
194 list($base_table, $view_controls) = $this->getTable($total_count, $columns);
195
196 $table_id = 'testing_data_table';
197 $table = $base_table
198 ->withId($table_id)
199 ->withRequest(
200 $this->getRequestMock([
201 'view_control/input_0/input_1' => 0,
202 'view_control/input_0/input_2' => 10,
203 'view_control/input_3/input_4' => 'f2',
204 'view_control/input_3/input_5' => 'DESC',
205 'view_control/input_6' => ['f2']
206 ])
207 );
208 list($table, $view_controls) = $table->applyViewControls([], []);
209 //applied values from viewcontrols
210 $this->assertEquals(new Range(0, 10), $table->getRange());
211 $this->assertEquals(new Order('f2', Order::DESC), $table->getOrder());
212 $this->assertEquals(1, count($table->getSelectedOptionalColumns()));
213
214 //default_values for different id
215 $table = $base_table
216 ->withId('other id')
217 ->withRequest($this->getRequestMock([]));
218 list($table, $view_controls) = $table->applyViewControls([], []);
219 $this->assertEquals(new Range(0, 12), $table->getRange());
220 $this->assertEquals(new Order('f1', Order::ASC), $table->getOrder());
221 $this->assertEquals(3, count($table->getSelectedOptionalColumns()));
222
223 //applied values from session with empty request
224 $table = $base_table
225 ->withId($table_id)
226 ->withRequest($this->getRequestMock([]));
227 list($table, $view_controls) = $table->applyViewControls([], []);
228 $this->assertEquals(new Range(0, 10), $table->getRange());
229 $this->assertEquals(new Order('f2', Order::DESC), $table->getOrder());
230 $this->assertEquals(1, count($table->getSelectedOptionalColumns()));
231 }
232}
Tests for the Data Table.
getRequestMock(array $returns)
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:13
A simple class to express a range of whole positive numbers.
Definition: Range.php:31
Basic Tests for all Tables.
$factory
Definition: metadata.php:75
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...