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\GridFieldDeleteAction;
16
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
17
use SilverStripe\ORM\DataList;
18
use SilverStripe\ORM\DataObject;
19
20
class CommentsGridFieldActionTest extends SapphireTest
21
{
22
    protected $usesDatabase = true;
23
24
    /** @var ArrayList */
25
    protected $list;
26
27
    /** @var GridField */
28
    protected $gridField;
29
30
    /** @var Form */
31
    protected $form;
32
33
    public function setUp()
34
    {
35
        parent::setUp();
36
        $this->list = new DataList(Team::class);
37
        $config = CommentsGridFieldConfig::create()->addComponent(new GridFieldDeleteAction());
38
        $this->gridField = new CommentsGridField('testfield', 'testfield', $this->list, $config);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \SilverStripe\Commen..., $this->list, $config) of type object<SilverStripe\Comm...dmin\CommentsGridField> is incompatible with the declared type object<SilverStripe\Comments\Tests\GridField> of property $gridField.

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...
39
        $this->form = new Form(new CommentAdmin(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
40
    }
41
42
    public function testAugmentColumns()
43
    {
44
        $action = new CommentsGridFieldAction();
45
46
        // an entry called 'Actions' is added to the columns array
47
        $columns = array();
48
        $action->augmentColumns($this->gridField, $columns);
49
        $expected = array('Actions');
50
        $this->assertEquals($expected, $columns);
51
52
        $columns = array('Actions');
53
        $action->augmentColumns($this->gridField, $columns);
54
        $expected = array('Actions');
55
        $this->assertEquals($expected, $columns);
56
    }
57
58
    public function testGetColumnAttributes()
59
    {
60
        $action = new CommentsGridFieldAction();
61
        $record = new Comment();
62
        $attrs = $action->getColumnAttributes($this->gridField, $record, Comment::class);
63
        $this->assertEquals(array('class' => 'col-buttons'), $attrs);
64
    }
65
66
    public function testGetColumnMetadata()
67
    {
68
        $action = new CommentsGridFieldAction();
69
        $result = $action->getColumnMetadata($this->gridField, 'Actions');
70
        $this->assertEquals(array('title' => ''), $result);
71
        $result = $action->getColumnMetadata($this->gridField, 'SomethingElse');
72
        $this->assertNull($result);
73
    }
74
75
    public function testGetColumnsHandled()
76
    {
77
        $action = new CommentsGridFieldAction();
78
        $result = $action->getColumnsHandled($this->gridField);
79
        $this->assertEquals(array('Actions'), $result);
80
    }
81
82
    public function testGetColumnContent()
83
    {
84
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
85
        $action = new CommentsGridFieldAction();
86
        $record = new Comment();
87
        $record->Name = 'Name of commeter';
88
        $record->Comment = 'This is a comment';
89
        $record->write();
90
        $recordID = $record->ID;
91
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
92
        $this->assertContains('data-url="admin/comments/mockform/field/testfield', $html);
93
        $spamAction = 'value="Spam" class="action" id="action_CustomAction' . $recordID . 'Spam"';
94
        $this->assertContains($spamAction, $html);
95
96
        $approveAction = 'value="Approve" class="action" id="action_CustomAction' . $recordID . 'Approve"';
97
        $this->assertContains($approveAction, $html);
98
99
        // If marked as spam, only the approve button should be available
100
        $record->markSpam();
101
        $record->write();
102
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
103
        $this->assertContains($approveAction, $html);
104
        $this->assertNotContains($spamAction, $html);
105
106
        // If marked as spam, only the approve button should be available
107
        $record->markApproved();
108
        $record->write();
109
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
110
        $this->assertNotContains($approveAction, $html);
111
        $this->assertContains($spamAction, $html);
112
    }
113
114
    public function testGetActions()
115
    {
116
        $action = new CommentsGridFieldAction();
117
        $result = $action->getActions($this->gridField);
118
        $this->assertEquals(array('spam', 'approve'), $result);
119
    }
120
121
    public function testHandleAction()
122
    {
123
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
124
        $item = new CommentableItem;
125
        $item->write();
126
127
        $action = new CommentsGridFieldAction();
128
        $record = new Comment();
129
        $record->Name = 'Name of commenter';
130
        $record->Comment = 'This is a comment';
131
        $record->ParentID = $item->ID;
132
        $record->ParentClass = $item->class;
133
        $record->write();
134
        $recordID = $record->ID;
135
        $arguments = array('RecordID' => $recordID);
136
        $data = array();
137
        $result = $action->handleAction($this->gridField, 'spam', $arguments, $data);
138
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
139
        $this->assertEquals(
140
            'Comment marked as spam.',
141
            Controller::curr()->getResponse()->getStatusDescription()
142
        );
143
        $record = DataObject::get_by_id(Comment::class, $recordID);
144
        $this->assertEquals(1, $record->Moderated);
145
        $this->assertEquals(1, $record->IsSpam);
146
147
        //getStatusDescription
148
        $result = $action->handleAction($this->gridField, 'approve', $arguments, $data);
149
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
150
        $this->assertEquals(
151
            'Comment approved.',
152
            Controller::curr()->getResponse()->getStatusDescription()
153
        );
154
155
        $record = DataObject::get_by_id(Comment::class, $recordID);
156
        $this->assertEquals(1, $record->Moderated);
157
        $this->assertEquals(0, $record->IsSpam);
158
    }
159
}
160