silverstripe-framework/tests/php/Forms/GridField/GridFieldTest.php

663 lines
23 KiB
PHP
Raw Normal View History

<?php
2016-10-14 03:30:05 +02:00
namespace SilverStripe\Forms\Tests\GridField;
use InvalidArgumentException;
use LogicException;
use SilverStripe\Dev\CSSContentParser;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
2016-10-14 03:30:05 +02:00
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldConfig_Base;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
2016-10-14 03:30:05 +02:00
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
use SilverStripe\Forms\GridField\GridFieldPageCount;
use SilverStripe\Forms\GridField\GridFieldPaginator;
2016-10-14 03:30:05 +02:00
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use SilverStripe\Forms\GridField\GridState;
use SilverStripe\Forms\GridField\GridState_Component;
use SilverStripe\Forms\GridField\GridState_Data;
use SilverStripe\Forms\RequiredFields;
2016-10-14 03:30:05 +02:00
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Component;
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Component2;
use SilverStripe\Forms\Tests\GridField\GridFieldTest\HTMLFragments;
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions;
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Player;
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
use SilverStripe\Forms\Tests\ValidatorTest\TestValidator;
2016-10-14 03:30:05 +02:00
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\ValidationResult;
API Refactor bootstrap, request handling See https://github.com/silverstripe/silverstripe-framework/pull/7037 and https://github.com/silverstripe/silverstripe-framework/issues/6681 Squashed commit of the following: commit 8f65e5653211240650eaa4fa65bb83b45aae6d58 Author: Ingo Schommer <me@chillu.com> Date: Thu Jun 22 22:25:50 2017 +1200 Fixed upgrade guide spelling commit 76f95944fa89b0b540704b8d744329f690f9698c Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 16:38:34 2017 +1200 BUG Fix non-test class manifest including sapphiretest / functionaltest commit 9379834cb4b2e5177a2600049feec05bf111c16b Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:50:47 2017 +1200 BUG Fix nesting bug in Kernel commit 188ce35d82599360c40f0f2de29579c56fb90761 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:14:51 2017 +1200 BUG fix db bootstrapping issues commit 7ed4660e7a63915e8e974deeaba9807bc4d38b0d Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 14:49:07 2017 +1200 BUG Fix issue in DetailedErrorFormatter commit 738f50c497166f81ccbe3f40fbcff895ce71f82f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:49:19 2017 +1200 Upgrading notes on mysite/_config.php commit 6279d28e5e455916f902a2f963c014d8899f7fc7 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:43:28 2017 +1200 Update developer documentation commit 5c90d53a84ef0139c729396949a7857fae60436f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 10:48:44 2017 +1200 Update installer to not use global databaseConfig commit f9b2ba4755371f08bd95f6908ac612fcbb7ca205 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 21:04:39 2017 +1200 Fix behat issues commit 5b59a912b60282b4dad4ef10ed3b97c5d0a761ac Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 17:07:11 2017 +1200 Move HTTPApplication to SilverStripe\Control namespace commit e2c4a18f637bdd3d276619554de60ee8b4d95ced Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 16:29:03 2017 +1200 More documentation Fix up remaining tests Refactor temp DB into TempDatabase class so it’s available outside of unit tests. commit 5d235e64f341d6251bfe9f4833f15cc8593c5034 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 12:13:15 2017 +1200 API HTTPRequestBuilder::createFromEnvironment() now cleans up live globals BUG Fix issue with SSViewer Fix Security / View tests commit d88d4ed4e48291cb65407f222f190064b1f1deeb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:39:43 2017 +1200 API Refactor AppKernel into CoreKernel commit f7946aec3391139ae1b4029c353c327a36552b36 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:00:40 2017 +1200 Docs and minor cleanup commit 12bd31f9366327650b5c0c0f96cd0327d44faf0a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 15:34:34 2017 +1200 API Remove OutputMiddleware API Move environment / global / ini management into Environment class API Move getTempFolder into TempFolder class API Implement HTTPRequestBuilder / CLIRequestBuilder BUG Restore SS_ALLOWED_HOSTS check in original location API CoreKernel now requires $basePath to be passed in API Refactor installer.php to use application to bootstrap API move memstring conversion globals to Convert BUG Fix error in CoreKernel nesting not un-nesting itself properly. commit bba979114624247cf463cf2a8c9e4be9a7c3a772 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 18:07:53 2017 +1200 API Create HTTPMiddleware and standardise middleware for request handling commit 2a10c2397bdc53001013f607b5d38087ce6c0730 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:42:42 2017 +1200 Fixed ORM tests commit d75a8d1d93398af4bd0432df9e4bc6295c15a3fe Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:15:07 2017 +1200 FIx i18n tests commit 06364af3c379c931889c4cc34dd920fee3db204a Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 16:59:34 2017 +1200 Fix controller namespace Move states to sub namespace commit 2a278e2953d2dbb19f78d91c919048e1fc935436 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 12:49:45 2017 +1200 Fix forms namespace commit b65c21241bee019730027071d815dbf7571197a4 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:56:48 2017 +1200 Update API usages commit d1d4375c95a264a6b63cbaefc2c1d12f808bfd82 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:41:44 2017 +1200 API Refactor $flush into HTPPApplication API Enforce health check in Controller::pushCurrent() API Better global backup / restore Updated Director::test() to use new API commit b220534f06732db4fa940d8724c2a85c0ba2495a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 22:05:57 2017 +1200 Move app nesting to a test state helper commit 603704165c08d0c1c81fd5e6bb9506326eeee17b Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 21:46:04 2017 +1200 Restore kernel stack to fix multi-level nesting commit 2f6336a15bf79dc8c2edd44cec1931da2dd51c28 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 17:23:21 2017 +1200 API Implement kernel nesting commit fc7188da7d6ad6785354bab61f08700454c81d91 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:43:13 2017 +1200 Fix core tests commit a0ae7235148fffd71f2f02d1fe7fe45bf3aa39eb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:23:52 2017 +1200 Fix manifest tests commit ca033952513633e182040d3d13e1caa9000ca184 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:00:00 2017 +1200 API Move extension management into test state commit c66d4339777663a8a04661fea32a0cf35b95d20f Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 14:10:59 2017 +1200 API Refactor SapphireTest state management into SapphireTestState API Remove Injector::unregisterAllObjects() API Remove FakeController commit f26ae75c6ecaafa0dec1093264e0187191e6764d Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 18:04:34 2017 +1200 Implement basic CLI application object commit 001d5596621404892de0a5413392379eff990641 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 17:39:38 2017 +1200 Remove references to SapphireTest::is_running_test() Upgrade various code commit de079c041dacd96bc4f4b66421fa2b2cc4c320f8 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 7 18:07:33 2017 +1200 API Implement APP object API Refactor of Session
2017-06-22 12:50:45 +02:00
use SilverStripe\Security\Group;
2016-10-14 03:30:05 +02:00
use SilverStripe\Security\Member;
use SilverStripe\Versioned\VersionedGridFieldStateExtension;
2016-10-14 03:30:05 +02:00
class GridFieldTest extends SapphireTest
{
protected static $extra_dataobjects = [
Permissions::class,
Cheerleader::class,
Player::class,
Team::class,
];
protected static $illegal_extensions = [
GridFieldConfig_RecordEditor::class => [
VersionedGridFieldStateExtension::class,
],
GridFieldConfig_Base::class => [
VersionedGridFieldStateExtension::class,
],
];
/**
* @covers \SilverStripe\Forms\GridField\GridField::__construct
*/
public function testGridField()
{
$obj = new GridField('testfield', 'testfield');
$this->assertTrue($obj instanceof GridField, 'Test that the constructor arguments are valid');
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::__construct
* @covers \SilverStripe\Forms\GridField\GridField::getList
*/
public function testGridFieldSetList()
{
$list = ArrayList::create([1 => 'hello', 2 => 'goodbye']);
$obj = new GridField('testfield', 'testfield', $list);
$this->assertEquals($list, $obj->getList(), 'Testing getList');
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::__construct
* @covers \SilverStripe\Forms\GridField\GridField::getConfig
* @covers \SilverStripe\Forms\GridField\GridFieldConfig_Base::__construct
* @covers \SilverStripe\Forms\GridField\GridFieldConfig::addComponent
*/
public function testGridFieldDefaultConfig()
{
$obj = new GridField('testfield', 'testfield');
$expectedComponents = new ArrayList([
new GridFieldToolbarHeader(),
new GridFieldButtonRow(),
$sort = new GridFieldSortableHeader(),
$filter = new GridFieldFilterHeader(),
new GridFieldDataColumns(),
new GridFieldPageCount('toolbar-header-right'),
$pagination = new GridFieldPaginator(),
new GridState_Component(),
]);
$sort->setThrowExceptionOnBadDataType(false);
$filter->setThrowExceptionOnBadDataType(false);
$pagination->setThrowExceptionOnBadDataType(false);
$this->assertEquals($expectedComponents, $obj->getConfig()->getComponents(), 'Testing default Config');
}
/**
* @covers \SilverStripe\Forms\GridField\GridFieldConfig::__construct
* @covers \SilverStripe\Forms\GridField\GridFieldConfig::addComponent
*/
public function testGridFieldSetCustomConfig()
{
$config = GridFieldConfig::create();
$config->addComponent(new GridFieldSortableHeader());
$config->addComponent(new GridFieldDataColumns());
$obj = new GridField('testfield', 'testfield', ArrayList::create([]), $config);
$expectedComponents = new ArrayList(
[
0 => new GridFieldSortableHeader,
1 => new GridFieldDataColumns,
2 => new GridState_Component,
]
);
$this->assertEquals($expectedComponents, $obj->getConfig()->getComponents(), 'Testing default Config');
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::getModelClass
* @covers \SilverStripe\Forms\GridField\GridField::setModelClass
*/
public function testGridFieldModelClass()
{
$obj = new GridField('testfield', 'testfield', Member::get());
API Refactor bootstrap, request handling See https://github.com/silverstripe/silverstripe-framework/pull/7037 and https://github.com/silverstripe/silverstripe-framework/issues/6681 Squashed commit of the following: commit 8f65e5653211240650eaa4fa65bb83b45aae6d58 Author: Ingo Schommer <me@chillu.com> Date: Thu Jun 22 22:25:50 2017 +1200 Fixed upgrade guide spelling commit 76f95944fa89b0b540704b8d744329f690f9698c Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 16:38:34 2017 +1200 BUG Fix non-test class manifest including sapphiretest / functionaltest commit 9379834cb4b2e5177a2600049feec05bf111c16b Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:50:47 2017 +1200 BUG Fix nesting bug in Kernel commit 188ce35d82599360c40f0f2de29579c56fb90761 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 15:14:51 2017 +1200 BUG fix db bootstrapping issues commit 7ed4660e7a63915e8e974deeaba9807bc4d38b0d Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 14:49:07 2017 +1200 BUG Fix issue in DetailedErrorFormatter commit 738f50c497166f81ccbe3f40fbcff895ce71f82f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:49:19 2017 +1200 Upgrading notes on mysite/_config.php commit 6279d28e5e455916f902a2f963c014d8899f7fc7 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 11:43:28 2017 +1200 Update developer documentation commit 5c90d53a84ef0139c729396949a7857fae60436f Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 22 10:48:44 2017 +1200 Update installer to not use global databaseConfig commit f9b2ba4755371f08bd95f6908ac612fcbb7ca205 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 21:04:39 2017 +1200 Fix behat issues commit 5b59a912b60282b4dad4ef10ed3b97c5d0a761ac Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 17:07:11 2017 +1200 Move HTTPApplication to SilverStripe\Control namespace commit e2c4a18f637bdd3d276619554de60ee8b4d95ced Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 16:29:03 2017 +1200 More documentation Fix up remaining tests Refactor temp DB into TempDatabase class so it’s available outside of unit tests. commit 5d235e64f341d6251bfe9f4833f15cc8593c5034 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 21 12:13:15 2017 +1200 API HTTPRequestBuilder::createFromEnvironment() now cleans up live globals BUG Fix issue with SSViewer Fix Security / View tests commit d88d4ed4e48291cb65407f222f190064b1f1deeb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:39:43 2017 +1200 API Refactor AppKernel into CoreKernel commit f7946aec3391139ae1b4029c353c327a36552b36 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 16:00:40 2017 +1200 Docs and minor cleanup commit 12bd31f9366327650b5c0c0f96cd0327d44faf0a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 20 15:34:34 2017 +1200 API Remove OutputMiddleware API Move environment / global / ini management into Environment class API Move getTempFolder into TempFolder class API Implement HTTPRequestBuilder / CLIRequestBuilder BUG Restore SS_ALLOWED_HOSTS check in original location API CoreKernel now requires $basePath to be passed in API Refactor installer.php to use application to bootstrap API move memstring conversion globals to Convert BUG Fix error in CoreKernel nesting not un-nesting itself properly. commit bba979114624247cf463cf2a8c9e4be9a7c3a772 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 18:07:53 2017 +1200 API Create HTTPMiddleware and standardise middleware for request handling commit 2a10c2397bdc53001013f607b5d38087ce6c0730 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:42:42 2017 +1200 Fixed ORM tests commit d75a8d1d93398af4bd0432df9e4bc6295c15a3fe Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 17:15:07 2017 +1200 FIx i18n tests commit 06364af3c379c931889c4cc34dd920fee3db204a Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 16:59:34 2017 +1200 Fix controller namespace Move states to sub namespace commit 2a278e2953d2dbb19f78d91c919048e1fc935436 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 19 12:49:45 2017 +1200 Fix forms namespace commit b65c21241bee019730027071d815dbf7571197a4 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:56:48 2017 +1200 Update API usages commit d1d4375c95a264a6b63cbaefc2c1d12f808bfd82 Author: Damian Mooyman <damian@silverstripe.com> Date: Thu Jun 15 18:41:44 2017 +1200 API Refactor $flush into HTPPApplication API Enforce health check in Controller::pushCurrent() API Better global backup / restore Updated Director::test() to use new API commit b220534f06732db4fa940d8724c2a85c0ba2495a Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 22:05:57 2017 +1200 Move app nesting to a test state helper commit 603704165c08d0c1c81fd5e6bb9506326eeee17b Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 21:46:04 2017 +1200 Restore kernel stack to fix multi-level nesting commit 2f6336a15bf79dc8c2edd44cec1931da2dd51c28 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 17:23:21 2017 +1200 API Implement kernel nesting commit fc7188da7d6ad6785354bab61f08700454c81d91 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:43:13 2017 +1200 Fix core tests commit a0ae7235148fffd71f2f02d1fe7fe45bf3aa39eb Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:23:52 2017 +1200 Fix manifest tests commit ca033952513633e182040d3d13e1caa9000ca184 Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 15:00:00 2017 +1200 API Move extension management into test state commit c66d4339777663a8a04661fea32a0cf35b95d20f Author: Damian Mooyman <damian@silverstripe.com> Date: Tue Jun 13 14:10:59 2017 +1200 API Refactor SapphireTest state management into SapphireTestState API Remove Injector::unregisterAllObjects() API Remove FakeController commit f26ae75c6ecaafa0dec1093264e0187191e6764d Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 18:04:34 2017 +1200 Implement basic CLI application object commit 001d5596621404892de0a5413392379eff990641 Author: Damian Mooyman <damian@silverstripe.com> Date: Mon Jun 12 17:39:38 2017 +1200 Remove references to SapphireTest::is_running_test() Upgrade various code commit de079c041dacd96bc4f4b66421fa2b2cc4c320f8 Author: Damian Mooyman <damian@silverstripe.com> Date: Wed Jun 7 18:07:33 2017 +1200 API Implement APP object API Refactor of Session
2017-06-22 12:50:45 +02:00
$this->assertEquals(Member::class, $obj->getModelClass(), 'Should return Member');
$obj->setModelClass(Group::class);
$this->assertEquals(Group::class, $obj->getModelClass(), 'Should return Group');
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::getModelClass
*/
public function testGridFieldModelClassThrowsException()
{
$this->expectException(LogicException::class);
$obj = new GridField('testfield', 'testfield', ArrayList::create());
$obj->getModelClass();
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::setList
* @covers \SilverStripe\Forms\GridField\GridField::getList
*/
public function testSetAndGetList()
{
$list = Member::get();
$arrayList = ArrayList::create([1, 2, 3]);
$obj = new GridField('testfield', 'testfield', $list);
$this->assertEquals($list, $obj->getList());
$obj->setList($arrayList);
$this->assertEquals($arrayList, $obj->getList());
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::getState
*/
public function testGetState()
{
$obj = new GridField('testfield', 'testfield');
$this->assertTrue($obj->getState() instanceof GridState_Data);
$this->assertTrue($obj->getState(false) instanceof GridState);
}
/**
* Tests usage of nested GridState values
*
* @covers \SilverStripe\Forms\GridField\GridState_Data::__get
* @covers \SilverStripe\Forms\GridField\GridState_Data::__call
* @covers \SilverStripe\Forms\GridField\GridState_Data::getData
*/
public function testGetStateData()
{
$obj = new GridField('testfield', 'testfield');
// @todo - PHP 7.0.6 change requires __isset() to return true
// for each reference from left to right along an isset() invocation.
// See https://bugs.php.net/bug.php?id=62059
// Check value persistance
$this->assertEquals(15, $obj->State->NoValue(15));
$this->assertEquals(15, $obj->State->NoValue(-1));
$obj->State->NoValue = 10;
$this->assertEquals(10, $obj->State->NoValue);
$this->assertEquals(10, $obj->State->NoValue(20));
// Test that values can be set, unset, and inspected
$this->assertFalse(isset($obj->State->NotSet));
$obj->State->NotSet = false;
$this->assertTrue(isset($obj->State->NotSet));
unset($obj->State->NotSet);
$this->assertFalse(isset($obj->State->NotSet));
// Test that false evaluating values are storable
$this->assertEquals(0, $obj->State->Falsey0(0)); // expect 0 back
$this->assertEquals(0, $obj->State->Falsey0(10)); // expect 0 back
$this->assertEquals(0, $obj->State->Falsey0); //expect 0 back
$obj->State->Falsey0 = 0; //manually assign 0
$this->assertEquals(0, $obj->State->Falsey0); //expect 0 back
// Test that false is storable
$this->assertFalse($obj->State->Falsey2(false));
$this->assertFalse($obj->State->Falsey2(true));
$this->assertFalse($obj->State->Falsey2);
$obj->State->Falsey2 = false;
$this->assertFalse($obj->State->Falsey2);
// Check nested values
$this->assertInstanceOf('SilverStripe\\Forms\\GridField\\GridState_Data', $obj->State->Nested);
$this->assertInstanceOf('SilverStripe\\Forms\\GridField\\GridState_Data', $obj->State->Nested->DeeperNested());
$this->assertEquals(3, $obj->State->Nested->DataValue(3));
$this->assertEquals(10, $obj->State->Nested->DeeperNested->DataValue(10));
}
/**
* @skipUpgrade
* @covers \SilverStripe\Forms\GridField\GridField::getColumns
*/
public function testGetColumns()
{
$obj = new GridField('testfield', 'testfield', Member::get());
$expected = [
0 => 'FirstName',
1 => 'Surname',
2 => 'Email',
];
$this->assertEquals($expected, $obj->getColumns());
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::getColumnCount
*/
public function testGetColumnCount()
{
$obj = new GridField('testfield', 'testfield', Member::get());
$this->assertEquals(3, $obj->getColumnCount());
}
/**
* @skipUpgrade
* @covers \SilverStripe\Forms\GridField\GridField::getColumnContent
*/
public function testGetColumnContent()
{
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
]
);
$obj = new GridField('testfield', 'testfield', $list);
$this->assertEquals('test@example.org', $obj->getColumnContent($list->first(), 'Email'));
}
/**
* @skipUpgrade
* @covers \SilverStripe\Forms\GridField\GridField::getColumnContent
*/
public function testGetColumnContentBadArguments()
{
$this->expectException(InvalidArgumentException::class);
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
]
);
$obj = new GridField('testfield', 'testfield', $list);
$obj->getColumnContent($list->first(), 'non-existing');
}
/**
* @skipUpgrade
* @covers \SilverStripe\Forms\GridField\GridField::getColumnAttributes
*/
public function testGetColumnAttributesEmptyArray()
{
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
]
);
$obj = new GridField('testfield', 'testfield', $list);
$this->assertEquals(['class' => 'col-Email'], $obj->getColumnAttributes($list->first(), 'Email'));
}
/**
* @skipUpgrade
* @covers \SilverStripe\Forms\GridField\GridField::getColumnAttributes
*/
public function testGetColumnAttributes()
{
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
]
);
$config = GridFieldConfig::create()->addComponent(new Component);
$obj = new GridField('testfield', 'testfield', $list, $config);
$this->assertEquals(['class' => 'css-class'], $obj->getColumnAttributes($list->first(), 'Email'));
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::getColumnAttributes
*/
public function testGetColumnAttributesBadArguments()
{
$this->expectException(\InvalidArgumentException::class);
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
]
);
$config = GridFieldConfig::create()->addComponent(new Component);
$obj = new GridField('testfield', 'testfield', $list, $config);
$obj->getColumnAttributes($list->first(), 'Non-existing');
}
public function testGetColumnAttributesBadResponseFromComponent()
{
$this->expectException(\LogicException::class);
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
]
);
$config = GridFieldConfig::create()->addComponent(new Component);
$obj = new GridField('testfield', 'testfield', $list, $config);
$obj->getColumnAttributes($list->first(), 'Surname');
}
/**
* @skipUpgrade
* @covers \SilverStripe\Forms\GridField\GridField::getColumnMetadata
*/
public function testGetColumnMetadata()
{
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
]
);
$config = GridFieldConfig::create()->addComponent(new Component);
$obj = new GridField('testfield', 'testfield', $list, $config);
$this->assertEquals(['metadata' => 'istrue'], $obj->getColumnMetadata('Email'));
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::getColumnMetadata
*/
public function testGetColumnMetadataBadResponseFromComponent()
{
$this->expectException(\LogicException::class);
$list = new ArrayList(
[
new Member(["ID" => 1, "Email" => "test@example.org"])
]
);
$config = GridFieldConfig::create()->addComponent(new Component);
$obj = new GridField('testfield', 'testfield', $list, $config);
$obj->getColumnMetadata('Surname');
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::getColumnMetadata
*/
public function testGetColumnMetadataBadArguments()
{
$this->expectException(\InvalidArgumentException::class);
$list = ArrayList::create();
$config = GridFieldConfig::create()->addComponent(new Component);
$obj = new GridField('testfield', 'testfield', $list, $config);
$obj->getColumnMetadata('non-exist-qweqweqwe');
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::handleAction
*/
public function testHandleActionBadArgument()
{
$this->expectException(\InvalidArgumentException::class);
$obj = new GridField('testfield', 'testfield');
$obj->handleAlterAction('prft', [], []);
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::handleAction
*/
public function testHandleAction()
{
$config = GridFieldConfig::create()->addComponent(new Component);
$obj = new GridField('testfield', 'testfield', ArrayList::create(), $config);
$this->assertEquals('handledAction is executed', $obj->handleAlterAction('jump', [], []));
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::getCastedValue
*/
public function testGetCastedValue()
{
$obj = new GridField('testfield', 'testfield');
$value = $obj->getCastedValue('This is a sentance. This ia another.', ['Text->FirstSentence']);
$this->assertEquals('This is a sentance.', $value);
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::getCastedValue
*/
public function testGetCastedValueObject()
{
$obj = new GridField('testfield', 'testfield');
$value = $obj->getCastedValue('Here is some <html> content', 'Text');
$this->assertEquals('Here is some &lt;html&gt; content', $value);
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::gridFieldAlterAction
*/
public function testGridFieldAlterAction()
{
$this->markTestIncomplete();
// $config = GridFieldConfig::create()->addComponent(new GridFieldTest_Component);
// $obj = new GridField('testfield', 'testfield', ArrayList::create(), $config);
// $id = 'testGridStateActionField';
// Session::set($id, array('grid'=>'', 'actionName'=>'jump'));
// $form = new Form(null, 'mockform', new FieldList(array($obj)), new FieldList());
// $request = new HTTPRequest('POST', 'url');
// $obj->gridFieldAlterAction(array('StateID'=>$id), $form, $request);
}
/**
* Test the interface for adding custom HTML fragment slots via a component
*/
public function testGridFieldCustomFragments()
{
new HTMLFragments(
[
"header-left-actions" => "left\$DefineFragment(nested-left)",
"header-right-actions" => "right",
]
);
new HTMLFragments(
[
"nested-left" => "[inner]",
]
);
$config = GridFieldConfig::create()->addComponents(
new HTMLFragments(
[
"header" => "<tr><td><div class=\"right\">\$DefineFragment(header-right-actions)</div>"
. "<div class=\"left\">\$DefineFragment(header-left-actions)</div></td></tr>",
]
),
new HTMLFragments(
[
"header-left-actions" => "left",
"header-right-actions" => "rightone",
]
),
new HTMLFragments(
[
"header-right-actions" => "righttwo",
]
)
);
$field = new GridField('testfield', 'testfield', ArrayList::create(), $config);
$form = new Form(null, 'testform', new FieldList([$field]), new FieldList());
$this->assertStringContainsString(
"<div class=\"right\">rightone\nrighttwo</div><div class=\"left\">left</div>",
$field->FieldHolder()
);
}
/**
* Test the nesting of custom fragments
*/
public function testGridFieldCustomFragmentsNesting()
{
$config = GridFieldConfig::create()->addComponents(
new HTMLFragments(
[
"level-one" => "first",
]
),
new HTMLFragments(
[
"before" => "<div>\$DefineFragment(level-one)</div>",
]
),
new HTMLFragments(
[
"level-one" => "<strong>\$DefineFragment(level-two)</strong>",
]
),
new HTMLFragments(
[
"level-two" => "second",
]
)
);
$field = new GridField('testfield', 'testfield', ArrayList::create(), $config);
$form = new Form(null, 'testform', new FieldList([$field]), new FieldList());
$this->assertStringContainsString(
"<div>first\n<strong>second</strong></div>",
$field->FieldHolder()
);
}
/**
* Test that circular dependencies throw an exception
*/
public function testGridFieldCustomFragmentsCircularDependencyThrowsException()
{
$this->expectException(\LogicException::class);
$config = GridFieldConfig::create()->addComponents(
new HTMLFragments(
[
"level-one" => "first",
]
),
new HTMLFragments(
[
"before" => "<div>\$DefineFragment(level-one)</div>",
]
),
new HTMLFragments(
[
"level-one" => "<strong>\$DefineFragment(level-two)</strong>",
]
),
new HTMLFragments(
[
"level-two" => "<blink>\$DefineFragment(level-one)</blink>",
]
)
);
$field = new GridField('testfield', 'testfield', ArrayList::create(), $config);
$form = new Form(null, 'testform', new FieldList([$field]), new FieldList());
$field->FieldHolder();
}
/**
* @covers \SilverStripe\Forms\GridField\GridField::FieldHolder
*/
public function testCanViewOnlyOddIDs()
{
$this->logInWithPermission();
$list = new ArrayList(
[
new Permissions(
[
"ID" => 1,
"Email" => "ongi.schwimmer@example.org",
'Name' => 'Ongi Schwimmer'
]
),
new Permissions(
[
"ID" => 2,
"Email" => "klaus.lozenge@example.org",
'Name' => 'Klaus Lozenge'
]
),
new Permissions(
[
"ID" => 3,
"Email" => "otto.fischer@example.org",
'Name' => 'Otto Fischer'
]
)
]
);
$config = new GridFieldConfig();
$config->addComponent(new GridFieldDataColumns());
$obj = new GridField('testfield', 'testfield', $list, $config);
$form = new Form(null, 'mockform', new FieldList([$obj]), new FieldList());
$content = new CSSContentParser($obj->FieldHolder());
$members = $content->getBySelector('.ss-gridfield-item tr');
$this->assertEquals(2, count($members));
$this->assertEquals(
(string)$members[0]->td[0],
'Ongi Schwimmer',
'First object Name should be Ongi Schwimmer'
);
$this->assertEquals(
(string)$members[0]->td[1],
'ongi.schwimmer@example.org',
'First object Email should be ongi.schwimmer@example.org'
);
$this->assertEquals(
(string)$members[1]->td[0],
'Otto Fischer',
'Second object Name should be Otto Fischer'
);
$this->assertEquals(
(string)$members[1]->td[1],
'otto.fischer@example.org',
'Second object Email should be otto.fischer@example.org'
);
}
public function testChainedDataManipulators()
{
$config = new GridFieldConfig();
$data = new ArrayList([1, 2, 3, 4, 5, 6]);
$gridField = new GridField('testfield', 'testfield', $data, $config);
$endList = $gridField->getManipulatedList();
$this->assertEquals($endList->count(), 6);
$config->addComponent(new Component2);
$endList = $gridField->getManipulatedList();
$this->assertEquals($endList->count(), 12);
$config->addComponent(new GridFieldPaginator(10));
$endList = $gridField->getManipulatedList();
$this->assertEquals($endList->count(), 10);
}
public function testValidationMessageInOutput()
{
$gridField = new GridField('testfield', 'testfield', new ArrayList(), new GridFieldConfig());
$fieldList = new FieldList([$gridField]);
$validator = new TestValidator();
$form = new Form(null, "testForm", $fieldList, new FieldList(), $validator);
// A form that fails validation should display the validation error in the FieldHolder output.
$form->validationResult();
$gridfieldOutput = $gridField->FieldHolder();
$this->assertStringContainsString('<p class="message ' . ValidationResult::TYPE_ERROR . '">error</p>', $gridfieldOutput);
// Clear validation error from previous assertion.
$validator->removeValidation();
$gridField->setMessage(null);
// A form that passes validation should not display a validation error in the FieldHolder output.
$form->setValidator(new RequiredFields());
$form->validationResult();
$gridfieldOutput = $gridField->FieldHolder();
$this->assertStringNotContainsString('<p class="message ' . ValidationResult::TYPE_ERROR . '">', $gridfieldOutput);
}
}