1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Comments\Tests; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_Error_Deprecated; |
6
|
|
|
use SilverStripe\Comments\Commenting; |
7
|
|
|
use SilverStripe\Comments\Extensions\CommentsExtension; |
8
|
|
|
use SilverStripe\Comments\Tests\Stubs\CommentableItem; |
9
|
|
|
use SilverStripe\Core\Config\Config; |
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
11
|
|
|
use SilverStripe\Security\Member; |
12
|
|
|
|
13
|
|
|
class CommentingTest extends SapphireTest |
14
|
|
|
{ |
15
|
|
|
public function setUpOnce() |
16
|
|
|
{ |
17
|
|
|
parent::setUpOnce(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function testDeprecatedMethods() |
21
|
|
|
{ |
22
|
|
|
$methods = array('add', 'remove', 'has_commenting'); |
23
|
|
|
foreach ($methods as $methodName) { |
24
|
|
|
try { |
25
|
|
|
Commenting::$methodName(Member::class); |
26
|
|
|
} catch (PHPUnit_Framework_Error_Deprecated $e) { |
27
|
|
|
$expected = 'Using Commenting:' . $methodName .' is deprecated.' |
28
|
|
|
. ' Please use the config API instead'; |
29
|
|
|
$this->assertEquals($expected, $e->getMessage()); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testSetConfigValue() |
35
|
|
|
{ |
36
|
|
|
// public static function set_config_value($class, $key, $value = false) { |
|
|
|
|
37
|
|
|
Commenting::set_config_value( |
38
|
|
|
CommentableItem::class, |
39
|
|
|
'comments_holder_id', |
40
|
|
|
'commentable_item' |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
$config = Config::inst()->get( |
44
|
|
|
CommentableItem::class, |
45
|
|
|
'comments' |
46
|
|
|
); |
47
|
|
|
$actual = $config['comments_holder_id']; |
48
|
|
|
|
49
|
|
|
$this->assertEquals( |
50
|
|
|
'commentable_item', |
51
|
|
|
$actual |
52
|
|
|
); |
53
|
|
|
Commenting::set_config_value( |
54
|
|
|
'all', |
55
|
|
|
'comments_holder_id', |
56
|
|
|
'all_items_actually_commentsextension' |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$config = Config::inst()->get( |
60
|
|
|
CommentsExtension::class, |
61
|
|
|
'comments' |
62
|
|
|
); |
63
|
|
|
$actual = $config['comments_holder_id']; |
64
|
|
|
$this->assertEquals( |
65
|
|
|
'all_items_actually_commentsextension', |
66
|
|
|
$actual |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testGetConfigValue() |
71
|
|
|
{ |
72
|
|
|
Config::inst()->update( |
73
|
|
|
CommentableItem::class, |
74
|
|
|
'comments', |
75
|
|
|
array( |
76
|
|
|
'comments_holder_id' => 'commentable_item' |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
$this->assertEquals( |
80
|
|
|
'commentable_item', |
81
|
|
|
Commenting::get_config_value(CommentableItem::class, 'comments_holder_id') |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
Config::inst()->update( |
85
|
|
|
CommentsExtension::class, |
86
|
|
|
'comments', |
87
|
|
|
array( |
88
|
|
|
'comments_holder_id' => 'comments_extension' |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
// if class is null, method uses the CommentsExtension property |
92
|
|
|
$this->assertEquals( |
93
|
|
|
'comments_extension', |
94
|
|
|
Commenting::get_config_value(null, 'comments_holder_id') |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$this->setExpectedException( |
98
|
|
|
'InvalidArgumentException', |
99
|
|
|
'Member does not have commenting enabled' |
100
|
|
|
); |
101
|
|
|
Commenting::get_config_value(Member::class, 'comments_holder_id'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testConfigValueEquals() |
105
|
|
|
{ |
106
|
|
|
Config::inst()->update( |
107
|
|
|
CommentableItem::class, |
108
|
|
|
'comments', |
109
|
|
|
array( |
110
|
|
|
'comments_holder_id' => 'some_value' |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$this->assertTrue( |
115
|
|
|
Commenting::config_value_equals( |
116
|
|
|
CommentableItem::class, |
117
|
|
|
'comments_holder_id', |
118
|
|
|
'some_value' |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
$this->assertNull( |
123
|
|
|
Commenting::config_value_equals( |
124
|
|
|
CommentableItem::class, |
125
|
|
|
'comments_holder_id', |
126
|
|
|
'not_some_value' |
127
|
|
|
) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function testAdd() |
132
|
|
|
{ |
133
|
|
|
Commenting::add(Member::class, array('comments_holder_id' => 'test_add_value')); |
134
|
|
|
|
135
|
|
|
$config = Config::inst()->get( |
136
|
|
|
Member::class, |
137
|
|
|
'comments' |
138
|
|
|
); |
139
|
|
|
$actual = $config['comments_holder_id']; |
140
|
|
|
$this->assertEquals( |
141
|
|
|
'test_add_value', |
142
|
|
|
$actual |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
Commenting::add(Member::class); |
146
|
|
|
|
147
|
|
|
$config = Config::inst()->get( |
148
|
|
|
Member::class, |
149
|
|
|
'comments' |
150
|
|
|
); |
151
|
|
|
$actual = $config['comments_holder_id']; |
152
|
|
|
// no settings updated |
153
|
|
|
$this->assertEquals( |
154
|
|
|
'test_add_value', |
155
|
|
|
$actual |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$this->setExpectedException('InvalidArgumentException', "\$settings needs to be an array or null"); |
159
|
|
|
Commenting::add(Member::class, 'illegal format, not an array'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testCanMemberPost() |
163
|
|
|
{ |
164
|
|
|
// logout |
165
|
|
|
if ($member = Member::currentUser()) { |
166
|
|
|
$member->logOut(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
Config::inst()->update( |
170
|
|
|
CommentableItem::class, |
171
|
|
|
'comments', |
172
|
|
|
array( |
173
|
|
|
'require_login' => false |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
$this->assertTrue(Commenting::can_member_post(CommentableItem::class)); |
177
|
|
|
|
178
|
|
|
Config::inst()->update( |
179
|
|
|
CommentableItem::class, |
180
|
|
|
'comments', |
181
|
|
|
array( |
182
|
|
|
'require_login' => true |
183
|
|
|
) |
184
|
|
|
); |
185
|
|
|
$this->assertFalse(Commenting::can_member_post(CommentableItem::class)); |
186
|
|
|
|
187
|
|
|
$this->logInWithPermission('CMS_ACCESS_CommentAdmin'); |
188
|
|
|
$this->assertTrue(Commenting::can_member_post(CommentableItem::class)); |
189
|
|
|
|
190
|
|
|
Config::inst()->update( |
191
|
|
|
CommentableItem::class, |
192
|
|
|
'comments', |
193
|
|
|
array( |
194
|
|
|
'require_login' => false |
195
|
|
|
) |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$this->assertTrue(Commenting::can_member_post(CommentableItem::class)); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.