Completed
Push — master ( e42a4c...f187a0 )
by Daniel
03:17
created

CommentsGridFieldActionTest::testGetActions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
class CommentsGridFieldActionTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    /** @var ArrayList */
7
    protected $list;
8
9
    /** @var GridField */
10
    protected $gridField;
11
12
    /** @var Form */
13
    protected $form;
14
15
    public function setUp()
16
    {
17
        parent::setUp();
18
        $this->list = new DataList('GridFieldAction_Delete_Team');
0 ignored issues
show
Documentation Bug introduced by
It seems like new \DataList('GridFieldAction_Delete_Team') of type object<DataList> is incompatible with the declared type object<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...
19
        $config = CommentsGridFieldConfig::create()->addComponent(new GridFieldDeleteAction());
20
        $this->gridField = new CommentsGridField('testfield', 'testfield', $this->list, $config);
21
        $this->form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
22
    }
23
24
    public function testAugmentColumns()
25
    {
26
        $action = new CommentsGridFieldAction();
27
28
        // an entry called 'Actions' is added to the columns array
29
        $columns = array();
30
        $action->augmentColumns($this->gridField, $columns);
0 ignored issues
show
Documentation introduced by
$columns is of type array, but the function expects a object<arary>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
31
        $expected = array('Actions');
32
        $this->assertEquals($expected, $columns);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33
34
        $columns = array('Actions');
35
        $action->augmentColumns($this->gridField, $columns);
0 ignored issues
show
Documentation introduced by
$columns is of type array<integer,string,{"0":"string"}>, but the function expects a object<arary>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
        $expected = array('Actions');
37
        $this->assertEquals($expected, $columns);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
    }
39
40
    public function testGetColumnAttributes()
41
    {
42
        $action = new CommentsGridFieldAction();
43
        $record = new Comment();
44
        $attrs = $action->getColumnAttributes($this->gridField, $record, 'Comment');
45
        $this->assertEquals(array('class' => 'col-buttons'), $attrs);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
    }
47
48
    public function testGetColumnMetadata()
49
    {
50
        $action = new CommentsGridFieldAction();
51
        $result = $action->getColumnMetadata($this->gridField, 'Actions');
52
        $this->assertEquals(array('title' => ''), $result);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
        $result = $action->getColumnMetadata($this->gridField, 'SomethingElse');
54
        $this->assertNull($result);
0 ignored issues
show
Bug introduced by
The method assertNull() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
    }
56
57
    public function testGetColumnsHandled()
58
    {
59
        $action = new CommentsGridFieldAction();
60
        $result = $action->getColumnsHandled($this->gridField);
61
        $this->assertEquals(array('Actions'), $result);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
    }
63
64
    public function testGetColumnContent()
65
    {
66
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
67
        $action = new CommentsGridFieldAction();
68
        $record = new Comment();
69
        $record->Name = 'Name of commeter';
70
        $record->Comment = 'This is a comment';
71
        $record->write();
72
        $recordID = $record->ID;
73
        $html = $action->getColumnContent($this->gridField, $record, 'Comment');
74
        $this->assertContains('data-url="Controller/mockform/field/testfield',
75
                                $html);
76
        $spamAction = 'value="Spam" class="action" id="action_CustomAction' .
77
                    $recordID . 'Spam"';
78
        $this->assertContains($spamAction, $html);
79
80
        $approveAction = 'value="Approve" class="action" id="action_CustomAction' .
81
                    $recordID . 'Approve"';
82
        $this->assertContains($approveAction, $html);
83
84
        // If marked as spam, only the approve button should be available
85
        $record->markSpam();
86
        $record->write();
87
        $html = $action->getColumnContent($this->gridField, $record, 'Comment');
88
        $this->assertContains($approveAction, $html);
89
        $this->assertNotContains($spamAction, $html);
90
91
        // If marked as spam, only the approve button should be available
92
        $record->markApproved();
93
        $record->write();
94
        $html = $action->getColumnContent($this->gridField, $record, 'Comment');
95
        $this->assertNotContains($approveAction, $html);
96
        $this->assertContains($spamAction, $html);
97
    }
98
99
    public function testGetActions()
100
    {
101
        $action = new CommentsGridFieldAction();
102
        $result = $action->getActions($this->gridField);
103
        $this->assertEquals(array('spam', 'approve'), $result);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
104
    }
105
106
107
    public function testHandleAction()
108
    {
109
        $action = new CommentsGridFieldAction();
110
        $record = new Comment();
111
        $record->Name = 'Name of commeter';
112
        $record->Comment = 'This is a comment';
113
        $record->write();
114
        $recordID = $record->ID;
115
        $arguments = array('RecordID' => $recordID);
116
        $data = array();
117
        $result = $action->handleAction($this->gridField, 'spam', $arguments, $data);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $action->handleAction($t...am', $arguments, $data) (which targets CommentsGridFieldAction::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...
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...
118
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
119
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
            'Comment marked as spam.',
121
            Controller::curr()->getResponse()->getStatusDescription()
122
        );
123
        $record = DataObject::get_by_id('Comment', $recordID);
124
        $this->assertEquals(1, $record->Moderated);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
125
        $this->assertEquals(1, $record->IsSpam);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
127
//getStatusDescription
128
        $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) (which targets CommentsGridFieldAction::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...
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...
129
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
        $this->assertEquals(
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
            'Comment approved.',
132
            Controller::curr()->getResponse()->getStatusDescription()
133
        );
134
135
        $record = DataObject::get_by_id('Comment', $recordID);
136
        $this->assertEquals(1, $record->Moderated);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
137
        $this->assertEquals(0, $record->IsSpam);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<CommentsGridFieldActionTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
139
        error_log(Controller::curr()->getResponse()->getStatusCode());
140
    }
141
}
142