Skip to content

Commit

Permalink
MNT Add tests for using GridField with arbitrary data
Browse files Browse the repository at this point in the history
Note that the main tests are added as behat tests in the admin module
  • Loading branch information
GuySartorelli committed Nov 23, 2023
1 parent bf52ace commit a9afa5b
Show file tree
Hide file tree
Showing 4 changed files with 371 additions and 7 deletions.
9 changes: 7 additions & 2 deletions tests/php/Forms/GridField/GridFieldExportButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldExportButton;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\View\ArrayData;

class GridFieldExportButtonTest extends SapphireTest
{
Expand Down Expand Up @@ -155,13 +157,16 @@ public function testNoCsvHeaders()
public function testArrayListInput()
{
$button = new GridFieldExportButton();
$columns = new GridFieldDataColumns();
$columns->setDisplayFields(['ID' => 'ID']);
$this->gridField->getConfig()->addComponent($columns);
$this->gridField->getConfig()->addComponent(new GridFieldPaginator());

//Create an ArrayList 1 greater the Paginator's default 15 rows
$arrayList = new ArrayList();
for ($i = 1; $i <= 16; $i++) {
$dataobject = new DataObject(['ID' => $i]);
$arrayList->add($dataobject);
$datum = new ArrayData(['ID' => $i]);
$arrayList->add($datum);
}
$this->gridField->setList($arrayList);

Expand Down
37 changes: 32 additions & 5 deletions tests/php/Forms/GridField/GridFieldPrintButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\Tests\GridField\GridFieldPrintButtonTest\TestObject;
use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;

class GridFieldPrintButtonTest extends SapphireTest
{
Expand All @@ -33,21 +35,19 @@ protected function setUp(): void

public function testLimit()
{
$this->assertEquals(42, $this->getTestableRows()->count());
$this->assertEquals(42, $this->getTestableRows(TestObject::get())->count());
}

public function testCanViewIsRespected()
{
$orig = TestObject::$canView;
TestObject::$canView = false;
$this->assertEquals(0, $this->getTestableRows()->count());
$this->assertEquals(0, $this->getTestableRows(TestObject::get())->count());
TestObject::$canView = $orig;
}

private function getTestableRows()
private function getTestableRows($list)
{
$list = TestObject::get();

$button = new GridFieldPrintButton();
$button->setPrintColumns(['Name' => 'My Name']);

Expand All @@ -62,4 +62,31 @@ private function getTestableRows()
$printData = $button->generatePrintData($gridField);
return $printData->ItemRows;
}

public function testGeneratePrintData()
{
$names = [
'Bob',
'Alice',
'John',
'Jane',
'Sam',
];

$list = new ArrayList();
foreach ($names as $name) {
$list->add(new ArrayData(['Name' => $name]));
}

$rows = $this->getTestableRows($list);

$foundNames = [];
foreach ($rows as $row) {
foreach ($row->ItemRow as $column) {
$foundNames[] = $column->CellString;
}
}

$this->assertSame($names, $foundNames);
}
}
Loading

0 comments on commit a9afa5b

Please sign in to comment.