silverstripe-framework/tests/forms/gridfield/GridFieldPaginatorTest.php

93 lines
3.5 KiB
PHP
Raw Normal View History

<?php
class GridFieldPaginatorTest extends FunctionalTest {
/** @var ArrayList */
protected $list;
2014-08-15 08:53:05 +02:00
/** @var GridField */
protected $gridField;
2014-08-15 08:53:05 +02:00
/** @var string */
protected static $fixture_file = 'GridFieldTest.yml';
2014-08-15 08:53:05 +02:00
/** @var Form */
protected $form;
2014-08-15 08:53:05 +02:00
/** @var array */
protected $extraDataObjects = array('GridFieldTest_Team', 'GridFieldTest_Player');
2014-08-15 08:53:05 +02:00
public function setUp() {
parent::setUp();
$this->list = new DataList('GridFieldTest_Team');
$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);
$this->form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
}
2014-08-15 08:53:05 +02: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')));
2014-08-15 08:53:05 +02:00
// Check that the header and footer both contains a page count
$this->assertEquals(2, count($content->getBySelector('.pagination-records-number')));
}
2014-08-15 08:53:05 +02:00
public function testThereIsNoPaginatorWhenOnlyOnePage() {
// We set the itemsPerPage to an reasonably big number so as to avoid test broke from small changes
2014-08-15 08:53:05 +02:00
// on the fixture YML file
$total = $this->list->count();
$this->gridField->getConfig()->getComponentByType("GridFieldPaginator")->setItemsPerPage($total);
$fieldHolder = $this->gridField->FieldHolder();
$content = new CSSContentParser($fieldHolder);
2014-08-15 08:53:05 +02:00
// Check that there is no paginator render into the footer
$this->assertEquals(0, count($content->getBySelector('.datagrid-pagination')));
2014-08-15 08:53:05 +02:00
// 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')));
}
public function testUnlimitedRowCount() {
$total = $this->list->count();
// set up the paginator
/** @var GridFieldPaginator $paginator */
$paginator = $this->gridField->getConfig()->getComponentByType("GridFieldPaginator");
$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']);
}
public function testPaginationAvoidsIllegalOffsets() {
$grid = $this->gridField;
$total = $this->list->count();
$perPage = $grid->getConfig()->getComponentByType('GridFieldPaginator')->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());
}
}