|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class AkismetTest extends FunctionalTest |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
protected $extraDataObjects = array('AkismetTest_Submission'); |
|
6
|
|
|
|
|
7
|
|
|
protected $usesDatabase = true; |
|
8
|
|
|
|
|
9
|
|
|
protected $requiredExtensions = array( |
|
10
|
|
|
'SiteConfig' => array('AkismetConfig') |
|
11
|
|
|
); |
|
12
|
|
|
|
|
13
|
|
|
public function setUp() |
|
14
|
|
|
{ |
|
15
|
|
|
parent::setUp(); |
|
16
|
|
|
Injector::nest(); |
|
17
|
|
|
Injector::inst()->unregisterAllObjects(); |
|
18
|
|
|
|
|
19
|
|
|
// Mock service |
|
20
|
|
|
Config::nest(); |
|
21
|
|
|
Config::inst()->update('Injector', 'AkismetService', 'AkismetTest_Service'); |
|
22
|
|
|
Config::inst()->update('AkismetSpamProtector', 'api_key', 'dummykey'); |
|
23
|
|
|
AkismetSpamProtector::set_api_key(null); |
|
24
|
|
|
|
|
25
|
|
|
// Reset options to reasonable default |
|
26
|
|
|
Config::inst()->remove('AkismetSpamProtector', 'save_spam'); |
|
27
|
|
|
Config::inst()->remove('AkismetSpamProtector', 'require_confirmation'); |
|
28
|
|
|
Config::inst()->remove('AkismetSpamProtector', 'bypass_members'); |
|
29
|
|
|
Config::inst()->update('AkismetSpamProtector', 'bypass_permission', 'ADMIN'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function tearDown() |
|
33
|
|
|
{ |
|
34
|
|
|
Config::unnest(); |
|
35
|
|
|
Injector::unnest(); |
|
36
|
|
|
parent::tearDown(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testSpamDetectionForm() |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
// Test "nice" setting |
|
43
|
|
|
$result = $this->post('AkismetTest_Controller/Form', array( |
|
44
|
|
|
'Name' => 'person', |
|
45
|
|
|
'Email' => '[email protected]', |
|
46
|
|
|
'Content' => 'what a nice comment', |
|
47
|
|
|
'action_doSubmit' => 'Submit', |
|
48
|
|
|
)); |
|
49
|
|
|
|
|
50
|
|
|
$this->assertContains('Thanks for your submission, person', $result->getBody()); |
|
51
|
|
|
$saved = AkismetTest_Submission::get()->last(); |
|
52
|
|
|
$this->assertNotEmpty($saved); |
|
|
|
|
|
|
53
|
|
|
$this->assertEquals('person', $saved->Name); |
|
|
|
|
|
|
54
|
|
|
$this->assertEquals('[email protected]', $saved->Email); |
|
|
|
|
|
|
55
|
|
|
$this->assertEquals('what a nice comment', $saved->Content); |
|
|
|
|
|
|
56
|
|
|
$this->assertEquals(false, (bool)$saved->IsSpam); |
|
|
|
|
|
|
57
|
|
|
$saved->delete(); |
|
58
|
|
|
|
|
59
|
|
|
// Test failed setting |
|
60
|
|
|
$result = $this->post('AkismetTest_Controller/Form', array( |
|
61
|
|
|
'Name' => 'spam', |
|
62
|
|
|
'Email' => '[email protected]', |
|
63
|
|
|
'Content' => 'spam', |
|
64
|
|
|
'action_doSubmit' => 'Submit', |
|
65
|
|
|
)); |
|
66
|
|
|
|
|
67
|
|
|
$errorMessage = _t( |
|
68
|
|
|
'AkismetField.SPAM', |
|
69
|
|
|
"Your submission has been rejected because it was treated as spam." |
|
70
|
|
|
); |
|
71
|
|
|
$this->assertContains($errorMessage, $result->getBody()); |
|
72
|
|
|
$saved = AkismetTest_Submission::get()->last(); |
|
73
|
|
|
$this->assertEmpty($saved); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testSaveSpam() |
|
77
|
|
|
{ |
|
78
|
|
|
Config::inst()->update('AkismetSpamProtector', 'save_spam', 'true'); |
|
79
|
|
|
|
|
80
|
|
|
// Test "nice" setting |
|
81
|
|
|
$result = $this->post('AkismetTest_Controller/Form', array( |
|
82
|
|
|
'Name' => 'person', |
|
83
|
|
|
'Email' => '[email protected]', |
|
84
|
|
|
'Content' => 'what a nice comment', |
|
85
|
|
|
'action_doSubmit' => 'Submit', |
|
86
|
|
|
)); |
|
87
|
|
|
|
|
88
|
|
|
$this->assertContains('Thanks for your submission, person', $result->getBody()); |
|
89
|
|
|
$saved = AkismetTest_Submission::get()->last(); |
|
90
|
|
|
$this->assertNotEmpty($saved); |
|
|
|
|
|
|
91
|
|
|
$this->assertEquals('person', $saved->Name); |
|
|
|
|
|
|
92
|
|
|
$this->assertEquals('[email protected]', $saved->Email); |
|
|
|
|
|
|
93
|
|
|
$this->assertEquals('what a nice comment', $saved->Content); |
|
|
|
|
|
|
94
|
|
|
$this->assertEquals(false, (bool)$saved->IsSpam); |
|
|
|
|
|
|
95
|
|
|
$saved->delete(); |
|
96
|
|
|
|
|
97
|
|
|
// Test failed setting |
|
98
|
|
|
$result = $this->post('AkismetTest_Controller/Form', array( |
|
99
|
|
|
'Name' => 'spam', |
|
100
|
|
|
'Email' => '[email protected]', |
|
101
|
|
|
'Content' => 'spam', |
|
102
|
|
|
'action_doSubmit' => 'Submit', |
|
103
|
|
|
)); |
|
104
|
|
|
|
|
105
|
|
|
$errorMessage = _t( |
|
106
|
|
|
'AkismetField.SPAM', |
|
107
|
|
|
"Your submission has been rejected because it was treated as spam." |
|
108
|
|
|
); |
|
109
|
|
|
$this->assertContains($errorMessage, $result->getBody()); |
|
110
|
|
|
$saved = AkismetTest_Submission::get()->last(); |
|
111
|
|
|
$this->assertNotEmpty($saved); |
|
|
|
|
|
|
112
|
|
|
$this->assertEquals('spam', $saved->Name); |
|
|
|
|
|
|
113
|
|
|
$this->assertEquals('[email protected]', $saved->Email); |
|
|
|
|
|
|
114
|
|
|
$this->assertEquals('spam', $saved->Content); |
|
|
|
|
|
|
115
|
|
|
$this->assertEquals(true, (bool)$saved->IsSpam); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Test that the request processor can safely activate when able (and only then) |
|
120
|
|
|
*/ |
|
121
|
|
|
public function testProcessor() |
|
122
|
|
|
{ |
|
123
|
|
|
$siteconfig = SiteConfig::current_site_config(); |
|
124
|
|
|
$siteconfig->write(); |
|
125
|
|
|
|
|
126
|
|
|
// Test assignment via request filter |
|
127
|
|
|
$processor = new AkismetTest_TestProcessor(); |
|
128
|
|
|
$this->assertTrue($processor->publicIsDBReady()); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
// Remove AkismetKey field |
|
131
|
|
|
DB::query('ALTER TABLE "SiteConfig" DROP COLUMN "AkismetKey"'); |
|
132
|
|
|
$this->assertFalse($processor->publicIsDBReady()); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
class AkismetTest_Submission extends DataObject implements TestOnly |
|
|
|
|
|
|
137
|
|
|
{ |
|
138
|
|
|
private static $db = array( |
|
|
|
|
|
|
139
|
|
|
'Name' => 'Varchar', |
|
140
|
|
|
'Email' => 'Varchar', |
|
141
|
|
|
'Content' => 'Text', |
|
142
|
|
|
'IsSpam' => 'Boolean', |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
private static $default_sort = 'ID'; |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
class AkismetTest_Controller extends Controller implements TestOnly |
|
|
|
|
|
|
149
|
|
|
{ |
|
150
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
151
|
|
|
'Form' |
|
152
|
|
|
); |
|
153
|
|
|
|
|
154
|
|
|
public function Form() |
|
155
|
|
|
{ |
|
156
|
|
|
$fields = new FieldList( |
|
157
|
|
|
new TextField('Name'), |
|
158
|
|
|
new EmailField('Email'), |
|
159
|
|
|
new TextareaField('Content') |
|
160
|
|
|
); |
|
161
|
|
|
$actions = new FieldList(new FormAction('doSubmit', 'Submit')); |
|
162
|
|
|
$validator = new RequiredFields('Name', 'Content'); |
|
163
|
|
|
$form = new Form($this, 'Form', $fields, $actions, $validator); |
|
164
|
|
|
|
|
165
|
|
|
$form->enableSpamProtection(array( |
|
166
|
|
|
'protector' => 'AkismetSpamProtector', |
|
167
|
|
|
'name' => 'IsSpam', |
|
168
|
|
|
'mapping' => array( |
|
169
|
|
|
'Content' => 'body', |
|
170
|
|
|
'Name' => 'authorName', |
|
171
|
|
|
'Email' => 'authorMail', |
|
172
|
|
|
) |
|
173
|
|
|
)); |
|
174
|
|
|
|
|
175
|
|
|
// Because we don't want to be testing this |
|
176
|
|
|
$form->disableSecurityToken(); |
|
177
|
|
|
return $form; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function doSubmit($data, Form $form) |
|
181
|
|
|
{ |
|
182
|
|
|
$item = new AkismetTest_Submission(); |
|
183
|
|
|
$form->saveInto($item); |
|
184
|
|
|
$item->write(); |
|
185
|
|
|
$form->sessionMessage('Thanks for your submission, '. $data['Name'], 'good'); |
|
186
|
|
|
return $this->redirect($this->Link()); |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
class AkismetTest_Service implements TestOnly, AkismetService |
|
|
|
|
|
|
191
|
|
|
{ |
|
192
|
|
|
public function __construct($apiKey, $url) |
|
|
|
|
|
|
193
|
|
|
{ |
|
194
|
|
|
if ($apiKey !== 'dummykey') { |
|
195
|
|
|
throw new Exception("Invalid key"); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function isSpam($content, $author = null, $email = null, $url = null, $permalink = null, $type = null) |
|
200
|
|
|
{ |
|
201
|
|
|
// This dummy service only checks the content |
|
202
|
|
|
return $content === 'spam'; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
class AkismetTest_TestProcessor extends AkismetProcessor implements TestOnly |
|
|
|
|
|
|
207
|
|
|
{ |
|
208
|
|
|
public function publicIsDBReady() |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->isDBReady(); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.