CommentsGridFieldActionTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 89
c 4
b 0
f 0
dl 0
loc 144
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetActions() 0 5 1
A testAugmentColumns() 0 14 1
A testGetColumnAttributes() 0 6 1
A testGetColumnMetadata() 0 7 1
A testGetColumnsHandled() 0 5 1
A setUp() 0 7 1
A testGetColumnContent() 0 31 1
A testHandleAction() 0 37 1
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\Comments\Tests\Stubs\Team;
12
use SilverStripe\Control\Controller;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\Forms\FieldList;
15
use SilverStripe\Forms\Form;
16
use SilverStripe\Forms\GridField\GridField;
17
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
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
        Team::class,
29
    ];
30
31
    /** @var ArrayList */
32
    protected $list;
33
34
    /** @var GridField */
35
    protected $gridField;
36
37
    /** @var Form */
38
    protected $form;
39
40
    protected function setUp()
41
    {
42
        parent::setUp();
43
        $this->list = new DataList(Team::class);
0 ignored issues
show
Documentation Bug introduced by
It seems like new SilverStripe\ORM\Dat...ests\Stubs\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...
44
        $config = CommentsGridFieldConfig::create()->addComponent(new GridFieldDeleteAction());
45
        $this->gridField = new CommentsGridField('testfield', 'testfield', $this->list, $config);
46
        $this->form = new Form(new CommentAdmin(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
47
    }
48
49
    public function testAugmentColumns()
50
    {
51
        $action = new CommentsGridFieldAction();
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\Comments\Ad...CommentsGridFieldAction has been deprecated: 3.2.0 {@see CommentsGridFieldApproveAction} and {@see CommentsGridFieldSpamAction} instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

51
        $action = /** @scrutinizer ignore-deprecated */ new CommentsGridFieldAction();
Loading history...
52
53
        // an entry called 'Actions' is added to the columns array
54
        $columns = array();
55
        $action->augmentColumns($this->gridField, $columns);
56
        $expected = array('Actions');
57
        $this->assertEquals($expected, $columns);
58
59
        $columns = array('Actions');
60
        $action->augmentColumns($this->gridField, $columns);
61
        $expected = array('Actions');
62
        $this->assertEquals($expected, $columns);
63
    }
64
65
    public function testGetColumnAttributes()
66
    {
67
        $action = new CommentsGridFieldAction();
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\Comments\Ad...CommentsGridFieldAction has been deprecated: 3.2.0 {@see CommentsGridFieldApproveAction} and {@see CommentsGridFieldSpamAction} instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

67
        $action = /** @scrutinizer ignore-deprecated */ new CommentsGridFieldAction();
Loading history...
68
        $record = new Comment();
69
        $attrs = $action->getColumnAttributes($this->gridField, $record, Comment::class);
70
        $this->assertEquals(array('class' => 'col-buttons'), $attrs);
71
    }
72
73
    public function testGetColumnMetadata()
74
    {
75
        $action = new CommentsGridFieldAction();
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\Comments\Ad...CommentsGridFieldAction has been deprecated: 3.2.0 {@see CommentsGridFieldApproveAction} and {@see CommentsGridFieldSpamAction} instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

75
        $action = /** @scrutinizer ignore-deprecated */ new CommentsGridFieldAction();
Loading history...
76
        $result = $action->getColumnMetadata($this->gridField, 'Actions');
77
        $this->assertEquals(array('title' => ''), $result);
78
        $result = $action->getColumnMetadata($this->gridField, 'SomethingElse');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $action->getColumnMetada...Field, 'SomethingElse') targeting SilverStripe\Comments\Ad...on::getColumnMetadata() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
79
        $this->assertNull($result);
80
    }
81
82
    public function testGetColumnsHandled()
83
    {
84
        $action = new CommentsGridFieldAction();
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\Comments\Ad...CommentsGridFieldAction has been deprecated: 3.2.0 {@see CommentsGridFieldApproveAction} and {@see CommentsGridFieldSpamAction} instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

84
        $action = /** @scrutinizer ignore-deprecated */ new CommentsGridFieldAction();
Loading history...
85
        $result = $action->getColumnsHandled($this->gridField);
86
        $this->assertEquals(array('Actions'), $result);
87
    }
88
89
    public function testGetColumnContent()
90
    {
91
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
92
        $action = new CommentsGridFieldAction();
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\Comments\Ad...CommentsGridFieldAction has been deprecated: 3.2.0 {@see CommentsGridFieldApproveAction} and {@see CommentsGridFieldSpamAction} instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

92
        $action = /** @scrutinizer ignore-deprecated */ new CommentsGridFieldAction();
Loading history...
93
        $record = new Comment();
94
        $record->Name = 'Name of commeter';
95
        $record->Comment = 'This is a comment';
96
        $record->write();
97
        $recordID = $record->ID;
98
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
99
        $this->assertContains('data-url="admin/comments/mockform/field/testfield', $html);
100
101
        $this->assertContains('value="Spam"', $html);
102
        $this->assertContains('id="action_CustomAction' . $recordID . 'Spam"', $html);
103
104
        $this->assertContains('value="Approve"', $html);
105
        $this->assertContains('id="action_CustomAction' . $recordID . 'Approve"', $html);
106
107
        // If marked as spam, only the approve button should be available
108
        $record->markSpam();
109
        $record->write();
110
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
111
        $this->assertContains('value="Approve"', $html);
112
        $this->assertNotContains('value="Spam"', $html);
113
114
        // If marked as spam, only the approve button should be available
115
        $record->markApproved();
116
        $record->write();
117
        $html = $action->getColumnContent($this->gridField, $record, Comment::class);
118
        $this->assertNotContains('value="Approve"', $html);
119
        $this->assertContains('value="Spam"', $html);
120
    }
121
122
    public function testGetActions()
123
    {
124
        $action = new CommentsGridFieldAction();
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\Comments\Ad...CommentsGridFieldAction has been deprecated: 3.2.0 {@see CommentsGridFieldApproveAction} and {@see CommentsGridFieldSpamAction} instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

124
        $action = /** @scrutinizer ignore-deprecated */ new CommentsGridFieldAction();
Loading history...
125
        $result = $action->getActions($this->gridField);
126
        $this->assertEquals(array('spam', 'approve'), $result);
127
    }
128
129
    public function testHandleAction()
130
    {
131
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
132
        $item = new CommentableItem;
133
        $item->write();
134
135
        $action = new CommentsGridFieldAction();
0 ignored issues
show
Deprecated Code introduced by
The class SilverStripe\Comments\Ad...CommentsGridFieldAction has been deprecated: 3.2.0 {@see CommentsGridFieldApproveAction} and {@see CommentsGridFieldSpamAction} instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

135
        $action = /** @scrutinizer ignore-deprecated */ new CommentsGridFieldAction();
Loading history...
136
        $record = new Comment();
137
        $record->Name = 'Name of commenter';
138
        $record->Comment = 'This is a comment';
139
        $record->ParentID = $item->ID;
140
        $record->ParentClass = $item->class;
0 ignored issues
show
Bug Best Practice introduced by
The property class does not exist on SilverStripe\Comments\Tests\Stubs\CommentableItem. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property ParentClass does not exist on SilverStripe\Comments\Model\Comment. Since you implemented __set, consider adding a @property annotation.
Loading history...
141
        $record->write();
142
        $recordID = $record->ID;
143
        $arguments = array('RecordID' => $recordID);
144
        $data = array();
145
        $result = $action->handleAction($this->gridField, 'spam', $arguments, $data);
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
Bug introduced by
Are you sure the assignment to $result is correct as $action->handleAction($t...am', $arguments, $data) targeting SilverStripe\Comments\Ad...dAction::handleAction() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
146
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
147
        $this->assertEquals(
148
            'Comment marked as spam.',
149
            Controller::curr()->getResponse()->getStatusDescription()
150
        );
151
        $record = DataObject::get_by_id(Comment::class, $recordID);
152
        $this->assertEquals(1, $record->Moderated);
153
        $this->assertEquals(1, $record->IsSpam);
154
155
        //getStatusDescription
156
        $result = $action->handleAction($this->gridField, 'approve', $arguments, $data);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $action->handleAction($t...ve', $arguments, $data) targeting SilverStripe\Comments\Ad...dAction::handleAction() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
157
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
158
        $this->assertEquals(
159
            'Comment approved.',
160
            Controller::curr()->getResponse()->getStatusDescription()
161
        );
162
163
        $record = DataObject::get_by_id(Comment::class, $recordID);
164
        $this->assertEquals(1, $record->Moderated);
165
        $this->assertEquals(0, $record->IsSpam);
166
    }
167
}
168