Passed
Push — master ( 05c65b...4375aa )
by Franco
03:27 queued 02:09
created

UserControllerTest::testAcceptForbiddenError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
class UserControllerTest extends FunctionalTest
0 ignored issues
show
Bug introduced by
The type FunctionalTest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
7
    public static $fixture_file = 'UserControllerTest.yml';
8
9
    /**
10
     * @var UserController
11
     */
12
    private $controller;
13
14
    public function setUp()
15
    {
16
        parent::setUp();
17
        $this->autoFollowRedirection = false;
18
        $this->logInWithPermission('ADMIN');
19
        $this->controller = new UserController();
20
    }
21
22
    private function logoutMember()
23
    {
24
        if ($member = Member::currentUser()) {
0 ignored issues
show
Bug introduced by
The type Member was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
            $member->logOut();
26
        }
27
    }
28
29
    /**
30
     * Tests redirected to a login screen if you're logged out.
31
     */
32
    public function testCantAccessWhenLoggedOut()
33
    {
34
        $this->logoutMember();
35
        $response = $this->get($this->controller->Link('index'));
36
        $this->assertFalse($response->isError());
37
        $this->assertEquals(302, $response->getStatusCode());
38
        $this->autoFollowRedirection = true;
39
    }
40
41
    /**
42
     * Tests if a form is returned and that expected fields are present
43
     */
44
    public function testInvitationForm()
45
    {
46
        $form = $this->controller->InvitationForm();
47
        $this->assertInstanceOf('Form', $form);
48
        $this->assertNotNull($form->Fields()->fieldByName('FirstName'));
49
        $this->assertNotNull($form->Fields()->fieldByName('Email'));
50
    }
51
52
    /**
53
     * Tests whether an email is sent and that an invitation record is created
54
     */
55
    public function testSendInvite()
56
    {
57
        /** @var Form $form */
58
        $data = array(
59
            'FirstName' => 'Joe',
60
            'Email' => '[email protected]'
61
        );
62
        $response = $this->controller->sendInvite($data, $this->controller->InvitationForm()->loadDataFrom($data));
63
        $invitation = UserInvitation::get()->filter('Email', '[email protected]');
64
        $this->assertEquals(1, $invitation->count());
65
        /** @var UserInvitation $invitation */
66
        $joe = $invitation->first();
67
        $this->assertEquals('Joe', $joe->FirstName);
68
        $this->assertEquals('[email protected]', $joe->Email);
69
        $this->assertEquals(302, $response->getStatusCode());
70
    }
71
72
    /**
73
     * Tests for 403 if no ID parameter given
74
     */
75
    public function testAcceptForbiddenError()
76
    {
77
        $response = $this->get($this->controller->Link('accept'));
78
        $this->assertEquals(403, $response->getStatusCode());
79
    }
80
81
    /**
82
     * Tests for expired TempHash and that it redirects to the expired page
83
     */
84 View Code Duplication
    public function testAcceptExpiredTempHash()
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...
85
    {
86
        /** @var UserInvitation $invitation */
87
        $invitation = $this->objFromFixture('UserInvitation', 'expired');
88
        $response = $this->get($this->controller->Link('accept/' . $invitation->TempHash));
89
        $this->assertEquals(302, $response->getStatusCode());
90
        $this->assertEquals('/user/expired', $response->getHeader('Location'));
91
    }
92
93
    /**
94
     * Tests if a form is returned and that expected fields are present
95
     */
96
    public function testAcceptForm()
97
    {
98
        $form = $this->controller->AcceptForm();
99
        $this->assertInstanceOf('Form', $form);
100
        $this->assertNotNull($form->Fields()->fieldByName('FirstName'));
101
        $this->assertNull($form->Fields()->fieldByName('Email'));
102
        $this->assertNotNull($form->Fields()->fieldByName('HashID'));
103
        $this->assertNotNull($form->Fields()->fieldByName('Surname'));
104
    }
105
106
    /**
107
     * Tests that redirected to not found if has not found
108
     */
109 View Code Duplication
    public function testSaveInviteWrongHashError()
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...
110
    {
111
        $data = array(
112
            'HashID' => '432'
113
        );
114
        $response = $this->controller->saveInvite($data, $this->controller->AcceptForm()->loadDataFrom($data));
115
        $this->assertEquals(302, $response->getStatusCode());
116
        $this->assertEquals('/user/notfound', $response->getHeader('Location'));
117
    }
118
119
    public function testSaveInvite()
120
    {
121
        /** @var UserInvitation $invitation */
122
        $invitation = $this->objFromFixture('UserInvitation', 'joe');
123
        $data = array(
124
            'HashID' => $invitation->TempHash,
125
            'FirstName' => $invitation->FirstName,
126
            'Surname' => 'Soap',
127
            'Password' => array(
128
                '_Password' => 'password',
129
                '_ConfirmPassword' => 'password'
130
            )
131
        );
132
133
        $response = $this->controller->saveInvite($data, $this->controller->AcceptForm()->loadDataFrom($data));
134
        $this->assertEquals(302, $response->getStatusCode());
135
        $this->assertEquals('/user/success', $response->getHeader('Location'));
136
137
        // Assert that invitation is deleted
138
        $this->assertNull(UserInvitation::get()->filter('Email', $invitation->Email)->first());
139
140
        // Assert that member is created
141
        $this->assertTrue(Member::get()->filter('Email', $invitation->Email)->first()->exists());
142
    }
143
144
    /**
145
     * Tests that a login link is presented to the user
146
     */
147
    public function testSuccess()
148
    {
149
        $this->logoutMember();
150
        $response = $this->get($this->controller->Link('success'));
151
        $body = Convert::nl2os($response->getBody(), '');
0 ignored issues
show
Bug introduced by
The type Convert was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
152
        $this->assertContains('Congratulations!', $body);
153
        $this->assertContains('You are now registered member', $body);
154
        $baseURL = Director::absoluteBaseURL();
155
        $this->assertContains("<a href=\"{$baseURL}//Security/login?BackURL=/\">", $body);
156
    }
157
158
    /**
159
     * Tests that the expired action is shown
160
     */
161 View Code Duplication
    public function testExpired()
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...
162
    {
163
        $this->logoutMember();
164
        $response = $this->get($this->controller->Link('expired'));
165
        $body = Convert::nl2os($response->getBody(), '');
166
        $this->assertContains('Invitation expired', $body);
167
        $this->assertContains('Oops, you took too long to accept this invitation', $body);
168
    }
169
170
    /**
171
     * Tests that the notfound action is shown.
172
     */
173 View Code Duplication
    public function testNotFound()
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...
174
    {
175
        $this->logoutMember();
176
        $response = $this->get($this->controller->Link('notfound'));
177
        $body = Convert::nl2os($response->getBody(), '');
178
        $this->assertContains('Invitation not found', $body);
179
        $this->assertContains('Oops, the invitation ID was not found.', $body);
180
    }
181
182
    /**
183
     * Tests whether links are correctly re-written.
184
     */
185
    public function testLink()
186
    {
187
        $this->assertEquals('user/accept', $this->controller->Link('accept'));
188
        $this->assertEquals('user/index', $this->controller->Link('index'));
189
    }
190
}
191