Completed
Push — master ( 6e2aa4...b8898e )
by Daniel
02:46
created

AkismetTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
class AkismetTest extends FunctionalTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<AkismetTest>.

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...
53
        $this->assertEquals('person', $saved->Name);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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...
54
        $this->assertEquals('[email protected]', $saved->Email);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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...
55
        $this->assertEquals('what a nice comment', $saved->Content);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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...
56
        $this->assertEquals(false, (bool)$saved->IsSpam);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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...
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);
0 ignored issues
show
Bug introduced by
The method assertEmpty() does not seem to exist on object<AkismetTest>.

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...
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);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<AkismetTest>.

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...
91
        $this->assertEquals('person', $saved->Name);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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...
92
        $this->assertEquals('[email protected]', $saved->Email);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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...
93
        $this->assertEquals('what a nice comment', $saved->Content);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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...
94
        $this->assertEquals(false, (bool)$saved->IsSpam);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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...
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);
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<AkismetTest>.

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...
112
        $this->assertEquals('spam', $saved->Name);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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...
113
        $this->assertEquals('[email protected]', $saved->Email);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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...
114
        $this->assertEquals('spam', $saved->Content);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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...
115
        $this->assertEquals(true, (bool)$saved->IsSpam);
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<AkismetTest>.

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
    }
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());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<AkismetTest>.

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...
129
130
        // Remove AkismetKey field
131
        DB::query('ALTER TABLE "SiteConfig" DROP COLUMN "AkismetKey"');
132
        $this->assertFalse($processor->publicIsDBReady());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<AkismetTest>.

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...
133
    }
134
}
135
136
class AkismetTest_Submission extends DataObject implements TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
137
{
138
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
139
        'Name' => 'Varchar',
140
        'Email' => 'Varchar',
141
        'Content' => 'Text',
142
        'IsSpam' => 'Boolean',
143
    );
144
145
    private static $default_sort = 'ID';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $default_sort is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
146
}
147
148
class AkismetTest_Controller extends Controller implements TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
149
{
150
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
191
{
192
    public function __construct($apiKey, $url)
0 ignored issues
show
Unused Code introduced by
The parameter $url is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
207
{
208
    public function publicIsDBReady()
209
    {
210
        return $this->isDBReady();
211
    }
212
}
213