ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
DataViewControlsTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once("libs/composer/vendor/autoload.php");
22 require_once(__DIR__ . "/TableTestBase.php");
23 
26 use ILIAS\UI\Component as I;
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,
47  Range $range,
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('Table', $columns, $this->getDataRetrieval($total_count));
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  $this->assertEquals(
138  [
139  C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
140  C\Table\Data::VIEWCONTROL_KEY_FIELDSELECTION,
141  ],
142  array_keys($view_controls->getInputs())
143  );
144  }
145 
147  {
148  $factory = $this->getTableFactory();
149  $columns = [
150  'f1' => $factory->column()->text('f1'),
151  'f2' => $factory->column()->text('f2'),
152  ];
153  $total_count = 200;
154  list($table, $view_controls) = $this->getTable($total_count, $columns);
155 
156  $this->assertEquals(
157  [
158  C\Table\Data::VIEWCONTROL_KEY_PAGINATION,
159  C\Table\Data::VIEWCONTROL_KEY_ORDERING,
160  ],
161  array_keys($view_controls->getInputs())
162  );
163  }
164 
165 
166  private function getRequestMock(array $returns): ServerRequestInterface
167  {
168  $request = $this->createMock(ServerRequestInterface::class);
169  $request
170  ->method("getUri")
171  ->willReturn(new class () {
172  public function __toString()
173  {
174  return 'http://localhost:80';
175  }
176  });
177  $request
178  ->method("getQueryParams")
179  ->willReturn($returns);
180  return $request;
181  }
182 
183  public function testDataTableViewControlStorage(): void
184  {
185  $factory = $this->getTableFactory();
186  $columns = [
187  'f1' => $factory->column()->text('f1')->withIsOptional(true),
188  'f2' => $factory->column()->text('f2')->withIsOptional(true),
189  'f3' => $factory->column()->text('f3')->withIsOptional(true),
190  ];
191  $total_count = 12;
192  list($base_table, $view_controls) = $this->getTable($total_count, $columns);
193 
194  $table_id = 'testing_data_table';
195  $table = $base_table
196  ->withId($table_id)
197  ->withRequest(
198  $this->getRequestMock([
199  'view_control/input_0/input_1' => 0,
200  'view_control/input_0/input_2' => 10,
201  'view_control/input_3/input_4' => 'f2',
202  'view_control/input_3/input_5' => 'DESC',
203  'view_control/input_6' => ['f2']
204  ])
205  );
206  list($table, $view_controls) = $table->applyViewControls([], []);
207  //applied values from viewcontrols
208  $this->assertEquals(new Range(0, 10), $table->getRange());
209  $this->assertEquals(new Order('f2', Order::DESC), $table->getOrder());
210  $this->assertEquals(1, count($table->getSelectedOptionalColumns()));
211 
212  //default_values for different id
213  $table = $base_table
214  ->withId('other id')
215  ->withRequest($this->getRequestMock([]));
216  list($table, $view_controls) = $table->applyViewControls([], []);
217  $this->assertEquals(new Range(0, 12), $table->getRange());
218  $this->assertEquals(new Order('f1', Order::ASC), $table->getOrder());
219  $this->assertEquals(3, count($table->getSelectedOptionalColumns()));
220 
221  //applied values from session with empty request
222  $table = $base_table
223  ->withId($table_id)
224  ->withRequest($this->getRequestMock([]));
225  list($table, $view_controls) = $table->applyViewControls([], []);
226  $this->assertEquals(new Range(0, 10), $table->getRange());
227  $this->assertEquals(new Order('f2', Order::DESC), $table->getOrder());
228  $this->assertEquals(1, count($table->getSelectedOptionalColumns()));
229  }
230 }
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...
getDataRetrieval(int $total_count)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Tests for the Data Table.
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:12
__construct(VocabulariesInterface $vocabularies)
getRequestMock(array $returns)
A simple class to express a range of whole positive numbers.
Definition: Range.php:30
Basic Tests for all Tables.
getTable(int $total_count, array $columns)