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