Completed
Push — master ( 118de6...3501ef )
by
unknown
10s
created

tests/CommentsGridFieldActionTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SilverStripe\Comments\Tests;
4
5
use SilverStripe\Comments\Admin\CommentAdmin;
6
use SilverStripe\Comments\Admin\CommentsGridField;
7
use SilverStripe\Comments\Admin\CommentsGridFieldAction;
8
use SilverStripe\Comments\Admin\CommentsGridFieldConfig;
9
use SilverStripe\Comments\Model\Comment;
10
use SilverStripe\Comments\Tests\Stubs\CommentableItem;
11
use SilverStripe\Control\Controller;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\Forms\FieldList;
14
use SilverStripe\Forms\Form;
15
use SilverStripe\Forms\GridField\GridField;
16
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
17
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
18
use SilverStripe\ORM\ArrayList;
19
use SilverStripe\ORM\DataList;
20
use SilverStripe\ORM\DataObject;
21
22
class CommentsGridFieldActionTest extends SapphireTest
23
{
24
    protected $usesDatabase = true;
25
26
    protected static $extra_dataobjects = [
27
        CommentableItem::class,
28
    ];
29
30
    /** @var ArrayList */
31
    protected $list;
32
33
    /** @var GridField */
34
    protected $gridField;
35
36
    /** @var Form */
37
    protected $form;
38
39
    protected function setUp()
40
    {
41
        parent::setUp();
42
        $this->list = new DataList(Team::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SilverStripe\ORM\Da...dFieldTest\Team::class) of type object<SilverStripe\ORM\DataList> is incompatible with the declared type object<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...
43
        $config = CommentsGridFieldConfig::create()->addComponent(new GridFieldDeleteAction());
44
        $this->gridField = new CommentsGridField('testfield', 'testfield', $this->list, $config);
45
        $this->form = new Form(new CommentAdmin(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
46
    }
47
48
    public function testAugmentColumns()
49
    {
50
        $action = new CommentsGridFieldAction();
51
52
        // an entry called 'Actions' is added to the columns array
53
        $columns = array();
54
        $action->augmentColumns($this->gridField, $columns);
55
        $expected = array('Actions');
56
        $this->assertEquals($expected, $columns);
57
58
        $columns = array('Actions');
59
        $action->augmentColumns($this->gridField, $columns);
60
        $expected = array('Actions');
61
        $this->assertEquals($expected, $columns);
62
    }
63
64
    public function testGetColumnAttributes()
65
    {
66
        $action = new CommentsGridFieldAction();
67
        $record = new Comment();
68
        $attrs = $action->getColumnAttributes($this->gridField, $record, Comment::class);
69
        $this->assertEquals(array('class' => 'col-buttons'), $attrs);
70
    }
71
72
    public function testGetColumnMetadata()
73
    {
74
        $action = new CommentsGridFieldAction();
75
        $result = $action->getColumnMetadata($this->gridField, 'Actions');
76
        $this->assertEquals(array('title' => ''), $result);
77
        $result = $action->getColumnMetadata($this->gridField, 'SomethingElse');
78
        $this->assertNull($result);
79
    }
80
81
    public function testGetColumnsHandled()
82
    {
83
        $action = new CommentsGridFieldAction();
84
        $result = $action->getColumnsHandled($this->gridField);
85
        $this->assertEquals(array('Actions'), $result);
86
    }
87
88
    public function testGetColumnContent()
89
    {
90
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
91
        $action = new CommentsGridFieldAction();
92
        $record = new Comment();
93
        $record->Name = 'Name of commeter';
94
        $record->Comment = 'This is a comment';
95
        $record->write();
96
        $recordID = $record->ID;
97
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
98
        $this->assertContains('data-url="admin/comments/mockform/field/testfield', $html);
99
100
        $this->assertContains('value="Spam"', $html);
101
        $this->assertContains('id="action_CustomAction' . $recordID . 'Spam"', $html);
102
103
        $this->assertContains('value="Approve"', $html);
104
        $this->assertContains('id="action_CustomAction' . $recordID . 'Approve"', $html);
105
106
        // If marked as spam, only the approve button should be available
107
        $record->markSpam();
108
        $record->write();
109
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
110
        $this->assertContains('value="Approve"', $html);
111
        $this->assertNotContains('value="Spam"', $html);
112
113
        // If marked as spam, only the approve button should be available
114
        $record->markApproved();
115
        $record->write();
116
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
117
        $this->assertNotContains('value="Approve"', $html);
118
        $this->assertContains('value="Spam"', $html);
119
    }
120
121
    public function testGetActions()
122
    {
123
        $action = new CommentsGridFieldAction();
124
        $result = $action->getActions($this->gridField);
125
        $this->assertEquals(array('spam', 'approve'), $result);
126
    }
127
128
    public function testHandleAction()
129
    {
130
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
131
        $item = new CommentableItem;
132
        $item->write();
133
134
        $action = new CommentsGridFieldAction();
135
        $record = new Comment();
136
        $record->Name = 'Name of commenter';
137
        $record->Comment = 'This is a comment';
138
        $record->ParentID = $item->ID;
139
        $record->ParentClass = $item->class;
140
        $record->write();
141
        $recordID = $record->ID;
142
        $arguments = array('RecordID' => $recordID);
143
        $data = array();
144
        $result = $action->handleAction($this->gridField, 'spam', $arguments, $data);
145
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
146
        $this->assertEquals(
147
            'Comment marked as spam.',
148
            Controller::curr()->getResponse()->getStatusDescription()
149
        );
150
        $record = DataObject::get_by_id(Comment::class, $recordID);
151
        $this->assertEquals(1, $record->Moderated);
152
        $this->assertEquals(1, $record->IsSpam);
153
154
        //getStatusDescription
155
        $result = $action->handleAction($this->gridField, 'approve', $arguments, $data);
156
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
157
        $this->assertEquals(
158
            'Comment approved.',
159
            Controller::curr()->getResponse()->getStatusDescription()
160
        );
161
162
        $record = DataObject::get_by_id(Comment::class, $recordID);
163
        $this->assertEquals(1, $record->Moderated);
164
        $this->assertEquals(0, $record->IsSpam);
165
    }
166
}
167