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

tests/CommentsGridFieldActionTest.php (8 issues)

Labels
Severity

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
class CommentsGridFieldActionTest extends SapphireTest {
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');
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);
28
        $expected = array('Actions');
29
        $this->assertEquals($expected, $columns);
0 ignored issues
show
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);
33
        $expected = array('Actions');
34
        $this->assertEquals($expected, $columns);
0 ignored issues
show
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);
42
	}
43
44
	public function testGetColumnMetadata() {
45
		$action = new CommentsGridFieldAction();
46
        $result = $action->getColumnMetadata($this->gridField, 'Actions');
47
        $this->assertEquals(array('title' => ''), $result);
48
        $result = $action->getColumnMetadata($this->gridField, 'SomethingElse');
49
        $this->assertNull($result);
50
	}
51
52
	public function testGetColumnsHandled() {
53
		$action = new CommentsGridFieldAction();
54
        $result = $action->getColumnsHandled($this->gridField);
55
        $this->assertEquals(array('Actions'), $result);
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);
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 );
109
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
0 ignored issues
show
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
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
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);
117
118
//getStatusDescription
119
        $result = $action->handleAction($this->gridField, 'approve', $arguments, $data );
120
        $this->assertEquals(200, Controller::curr()->getResponse()->getStatusCode());
0 ignored issues
show
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
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
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);
129
130
        error_log(Controller::curr()->getResponse()->getStatusCode());
131
	}
132
133
}
134