Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
35.60% |
68 / 191 |
|
64.29% |
9 / 14 |
CRAP | |
0.00% |
0 / 1 |
| phpbb_pagination_pagination_test | |
35.60% |
68 / 191 |
|
64.29% |
9 / 14 |
66.34 | |
0.00% |
0 / 1 |
| return_callback_implode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setUp | |
100.00% |
45 / 45 |
|
100.00% |
1 / 1 |
1 | |||
| generate_template_pagination_data | |
0.00% |
0 / 44 |
|
0.00% |
0 / 1 |
2 | |||
| test_generate_template_pagination | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| test_generate_template_pagination_sub | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| test_generate_template_pagination_double_nested | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| on_page_data | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
| test_on_page | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validate_start_data | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
2 | |||
| test_validate_start | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| reverse_start_data | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
| test_reverse_start | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| reverse_limit_data | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
2 | |||
| test_reverse_limit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | require_once __DIR__ . '/../template/template_test_case.php'; |
| 15 | |
| 16 | class phpbb_pagination_pagination_test extends phpbb_template_template_test_case |
| 17 | { |
| 18 | protected $config; |
| 19 | protected $helper; |
| 20 | protected $routing_helper; |
| 21 | protected $pagination; |
| 22 | protected $test_path = 'tests/pagination'; |
| 23 | |
| 24 | public function return_callback_implode() |
| 25 | { |
| 26 | return implode('-', func_get_args()); |
| 27 | } |
| 28 | |
| 29 | protected function setUp(): void |
| 30 | { |
| 31 | parent::setUp(); |
| 32 | |
| 33 | global $phpbb_dispatcher, $phpbb_root_path, $phpEx; |
| 34 | |
| 35 | $phpbb_dispatcher = new phpbb_mock_event_dispatcher(); |
| 36 | $this->user = $this->createMock('\phpbb\user'); |
| 37 | $this->user->expects($this->any()) |
| 38 | ->method('lang') |
| 39 | ->will($this->returnCallback(array($this, 'return_callback_implode'))); |
| 40 | |
| 41 | $this->config = new \phpbb\config\config(array('enable_mod_rewrite' => '1')); |
| 42 | |
| 43 | $manager = new phpbb_mock_extension_manager(__DIR__ . '/', array()); |
| 44 | |
| 45 | $loader = new \Symfony\Component\Routing\Loader\YamlFileLoader( |
| 46 | new \phpbb\routing\file_locator(__DIR__ . '/') |
| 47 | ); |
| 48 | $resources_locator = new \phpbb\routing\resources_locator\default_resources_locator(__DIR__ . '/', PHPBB_ENVIRONMENT, $manager); |
| 49 | |
| 50 | $mock_container = new phpbb_mock_container_builder(); |
| 51 | $mock_container->set('cron.task_collection', []); |
| 52 | |
| 53 | $router = new phpbb_mock_router(new phpbb_mock_container_builder(), $resources_locator, $loader, 'php', __DIR__ . '/', true, true); |
| 54 | |
| 55 | $request = new phpbb_mock_request(); |
| 56 | $request->overwrite('SCRIPT_NAME', '/app.php', \phpbb\request\request_interface::SERVER); |
| 57 | $request->overwrite('SCRIPT_FILENAME', 'app.php', \phpbb\request\request_interface::SERVER); |
| 58 | $request->overwrite('REQUEST_URI', '/app.php', \phpbb\request\request_interface::SERVER); |
| 59 | |
| 60 | $symfony_request = new \phpbb\symfony_request( |
| 61 | $request |
| 62 | ); |
| 63 | |
| 64 | $db = $this->getMockBuilder('\phpbb\db\driver\mysqli') |
| 65 | ->disableOriginalConstructor() |
| 66 | ->getMock(); |
| 67 | |
| 68 | $this->routing_helper = new \phpbb\routing\helper($this->config, $router, $symfony_request, $request, '', 'php'); |
| 69 | $this->helper = new phpbb_mock_controller_helper( |
| 70 | new \phpbb\auth\auth(), |
| 71 | new \phpbb\cache\driver\dummy(), |
| 72 | $this->config, |
| 73 | new \phpbb\cron\manager($mock_container, $this->routing_helper, '', 'php', null), |
| 74 | $db, |
| 75 | new phpbb_mock_event_dispatcher(), |
| 76 | new \phpbb\language\language(new \phpbb\language\language_file_loader($phpbb_root_path, $phpEx)), |
| 77 | $request, |
| 78 | $this->routing_helper, |
| 79 | $symfony_request, |
| 80 | $this->template, |
| 81 | $this->user, |
| 82 | $phpbb_root_path, |
| 83 | 'adm/', |
| 84 | 'php' |
| 85 | ); |
| 86 | $this->pagination = new \phpbb\pagination($this->template, $this->user, $this->helper, $phpbb_dispatcher); |
| 87 | } |
| 88 | |
| 89 | public static function generate_template_pagination_data() |
| 90 | { |
| 91 | return array( |
| 92 | array( |
| 93 | 'page.php', |
| 94 | 'start', |
| 95 | 95, |
| 96 | 10, |
| 97 | 10, |
| 98 | 'pagination |
| 99 | :per_page:10 |
| 100 | :current_page:2 |
| 101 | :base_url:page.php |
| 102 | :previous::page.php |
| 103 | :else:1:page.php |
| 104 | :current:2:page.php?start=10 |
| 105 | :else:3:page.php?start=20 |
| 106 | :else:4:page.php?start=30 |
| 107 | :else:5:page.php?start=40 |
| 108 | :ellipsis:9:page.php?start=80 |
| 109 | :else:10:page.php?start=90 |
| 110 | :next::page.php?start=20 |
| 111 | :u_prev:page.php |
| 112 | :u_next:page.php?start=20', |
| 113 | ), |
| 114 | array( |
| 115 | 'page.php', |
| 116 | 'start', |
| 117 | 95, |
| 118 | 10, |
| 119 | 20, |
| 120 | 'pagination |
| 121 | :per_page:10 |
| 122 | :current_page:3 |
| 123 | :base_url:page.php |
| 124 | :previous::page.php?start=10 |
| 125 | :else:1:page.php |
| 126 | :else:2:page.php?start=10 |
| 127 | :current:3:page.php?start=20 |
| 128 | :else:4:page.php?start=30 |
| 129 | :else:5:page.php?start=40 |
| 130 | :ellipsis:9:page.php?start=80 |
| 131 | :else:10:page.php?start=90 |
| 132 | :next::page.php?start=30 |
| 133 | :u_prev:page.php?start=10 |
| 134 | :u_next:page.php?start=30', |
| 135 | ), |
| 136 | array( |
| 137 | array('routes' => array( |
| 138 | 'core_controller', |
| 139 | 'core_page_controller', |
| 140 | )), |
| 141 | 'page', |
| 142 | 95, |
| 143 | 10, |
| 144 | 10, |
| 145 | 'pagination |
| 146 | :per_page:10 |
| 147 | :current_page:2 |
| 148 | :base_url: |
| 149 | :previous::/test |
| 150 | :else:1:/test |
| 151 | :current:2:/test/page/2 |
| 152 | :else:3:/test/page/3 |
| 153 | :else:4:/test/page/4 |
| 154 | :else:5:/test/page/5 |
| 155 | :ellipsis:9:/test/page/9 |
| 156 | :else:10:/test/page/10 |
| 157 | :next::/test/page/3 |
| 158 | :u_prev:/test |
| 159 | :u_next:/test/page/3', |
| 160 | ), |
| 161 | array( |
| 162 | array('routes' => array( |
| 163 | 'core_controller', |
| 164 | 'core_page_controller', |
| 165 | )), |
| 166 | 'page', |
| 167 | 95, |
| 168 | 10, |
| 169 | 20, |
| 170 | 'pagination |
| 171 | :per_page:10 |
| 172 | :current_page:3 |
| 173 | :base_url: |
| 174 | :previous::/test/page/2 |
| 175 | :else:1:/test |
| 176 | :else:2:/test/page/2 |
| 177 | :current:3:/test/page/3 |
| 178 | :else:4:/test/page/4 |
| 179 | :else:5:/test/page/5 |
| 180 | :ellipsis:9:/test/page/9 |
| 181 | :else:10:/test/page/10 |
| 182 | :next::/test/page/4 |
| 183 | :u_prev:/test/page/2 |
| 184 | :u_next:/test/page/4', |
| 185 | ), |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @dataProvider generate_template_pagination_data |
| 191 | */ |
| 192 | public function test_generate_template_pagination($base_url, $start_name, $num_items, $per_page, $start_item, $expect) |
| 193 | { |
| 194 | $this->pagination->generate_template_pagination($base_url, 'pagination', $start_name, $num_items, $per_page, $start_item); |
| 195 | $this->template->set_filenames(array('test' => 'pagination.html')); |
| 196 | |
| 197 | $this->assertEquals(str_replace("\t", '', $expect), $this->display('test')); |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * @dataProvider generate_template_pagination_data |
| 202 | */ |
| 203 | public function test_generate_template_pagination_sub($base_url, $start_name, $num_items, $per_page, $start_item, $expect) |
| 204 | { |
| 205 | // Block needs to be assigned before pagination |
| 206 | $this->template->assign_block_vars('sub', array( |
| 207 | 'FOO' => 'bar', |
| 208 | )); |
| 209 | |
| 210 | $this->pagination->generate_template_pagination($base_url, 'sub.pagination', $start_name, $num_items, $per_page, $start_item); |
| 211 | $this->template->set_filenames(array('test' => 'pagination_sub.html')); |
| 212 | |
| 213 | $this->assertEquals(str_replace("\t", '', $expect), $this->display('test')); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @dataProvider generate_template_pagination_data |
| 218 | */ |
| 219 | public function test_generate_template_pagination_double_nested($base_url, $start_name, $num_items, $per_page, $start_item, $expect) |
| 220 | { |
| 221 | // Block needs to be assigned before pagination |
| 222 | $this->template->assign_block_vars('sub', array( |
| 223 | 'FOO' => 'bar', |
| 224 | )); |
| 225 | |
| 226 | $this->template->assign_block_vars('sub.level2', array( |
| 227 | 'BAR' => 'foo', |
| 228 | )); |
| 229 | |
| 230 | $this->pagination->generate_template_pagination($base_url, 'sub.level2.pagination', $start_name, $num_items, $per_page, $start_item); |
| 231 | $this->template->set_filenames(array('test' => 'pagination_double_nested.html')); |
| 232 | |
| 233 | $this->assertEquals(str_replace("\t", '', $expect), $this->display('test')); |
| 234 | } |
| 235 | |
| 236 | public static function on_page_data() |
| 237 | { |
| 238 | return array( |
| 239 | array( |
| 240 | 10, |
| 241 | 10, |
| 242 | 0, |
| 243 | 'PAGE_OF-1-1', |
| 244 | ), |
| 245 | array( |
| 246 | '10', |
| 247 | '10', |
| 248 | '0', |
| 249 | 'PAGE_OF-1-1', |
| 250 | ), |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * @dataProvider on_page_data |
| 256 | */ |
| 257 | public function test_on_page($num_items, $per_page, $start_item, $expect_return) |
| 258 | { |
| 259 | $this->assertEquals($expect_return, $this->pagination->on_page($num_items, $per_page, $start_item)); |
| 260 | } |
| 261 | |
| 262 | public static function validate_start_data() |
| 263 | { |
| 264 | return array( |
| 265 | array( |
| 266 | 0, |
| 267 | 0, |
| 268 | 0, |
| 269 | ), |
| 270 | array( |
| 271 | -1, |
| 272 | 20, |
| 273 | 0, |
| 274 | ), |
| 275 | array( |
| 276 | 20, |
| 277 | -30, |
| 278 | 0, |
| 279 | ), |
| 280 | array( |
| 281 | 0, |
| 282 | 20, |
| 283 | 0, |
| 284 | ), |
| 285 | array( |
| 286 | 10, |
| 287 | 20, |
| 288 | 10, |
| 289 | ), |
| 290 | array( |
| 291 | 20, |
| 292 | 20, |
| 293 | 10, |
| 294 | ), |
| 295 | array( |
| 296 | 30, |
| 297 | 20, |
| 298 | 10, |
| 299 | ), |
| 300 | ); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * @dataProvider validate_start_data |
| 305 | */ |
| 306 | public function test_validate_start($start, $num_items, $expect) |
| 307 | { |
| 308 | $this->assertEquals($expect, $this->pagination->validate_start($start, 10, $num_items)); |
| 309 | } |
| 310 | |
| 311 | public static function reverse_start_data() |
| 312 | { |
| 313 | return array( |
| 314 | array( |
| 315 | 10, |
| 316 | 5, |
| 317 | 15, |
| 318 | 0, |
| 319 | ), |
| 320 | array( |
| 321 | 10, |
| 322 | 10, |
| 323 | 25, |
| 324 | 5, |
| 325 | ), |
| 326 | ); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * @dataProvider reverse_start_data |
| 331 | */ |
| 332 | public function test_reverse_start($start, $limit, $num_items, $expect) |
| 333 | { |
| 334 | $this->assertEquals($expect, $this->pagination->reverse_start($start, $limit, $num_items)); |
| 335 | } |
| 336 | |
| 337 | public static function reverse_limit_data() |
| 338 | { |
| 339 | return array( |
| 340 | array( |
| 341 | 10, |
| 342 | 10, |
| 343 | 15, |
| 344 | 5, |
| 345 | ), |
| 346 | array( |
| 347 | 20, |
| 348 | 10, |
| 349 | 15, |
| 350 | 1, |
| 351 | ), |
| 352 | ); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * @dataProvider reverse_limit_data |
| 357 | */ |
| 358 | public function test_reverse_limit($start, $per_page, $num_items, $expect) |
| 359 | { |
| 360 | $this->assertEquals($expect, $this->pagination->reverse_limit($start, $per_page, $num_items)); |
| 361 | } |
| 362 | } |