2018-10-08 02:53:44 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\Tests\GridField;
|
|
|
|
|
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldLazyLoader;
|
|
|
|
use SilverStripe\Forms\Tab;
|
|
|
|
use SilverStripe\Forms\TabSet;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Player;
|
2018-10-11 12:04:23 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2018-10-08 02:53:44 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
|
|
|
|
class GridFieldLazyLoaderTest extends SapphireTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ArrayList
|
|
|
|
*/
|
|
|
|
protected $list;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GridField
|
|
|
|
*/
|
|
|
|
protected $gridField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GridFieldLazyLoader
|
|
|
|
*/
|
|
|
|
protected $component;
|
|
|
|
|
|
|
|
protected static $fixture_file = 'GridFieldTest.yml';
|
|
|
|
|
|
|
|
protected static $extra_dataobjects = [
|
|
|
|
Permissions::class,
|
|
|
|
Cheerleader::class,
|
|
|
|
Player::class,
|
|
|
|
Team::class,
|
|
|
|
];
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->list = DataList::create(Team::class);
|
|
|
|
$this->component = new GridFieldLazyLoader();
|
|
|
|
$config = GridFieldConfig_RecordEditor::create()->addComponent($this->component);
|
|
|
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetManipulatedDataWithoutHeader()
|
|
|
|
{
|
2018-10-11 00:56:39 +02:00
|
|
|
$gridField = $this->getHeaderlessGridField();
|
2018-10-08 02:53:44 +02:00
|
|
|
$this->assertCount(
|
|
|
|
0,
|
2018-10-11 00:56:39 +02:00
|
|
|
$this->component->getManipulatedData($gridField, $this->list)->toArray(),
|
2018-10-08 02:53:44 +02:00
|
|
|
'GridFieldLazyLoader::getManipulatedData should return an empty list if the X-Pjax is unset'
|
|
|
|
);
|
|
|
|
}
|
2018-10-11 00:56:39 +02:00
|
|
|
|
2018-10-08 02:53:44 +02:00
|
|
|
public function testGetManipulatedDataWithoutTabSet()
|
|
|
|
{
|
2018-10-11 00:56:39 +02:00
|
|
|
$gridField = $this->getOutOfTabSetGridField();
|
2018-10-08 02:53:44 +02:00
|
|
|
$this->assertSameSize(
|
|
|
|
$this->list,
|
2018-10-11 00:56:39 +02:00
|
|
|
$this->component->getManipulatedData($gridField, $this->list)->toArray(),
|
2018-10-11 12:04:23 +02:00
|
|
|
'GridFieldLazyLoader::getManipulatedData should return a proper list if GridField is not in a tab'
|
2018-10-08 02:53:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetManipulatedDataNonLazy()
|
|
|
|
{
|
2018-10-11 00:56:39 +02:00
|
|
|
$gridField = $this->getNonLazyGridField();
|
2018-10-08 02:53:44 +02:00
|
|
|
$this->assertSameSize(
|
|
|
|
$this->list,
|
2018-10-11 00:56:39 +02:00
|
|
|
$this->component->getManipulatedData($gridField, $this->list),
|
2018-10-11 12:04:23 +02:00
|
|
|
'GridFieldLazyLoader::getManipulatedData should return a proper list if GridField'
|
|
|
|
. ' is in a tab with the pajax header'
|
2018-10-08 02:53:44 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetHTMLFragmentsWithoutHeader()
|
|
|
|
{
|
2018-10-11 00:56:39 +02:00
|
|
|
$gridField = $this->getHeaderlessGridField();
|
|
|
|
$actual = $this->component->getHTMLFragments($gridField);
|
2018-10-08 02:53:44 +02:00
|
|
|
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
|
2018-10-11 12:04:23 +02:00
|
|
|
$this->assertContains('grid-field--lazy-loadable', $gridField->extraClass());
|
|
|
|
$this->assertNotContains('grid-field--lazy-loaded', $gridField->extraClass());
|
2018-10-08 02:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetHTMLFragmentsWithoutTabSet()
|
|
|
|
{
|
2018-10-11 00:56:39 +02:00
|
|
|
$gridField = $this->getOutOfTabSetGridField();
|
|
|
|
$actual = $this->component->getHTMLFragments($gridField);
|
2018-10-08 02:53:44 +02:00
|
|
|
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
|
2018-10-11 12:04:23 +02:00
|
|
|
$this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
|
|
|
|
$this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
|
2018-10-08 02:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetHTMLFragmentsNonLazy()
|
|
|
|
{
|
2018-10-11 00:56:39 +02:00
|
|
|
$gridField = $this->getNonLazyGridField();
|
|
|
|
$actual = $this->component->getHTMLFragments($gridField);
|
|
|
|
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
|
2018-10-11 12:04:23 +02:00
|
|
|
$this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
|
|
|
|
$this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
|
2018-10-11 00:56:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testReadOnlyGetManipulatedDataWithoutHeader()
|
|
|
|
{
|
|
|
|
$gridField = $this->makeGridFieldReadonly($this->getHeaderlessGridField());
|
|
|
|
$this->assertCount(
|
|
|
|
0,
|
|
|
|
$this->component->getManipulatedData($gridField, $this->list)->toArray(),
|
2018-10-11 12:04:23 +02:00
|
|
|
'Readonly GridFieldLazyLoader::getManipulatedData should return an empty list if the X-Pjax'
|
|
|
|
. ' is unset'
|
2018-10-11 00:56:39 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadOnlyGetManipulatedDataWithoutTabSet()
|
|
|
|
{
|
|
|
|
$gridField = $this->makeGridFieldReadonly($this->getOutOfTabSetGridField());
|
|
|
|
$this->assertSameSize(
|
|
|
|
$this->list,
|
|
|
|
$this->component->getManipulatedData($gridField, $this->list)->toArray(),
|
2018-10-11 12:04:23 +02:00
|
|
|
'Readonly GridFieldLazyLoader::getManipulatedData should return a proper list if GridField is'
|
|
|
|
. ' not in a tab'
|
2018-10-11 00:56:39 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadOnlyGetManipulatedDataNonLazy()
|
|
|
|
{
|
|
|
|
$gridField = $this->makeGridFieldReadonly($this->getNonLazyGridField());
|
|
|
|
$this->assertSameSize(
|
|
|
|
$this->list,
|
|
|
|
$this->component->getManipulatedData($gridField, $this->list),
|
2018-10-11 12:04:23 +02:00
|
|
|
'Readonly GridFieldLazyLoader::getManipulatedData should return a proper list if GridField is in'
|
|
|
|
. ' a tab with the pajax header'
|
2018-10-11 00:56:39 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadOnlyGetHTMLFragmentsWithoutHeader()
|
|
|
|
{
|
|
|
|
$gridField = $this->makeGridFieldReadonly($this->getHeaderlessGridField());
|
|
|
|
$actual = $this->component->getHTMLFragments($gridField);
|
2018-10-08 02:53:44 +02:00
|
|
|
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
|
2018-10-11 12:04:23 +02:00
|
|
|
$this->assertContains('grid-field--lazy-loadable', $gridField->extraClass());
|
|
|
|
$this->assertNotContains('grid-field--lazy-loaded', $gridField->extraClass());
|
2018-10-11 00:56:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadOnlyGetHTMLFragmentsWithoutTabSet()
|
|
|
|
{
|
|
|
|
$gridField = $this->makeGridFieldReadonly($this->getOutOfTabSetGridField());
|
|
|
|
$actual = $this->component->getHTMLFragments($gridField);
|
|
|
|
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
|
2018-10-11 12:04:23 +02:00
|
|
|
$this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
|
|
|
|
$this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
|
2018-10-11 00:56:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadOnlyGetHTMLFragmentsNonLazy()
|
|
|
|
{
|
|
|
|
$gridField = $this->makeGridFieldReadonly($this->getNonLazyGridField());
|
|
|
|
$actual = $this->component->getHTMLFragments($gridField);
|
|
|
|
$this->assertEmpty($actual, 'getHTMLFragments should always return an array');
|
2018-10-11 12:04:23 +02:00
|
|
|
$this->assertContains('grid-field--lazy-loaded', $gridField->extraClass());
|
|
|
|
$this->assertNotContains('grid-field--lazy-loadable', $gridField->extraClass());
|
2018-10-08 02:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This GridField will be lazy because it doesn't have a `X-Pjax` header.
|
|
|
|
* @return GridField
|
|
|
|
*/
|
2018-10-08 03:25:37 +02:00
|
|
|
private function getHeaderlessGridField()
|
|
|
|
{
|
2018-10-08 02:53:44 +02:00
|
|
|
$this->gridField->setRequest(new HTTPRequest('GET', 'admin/pages/edit/show/9999'));
|
2018-10-11 12:04:23 +02:00
|
|
|
$fieldList = FieldList::create(new TabSet('Root', new Tab('Main')));
|
2018-10-08 02:53:44 +02:00
|
|
|
$fieldList->addFieldToTab('Root.GridField', $this->gridField);
|
|
|
|
Form::create(null, 'Form', $fieldList, FieldList::create());
|
|
|
|
return $this->gridField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This GridField will not be lazy because it's in not in a tab set.
|
|
|
|
* @return GridField
|
|
|
|
*/
|
|
|
|
private function getOutOfTabSetGridField()
|
|
|
|
{
|
|
|
|
$r = new HTTPRequest('POST', 'admin/pages/edit/EditForm/9999/field/testfield');
|
|
|
|
$r->addHeader('X-Pjax', 'CurrentField');
|
|
|
|
$this->gridField->setRequest($r);
|
|
|
|
$fieldList = new FieldList($this->gridField);
|
|
|
|
Form::create(null, 'Form', $fieldList, FieldList::create());
|
|
|
|
return $this->gridField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This gridfield will not be lazy, because it has `X-Pjax` header equal to `CurrentField`
|
|
|
|
* @return GridField
|
|
|
|
*/
|
|
|
|
private function getNonLazyGridField()
|
|
|
|
{
|
|
|
|
$r = new HTTPRequest('POST', 'admin/pages/edit/EditForm/9999/field/testfield');
|
|
|
|
$r->addHeader('X-Pjax', 'CurrentField');
|
|
|
|
$this->gridField->setRequest($r);
|
2018-10-11 12:04:23 +02:00
|
|
|
$fieldList = new FieldList(new TabSet('Root', new Tab('Main')));
|
2018-10-08 02:53:44 +02:00
|
|
|
$fieldList->addFieldToTab('Root', $this->gridField);
|
|
|
|
Form::create(null, 'Form', $fieldList, FieldList::create());
|
|
|
|
return $this->gridField;
|
|
|
|
}
|
2018-10-11 00:56:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a readonly transformation on our GridField's Form and return the ReadOnly GridField.
|
|
|
|
*
|
|
|
|
* We need to make sure the LazyLoader component still works after our GridField has been made readonly.
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return GridField
|
|
|
|
*/
|
|
|
|
private function makeGridFieldReadonly(GridField $gridField)
|
|
|
|
{
|
|
|
|
$form = $gridField->getForm()->makeReadonly();
|
|
|
|
$fields = $form->Fields()->dataFields();
|
|
|
|
foreach ($fields as $field) {
|
2018-10-11 12:04:23 +02:00
|
|
|
if ($field->getName() === 'testfield') {
|
2018-10-11 00:56:39 +02:00
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-08 02:53:44 +02:00
|
|
|
}
|