Completed
Pull Request — master (#195)
by Robbie
02:05
created

CommentsGridFieldActionTest::testHandleAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 25
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\Comments\Tests;
4
5
use SilverStripe\Comments\Admin\CommentsGridField;
6
use SilverStripe\Comments\Admin\CommentsGridFieldAction;
7
use SilverStripe\Comments\Admin\CommentsGridFieldConfig;
8
use SilverStripe\Comments\Model\Comment;
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Dev\SapphireTest;
11
use SilverStripe\Forms\FieldList;
12
use SilverStripe\Forms\Form;
13
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
14
use SilverStripe\Forms\Tests\GridField\GridFieldTest\Team;
15
use SilverStripe\ORM\DataList;
16
use SilverStripe\ORM\DataObject;
17
18
class CommentsGridFieldActionTest extends SapphireTest
19
{
20
    /** @var ArrayList */
21
    protected $list;
22
23
    /** @var GridField */
24
    protected $gridField;
25
26
    /** @var Form */
27
    protected $form;
28
29
    public function setUp()
30
    {
31
        parent::setUp();
32
        $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\Comments\Tests\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...
33
        $config = CommentsGridFieldConfig::create()->addComponent(new GridFieldDeleteAction());
34
        $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...
35
        $this->form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
36
    }
37
38
    public function testAugmentColumns()
39
    {
40
        $action = new CommentsGridFieldAction();
41
42
        // an entry called 'Actions' is added to the columns array
43
        $columns = array();
44
        $action->augmentColumns($this->gridField, $columns);
45
        $expected = array('Actions');
46
        $this->assertEquals($expected, $columns);
47
48
        $columns = array('Actions');
49
        $action->augmentColumns($this->gridField, $columns);
50
        $expected = array('Actions');
51
        $this->assertEquals($expected, $columns);
52
    }
53
54
    public function testGetColumnAttributes()
55
    {
56
        $action = new CommentsGridFieldAction();
57
        $record = new Comment();
58
        $attrs = $action->getColumnAttributes($this->gridField, $record, Comment::class);
59
        $this->assertEquals(array('class' => 'col-buttons'), $attrs);
60
    }
61
62
    public function testGetColumnMetadata()
63
    {
64
        $action = new CommentsGridFieldAction();
65
        $result = $action->getColumnMetadata($this->gridField, 'Actions');
66
        $this->assertEquals(array('title' => ''), $result);
67
        $result = $action->getColumnMetadata($this->gridField, 'SomethingElse');
68
        $this->assertNull($result);
69
    }
70
71
    public function testGetColumnsHandled()
72
    {
73
        $action = new CommentsGridFieldAction();
74
        $result = $action->getColumnsHandled($this->gridField);
75
        $this->assertEquals(array('Actions'), $result);
76
    }
77
78
    public function testGetColumnContent()
79
    {
80
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
81
        $action = new CommentsGridFieldAction();
82
        $record = new Comment();
83
        $record->Name = 'Name of commeter';
84
        $record->Comment = 'This is a comment';
85
        $record->write();
86
        $recordID = $record->ID;
87
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
88
        $this->assertContains('data-url="Controller/mockform/field/testfield', $html);
89
        $spamAction = 'value="Spam" class="action" id="action_CustomAction' . $recordID . 'Spam"';
90
        $this->assertContains($spamAction, $html);
91
92
        $approveAction = 'value="Approve" class="action" id="action_CustomAction' . $recordID . 'Approve"';
93
        $this->assertContains($approveAction, $html);
94
95
        // If marked as spam, only the approve button should be available
96
        $record->markSpam();
97
        $record->write();
98
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
99
        $this->assertContains($approveAction, $html);
100
        $this->assertNotContains($spamAction, $html);
101
102
        // If marked as spam, only the approve button should be available
103
        $record->markApproved();
104
        $record->write();
105
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
106
        $this->assertNotContains($approveAction, $html);
107
        $this->assertContains($spamAction, $html);
108
    }
109
110
    public function testGetActions()
111
    {
112
        $action = new CommentsGridFieldAction();
113
        $result = $action->getActions($this->gridField);
114
        $this->assertEquals(array('spam', 'approve'), $result);
115
    }
116
117
    public function testHandleAction()
118
    {
119
        $action = new CommentsGridFieldAction();
120
        $record = new Comment();
121
        $record->Name = 'Name of commenter';
122
        $record->Comment = 'This is a comment';
123
        $record->write();
124
        $recordID = $record->ID;
125
        $arguments = array('RecordID' => $recordID);
126
        $data = array();
127
        $result = $action->handleAction($this->gridField, 'spam', $arguments, $data);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
128
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
129
        $this->assertEquals(
130
            'Comment marked as spam.',
131
            Controller::curr()->getResponse()->getStatusDescription()
132
        );
133
        $record = DataObject::get_by_id(Comment::class, $recordID);
134
        $this->assertEquals(1, $record->Moderated);
135
        $this->assertEquals(1, $record->IsSpam);
136
137
        //getStatusDescription
138
        $result = $action->handleAction($this->gridField, 'approve', $arguments, $data);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
139
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
140
        $this->assertEquals(
141
            'Comment approved.',
142
            Controller::curr()->getResponse()->getStatusDescription()
143
        );
144
145
        $record = DataObject::get_by_id(Comment::class, $recordID);
146
        $this->assertEquals(1, $record->Moderated);
147
        $this->assertEquals(0, $record->IsSpam);
148
    }
149
}
150