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;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Player;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldPaginator;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldPageCount;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
|
2012-05-18 01:18:55 +02:00
|
|
|
class GridFieldPaginatorTest extends FunctionalTest {
|
|
|
|
/** @var ArrayList */
|
|
|
|
protected $list;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-18 01:18:55 +02:00
|
|
|
/** @var GridField */
|
|
|
|
protected $gridField;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-18 01:18:55 +02:00
|
|
|
/** @var string */
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = 'GridFieldTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-18 01:18:55 +02:00
|
|
|
/** @var Form */
|
|
|
|
protected $form;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-18 01:18:55 +02:00
|
|
|
/** @var array */
|
2016-10-14 03:30:05 +02:00
|
|
|
protected $extraDataObjects = array(
|
|
|
|
Team::class,
|
|
|
|
Player::class
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-18 01:18:55 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->list = new DataList(Team::class);
|
2012-09-25 05:55:59 +02:00
|
|
|
$config = GridFieldConfig::create()->addComponents(
|
|
|
|
new GridFieldToolbarHeader(), // Required to support pagecount
|
|
|
|
new GridFieldPaginator(2),
|
|
|
|
new GridFieldPageCount('toolbar-header-right')
|
|
|
|
);
|
2012-05-18 01:18:55 +02:00
|
|
|
$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
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testThereIsPaginatorWhenMoreThanOnePage() {
|
2012-05-18 01:18:55 +02:00
|
|
|
$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
|
|
|
|
2012-09-25 05:55:59 +02:00
|
|
|
// Check that the header and footer both contains a page count
|
|
|
|
$this->assertEquals(2, count($content->getBySelector('.pagination-records-number')));
|
2012-05-18 01:18:55 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testThereIsNoPaginatorWhenOnlyOnePage() {
|
2012-09-26 23:34:00 +02:00
|
|
|
// 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
|
2012-05-18 01:18:55 +02:00
|
|
|
$total = $this->list->count();
|
2016-10-14 03:30:05 +02:00
|
|
|
$this->gridField->getConfig()->getComponentByType(GridFieldPaginator::class)->setItemsPerPage($total);
|
2012-05-18 01:18:55 +02:00
|
|
|
$fieldHolder = $this->gridField->FieldHolder();
|
|
|
|
$content = new CSSContentParser($fieldHolder);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-18 01:18:55 +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
|
|
|
|
2012-05-18 01:18:55 +02:00
|
|
|
// Check that there is still 'View 1 - 4 of 4' part on the left of the paginator
|
2012-09-25 05:55:59 +02:00
|
|
|
$this->assertEquals(2, count($content->getBySelector('.pagination-records-number')));
|
2012-05-18 01:18:55 +02:00
|
|
|
}
|
2014-10-29 13:16:47 +01:00
|
|
|
|
|
|
|
public function testPaginationAvoidsIllegalOffsets() {
|
|
|
|
$grid = $this->gridField;
|
|
|
|
$total = $this->list->count();
|
2016-10-14 03:30:05 +02:00
|
|
|
$perPage = $grid->getConfig()->getComponentByType(GridFieldPaginator::class)->getItemsPerPage();
|
2014-10-29 13:16:47 +01:00
|
|
|
// 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
|
|
|
}
|