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); |
|
|
|
|
33
|
|
|
$config = CommentsGridFieldConfig::create()->addComponent(new GridFieldDeleteAction()); |
34
|
|
|
$this->gridField = new CommentsGridField('testfield', 'testfield', $this->list, $config); |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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..