2012-03-08 01:58:53 +01:00
|
|
|
<?php
|
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Forms\Tests\GridField;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
|
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions;
|
|
|
|
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-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton;
|
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
2017-05-20 06:32:25 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class GridFieldEditButtonTest extends SapphireTest
|
|
|
|
{
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
2017-03-24 12:17:26 +01:00
|
|
|
* @var ArrayList
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $list;
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
2017-03-24 12:17:26 +01:00
|
|
|
* @var GridField
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $gridField;
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
2017-03-24 12:17:26 +01:00
|
|
|
* @var Form
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $form;
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
2017-03-24 12:17:26 +01:00
|
|
|
* @var string
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected static $fixture_file = 'GridFieldActionTest.yml';
|
2012-03-08 01:58:53 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
2017-03-24 12:17:26 +01:00
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2016-12-16 05:34:21 +01:00
|
|
|
Team::class,
|
|
|
|
Cheerleader::class,
|
|
|
|
Player::class,
|
2017-03-24 12:17:26 +01:00
|
|
|
Permissions::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->list = new DataList(Team::class);
|
|
|
|
$config = GridFieldConfig::create()->addComponent(new GridFieldEditButton());
|
|
|
|
$this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->form = new Form(null, 'mockform', new FieldList([$this->gridField]), new FieldList());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testShowEditLinks()
|
|
|
|
{
|
2017-05-20 06:32:25 +02:00
|
|
|
if (Security::getCurrentUser()) {
|
2017-05-30 09:42:00 +02:00
|
|
|
Security::setCurrentUser(null);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$content = new CSSContentParser($this->gridField->FieldHolder());
|
|
|
|
// Check that there are content
|
|
|
|
$this->assertEquals(4, count($content->getBySelector('.ss-gridfield-item')));
|
|
|
|
// Make sure that there are edit links, even though the user doesn't have "edit" permissions
|
|
|
|
// (they can still view the records)
|
|
|
|
$this->assertEquals(
|
|
|
|
3,
|
|
|
|
count($content->getBySelector('.edit-link')),
|
|
|
|
'Edit links should show when not logged in.'
|
|
|
|
);
|
|
|
|
}
|
2015-03-07 13:32:04 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testShowEditLinksWithAdminPermission()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$content = new CSSContentParser($this->gridField->FieldHolder());
|
|
|
|
$editLinks = $content->getBySelector('.edit-link');
|
|
|
|
$this->assertEquals(3, count($editLinks), 'Edit links should show when logged in.');
|
|
|
|
}
|
2017-09-06 00:58:06 +02:00
|
|
|
|
|
|
|
public function testDefaultClassesAreSet()
|
|
|
|
{
|
|
|
|
$button = new GridFieldEditButton;
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'grid-field__icon-action--hidden-on-hover',
|
|
|
|
'font-icon-edit',
|
|
|
|
'btn--icon-large'
|
|
|
|
];
|
|
|
|
|
|
|
|
$result = $button->getExtraClass();
|
|
|
|
|
|
|
|
foreach ($expected as $className) {
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString($className, $result);
|
2017-09-06 00:58:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddAndRemoveExtraClass()
|
|
|
|
{
|
|
|
|
$button = new GridFieldEditButton;
|
|
|
|
|
|
|
|
$button->addExtraClass('foobar');
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('foobar', $button->getExtraClass());
|
2017-09-06 00:58:06 +02:00
|
|
|
|
|
|
|
$button->removeExtraClass('foobar');
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('foobar', $button->getExtraClass());
|
2017-09-06 00:58:06 +02:00
|
|
|
|
|
|
|
// Check that duplicates are removed
|
|
|
|
$button->addExtraClass('foobar');
|
|
|
|
$button->addExtraClass('foobar');
|
|
|
|
$button->removeExtraClass('foobar');
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringNotContainsString('foobar', $button->getExtraClass());
|
2017-09-06 00:58:06 +02:00
|
|
|
}
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|