2012-05-18 01:18:55 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests\GridField;
|
|
|
|
|
2017-01-10 14:08:47 +01:00
|
|
|
use SilverStripe\Control\Controller;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
2017-01-10 14:08:47 +01:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldPageCount;
|
2017-01-10 14:08:47 +01:00
|
|
|
use SilverStripe\Forms\GridField\GridFieldPaginator;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
|
2017-02-21 21:09:20 +01:00
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
|
2017-01-10 14:08:47 +01:00
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Player;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
|
|
|
use SilverStripe\ORM\DataList;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class GridFieldPaginatorTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
/**
|
2017-01-10 14:08:47 +01:00
|
|
|
* @var ArrayList
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $list;
|
|
|
|
|
|
|
|
/**
|
2017-01-10 14:08:47 +01:00
|
|
|
* @var GridField
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $gridField;
|
|
|
|
|
|
|
|
/**
|
2017-01-10 14:08:47 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected static $fixture_file = 'GridFieldTest.yml';
|
|
|
|
|
|
|
|
/**
|
2017-01-10 14:08:47 +01:00
|
|
|
* @var Form
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $form;
|
|
|
|
|
|
|
|
/**
|
2017-01-10 14:08:47 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
Team::class,
|
2017-02-21 21:09:20 +01:00
|
|
|
Player::class,
|
|
|
|
Cheerleader::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->list = new DataList(Team::class);
|
|
|
|
$config = GridFieldConfig::create()->addComponents(
|
|
|
|
new GridFieldToolbarHeader(), // Required to support pagecount
|
|
|
|
new GridFieldPaginator(2),
|
|
|
|
new GridFieldPageCount('toolbar-header-right')
|
|
|
|
);
|
|
|
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->form = new Form(null, 'mockform', new FieldList([$this->gridField]), new FieldList());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testThereIsPaginatorWhenMoreThanOnePage()
|
|
|
|
{
|
|
|
|
$fieldHolder = $this->gridField->FieldHolder();
|
|
|
|
$content = new CSSContentParser($fieldHolder);
|
|
|
|
// Check that there is paginator render into the footer
|
|
|
|
$this->assertEquals(1, count($content->getBySelector('.datagrid-pagination')));
|
|
|
|
|
|
|
|
// Check that the header and footer both contains a page count
|
|
|
|
$this->assertEquals(2, count($content->getBySelector('.pagination-records-number')));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testThereIsNoPaginatorWhenOnlyOnePage()
|
|
|
|
{
|
|
|
|
// We set the itemsPerPage to an reasonably big number so as to avoid test broke from small changes
|
|
|
|
// on the fixture YML file
|
|
|
|
$total = $this->list->count();
|
|
|
|
$this->gridField->getConfig()->getComponentByType(GridFieldPaginator::class)->setItemsPerPage($total);
|
|
|
|
$fieldHolder = $this->gridField->FieldHolder();
|
|
|
|
$content = new CSSContentParser($fieldHolder);
|
|
|
|
|
|
|
|
// Check that there is no paginator render into the footer
|
|
|
|
$this->assertEquals(0, count($content->getBySelector('.datagrid-pagination')));
|
|
|
|
|
|
|
|
// Check that there is still 'View 1 - 4 of 4' part on the left of the paginator
|
|
|
|
$this->assertEquals(2, count($content->getBySelector('.pagination-records-number')));
|
|
|
|
}
|
|
|
|
|
2017-01-10 14:08:47 +01:00
|
|
|
public function testUnlimitedRowCount()
|
|
|
|
{
|
|
|
|
$total = $this->list->count();
|
|
|
|
// set up the paginator
|
|
|
|
/** @var GridFieldPaginator $paginator */
|
|
|
|
$paginator = $this->gridField->getConfig()->getComponentByType(GridFieldPaginator::class);
|
|
|
|
$paginator->setThrowExceptionOnBadDataType(true);
|
|
|
|
$paginator->setItemsPerPage(1);
|
|
|
|
$paginator->getManipulatedData($this->gridField, $this->list);
|
|
|
|
|
|
|
|
|
|
|
|
$params = $paginator->getTemplateParameters($this->gridField)->toMap();
|
|
|
|
$this->assertFalse($params['OnlyOnePage']);
|
|
|
|
$this->assertEquals($total, $params['NumRecords']);
|
|
|
|
|
|
|
|
$paginator->setItemsPerPage(0);
|
|
|
|
$params = $paginator->getTemplateParameters($this->gridField)->toMap();
|
|
|
|
$this->assertTrue($params['OnlyOnePage']);
|
|
|
|
$this->assertEquals($total, $params['NumRecords']);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testPaginationAvoidsIllegalOffsets()
|
|
|
|
{
|
|
|
|
$grid = $this->gridField;
|
|
|
|
$total = $this->list->count();
|
|
|
|
$perPage = $grid->getConfig()->getComponentByType(GridFieldPaginator::class)->getItemsPerPage();
|
|
|
|
// Get the last page that will contain results
|
|
|
|
$lastPage = ceil($total / $perPage);
|
|
|
|
// Set the paginator state to point to an 'invalid' page
|
|
|
|
$grid->State->GridFieldPaginator->currentPage = $lastPage + 1;
|
|
|
|
|
|
|
|
// Get the paginated list
|
|
|
|
$list = $grid->getManipulatedList();
|
|
|
|
|
|
|
|
// Assert that the paginator state has been corrected and the list contains items
|
|
|
|
$this->assertEquals(1, $grid->State->GridFieldPaginator->currentPage);
|
|
|
|
$this->assertEquals($perPage, $list->count());
|
|
|
|
}
|
2012-05-18 01:18:55 +02:00
|
|
|
}
|