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,
|
|
|
|
]);
|
2024-03-28 16:27:15 +08:00
|
|
|
$this->assertStringContainsString('<li class="page-item first"><a class="page-link" href="/?r=test&page=1" data-page="0">1</a></li>', $output);
|
|
|
|
$this->assertStringContainsString('<li class="page-item last"><a class="page-link" href="/?r=test&page=25" data-page="24">25</a></li>', $output);
|
2021-07-09 11:53:24 +02:00
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $pagination,
|
|
|
|
'firstPageLabel' => 'First',
|
|
|
|
'lastPageLabel' => 'Last',
|
|
|
|
]);
|
2024-03-28 16:27:15 +08:00
|
|
|
$this->assertStringContainsString('<li class="page-item first"><a class="page-link" href="/?r=test&page=1" data-page="0">First</a></li>', $output);
|
|
|
|
$this->assertStringContainsString('<li class="page-item last"><a class="page-link" href="/?r=test&page=25" data-page="24">Last</a></li>', $output);
|
2021-07-09 11:53:24 +02:00
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $pagination,
|
|
|
|
'firstPageLabel' => false,
|
|
|
|
'lastPageLabel' => false,
|
|
|
|
]);
|
2024-03-28 16:27:15 +08:00
|
|
|
$this->assertStringNotContainsString('<li class="page-item first">', $output);
|
|
|
|
$this->assertStringNotContainsString('<li class="page-item last">', $output);
|
2021-07-09 11:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisabledPageElementOptions()
|
|
|
|
{
|
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $this->getPagination(0),
|
|
|
|
'disabledListItemSubTagOptions' => ['class' => ['foo-bar']],
|
|
|
|
]);
|
2024-03-28 16:27:15 +08:00
|
|
|
$this->assertStringContainsString('<li class="page-item prev disabled"><a class="page-link foo-bar"', $output);
|
2021-07-09 11:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
public function testOverrideDisabledPageElementOptions()
|
|
|
|
{
|
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $this->getPagination(0),
|
|
|
|
'disabledListItemSubTagOptions' => ['class' => new ReplaceArrayValue(['foo-bar'])],
|
|
|
|
]);
|
2024-03-28 16:27:15 +08:00
|
|
|
$this->assertStringContainsString('<li class="page-item prev disabled"><a class="foo-bar"', $output);
|
2021-07-09 11:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisableCurrentPageButton()
|
|
|
|
{
|
|
|
|
$pagination = $this->getPagination(5);
|
|
|
|
$output = LinkPager::widget([
|
|
|
|
'pagination' => $pagination,
|
|
|
|
'disableCurrentPageButton' => false,
|
|
|
|
]);
|
2024-03-28 16:27:15 +08:00
|
|
|
$this->assertStringContainsString('<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,
|
|
|
|
]);
|
2024-03-28 16:27:15 +08:00
|
|
|
$this->assertStringContainsString('<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',
|
|
|
|
],
|
|
|
|
]);
|
2024-03-28 16:27:15 +08:00
|
|
|
$this->assertStringContainsString(
|
2021-07-09 11:53:24 +02:00
|
|
|
'<div class="my-class page-item"><a class="page-link" href="/?r=test&page=3" data-page="2">3</a></div>',
|
|
|
|
$output
|
|
|
|
);
|
2024-03-28 16:27:15 +08:00
|
|
|
$this->assertStringContainsString(
|
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
|
|
|
|
2024-03-28 16:27:15 +08:00
|
|
|
protected function setUp(): void
|
2021-08-04 08:21:51 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|