2021-07-09 11:53:24 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2022-12-30 08:47:02 +02:00
|
|
|
* @link https://www.yiiframework.com/
|
2021-07-09 11:53:24 +02:00
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
2022-12-30 08:47:02 +02:00
|
|
|
* @license https://www.yiiframework.com/license/
|
2021-07-09 11:53:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace yiiunit\extensions\bootstrap5;
|
|
|
|
|
|
|
|
use yii\bootstrap5\LinkPager;
|
|
|
|
use yii\data\Pagination;
|
|
|
|
use yii\helpers\ReplaceArrayValue;
|
|
|
|
use yii\helpers\StringHelper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group bootstrap5
|
|
|
|
*/
|
|
|
|
class LinkPagerTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testFirstLastPageLabels()
|
|
|
|
{
|
|
|
|
$pagination = $this->getPagination(5);
|
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $pagination,
|
|
|
|
'firstPageLabel' => true,
|
|
|
|
'lastPageLabel' => true,
|
|
|
|
]);
|
|
|
|
$this->assertContains('<li class="page-item first"><a class="page-link" href="/?r=test&page=1" data-page="0">1</a></li>', $output);
|
|
|
|
$this->assertContains('<li class="page-item last"><a class="page-link" href="/?r=test&page=25" data-page="24">25</a></li>', $output);
|
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $pagination,
|
|
|
|
'firstPageLabel' => 'First',
|
|
|
|
'lastPageLabel' => 'Last',
|
|
|
|
]);
|
|
|
|
$this->assertContains('<li class="page-item first"><a class="page-link" href="/?r=test&page=1" data-page="0">First</a></li>', $output);
|
|
|
|
$this->assertContains('<li class="page-item last"><a class="page-link" href="/?r=test&page=25" data-page="24">Last</a></li>', $output);
|
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $pagination,
|
|
|
|
'firstPageLabel' => false,
|
|
|
|
'lastPageLabel' => false,
|
|
|
|
]);
|
|
|
|
$this->assertNotContains('<li class="page-item first">', $output);
|
|
|
|
$this->assertNotContains('<li class="page-item last">', $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisabledPageElementOptions()
|
|
|
|
{
|
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $this->getPagination(0),
|
|
|
|
'disabledListItemSubTagOptions' => ['class' => ['foo-bar']],
|
|
|
|
]);
|
|
|
|
$this->assertContains('<li class="page-item prev disabled"><a class="page-link foo-bar"', $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
public function testOverrideDisabledPageElementOptions()
|
|
|
|
{
|
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $this->getPagination(0),
|
|
|
|
'disabledListItemSubTagOptions' => ['class' => new ReplaceArrayValue(['foo-bar'])],
|
|
|
|
]);
|
|
|
|
$this->assertContains('<li class="page-item prev disabled"><a class="foo-bar"', $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisableCurrentPageButton()
|
|
|
|
{
|
|
|
|
$pagination = $this->getPagination(5);
|
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $pagination,
|
|
|
|
'disableCurrentPageButton' => false,
|
|
|
|
]);
|
2021-11-26 19:13:30 +03:00
|
|
|
$this->assertContains('<li class="page-item active" aria-current="page"><a class="page-link" href="/?r=test&page=6" data-page="5">6</a></li>', $output);
|
2021-07-09 11:53:24 +02:00
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $pagination,
|
|
|
|
'disableCurrentPageButton' => true,
|
|
|
|
]);
|
2021-11-26 19:13:30 +03:00
|
|
|
$this->assertContains('<li class="page-item active disabled" aria-current="page"><a class="page-link" href="/?r=test&page=6" data-page="5" tabindex="-1">6</a></li>', $output);
|
2021-07-09 11:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testOptionsWithTagOption()
|
|
|
|
{
|
|
|
|
LinkPager::$counter = 0;
|
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $this->getPagination(5),
|
|
|
|
'options' => [
|
|
|
|
'tag' => 'div',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
$this->assertTrue(StringHelper::startsWith($output, '<div id="w0">'));
|
|
|
|
$this->assertTrue(StringHelper::endsWith($output, '</div>'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLinkWrapOptions()
|
|
|
|
{
|
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $this->getPagination(1),
|
|
|
|
'linkContainerOptions' => [
|
|
|
|
'tag' => 'div',
|
|
|
|
'class' => 'my-class',
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
$this->assertContains(
|
|
|
|
'<div class="my-class page-item"><a class="page-link" href="/?r=test&page=3" data-page="2">3</a></div>',
|
|
|
|
$output
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
2021-11-26 19:13:30 +03:00
|
|
|
'<div class="my-class page-item active" aria-current="page"><a class="page-link" href="/?r=test&page=2" data-page="1">2</a></div>',
|
2021-07-09 11:53:24 +02:00
|
|
|
$output
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://github.com/yiisoft/yii2/issues/15536
|
|
|
|
*/
|
|
|
|
public function testShouldTriggerInitEvent()
|
|
|
|
{
|
|
|
|
$initTriggered = false;
|
|
|
|
LinkPager::widget([
|
|
|
|
'pagination' => $this->getPagination(1),
|
|
|
|
'on init' => function () use (&$initTriggered) {
|
|
|
|
$initTriggered = true;
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
$this->assertTrue($initTriggered);
|
|
|
|
}
|
2021-08-04 08:21:51 +02:00
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->mockWebApplication([
|
|
|
|
'components' => [
|
|
|
|
'urlManager' => [
|
|
|
|
'scriptUrl' => '/',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get pagination.
|
|
|
|
* @param int $page
|
|
|
|
* @return Pagination
|
|
|
|
*/
|
|
|
|
private function getPagination($page)
|
|
|
|
{
|
|
|
|
$pagination = new Pagination();
|
|
|
|
$pagination->setPage($page);
|
|
|
|
$pagination->totalCount = 500;
|
|
|
|
$pagination->route = 'test';
|
|
|
|
|
|
|
|
return $pagination;
|
|
|
|
}
|
2021-07-09 11:53:24 +02:00
|
|
|
}
|