Completed
Pull Request — master (#51)
by Robbie
02:02 queued 51s
created

testInsertBeforeMissing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\SpamProtection\Tests;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\Forms\TextField;
11
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
12
use SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest\FooProtector;
13
use SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest\BarProtector;
14
use SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest\BazProtector;
15
16
/**
17
 * @package spamprotection
18
 */
19
class FormSpamProtectionExtensionTest extends SapphireTest
20
{
21
    protected $usesDatabase = false;
22
23
    /**
24
     * @var Form
25
     */
26
    protected $form = null;
27
28
    protected function setUp()
29
    {
30
        parent::setUp();
31
32
        $this->form = new Form(
33
            new Controller,
34
            'Form',
35
            new FieldList(
36
                new TextField('Title'),
37
                new TextField('Comment'),
38
                new TextField('URL')
39
            ),
40
            new FieldList()
41
        );
42
        $this->form->disableSecurityToken();
43
    }
44
45
    public function testEnableSpamProtection()
46
    {
47
        Config::modify()->set(
48
            FormSpamProtectionExtension::class,
49
            'default_spam_protector',
50
            FooProtector::class
51
        );
52
53
        $form = $this->form->enableSpamProtection();
54
55
        $this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title());
56
    }
57
58
    public function testEnableSpamProtectionCustomProtector()
59
    {
60
        $form = $this->form->enableSpamProtection(array(
61
            'protector' => BarProtector::class
62
        ));
63
64
        $this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title());
65
    }
66
67 View Code Duplication
    public function testEnableSpamProtectionCustomTitle()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        $form = $this->form->enableSpamProtection(array(
70
            'protector' => BarProtector::class,
71
            'title' => 'Baz',
72
        ));
73
74
        $this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title());
75
    }
76
77 View Code Duplication
    public function testCustomOptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $form = $this->form->enableSpamProtection(array(
80
            'protector' => BazProtector::class,
81
            'title' => 'Qux',
82
            'name' => 'Borris'
83
        ));
84
85
        $this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title());
86
    }
87
88
    public function testConfigurableName()
89
    {
90
        $field_name = "test_configurable_name";
91
        Config::modify()->set(
92
            FormSpamProtectionExtension::class,
93
            'default_spam_protector',
94
            FooProtector::class
95
        );
96
        Config::modify()->set(
97
            FormSpamProtectionExtension::class,
98
            'field_name',
99
            $field_name
100
        );
101
        $form = $this->form->enableSpamProtection();
102
        // remove for subsequent tests
103
        Config::modify()->remove(FormSpamProtectionExtension::class, 'field_name');
104
        // field should take up configured name
105
        $this->assertEquals('Foo', $form->Fields()->fieldByName($field_name)->Title());
106
    }
107
108 View Code Duplication
    public function testInsertBefore()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
    {
110
        $form = $this->form->enableSpamProtection(array(
111
            'protector' => FooProtector::class,
112
            'insertBefore' => 'URL'
113
        ));
114
115
        $fields = $form->Fields();
116
        $this->assertEquals('Title', $fields[0]->Title());
117
        $this->assertEquals('Comment', $fields[1]->Title());
118
        $this->assertEquals('Foo', $fields[2]->Title());
119
        $this->assertEquals('URL', $fields[3]->Title());
120
    }
121
122 View Code Duplication
    public function testInsertBeforeMissing()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        $form = $this->form->enableSpamProtection(array(
125
            'protector' => FooProtector::class,
126
            'insertBefore' => 'NotAField'
127
        ));
128
129
        // field should default to the end instead
130
        $fields = $form->Fields();
131
        $this->assertEquals('Title', $fields[0]->Title());
132
        $this->assertEquals('Comment', $fields[1]->Title());
133
        $this->assertEquals('URL', $fields[2]->Title());
134
        $this->assertEquals('Foo', $fields[3]->Title());
135
    }
136
}
137