yii2-bootstrap5/tests/LinkPagerTest.php

155 lines
5.3 KiB
PHP
Raw Normal View History

<?php
/**
2022-12-30 08:47:02 +02:00
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
2022-12-30 08:47:02 +02:00
* @license https://www.yiiframework.com/license/
*/
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&amp;page=1" data-page="0">1</a></li>', $output);
$this->assertStringContainsString('<li class="page-item last"><a class="page-link" href="/?r=test&amp;page=25" data-page="24">25</a></li>', $output);
$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&amp;page=1" data-page="0">First</a></li>', $output);
$this->assertStringContainsString('<li class="page-item last"><a class="page-link" href="/?r=test&amp;page=25" data-page="24">Last</a></li>', $output);
$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);
}
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);
}
/**
*/
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);
}
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&amp;page=6" data-page="5">6</a></li>', $output);
$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&amp;page=6" data-page="5" tabindex="-1">6</a></li>', $output);
}
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(
'<div class="my-class page-item"><a class="page-link" href="/?r=test&amp;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&amp;page=2" data-page="1">2</a></div>',
$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);
}
2024-03-28 16:27:15 +08:00
protected function setUp(): void
{
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;
}
}