ILIAS  release_8 Revision v8.24
ViewControlPaginationTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25use ILIAS\Data;
26use ILIAS\Refinery\Factory as Refinery;
29
30require_once('ViewControlBaseTest.php');
31
32
34{
35 public function mock_buildRanges(int $total_count, int $page_limit)
36 {
37 return $this->buildRanges($total_count, $page_limit);
38 }
39 public function mock_findCurrentPage(array $ranges, int $offset)
40 {
41 return $this->findCurrentPage($ranges, $offset);
42 }
43 public function mocK_sliceRangesToVisibleEntries(array $ranges, int $current, int $number_of_visible_entries)
44 {
45 return $this->sliceRangesToVisibleEntries($ranges, $current, $number_of_visible_entries);
46 }
47}
48
50{
51 public function testViewControlPaginationConstruct(): void
52 {
53 $vc = $this->buildVCFactory()->pagination();
54
55 $this->assertInstanceOf(Signal::class, $vc->getInternalSignal());
56 $this->assertIsArray($vc->getLimitOptions());
57 $this->assertEquals('', $vc->getLabelLimit());
58 }
59
61 {
62 $this->expectException(\Exception::class);
63 $vc = $this->buildVCFactory()->pagination()
64 ->withValue('notokvalue:-2');
65 }
66
67 public function testViewControlPaginationMutators(): void
68 {
69 $o = [1,2,3];
70 $l = 'limitlabel';
71 $vc = $this->buildVCFactory()->pagination();
72 $this->assertEquals($o, $vc->withLimitOptions($o)->getLimitOptions($o));
73 }
74
75 public static function providePaginationInput(): array
76 {
77 return [
78 [
79 'offset' => 24,
80 'limit' => 25,
81 'expected' => [0, 25]
82 ],
83 [
84 'offset' => 25,
85 'limit' => 25,
86 'expected' => [25, 25]
87 ],
88 [
89 'offset' => 52,
90 'limit' => 25,
91 'expected' => [50, 25]
92 ],
93 [
94 'offset' => 7,
95 'limit' => 5,
96 'expected' => [5, 5]
97 ],
98 [
99 'offset' => 99,
100 'limit' => 5,
101 'expected' => [95, 5]
102 ],
103 [
104 'offset' => 4,
105 'limit' => 3,
106 'expected' => [3, 3]
107 ],
108 [
109 'offset' => 4,
110 'limit' => PHP_INT_MAX,
111 'expected' => [0, PHP_INT_MAX]
112 ],
113 [
114 'offset' => 0,
115 'limit' => 2,
116 'expected' => [0, 2]
117 ],
118 [
119 'offset' => 10,
120 'limit' => 0,
121 'expected' => [10, 5] //default smallest limit
122 ],
123
124 ];
125 }
126
131 int $offset,
132 int $page_size,
133 array $expected
134 ): void {
135 $v = [
136 Pagination::FNAME_OFFSET => $offset,
137 Pagination::FNAME_LIMIT => $page_size
138 ];
139 $input = $this->createMock(InputData::class);
140 $input->expects($this->exactly(2))
141 ->method("getOr")
142 ->will(
143 $this->onConsecutiveCalls($v[Pagination::FNAME_OFFSET], $v[Pagination::FNAME_LIMIT])
144 );
145
146 $vc = $this->buildVCFactory()->pagination()
147 ->withNameFrom($this->getNamesource())
148 ->withInput($input);
149
150 $df = $this->buildDataFactory();
151 $this->assertEquals(
152 $df->ok($df->range(...$expected)),
153 $vc->getContent()
154 );
155
156 $this->assertEquals(
157 [Pagination::FNAME_OFFSET => $offset, Pagination::FNAME_LIMIT => $page_size],
158 $vc->getValue()
159 );
160 }
161
163 {
164 $r = $this->getDefaultRenderer();
165 $vc = $this->buildVCFactory()->pagination()
166 ->withLimitOptions([2, 5, 10])
167 ->withTotalCount(42)
168 ->withValue([Pagination::FNAME_OFFSET => 12, Pagination::FNAME_LIMIT => 2])
169 ->withOnChange((new SignalGenerator())->create());
170
171 $expected = $this->brutallyTrimHTML('
172<div class="il-viewcontrol il-viewcontrol-pagination l-bar__element" id="id_13">
173 <div class="dropdown il-viewcontrol-pagination__sectioncontrol">
174 <div class="btn btn-ctrl browse previous">
175 <a tabindex="0" class="glyph" aria-label="back" id="id_8">
176 <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
177 </a>
178 </div>
179
180 <button class="btn btn-link" id="id_1">1</button>
181 <span class="il-viewcontrol-pagination__spacer">...</span>
182 <button class="btn btn-link" id="id_2">5</button>
183 <button class="btn btn-link" id="id_3">6</button>
184 <button class="btn btn-link engaged" aria-pressed="true" id="id_4">7</button>
185 <button class="btn btn-link" id="id_5">8</button>
186 <button class="btn btn-link" id="id_6">9</button>
187 <span class="il-viewcontrol-pagination__spacer">...</span>
188 <button class="btn btn-link" id="id_7">21</button>
189
190 <div class="btn btn-ctrl browse next">
191 <a tabindex="0" class="glyph" aria-label="next" id="id_9">
192 <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
193 </a>
194 </div>
195 </div>
196
197 <div class="dropdown il-viewcontrol-pagination__num-of-items">
198 <button class="btn btn-ctrl dropdown-toggle" type="button" data-toggle="dropdown" aria-label="label_pagination_limit" aria-haspopup="true" aria-expanded="false" aria-controls="id_13_ctrl_limit">
199 <span class="caret"></span>
200 </button>
201 <ul id="id_13_ctrl_limit" class="dropdown-menu">
202 <li class="selected"><button class="btn btn-link" id="id_10">2</button></li>
203 <li><button class="btn btn-link" id="id_11">5</button></li>
204 <li><button class="btn btn-link" id="id_12">10</button></li>
205 </ul>
206 </div>
207
208 <div class="il-viewcontrol-value hidden" role="none">
209 <input id="id_14" type="hidden" value="12" />
210 <input id="id_15" type="hidden" value="2" />
211 </div>
212</div>
213 ');
214
215 $html = $this->brutallyTrimHTML($r->render($vc));
216 $this->assertEquals($expected, $html);
217 }
218
219 protected function getStubRenderer()
220 {
221 return new VCPaginationRendererMock(
222 $this->getUIFactory(),
223 $this->getTemplateFactory(),
224 $this->getLanguage(),
225 $this->getJavaScriptBinding(),
226 $this->getRefinery(),
227 $this->getImagePathResolver(),
228 $this->getDataFactory()
229 );
230 }
231
233 {
234 $r = $this->getStubRenderer();
235 $ranges = $r->mock_buildRanges($total = 8, $pagelimit = 3); //0-2, 3-5, 6-7
236 $this->assertEquals(3, count($ranges));
237 $ranges = $r->mock_buildRanges(10, 5); //0-4, 5-9
238 $this->assertEquals(2, count($ranges));
239 $ranges = $r->mock_buildRanges(101, 5);
240 $this->assertEquals(21, count($ranges));
241 }
242
244 {
245 $r = $this->getStubRenderer();
246 $ranges = $r->mock_buildRanges(20, 5);
247 $this->assertEquals(0, $r->mock_findCurrentPage($ranges, 3));
248 $this->assertEquals(1, $r->mock_findCurrentPage($ranges, 5));
249 $this->assertEquals(1, $r->mock_findCurrentPage($ranges, 6));
250 $this->assertEquals(2, $r->mock_findCurrentPage($ranges, 10));
251 $this->assertEquals(3, $r->mock_findCurrentPage($ranges, 19));
252 }
253
255 {
256 $r = $this->getStubRenderer();
257 $ranges = $r->mock_buildRanges(203, 5);
258 $slices = $r->mock_sliceRangesToVisibleEntries($ranges, $current = 6, $visible_entries = 5);
259 $this->assertEquals(5, count($slices));
260 $this->assertEquals(0, $slices[0]->getStart());
261 $this->assertEquals(25, $slices[1]->getStart());
262 $this->assertEquals(30, $slices[2]->getStart());
263 $this->assertEquals(35, $slices[3]->getStart());
264 $this->assertEquals(200, $slices[4]->getStart());
265 }
266
268 {
269 $this->expectException(\LogicException::class);
270 $this->buildVCFactory()->pagination()->getOnChangeSignal();
271 }
272}
Builds data types.
Definition: Factory.php:21
sliceRangesToVisibleEntries(array $ranges, int $current, int $number_of_visible_entries)
Definition: Renderer.php:228
mocK_sliceRangesToVisibleEntries(array $ranges, int $current, int $number_of_visible_entries)
mock_findCurrentPage(array $ranges, int $offset)
mock_buildRanges(int $total_count, int $page_limit)
testViewControlPaginationWithInput(int $offset, int $page_size, array $expected)
@dataProvider providePaginationInput
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
Describes a source for input names.
Definition: NameSource.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Factory.php:21