Issues (4)

tests/AkismetTestController.php (1 issue)

Severity
1
<?php
2
3
namespace SilverStripe\Akismet\Tests;
4
5
use SilverStripe\Akismet\AkismetSpamProtector;
6
use SilverStripe\Forms\TextField;
7
use SilverStripe\Control\Email\Email;
8
use SilverStripe\Forms\EmailField;
9
use SilverStripe\Forms\TextareaField;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Forms\FormAction;
12
use SilverStripe\Forms\RequiredFields;
13
use SilverStripe\Forms\Form;
14
use SilverStripe\Control\Controller;
15
use SilverStripe\Dev\TestOnly;
16
17
/**
18
 * @skipUpgrade
19
 */
20
class AkismetTestController extends Controller implements TestOnly
21
{
22
    private static $allowed_actions = array(
0 ignored issues
show
The private property $allowed_actions is not used, and could be removed.
Loading history...
23
        'Form'
24
    );
25
26
    public function Form()
27
    {
28
        $fields = new FieldList(
29
            new TextField('Name'),
30
            new EmailField('Email'),
31
            new TextareaField('Content')
32
        );
33
        $actions = new FieldList(new FormAction('doSubmit', 'Submit'));
34
        $validator = new RequiredFields('Name', 'Content');
35
        $form = new Form($this, 'Form', $fields, $actions, $validator);
36
37
        $form->enableSpamProtection(array(
38
            'protector' => AkismetSpamProtector::class,
39
            'name' => 'IsSpam',
40
            'mapping' => array(
41
                'Content' => 'body',
42
                'Name' => 'authorName',
43
                'Email' => 'authorMail',
44
            )
45
        ));
46
47
        // Because we don't want to be testing this
48
        $form->disableSecurityToken();
49
        return $form;
50
    }
51
52
    public function doSubmit($data, Form $form)
53
    {
54
        $item = new AkismetTestSubmission();
55
        $form->saveInto($item);
56
        $item->write();
57
        $form->sessionMessage('Thanks for your submission, ' . $data['Name'], 'good');
58
        return $this->redirect($this->Link());
59
    }
60
61
    public function Link($action = null)
62
    {
63
        return 'AkismetTestController';
64
    }
65
}
66