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); |
|
|
|
|
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(); |
|
|
|
|
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(); |
|
|
|
|
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(); |
|
|
|
|
76
|
|
|
$result = $action->getColumnMetadata($this->gridField, 'Actions'); |
77
|
|
|
$this->assertEquals(array('title' => ''), $result); |
78
|
|
|
$result = $action->getColumnMetadata($this->gridField, 'SomethingElse'); |
|
|
|
|
79
|
|
|
$this->assertNull($result); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testGetColumnsHandled() |
83
|
|
|
{ |
84
|
|
|
$action = new CommentsGridFieldAction(); |
|
|
|
|
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(); |
|
|
|
|
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(); |
|
|
|
|
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(); |
|
|
|
|
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; |
|
|
|
|
141
|
|
|
$record->write(); |
142
|
|
|
$recordID = $record->ID; |
143
|
|
|
$arguments = array('RecordID' => $recordID); |
144
|
|
|
$data = array(); |
145
|
|
|
$result = $action->handleAction($this->gridField, 'spam', $arguments, $data); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
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..