Passed
Push — master ( 1155ca...3f5943 )
by Damian
08:13
created

GridFieldActionMenuTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 105
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testShowActionMenu() 0 15 2
B testHiddenActionMenuItems() 0 34 1
A setUp() 0 10 1
A testShowEditLinksWithAdminPermission() 0 6 1
1
<?php
2
3
namespace SilverStripe\Forms\Tests\GridField;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Dev\CSSContentParser;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\Forms\GridField\GridField;
11
use SilverStripe\Forms\GridField\GridField_ActionMenu;
12
use SilverStripe\Forms\GridField\GridFieldConfig;
13
use SilverStripe\Forms\GridField\GridFieldEditButton;
14
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
15
use SilverStripe\Forms\Tests\GridField\GridFieldConfigTest\MyActionMenuItemComponent;
16
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Cheerleader;
17
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions;
18
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Player;
19
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
20
use SilverStripe\ORM\ArrayList;
21
use SilverStripe\ORM\DataList;
22
use SilverStripe\Security\Member;
23
use SilverStripe\Security\Security;
24
25
class GridFieldActionMenuTest extends SapphireTest
26
{
27
28
    /**
29
     * @var ArrayList
30
     */
31
    protected $list;
32
33
    /**
34
     * @var GridField
35
     */
36
    protected $gridField;
37
38
    /**
39
     * @var Form
40
     */
41
    protected $form;
42
43
    /**
44
     * @var string
45
     */
46
    protected static $fixture_file = 'GridFieldActionTest.yml';
47
48
    /**
49
     * @var array
50
     */
51
    protected static $extra_dataobjects = array(
52
        Team::class,
53
        Cheerleader::class,
54
        Player::class,
55
        Permissions::class,
56
    );
57
58
    protected function setUp()
59
    {
60
        parent::setUp();
61
        $this->list = new DataList(Team::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like new SilverStripe\ORM\Dat...dFieldTest\Team::class) of type SilverStripe\ORM\DataList is incompatible with the declared type SilverStripe\ORM\ArrayList of property $list.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
62
        $config = GridFieldConfig::create()
63
            ->addComponent(new GridFieldEditButton())
64
            ->addComponent(new GridFieldDeleteAction())
65
            ->addComponent(new GridField_ActionMenu());
66
        $this->gridField = new GridField('testfield', 'testfield', $this->list, $config);
67
        $this->form = new Form(null, 'mockform', new FieldList(array($this->gridField)), new FieldList());
68
    }
69
70
    public function testShowActionMenu()
71
    {
72
        if (Security::getCurrentUser()) {
73
            Security::setCurrentUser(null);
74
        }
75
76
        $content = new CSSContentParser($this->gridField->FieldHolder());
77
        // Check that there are content
78
        $this->assertEquals(4, count($content->getBySelector('.ss-gridfield-item')));
79
        // Make sure that there are edit links, even though the user doesn't have "edit" permissions
80
        // (they can still view the records)
81
        $this->assertEquals(
82
            3,
83
            count($content->getBySelector('.gridfield-actionmenu__container')),
84
            'Edit links should show when not logged in.'
85
        );
86
    }
87
88
    public function testHiddenActionMenuItems()
89
    {
90
        $config = GridFieldConfig::create()
91
            ->addComponent(new MyActionMenuItemComponent(true))
92
            ->addComponent(new GridFieldDeleteAction())
93
            ->addComponent($menu = new GridField_ActionMenu());
94
        $this->gridField->setConfig($config);
95
96
        $html = $menu->getColumnContent($this->gridField, new Team(), 'test');
97
        $content = new CSSContentParser($html);
98
        /* @var \SimpleXMLElement $node */
99
        $node = $content->getBySelector('.gridfield-actionmenu__container');
100
        $this->assertNotNull($node);
101
        $this->assertCount(1, $node);
102
        $schema = (string) $node[0]->attributes()['data-schema'];
103
        $json = json_decode($schema, true);
104
        $this->assertCount(2, $json);
105
106
        // Now set the component to not display
107
        $config = GridFieldConfig::create()
108
            ->addComponent(new MyActionMenuItemComponent(false))
109
            ->addComponent(new GridFieldDeleteAction())
110
            ->addComponent($menu = new GridField_ActionMenu());
111
        $this->gridField->setConfig($config);
112
113
        $html = $menu->getColumnContent($this->gridField, new Team(), 'test');
114
        $content = new CSSContentParser($html);
115
        /* @var \SimpleXMLElement $node */
116
        $node = $content->getBySelector('.gridfield-actionmenu__container');
117
        $this->assertNotNull($node);
118
        $this->assertCount(1, $node);
119
        $schema = (string) $node[0]->attributes()['data-schema'];
120
        $json = json_decode($schema, true);
121
        $this->assertCount(1, $json);
122
    }
123
124
    public function testShowEditLinksWithAdminPermission()
125
    {
126
        $this->logInWithPermission('ADMIN');
127
        $content = new CSSContentParser($this->gridField->FieldHolder());
128
        $editLinks = $content->getBySelector('.gridfield-actionmenu__container');
129
        $this->assertEquals(3, count($editLinks), 'Edit links should show when logged in.');
130
    }
131
}
132