2012-01-30 15:47:22 +01: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\GridFieldConfigTest\MyOtherComponent;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldConfigTest\MyComponent;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
|
|
|
use SilverStripe\Forms\GridField\GridField_URLHandler;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class GridFieldConfigTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
public function testGetComponents()
|
|
|
|
{
|
|
|
|
$config = GridFieldConfig::create();
|
|
|
|
$this->assertInstanceOf(ArrayList::class, $config->getComponents());
|
|
|
|
$this->assertEquals($config->getComponents()->count(), 0);
|
|
|
|
|
|
|
|
$config
|
|
|
|
->addComponent($c1 = new MyComponent())
|
|
|
|
->addComponent($c2 = new MyOtherComponent())
|
|
|
|
->addComponent($c3 = new MyOtherComponent());
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
new ArrayList([$c1, $c2, $c3]),
|
2016-12-16 05:34:21 +01:00
|
|
|
$config->getComponents()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetComponentsByType()
|
|
|
|
{
|
|
|
|
$config = GridFieldConfig::create()
|
|
|
|
->addComponent($c1 = new MyComponent())
|
|
|
|
->addComponent($c2 = new MyOtherComponent())
|
|
|
|
->addComponent($c3 = new MyOtherComponent());
|
|
|
|
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
new ArrayList([$c1]),
|
2016-12-16 05:34:21 +01:00
|
|
|
$config->getComponentsByType(MyComponent::class)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
new ArrayList([$c2, $c3]),
|
2016-12-16 05:34:21 +01:00
|
|
|
$config->getComponentsByType(MyOtherComponent::class)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
2020-04-20 19:58:09 +02:00
|
|
|
new ArrayList([$c1, $c2, $c3]),
|
2016-12-16 05:34:21 +01:00
|
|
|
$config->getComponentsByType(GridField_URLHandler::class)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
new ArrayList(),
|
|
|
|
$config->getComponentsByType('GridFieldConfigTest_UnknownComponent')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetComponentByType()
|
|
|
|
{
|
|
|
|
$config = GridFieldConfig::create()
|
|
|
|
->addComponent($c1 = new MyComponent())
|
|
|
|
->addComponent($c2 = new MyOtherComponent())
|
|
|
|
->addComponent($c3 = new MyOtherComponent());
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$c1,
|
|
|
|
$config->getComponentByType(MyComponent::class)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$c2,
|
|
|
|
$config->getComponentByType(MyOtherComponent::class)
|
|
|
|
);
|
|
|
|
$this->assertNull(
|
|
|
|
$config->getComponentByType('GridFieldConfigTest_UnknownComponent')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddComponents()
|
|
|
|
{
|
|
|
|
$config = GridFieldConfig::create()
|
|
|
|
->addComponents(
|
|
|
|
$c1 = new MyComponent(),
|
|
|
|
$c2 = new MyOtherComponent()
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$c1,
|
|
|
|
$config->getComponentByType(MyComponent::class)
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$c2,
|
|
|
|
$config->getComponentByType(MyOtherComponent::class)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-02-07 23:06:27 +01:00
|
|
|
public function testAddComponentsByArray()
|
|
|
|
{
|
|
|
|
$config = GridFieldConfig::create()
|
|
|
|
->addComponents([
|
|
|
|
$c1 = new MyComponent(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$c1,
|
|
|
|
$config->getComponentByType(MyComponent::class)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testRemoveComponents()
|
|
|
|
{
|
|
|
|
$config = GridFieldConfig::create()
|
|
|
|
->addComponent($c1 = new MyComponent())
|
|
|
|
->addComponent($c2 = new MyComponent())
|
|
|
|
->addComponent($c3 = new MyOtherComponent())
|
|
|
|
->addComponent($c4 = new MyOtherComponent());
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
4,
|
|
|
|
$config->getComponents()->count()
|
|
|
|
);
|
|
|
|
|
|
|
|
$config->removeComponent($c1);
|
|
|
|
$this->assertEquals(
|
|
|
|
3,
|
|
|
|
$config->getComponents()->count()
|
|
|
|
);
|
|
|
|
|
|
|
|
$config->removeComponentsByType(MyComponent::class);
|
|
|
|
$this->assertEquals(
|
|
|
|
2,
|
|
|
|
$config->getComponents()->count()
|
|
|
|
);
|
|
|
|
|
|
|
|
$config->removeComponentsByType(MyOtherComponent::class);
|
|
|
|
$this->assertEquals(
|
|
|
|
0,
|
|
|
|
$config->getComponents()->count()
|
|
|
|
);
|
|
|
|
}
|
2017-01-13 02:39:42 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that components can be removed with an array of class names or interfaces
|
|
|
|
*/
|
|
|
|
public function testRemoveMultipleComponents()
|
|
|
|
{
|
|
|
|
$config = GridFieldConfig::create()
|
|
|
|
->addComponent(new MyComponent)
|
|
|
|
->addComponent(new MyComponent)
|
|
|
|
->addComponent(new MyOtherComponent);
|
|
|
|
|
|
|
|
$config->removeComponentsByType(
|
|
|
|
[
|
|
|
|
MyComponent::class,
|
|
|
|
MyOtherComponent::class
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(0, $config->getComponents()->count());
|
|
|
|
}
|
2012-01-30 15:47:22 +01:00
|
|
|
}
|