Completed
Push — master ( eb92ab...a3e742 )
by Robbie
11s
created

MultiFormTest::testIncorrectSessionIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\MultiForm\Tests;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Control\Session;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\FunctionalTest;
10
use SilverStripe\MultiForm\Models\MultiForm;
11
use SilverStripe\MultiForm\Models\MultiFormSession;
12
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestController;
13
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestForm;
14
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestStepOne;
15
use SilverStripe\MultiForm\Tests\Stubs\MultiFormTestStepTwo;
16
17
/**
18
 * MultiFormTest
19
 * For testing purposes, we have some test classes:
20
 *
21
 *  - MultiFormTestController (simulation of a real Controller class)
22
 *  - MultiFormTestForm (subclass of MultiForm)
23
 *  - MultiFormTestStepOne (subclass of MultiFormStep)
24
 *  - MultiFormTestStepTwo (subclass of MultiFormStep)
25
 *  - MultiFormTestStepThree (subclass of MultiFormStep)
26
 *
27
 * The above classes are used to simulate real-world behaviour
28
 * of the multiform module - for example, MultiFormTestController
29
 * is a simulation of a page where MultiFormTest_Form is a simple
30
 * multi-step contact form it belongs to.
31
 *
32
 */
33
class MultiFormTest extends FunctionalTest
34
{
35
    protected static $fixture_file = 'MultiFormTest.yml';
36
37
    /**
38
     * @var MultiFormTestController
39
     */
40
    protected $controller;
41
42
    /**
43
     * @var MultiFormTestForm
44
     */
45
    protected $form;
46
47
    protected function setUp()
48
    {
49
        parent::setUp();
50
51
        $this->controller = new MultiFormTestController();
52
        $this->controller->setRequest(new HTTPRequest('GET', '/'));
53
        $this->controller->getRequest()->setSession(new Session([]));
54
        $this->controller->pushCurrent();
55
        $form = $this->form = $this->controller->Form();
56
        Injector::inst()->registerService($form, MultiForm::class);
57
        $this->form =  $form;
58
    }
59
60
    public function testInitialisingForm()
61
    {
62
        $this->assertTrue(is_numeric($this->form->getCurrentStep()->ID) && ($this->form->getCurrentStep()->ID > 0));
63
        $this->assertTrue(
64
            is_numeric($this->form->getMultiFormSession()->ID)
65
            && ($this->form->getMultiFormSession()->ID > 0)
66
        );
67
        $this->assertEquals(MultiFormTestStepOne::class, $this->form->getStartStep());
68
    }
69
70
    public function testSessionGeneration()
71
    {
72
        $this->assertTrue($this->form->getMultiFormSession()->ID > 0);
73
    }
74
75
    public function testMemberLogging()
76
    {
77
        // Grab any user to fake being logged in as, and ensure that after a session is written it has
78
        // that user as the submitter.
79
80
        $userId = $this->logInWithPermission('ADMIN');
81
82
        $session = $this->form->getMultiFormSession();
83
        $session->write();
84
85
        $this->assertEquals($userId, $session->SubmitterID);
86
    }
87
88
    public function testSecondStep()
89
    {
90
        $this->assertEquals(MultiFormTestStepTwo::class, $this->form->getCurrentStep()->getNextStep());
91
    }
92
93
    public function testParentForm()
94
    {
95
        $currentStep = $this->form->getCurrentStep();
96
        $this->assertEquals($currentStep->getForm()->class, $this->form->class);
97
    }
98
99
    public function testTotalStepCount()
100
    {
101
        $this->assertEquals(3, $this->form->getAllStepsLinear()->Count());
102
    }
103
104
    public function testCompletedSession()
105
    {
106
        $this->form->setCurrentSessionHash($this->form->getMultiFormSession()->Hash);
107
        $this->assertInstanceOf(MultiFormSession::class, $this->form->getCurrentSession());
108
        $this->form->getMultiFormSession()->markCompleted();
109
        $this->assertNull($this->form->getCurrentSession());
110
    }
111
112
    public function testIncorrectSessionIdentifier()
113
    {
114
        $this->form->setCurrentSessionHash('sdfsdf3432325325sfsdfdf'); // made up!
115
116
        // A new session is generated, even though we made up the identifier
117
        $this->assertInstanceOf(MultiFormSession::class, $this->form->getMultiFormSession());
118
    }
119
120
    public function testCustomGetVar()
121
    {
122
        Config::modify()->set(MultiForm::class, 'get_var', 'SuperSessionID');
123
124
        $form = $this->controller->Form();
125
        $this->assertContains(
126
            'SuperSessionID',
127
            $form->config()->get('ignored_fields'),
128
            'GET var wasn\'t added to ignored fields'
129
        );
130
        $this->assertContains(
131
            'SuperSessionID',
132
            $form->FormAction(),
133
            "Form action doesn't contain correct session ID parameter"
134
        );
135
        $this->assertContains(
136
            'SuperSessionID',
137
            $form->getCurrentStep()->Link(),
138
            "Form step doesn't contain correct session ID parameter"
139
        );
140
    }
141
}
142