Completed
Pull Request — master (#63)
by Franco
09:30
created

MultiFormTest_StepTwo   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 11
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 2
Metric Value
wmc 2
lcom 0
cbo 2
dl 11
loc 11
rs 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
13
/**
14
 * MultiFormTest
15
 * For testing purposes, we have some test classes:
16
 *
17
 *  - MultiFormTestController (simulation of a real Controller class)
18
 *  - MultiFormTestForm (subclass of MultiForm)
19
 *  - MultiFormTestStepOne (subclass of MultiFormStep)
20
 *  - MultiFormTestStepTwo (subclass of MultiFormStep)
21
 *  - MultiFormTestStepThree (subclass of MultiFormStep)
22
 *
23
 * The above classes are used to simulate real-world behaviour
24
 * of the multiform module - for example, MultiFormTestController
25
 * is a simulation of a page where MultiFormTest_Form is a simple
26
 * multi-step contact form it belongs to.
27
 *
28
 * @package multiform
29
 * @subpackage tests
30
 */
31
class MultiFormTest extends FunctionalTest
32
{
33
    public static $fixture_file = 'MultiFormTest.yml';
34
35
    /**
36
     * @var MultiFormTestController
37
     */
38
    protected $controller;
39
40
    /**
41
     * @var MultiFormTestForm
42
     */
43
    protected $form;
44
45
    protected function setUp()
46
    {
47
        parent::setUp();
48
49
        $this->controller = new MultiFormTestController();
50
        $this->controller->setRequest(new HTTPRequest('GET', '/'));
51
        $this->controller->getRequest()->setSession(new Session([]));
0 ignored issues
show
Bug introduced by
The method setSession() does not seem to exist on object<SilverStripe\Control\HTTPRequest>.

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...
52
        $this->controller->pushCurrent();
53
        $form = $this->form = $this->controller->Form();
54
        Injector::inst()->registerService($form, MultiForm::class);
55
        $this->form =  $form;
56
    }
57
58
    public function testInitialisingForm()
59
    {
60
        $this->assertTrue(is_numeric($this->form->getCurrentStep()->ID) && ($this->form->getCurrentStep()->ID > 0));
61
        $this->assertTrue(
62
            is_numeric($this->form->getMultiFormSession()->ID)
63
            && ($this->form->getMultiFormSession()->ID > 0)
64
        );
65
        $this->assertEquals(MultiFormTestStepOne::class, $this->form->getStartStep());
66
    }
67
68
    public function testSessionGeneration()
69
    {
70
        $this->assertTrue($this->form->getMultiFormSession()->ID > 0);
71
    }
72
73
    public function testMemberLogging()
74
    {
75
        // Grab any user to fake being logged in as, and ensure that after a session is written it has
76
        // that user as the submitter.
77
78
        $userId = $this->logInWithPermission('ADMIN');
79
80
        $session = $this->form->getMultiFormSession();
81
        $session->write();
82
83
        $this->assertEquals($userId, $session->SubmitterID);
84
    }
85
86
    public function testSecondStep()
87
    {
88
        $this->assertEquals(MultiFormTestStepTwo::class, $this->form->getCurrentStep()->getNextStep());
89
    }
90
91
    public function testParentForm()
92
    {
93
        $currentStep = $this->form->getCurrentStep();
94
        $this->assertEquals($currentStep->getForm()->class, $this->form->class);
95
    }
96
97
    public function testTotalStepCount()
98
    {
99
        $this->assertEquals(3, $this->form->getAllStepsLinear()->Count());
100
    }
101
102
    public function testCompletedSession()
103
    {
104
        $this->form->setCurrentSessionHash($this->form->getMultiFormSession()->Hash);
105
        $this->assertInstanceOf(MultiFormSession::class, $this->form->getCurrentSession());
106
        $this->form->getMultiFormSession()->markCompleted();
107
        $this->assertNull($this->form->getCurrentSession());
108
    }
109
110
    public function testIncorrectSessionIdentifier()
111
    {
112
        $this->form->setCurrentSessionHash('sdfsdf3432325325sfsdfdf'); // made up!
113
114
        // A new session is generated, even though we made up the identifier
115
        $this->assertInstanceOf(MultiFormSession::class, $this->form->getMultiFormSession());
116
    }
117
118
    public function testCustomGetVar()
119
    {
120
        Config::modify()->set(MultiForm::class, 'get_var', 'SuperSessionID');
0 ignored issues
show
Bug introduced by
The method modify() does not seem to exist on object<SilverStripe\Core\Config\Config>.

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...
121
122
        $form = $this->controller->Form();
123
        $this->assertContains('SuperSessionID', $form::$ignored_fields, "GET var wasn't added to ignored fields");
124
        $this->assertContains(
125
            'SuperSessionID',
126
            $form->FormAction(),
127
            "Form action doesn't contain correct session ID parameter"
128
        );
129
        $this->assertContains(
130
            'SuperSessionID',
131
            $form->getCurrentStep()->Link(),
132
            "Form step doesn't contain correct session ID parameter"
133
        );
134
    }
135
}
136