Completed
Push — master ( 02d548...e42a4c )
by Damian
02:25
created

CommentsGridFieldActionTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 1
eloc 6
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
    /** @var ArrayList */
6
    protected $list;
7
8
    /** @var GridField */
9
    protected $gridField;
10
11
    /** @var Form */
12
    protected $form;
13
14
     public function setUp() {
15
        parent::setUp();
16
        $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...
17
        $config = CommentsGridFieldConfig::create()->addComponent(new GridFieldDeleteAction());
18
        $this->gridField = new CommentsGridField('testfield', 'testfield', $this->list, $config);
19
        $this->form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField)), new FieldList());
20
    }
21
22
	public function testAugmentColumns() {
23
        $action = new CommentsGridFieldAction();
24
25
        // an entry called 'Actions' is added to the columns array
26
        $columns = array();
27
        $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...
28
        $expected = array('Actions');
29
        $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...
30
31
        $columns = array('Actions');
32
        $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...
33
        $expected = array('Actions');
34
        $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...
35
	}
36
37
	public function testGetColumnAttributes() {
38
		$action = new CommentsGridFieldAction();
39
        $record = new Comment();
40
        $attrs = $action->getColumnAttributes($this->gridField, $record, 'Comment');
41
        $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...
42
	}
43
44
	public function testGetColumnMetadata() {
45
		$action = new CommentsGridFieldAction();
46
        $result = $action->getColumnMetadata($this->gridField, 'Actions');
47
        $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...
48
        $result = $action->getColumnMetadata($this->gridField, 'SomethingElse');
49
        $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...
50
	}
51
52
	public function testGetColumnsHandled() {
53
		$action = new CommentsGridFieldAction();
54
        $result = $action->getColumnsHandled($this->gridField);
55
        $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...
56
	}
57
58
	public function testGetColumnContent() {
59
        $this->logInWithPermission('CMS_ACCESS_CommentAdmin');
60
		$action = new CommentsGridFieldAction();
61
        $record = new Comment();
62
        $record->Name = 'Name of commeter';
63
        $record->Comment = 'This is a comment';
64
        $record->write();
65
        $recordID = $record->ID;
66
        $html = $action->getColumnContent($this->gridField, $record, 'Comment');
67
        $this->assertContains('data-url="Controller/mockform/field/testfield',
68
                                $html);
69
        $spamAction = 'value="Spam" class="action" id="action_CustomAction' .
70
                    $recordID . 'Spam"';
71
        $this->assertContains($spamAction, $html);
72
73
        $approveAction = 'value="Approve" class="action" id="action_CustomAction' .
74
                    $recordID . 'Approve"';
75
        $this->assertContains($approveAction, $html);
76
77
        // If marked as spam, only the approve button should be available
78
        $record->markSpam();
79
        $record->write();
80
        $html = $action->getColumnContent($this->gridField, $record, 'Comment');
81
        $this->assertContains($approveAction, $html);
82
        $this->assertNotContains($spamAction, $html);
83
84
        // If marked as spam, only the approve button should be available
85
        $record->markApproved();
86
        $record->write();
87
        $html = $action->getColumnContent($this->gridField, $record, 'Comment');
88
        $this->assertNotContains($approveAction, $html);
89
        $this->assertContains($spamAction, $html);
90
	}
91
92
	public function testGetActions() {
93
		$action = new CommentsGridFieldAction();
94
        $result = $action->getActions($this->gridField);
95
        $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...
96
	}
97
98
99
	public function testHandleAction() {
100
		$action = new CommentsGridFieldAction();
101
        $record = new Comment();
102
        $record->Name = 'Name of commeter';
103
        $record->Comment = 'This is a comment';
104
        $record->write();
105
        $recordID = $record->ID;
106
        $arguments = array('RecordID' => $recordID);
107
        $data = array();
108
        $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...
109
        $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...
110
        $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...
111
            'Comment marked as spam.',
112
            Controller::curr()->getResponse()->getStatusDescription()
113
        );
114
        $record = DataObject::get_by_id('Comment', $recordID);
115
        $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...
116
        $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...
117
118
//getStatusDescription
119
        $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...
120
        $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...
121
        $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...
122
            'Comment approved.',
123
            Controller::curr()->getResponse()->getStatusDescription()
124
        );
125
126
        $record = DataObject::get_by_id('Comment', $recordID);
127
        $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...
128
        $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...
129
130
        error_log(Controller::curr()->getResponse()->getStatusCode());
131
	}
132
133
}
134