ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 ?array $filter_data,
50 ?array $additional_parameters
51 ): \Generator {
52 yield $row_builder->buildStandardRow('', []);
53 }
54 public function getTotalRowCount(
55 ?array $filter_data,
56 ?array $additional_parameters
57 ): ?int {
58 return $this->total_count;
59 }
60 };
61 }
62
63 protected function getTable(int $total_count, array $columns): array
64 {
65 $factory = $this->getTableFactory();
66 $table = $factory->data($this->getDataRetrieval($total_count), 'Table', $columns);
67 return $table->applyViewControls([], []);
68 }
69
70 public function testDataTableHasViewControls(): void
71 {
72 $factory = $this->getTableFactory();
73 $columns = [
74 'f1' => $factory->column()->text('f1'),
75 'f2' => $factory->column()->text('f2')->withIsOptional(true),
76 ];
77 $total_count = 12;
78 list($table, $view_controls) = $this->getTable($total_count, $columns);
79
80 $this->assertInstanceOf(I\Input\Container\ViewControl\ViewControl::class, $view_controls);
81 $this->assertEquals(
82 [
83 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
84 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
85 C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
86 ],
87 array_keys($view_controls->getInputs())
88 );
89 foreach (array_values($view_controls->getInputs()) as $vc) {
90 $this->assertInstanceOf(I\Input\Container\ViewControl\ViewControlInput::class, $vc);
91 }
92 }
93
95 {
96 $factory = $this->getTableFactory();
97 $columns = [
98 'f1' => $factory->column()->text('f1'),
99 'f2' => $factory->column()->text('f2')->withIsOptional(true),
100 ];
101 $total_count = current(C\Input\ViewControl\Pagination::DEFAULT_LIMITS) - 1;
102 list($table, $view_controls) = $this->getTable($total_count, $columns);
103
104 $this->assertEquals(
105 [
106 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
107 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
108 C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
109 ],
110 array_keys($view_controls->getInputs())
111 );
112 $this->assertInstanceOf(
113 I\Input\ViewControl\Group::class,
114 $view_controls->getInputs()[C\Table\Data::VIEWCONTROL_KEY_PAGINATION]
115 );
116
117 $group_contents = $view_controls->getInputs()[C\Table\Data::VIEWCONTROL_KEY_PAGINATION]->getInputs();
118 array_walk(
119 $group_contents,
120 fn($vc) => $this->assertInstanceOf(I\Input\ViewControl\NullControl::class, $vc)
121 );
122 }
123
125 {
126 $factory = $this->getTableFactory();
127 $columns = [
128 'f1' => $factory->column()->text('f1')
129 ->withIsSortable(false),
130 'f2' => $factory->column()->text('f2')
131 ->withIsSortable(false)
132 ->withIsOptional(true),
133 ];
134 $total_count = 200;
135 list($table, $view_controls) = $this->getTable($total_count, $columns);
136
137
138 $inputs = $view_controls->getInputs();
139 $this->assertEquals(
140 [
141 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
142 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
143 C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
144 ],
145 array_keys($inputs)
146 );
147 $this->assertInstanceOf(
148 I\Input\ViewControl\Group::class,
149 $inputs[C\Table\Data::VIEWCONTROL_KEY_ORDERING]
150 );
151 }
152
154 {
155 $factory = $this->getTableFactory();
156 $columns = [
157 'f1' => $factory->column()->text('f1')
158 ->withIsSortable(true),
159 'f2' => $factory->column()->text('f2')
160 ->withIsSortable(true)
161 ->withIsOptional(true),
162 ];
163 $total_count = 1;
164 list($table, $view_controls) = $this->getTable($total_count, $columns);
165
166 $inputs = $view_controls->getInputs();
167 $this->assertEquals(
168 [
169 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
170 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
171 C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
172 ],
173 array_keys($inputs)
174 );
175 $this->assertInstanceOf(
176 I\Input\ViewControl\Group::class,
177 $inputs[C\Table\Data::VIEWCONTROL_KEY_ORDERING]
178 );
179 }
180
182 {
183 $factory = $this->getTableFactory();
184 $columns = [
185 'f1' => $factory->column()->text('f1'),
186 'f2' => $factory->column()->text('f2'),
187 ];
188 $total_count = 200;
189 list($table, $view_controls) = $this->getTable($total_count, $columns);
190
191 $this->assertEquals(
192 [
193 C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
194 C\Table\Data::VIEWCONTROL_KEY_ORDERING,
195 ],
196 array_keys($view_controls->getInputs())
197 );
198 }
199
200
201 private function getRequestMock(array $returns): ServerRequestInterface
202 {
203 $request = $this->createMock(ServerRequestInterface::class);
204 $request
205 ->method("getUri")
206 ->willReturn(new \GuzzleHttp\Psr7\Uri('http://localhost:80'));
207 $request
208 ->method("getQueryParams")
209 ->willReturn($returns);
210 return $request;
211 }
212
213 public function testDataTableViewControlStorage(): void
214 {
215 $factory = $this->getTableFactory();
216 $columns = [
217 'f1' => $factory->column()->text('f1')->withIsOptional(true),
218 'f2' => $factory->column()->text('f2')->withIsOptional(true),
219 'f3' => $factory->column()->text('f3')->withIsOptional(true),
220 ];
221 $total_count = 12;
222 list($base_table, $view_controls) = $this->getTable($total_count, $columns);
223
224 $table_id = 'testing_data_table';
225 $table = $base_table
226 ->withId($table_id)
227 ->withRequest(
228 $this->getRequestMock([
229 'view_control/input_0/input_1' => 0,
230 'view_control/input_0/input_2' => 10,
231 'view_control/input_3/input_4' => 'f2',
232 'view_control/input_3/input_5' => 'DESC',
233 'view_control/input_6' => ['f2']
234 ])
235 );
236 list($table, $view_controls) = $table->applyViewControls([], []);
237 //applied values from viewcontrols
238 $this->assertEquals(new Range(0, 10), $table->getRange());
239 $this->assertEquals(new Order('f2', Order::DESC), $table->getOrder());
240 $this->assertEquals(1, count($table->getSelectedOptionalColumns()));
241
242 //default_values for different id
243 $table = $base_table
244 ->withId('other id')
245 ->withRequest($this->getRequestMock([]));
246 list($table, $view_controls) = $table->applyViewControls([], []);
247 $this->assertEquals(new Range(0, 12), $table->getRange());
248 $this->assertEquals(new Order('f1', Order::ASC), $table->getOrder());
249 $this->assertEquals(3, count($table->getSelectedOptionalColumns()));
250
251 //applied values from session with empty request
252 $table = $base_table
253 ->withId($table_id)
254 ->withRequest($this->getRequestMock([]));
255 list($table, $view_controls) = $table->applyViewControls([], []);
256 $this->assertEquals(new Range(0, 10), $table->getRange());
257 $this->assertEquals(new Order('f2', Order::DESC), $table->getOrder());
258 $this->assertEquals(1, count($table->getSelectedOptionalColumns()));
259 }
260}
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: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