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); $this->form = new Form(null, 'mockform', new FieldList([$this->gridField]), new FieldList()); } public function testShowActionMenu() { if (Security::getCurrentUser()) { Security::setCurrentUser(null); } $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('.gridfield-actionmenu__container')), 'Edit links should show when not logged in.' ); } 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']; $json = json_decode($schema, true); $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']; $json = json_decode($schema, true); $this->assertCount(1, $json); } public function testShowEditLinksWithAdminPermission() { $this->logInWithPermission('ADMIN'); $content = new CSSContentParser($this->gridField->FieldHolder()); $editLinks = $content->getBySelector('.gridfield-actionmenu__container'); $this->assertEquals(3, count($editLinks), 'Edit links should show when logged in.'); } }