ILIAS  release_8 Revision v8.24
DataTest.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(): I\Table\DataRetrieval
38 {
39 return new class () implements I\Table\DataRetrieval {
40 public function getRows(
41 I\Table\DataRowBuilder $row_builder,
42 array $visible_column_ids,
43 Range $range,
44 Order $order,
45 ?array $filter_data,
46 ?array $additional_parameters
47 ): \Generator {
48 yield $row_builder->buildStandardRow('', []);
49 }
50 public function getTotalRowCount(
51 ?array $filter_data,
52 ?array $additional_parameters
53 ): ?int {
54 return null;
55 }
56 };
57 }
58
59 public function testDataTableBasicConstruction(): void
60 {
61 $data = $this->getDataRetrieval();
62 $cols = ['f0' => $this->getTableFactory()->column()->text("col1")];
63 $table = $this->getTableFactory()->data('title', $cols, $data);
64 $this->assertEquals(800, $table->getNumberOfRows());
65 $this->assertInstanceOf(Order::class, $table->getOrder());
66 $this->assertInstanceOf(Range::class, $table->getRange());
67 $this->assertInstanceOf(I\Signal::class, $table->getAsyncActionSignal());
68 $this->assertInstanceOf(I\Signal::class, $table->getMultiActionSignal());
69 $this->assertInstanceOf(I\Signal::class, $table->getSelectionSignal());
70 $this->assertFalse($table->hasSingleActions());
71 $this->assertFalse($table->hasMultiActions());
72 $this->assertEquals($data, $table->getDataRetrieval());
73 }
74
76 {
77 $this->expectException(\InvalidArgumentException::class);
78 $data = $this->getDataRetrieval();
79 $cols = ['f0' => "col1"];
80 $table = $this->getTableFactory()->data('title', $cols, $data);
81 }
82
84 {
85 $this->expectException(\InvalidArgumentException::class);
86 $data = $this->getDataRetrieval();
87 $cols = [];
88 $table = $this->getTableFactory()->data('title', $cols, $data);
89 }
90
91 public function testDataTableColumns(): void
92 {
93 $f = $this->getTableFactory()->column();
94 $cols = [
95 'f0' => $f->text("col1"),
96 'f1' => $f->text("col2")
97 ];
98 $table = $this->getTableFactory()->data('title', $cols, $this->getDataRetrieval());
99
100 $this->assertEquals(2, $table->getColumnCount());
101 $check = [
102 'f0' => $f->text("col1")->withIndex(0),
103 'f1' => $f->text("col2")->withIndex(1)
104 ];
105 $this->assertEquals($check, $table->getColumns());
106 $this->assertEquals($check, $table->getVisibleColumns());
107 }
108
109 public function testDataTableActions(): void
110 {
111 $f = $this->getTableFactory()->action();
112 $df = new \ILIAS\Data\Factory();
113 $target = $df->uri('http://wwww.ilias.de?ref_id=1');
114 $url_builder = new URLBuilder($target);
115 list($builder, $token) = $url_builder->acquireParameter(['namespace'], 'rowids');
116 $actions = [
117 $f->single('act1', $builder, $token),
118 $f->multi('act2', $builder, $token),
119 $f->standard('act0', $builder, $token)
120 ];
121 $cols = ['f0' => $this->getTableFactory()->column()->text("col1")];
122 $table = $this->getTableFactory()->data('title', $cols, $this->getDataRetrieval())
123 ->withActions($actions);
124
125 $this->assertEquals($actions, $table->getAllActions());
126 $this->assertEqualsCanonicalizing([$actions[0], $actions[2]], $table->getSingleActions());
127 $this->assertEqualsCanonicalizing([$actions[1], $actions[2]], $table->getMultiActions());
128 }
129
130 protected function getTable(): I\Table\Data
131 {
132 $data = $this->getDataRetrieval();
133 $cols = ['f0' => $this->getTableFactory()->column()->text("col1")];
134 $table = $this->getTableFactory()->data('title', $cols, $data);
135 return $table;
136 }
137
138 public function testDataTableWithRequest(): void
139 {
140 $table = $this->getTable();
141 $request = $this->createMock(ServerRequestInterface::class);
142 $this->assertEquals($request, $table->withRequest($request)->getRequest());
143 }
144
145 public function testDataTableWithNumberOfRows(): void
146 {
147 $table = $this->getTable();
148 $nor = 12;
149 $this->assertEquals($nor, $table->withNumberOfRows($nor)->getNumberOfRows());
150 }
151
152 public function testDataTableWithOrder(): void
153 {
154 $table = $this->getTable();
155 $order = new Order('aspect', 'DESC');
156 $this->assertEquals($order, $table->withOrder($order)->getOrder());
157 }
158
159 public function testDataTableWithRange(): void
160 {
161 $table = $this->getTable();
162 $range = new Range(17, 53);
163 $this->assertEquals($range, $table->withRange($range)->getRange());
164 }
165
166 public function testDataTableWithFilter(): void
167 {
168 $table = $this->getTable();
169 $filter = [
170 'aspect' => ['values']
171 ];
172 $this->assertEquals($filter, $table->withFilter($filter)->getFilter());
173 }
174
175 public function testDataTableWithAdditionalParams(): void
176 {
177 $table = $this->getTable();
178 $params = [
179 'param' => 'value'
180 ];
181 $this->assertEquals($params, $table->withAdditionalParameters($params)->getAdditionalParameters());
182 }
183
185 {
186 $data = $this->getDataRetrieval();
187 $cols = [
188 'f0' => $this->getTableFactory()->column()->text(''),
189 'f1' => $this->getTableFactory()->column()->text('')
190 ->withIsOptional(true, false),
191 'f2' => $this->getTableFactory()->column()->text('')
192 ];
193 $table = $this->getTableFactory()->data('title', $cols, $data);
194 $this->assertEquals(3, $table->getColumnCount());
195 $this->assertEquals(['f0', 'f2'], array_keys($table->getVisibleColumns()));
196 $this->assertEquals(0, $table->getVisibleColumns()['f0']->getIndex());
197 $this->assertEquals(2, $table->getVisibleColumns()['f2']->getIndex());
198 }
199
200 public function testDataTableWithId(): void
201 {
202 $table = new class () extends C\Table\Data {
203 public function __construct()
204 {
205 }
206 public function mockGetStorageId(): ?string
207 {
208 return $this->getStorageId();
209 }
210 public function mockGetId(): ?string
211 {
212 return $this->getId();
213 }
214 };
215
216 $this->assertNull($table->mockGetId());
217 $this->assertNull($table->mockGetStorageId());
218
219 $table_id = 'some_id';
220 $internal_table_id = C\Table\Data::STORAGE_ID_PREFIX . $table_id;
221 $table = $table->withId($table_id);
222 $this->assertEquals($table_id, $table->mockGetId());
223 $this->assertEquals($internal_table_id, $table->mockGetStorageId());
224 }
225
226 public function testDataTableWithIdAndStorage(): void
227 {
228 $table_id = 'some_id';
229 $internal_table_id = C\Table\Data::STORAGE_ID_PREFIX . $table_id;
230 $table_data = ['a' => 'b'];
231 $storage = $this->getMockStorage();
232 $storage[$internal_table_id] = $table_data;
233
234 $table = new class ($storage) extends C\Table\Data {
235 protected \ArrayAccess $storage;
236 public function __construct(
237 \ArrayAccess $storage
238 ) {
239 $this->storage = $storage;
240 }
241 public function mockGetStorageData(): ?array
242 {
243 return $this->getStorageData();
244 }
245 };
246
247 $table = $table->withId($table_id);
248 $this->assertEquals($table_data, $table->mockGetStorageData());
249 }
250}
$check
Definition: buildRTE.php:81
Tests for the Data Table.
Definition: DataTest.php:36
testDataTableWithFilter()
Definition: DataTest.php:166
testDataTableWithIdAndStorage()
Definition: DataTest.php:226
testDataTableBasicConstruction()
Definition: DataTest.php:59
testDataTableWithRequest()
Definition: DataTest.php:138
testDataTableWithNumberOfRows()
Definition: DataTest.php:145
testDataTableWithId()
Definition: DataTest.php:200
testDataTableActions()
Definition: DataTest.php:109
getTable()
Definition: DataTest.php:130
testDataTableWithOrder()
Definition: DataTest.php:152
getDataRetrieval()
Definition: DataTest.php:37
testDataTableConstructionWithErrorColumns()
Definition: DataTest.php:75
testDataTableConstructionWithoutColumns()
Definition: DataTest.php:83
testDataTableWithRange()
Definition: DataTest.php:159
testDataTableWithSelectedOptionalCols()
Definition: DataTest.php:184
testDataTableWithAdditionalParams()
Definition: DataTest.php:175
testDataTableColumns()
Definition: DataTest.php:91
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.
if(! $DIC->user() ->getId()||!ilLTIConsumerAccess::hasCustomProviderCreationAccess()) $params
Definition: ltiregstart.php:33
__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...
$token
Definition: xapitoken.php:70
$cols
Definition: xhr_table.php:11