ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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('ViewControlTestBase.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 'page_size' => 25,
81 'expected' => [0, 25]
82 ],
83 [
84 'offset' => 25,
85 'page_size' => 25,
86 'expected' => [25, 25]
87 ],
88 [
89 'offset' => 52,
90 'page_size' => 25,
91 'expected' => [50, 25]
92 ],
93 [
94 'offset' => 7,
95 'page_size' => 5,
96 'expected' => [5, 5]
97 ],
98 [
99 'offset' => 99,
100 'page_size' => 5,
101 'expected' => [95, 5]
102 ],
103 [
104 'offset' => 4,
105 'page_size' => 3,
106 'expected' => [3, 3]
107 ],
108 [
109 'offset' => 4,
110 'page_size' => PHP_INT_MAX,
111 'expected' => [0, PHP_INT_MAX]
112 ],
113 [
114 'offset' => 0,
115 'page_size' => 2,
116 'expected' => [0, 2]
117 ],
118 [
119 'offset' => 10,
120 'page_size' => 0,
121 'expected' => [10, 5] //default smallest limit
122 ],
123
124 ];
125 }
126
127 #[\PHPUnit\Framework\Attributes\DataProvider('providePaginationInput')]
129 int $offset,
130 int $page_size,
131 array $expected
132 ): void {
133 $v = [
134 Pagination::FNAME_OFFSET => $offset,
135 Pagination::FNAME_LIMIT => $page_size
136 ];
137 $input = $this->createMock(InputData::class);
138 $input->expects($this->exactly(2))
139 ->method("getOr")
140 ->will(
141 $this->onConsecutiveCalls($v[Pagination::FNAME_OFFSET], $v[Pagination::FNAME_LIMIT])
142 );
143
144 $vc = $this->buildVCFactory()->pagination()
145 ->withNameFrom($this->getNamesource())
146 ->withInput($input);
147
148 $df = $this->buildDataFactory();
149 $this->assertEquals(
150 $df->ok($df->range(...$expected)),
151 $vc->getContent()
152 );
153
154 $this->assertEquals(
155 [Pagination::FNAME_OFFSET => $offset, Pagination::FNAME_LIMIT => $page_size],
156 $vc->getValue()
157 );
158 }
159
161 {
162 $r = $this->getDefaultRenderer();
163 $vc = $this->buildVCFactory()->pagination()
164 ->withLimitOptions([2, 5, 10])
165 ->withTotalCount(42)
166 ->withValue([Pagination::FNAME_OFFSET => 12, Pagination::FNAME_LIMIT => 2])
167 ->withOnChange((new SignalGenerator())->create());
168
169 $expected = $this->brutallyTrimHTML('
170<div class="il-viewcontrol il-viewcontrol-pagination l-bar__element" id="id_13">
171 <div class="dropdown il-viewcontrol-pagination__sectioncontrol">
172 <button class="btn btn-link" id="id_8">
173 <span class="glyph" aria-label="back" role="img"><span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span></span>
174 </button>
175
176 <button class="btn btn-link" id="id_1">1</button>
177 <span class="il-viewcontrol-pagination__spacer">...</span>
178 <button class="btn btn-link" id="id_2">5</button>
179 <button class="btn btn-link" id="id_3">6</button>
180 <button class="btn btn-link engaged" aria-pressed="true" id="id_4">7</button>
181 <button class="btn btn-link" id="id_5">8</button>
182 <button class="btn btn-link" id="id_6">9</button>
183 <span class="il-viewcontrol-pagination__spacer">...</span>
184 <button class="btn btn-link" id="id_7">21</button>
185
186 <button class="btn btn-link" id="id_9">
187 <span class="glyph" aria-label="next" role="img"><span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span></span>
188 </button>
189 </div>
190
191 <div class="dropdown il-viewcontrol-pagination__num-of-items">
192 <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">
193 <span class="caret"></span>
194 </button>
195 <ul id="id_13_ctrl_limit" class="dropdown-menu">
196 <li class="selected"><button class="btn btn-link" id="id_10">2</button></li>
197 <li><button class="btn btn-link" id="id_11">5</button></li>
198 <li><button class="btn btn-link" id="id_12">10</button></li>
199 </ul>
200 </div>
201
202 <div class="il-viewcontrol-value hidden" role="none">
203 <input id="id_14" type="hidden" value="12" />
204 <input id="id_15" type="hidden" value="2" />
205 </div>
206</div>
207 ');
208
209 $html = $this->brutallyTrimHTML($r->render($vc));
210 $this->assertEquals($expected, $html);
211 }
212
213 protected function getStubRenderer()
214 {
215 return new VCPaginationRendererMock(
216 $this->getUIFactory(),
217 $this->getTemplateFactory(),
218 $this->getLanguage(),
219 $this->getJavaScriptBinding(),
220 $this->getImagePathResolver(),
221 $this->getDataFactory(),
222 $this->getHelpTextRetriever(),
223 $this->getUploadLimitResolver()
224 );
225 }
226
228 {
229 $r = $this->getStubRenderer();
230 $ranges = $r->mock_buildRanges($total = 8, $pagelimit = 3); //0-2, 3-5, 6-7
231 $this->assertEquals(3, count($ranges));
232 $ranges = $r->mock_buildRanges(10, 5); //0-4, 5-9
233 $this->assertEquals(2, count($ranges));
234 $ranges = $r->mock_buildRanges(101, 5);
235 $this->assertEquals(21, count($ranges));
236 }
237
239 {
240 $r = $this->getStubRenderer();
241 $ranges = $r->mock_buildRanges(20, 5);
242 $this->assertEquals(0, $r->mock_findCurrentPage($ranges, 3));
243 $this->assertEquals(1, $r->mock_findCurrentPage($ranges, 5));
244 $this->assertEquals(1, $r->mock_findCurrentPage($ranges, 6));
245 $this->assertEquals(2, $r->mock_findCurrentPage($ranges, 10));
246 $this->assertEquals(3, $r->mock_findCurrentPage($ranges, 19));
247 }
248
250 {
251 $r = $this->getStubRenderer();
252 $ranges = $r->mock_buildRanges(203, 5);
253 $slices = $r->mock_sliceRangesToVisibleEntries($ranges, $current = 6, $visible_entries = 5);
254 $this->assertEquals(5, count($slices));
255 $this->assertEquals(0, $slices[0]->getStart());
256 $this->assertEquals(25, $slices[1]->getStart());
257 $this->assertEquals(30, $slices[2]->getStart());
258 $this->assertEquals(35, $slices[3]->getStart());
259 $this->assertEquals(200, $slices[4]->getStart());
260 }
261
263 {
264 $this->expectException(\LogicException::class);
265 $this->buildVCFactory()->pagination()->getOnChangeSignal();
266 }
267}
Builds data types.
Definition: Factory.php:36
sliceRangesToVisibleEntries(array $ranges, int $current, int $number_of_visible_entries)
Definition: Renderer.php:205
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)
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
getLanguage()