Completed
Push — master ( 2f705a...c06b8c )
by Simon
01:46
created

PartialUserFormControllerTest::testArrayData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace Firesphere\PartialUserforms\Tests;
4
5
use Firesphere\PartialUserforms\Controllers\PartialUserFormController;
6
use Firesphere\PartialUserforms\Models\PartialFieldSubmission;
7
use Firesphere\PartialUserforms\Models\PartialFormSubmission;
8
use SilverStripe\Control\HTTPRequest;
9
use SilverStripe\Control\Session;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Dev\SapphireTest;
12
use SilverStripe\ORM\DataList;
13
14
class PartialUserFormControllerTest extends SapphireTest
15
{
16
    protected static $fixture_file = '../fixtures/partialformtest.yml';
17
18
    /**
19
     * @var PartialUserFormController
20
     */
21
    protected $controller;
22
23
    public function setUp()
24
    {
25
        $this->controller = Injector::inst()->get(PartialUserFormController::class);
26
        parent::setUp();
27
    }
28
29
    public function testClassExists()
30
    {
31
        $this->assertInstanceOf(PartialUserFormController::class, $this->controller);
32
    }
33
34
    public function testSavePartialSubmissionExists()
35
    {
36
        $this->assertTrue(method_exists($this->controller, 'savePartialSubmission'));
37
    }
38
39 View Code Duplication
    public function testSavePartialSubmissionFormCreated()
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...
40
    {
41
        $request = new HTTPRequest('POST', '/partialuserform', [], ['Field1' => 'Value1']);
42
        $session = new Session(['hi' => 'bye']);
43
        $request->setSession($session);
44
45
        $id = $this->controller->savePartialSubmission($request);
46
47
        $this->assertInternalType('numeric', $id);
48
49
        $form = PartialFormSubmission::get()->byID($id);
50
51
        $this->assertInstanceOf(PartialFormSubmission::class, $form);
52
    }
53
54 View Code Duplication
    public function testSavePartialSubmissionFieldCreated()
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...
55
    {
56
        $request = new HTTPRequest('POST', '/partialuserform', [], ['Field1' => 'Value1']);
57
        $session = new Session(['hi' => 'bye']);
58
        $request->setSession($session);
59
60
        $id = $this->controller->savePartialSubmission($request);
61
62
        $fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $id]);
63
64
        $this->assertEquals(1, $fields->count());
65
    }
66
67
    public function testPartialFormSubmissionExists()
68
    {
69
        $request = new HTTPRequest('POST', '/partialuserform', [], ['Field1' => 'Value1', 'Field2' => 'Value2']);
70
        $session = new Session(['hi' => 'bye']);
71
        $request->setSession($session);
72
73
        $id = $this->controller->savePartialSubmission($request);
74
75
        $session = $request->getSession();
76
        $request = new HTTPRequest('POST', '/partialuserform', [], ['Field2' => 'Value2']);
77
        $request->setSession($session);
78
79
        $secondId = $this->controller->savePartialSubmission($request);
80
81
        $this->assertEquals($id, $secondId);
82
    }
83
84
    public function testPartialFormSubmissionExistingField()
85
    {
86
        $values = [
87
            'Field1' => 'Value1',
88
            'Field2' => 'Value2',
89
            'Field3' => 'null'
90
        ];
91
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
92
        $session = new Session(['hi' => 'bye']);
93
        $request->setSession($session);
94
95
        $this->controller->savePartialSubmission($request);
96
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
97
        $field3 = PartialFieldSubmission::get()
98
            ->filter([
99
                'Name'            => 'Field3',
100
                'SubmittedFormID' => $sessionKey
101
            ])
102
            ->first();
103
104
        $this->assertEquals('null', $field3->Value);
105
        // Update the values
106
        $values['Field3'] = 'Value3';
107
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
108
        $request->setSession($session);
109
        $this->controller->savePartialSubmission($request);
110
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
111
112
        $field3 = PartialFieldSubmission::get()
113
            ->filter([
114
                'Name'            => 'Field3',
115
                'SubmittedFormID' => $sessionKey
116
            ])
117
            ->first();
118
        $this->assertEquals('Value3', $field3->Value);
119
    }
120
121
    public function testSubmittedFieldTitle()
122
    {
123
        $values = [
124
            'Field1' => 'Value1',
125
            'Field2' => 'Value2',
126
            'Field3' => 'null'
127
        ];
128
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
129
        $session = new Session(['hi' => 'bye']);
130
        $request->setSession($session);
131
132
        $this->controller->savePartialSubmission($request);
133
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
134
        /** @var DataList|PartialFieldSubmission[] $fields */
135
        $fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $sessionKey]);
136
137
        foreach ($fields as $key => $field) {
138
            $this->assertEquals('Field ' . ($key + 1), $field->Title, 'Test field ' . $key);
139
        }
140
    }
141
142
    public function testParent()
143
    {
144
        $values = [
145
            'Field1' => 'Value1',
146
            'Field2' => 'Value2',
147
            'Field3' => 'null'
148
        ];
149
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
150
        $session = new Session(['hi' => 'bye']);
151
        $request->setSession($session);
152
153
        $this->controller->savePartialSubmission($request);
154
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
155
        /** @var DataList|PartialFieldSubmission[] $fields */
156
        $partialForm = PartialFormSubmission::get()->byID($sessionKey);
157
158
        $this->assertEquals('SilverStripe\UserForms\Model\UserDefinedForm', $partialForm->ParentClass);
159
    }
160
161
    public function testUnwantedFields()
162
    {
163
        $values = [
164
            'Field1'         => 'Value1',
165
            'Field2'         => 'Value2',
166
            'Field3'         => 'null',
167
            'SecurityID'     => '123456789aoeu',
168
            'action_process' => 'Submit'
169
        ];
170
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
171
        $session = new Session(['hi' => 'bye']);
172
        $request->setSession($session);
173
174
        $this->controller->savePartialSubmission($request);
175
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
176
        /** @var DataList|PartialFieldSubmission[] $fields */
177
        $fields = PartialFieldSubmission::get()->filter(['SubmittedFormID' => $sessionKey]);
178
179
        $items = $fields->column('Name');
180
        $this->assertFalse(in_array('SecurityID', $items, true));
181
        $this->assertFalse(in_array('action_process', $items, true));
182
    }
183
184
    public function testArrayData()
185
    {
186
        $values = [
187
            'Field1' => 'Value1',
188
            'Field2' => 'Value2',
189
            'Field3' => ['Value1','Value2']
190
        ];
191
        $request = new HTTPRequest('POST', '/partialuserform', [], $values);
192
        $session = new Session(['hi' => 'bye']);
193
        $request->setSession($session);
194
195
        $this->controller->savePartialSubmission($request);
196
        $sessionKey = $session->get(PartialUserFormController::SESSION_KEY);
197
        $field3 = PartialFieldSubmission::get()
198
            ->filter([
199
                'Name'            => 'Field3',
200
                'SubmittedFormID' => $sessionKey
201
            ])
202
            ->first();
203
        $this->assertEquals('Value1, Value2', $field3->Value);
204
    }
205
}
206