2018-05-29 06:10:52 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\Tests\GridField;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
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\GridField_ActionMenu;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldEditButton;
|
|
|
|
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
|
2018-06-06 11:14:29 +02:00
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldConfigTest\MyActionMenuItemComponent;
|
2018-05-29 06:10:52 +02:00
|
|
|
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;
|
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Security;
|
|
|
|
|
|
|
|
class GridFieldActionMenuTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ArrayList
|
|
|
|
*/
|
|
|
|
protected $list;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GridField
|
|
|
|
*/
|
|
|
|
protected $gridField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Form
|
|
|
|
*/
|
|
|
|
protected $form;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected static $fixture_file = 'GridFieldActionTest.yml';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2020-04-20 19:58:09 +02:00
|
|
|
protected static $extra_dataobjects = [
|
2018-05-29 06:10:52 +02:00
|
|
|
Team::class,
|
|
|
|
Cheerleader::class,
|
|
|
|
Player::class,
|
|
|
|
Permissions::class,
|
2020-04-20 19:58:09 +02:00
|
|
|
];
|
2018-05-29 06:10:52 +02:00
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
2018-05-29 06:10:52 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->list = new DataList(Team::class);
|
|
|
|
$config = GridFieldConfig::create()
|
|
|
|
->addComponent(new GridFieldEditButton())
|
|
|
|
->addComponent(new GridFieldDeleteAction())
|
|
|
|
->addComponent(new GridField_ActionMenu());
|
|
|
|
$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());
|
2018-05-29 06:10:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testShowActionMenu()
|
|
|
|
{
|
|
|
|
if (Security::getCurrentUser()) {
|
|
|
|
Security::setCurrentUser(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = new CSSContentParser($this->gridField->FieldHolder());
|
|
|
|
// Check that there are content
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(4, count($content->getBySelector('.ss-gridfield-item') ?? []));
|
2018-05-29 06:10:52 +02:00
|
|
|
// 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,
|
2022-04-14 03:12:59 +02:00
|
|
|
count($content->getBySelector('.gridfield-actionmenu__container') ?? []),
|
2018-05-29 06:10:52 +02:00
|
|
|
'Edit links should show when not logged in.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-06-06 11:14:29 +02:00
|
|
|
public function testHiddenActionMenuItems()
|
|
|
|
{
|
|
|
|
$config = GridFieldConfig::create()
|
|
|
|
->addComponent(new MyActionMenuItemComponent(true))
|
|
|
|
->addComponent(new GridFieldDeleteAction())
|
|
|
|
->addComponent($menu = new GridField_ActionMenu());
|
|
|
|
$this->gridField->setConfig($config);
|
|
|
|
|
|
|
|
$html = $menu->getColumnContent($this->gridField, new Team(), 'test');
|
|
|
|
$content = new CSSContentParser($html);
|
|
|
|
/* @var \SimpleXMLElement $node */
|
|
|
|
$node = $content->getBySelector('.gridfield-actionmenu__container');
|
|
|
|
$this->assertNotNull($node);
|
|
|
|
$this->assertCount(1, $node);
|
|
|
|
$schema = (string) $node[0]->attributes()['data-schema'];
|
2022-04-14 03:12:59 +02:00
|
|
|
$json = json_decode($schema ?? '', true);
|
2018-06-06 11:14:29 +02:00
|
|
|
$this->assertCount(2, $json);
|
|
|
|
|
|
|
|
// Now set the component to not display
|
|
|
|
$config = GridFieldConfig::create()
|
|
|
|
->addComponent(new MyActionMenuItemComponent(false))
|
|
|
|
->addComponent(new GridFieldDeleteAction())
|
|
|
|
->addComponent($menu = new GridField_ActionMenu());
|
|
|
|
$this->gridField->setConfig($config);
|
|
|
|
|
|
|
|
$html = $menu->getColumnContent($this->gridField, new Team(), 'test');
|
|
|
|
$content = new CSSContentParser($html);
|
|
|
|
/* @var \SimpleXMLElement $node */
|
|
|
|
$node = $content->getBySelector('.gridfield-actionmenu__container');
|
|
|
|
$this->assertNotNull($node);
|
|
|
|
$this->assertCount(1, $node);
|
|
|
|
$schema = (string) $node[0]->attributes()['data-schema'];
|
2022-04-14 03:12:59 +02:00
|
|
|
$json = json_decode($schema ?? '', true);
|
2018-06-06 11:14:29 +02:00
|
|
|
$this->assertCount(1, $json);
|
|
|
|
}
|
|
|
|
|
2018-05-29 06:10:52 +02:00
|
|
|
public function testShowEditLinksWithAdminPermission()
|
|
|
|
{
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$content = new CSSContentParser($this->gridField->FieldHolder());
|
|
|
|
$editLinks = $content->getBySelector('.gridfield-actionmenu__container');
|
2022-04-14 03:12:59 +02:00
|
|
|
$this->assertEquals(3, count($editLinks ?? []), 'Edit links should show when logged in.');
|
2018-05-29 06:10:52 +02:00
|
|
|
}
|
|
|
|
}
|