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