Completed
Push — master ( 68ea64...ebf220 )
by Franco
17s queued 11s
created

UserControllerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
rs 9.4285
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;
0 ignored issues
show
Bug Best Practice introduced by
The property autoFollowRedirection does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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 View Code Duplication
    public function testCantAccessWhenLoggedOut()
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...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property autoFollowRedirection does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39
    }
40
41
    /**
42
     * Tests that you can't access the index action without the requisite permission
43
     */
44 View Code Duplication
    public function testCantAccessWithoutPermission()
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...
45
    {
46
        $this->loginInAsSomeone('john');
47
        $response = $this->get($this->controller->Link('index'));
48
        $this->assertEquals(403, $response->getStatusCode());
49
        $this->autoFollowRedirection = true;
0 ignored issues
show
Bug Best Practice introduced by
The property autoFollowRedirection does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
    }
51
52
    /**
53
     * Tests if a form is returned and that expected fields are present
54
     */
55
    public function testInvitationForm()
56
    {
57
        $form = $this->controller->InvitationForm();
58
        $this->assertInstanceOf('Form', $form);
59
        $this->assertNotNull($form->Fields()->fieldByName('FirstName'));
60
        $this->assertNotNull($form->Fields()->fieldByName('Email'));
61
        $this->assertNotNull($form->Fields()->fieldByName('Groups'));
62
    }
63
64
    /**
65
     * Tests whether an email is sent and that an invitation record is created
66
     */
67
    public function testSendInvite()
68
    {
69
        $this->loginInAsSomeone('jane');
70
        /** @var Form $form */
71
        $data = $this->invitationData();
72
        $response = $this->controller->sendInvite($data, $this->controller->InvitationForm()->loadDataFrom($data));
73
        $invitation = UserInvitation::get()->filter('Email', $data['Email']);
74
        $this->assertCount(1, $invitation);
75
        /** @var UserInvitation $invitation */
76
        $joe = $invitation->first();
77
        $this->assertEquals('Joe', $joe->FirstName);
78
        $this->assertEquals('[email protected]', $joe->Email);
79
        $this->assertEquals('test1,test2', $joe->Groups);
80
        $this->assertEquals(302, $response->getStatusCode());
81
    }
82
83
    public function invitationData()
84
    {
85
        return array(
86
            'FirstName' => 'Joe',
87
            'Email' => '[email protected]',
88
            'Groups' => array('test1', 'test2')
89
        );
90
    }
91
92
    /**
93
     * Tests that no invitation gets sent out without requisite permissions.
94
     */
95
    public function testSendInvitePermissionDenied()
96
    {
97
        $this->loginInAsSomeone('john');
98
        /** @var Form $form */
99
        $data = $this->invitationData();
100
        $response = $this->controller->sendInvite($data, $this->controller->InvitationForm()->loadDataFrom($data));
101
        $invitation = UserInvitation::get()->filter('Email', $data['Email']);
102
        $this->assertCount(0, $invitation);
103
        $this->assertEquals(302, $response->getStatusCode());
104
    }
105
106
    /**
107
     * Tests whether switching the UserInvitation::force_require_group has an effect or not
108
     */
109
    public function testGroupFieldNotRequired()
110
    {
111
        $this->loginInAsSomeone('jane');
112
        $data = array(
113
            'FirstName' => 'Joe',
114
            'Email' => '[email protected]',
115
            'Groups' => array('test1', 'test2')
116
        );
117
        $form = $this->controller->InvitationForm()->loadDataFrom($data);
118
        $this->assertTrue($form->validate());
119
120
        Config::inst()->update('UserInvitation', 'force_require_group', true);
0 ignored issues
show
Bug introduced by
The type Config 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...
121
        unset($data['Groups']);
122
        $form = $this->controller->InvitationForm()->loadDataFrom($data);
123
        $this->assertFalse($form->validate());
124
    }
125
126
    private function loginInAsSomeone($name)
127
    {
128
        /** @var Member $member */
129
        $member = $this->objFromFixture('Member', $name);
130
        $this->logInAs($member);
131
    }
132
133
    /**
134
     * Tests for 403 if no ID parameter given
135
     */
136
    public function testAcceptForbiddenError()
137
    {
138
        $response = $this->get($this->controller->Link('accept'));
139
        $this->assertEquals(403, $response->getStatusCode());
140
    }
141
142
    /**
143
     * Tests for expired TempHash and that it redirects to the expired page
144
     */
145 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...
146
    {
147
        /** @var UserInvitation $invitation */
148
        $invitation = $this->objFromFixture('UserInvitation', 'expired');
149
        $response = $this->get($this->controller->Link('accept/' . $invitation->TempHash));
150
        $this->assertEquals(302, $response->getStatusCode());
151
        $this->assertEquals('/user/expired', $response->getHeader('Location'));
152
    }
153
154
    /**
155
     * Tests if a form is returned and that expected fields are present
156
     */
157
    public function testAcceptForm()
158
    {
159
        $form = $this->controller->AcceptForm();
160
        $this->assertInstanceOf('Form', $form);
161
        $this->assertNotNull($form->Fields()->fieldByName('FirstName'));
162
        $this->assertNull($form->Fields()->fieldByName('Email'));
163
        $this->assertNotNull($form->Fields()->fieldByName('HashID'));
164
        $this->assertNotNull($form->Fields()->fieldByName('Surname'));
165
    }
166
167
    /**
168
     * Tests that redirected to not found if has not found
169
     */
170 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...
171
    {
172
        $data = array(
173
            'HashID' => '432'
174
        );
175
        $response = $this->controller->saveInvite($data, $this->controller->AcceptForm()->loadDataFrom($data));
176
        $this->assertEquals(302, $response->getStatusCode());
177
        $this->assertEquals('/user/notfound', $response->getHeader('Location'));
178
    }
179
180
    public function testSaveInvite()
181
    {
182
        /** @var UserInvitation $invitation */
183
        $invitation = $this->objFromFixture('UserInvitation', 'joe');
184
        $data = array(
185
            'HashID' => $invitation->TempHash,
186
            'FirstName' => $invitation->FirstName,
187
            'Surname' => 'Soap',
188
            'Password' => array(
189
                '_Password' => 'password',
190
                '_ConfirmPassword' => 'password'
191
            )
192
        );
193
194
        $response = $this->controller->saveInvite($data, $this->controller->AcceptForm()->loadDataFrom($data));
195
        $this->assertEquals(302, $response->getStatusCode());
196
        $this->assertEquals('/user/success', $response->getHeader('Location'));
197
198
        // Assert that invitation is deleted
199
        $this->assertNull(UserInvitation::get()->filter('Email', $invitation->Email)->first());
200
201
        /** @var Member $joe */
202
        $joe = Member::get()->filter('Email', $invitation->Email)->first();
203
        // Assert that member is created
204
        $this->assertTrue($joe->exists());
205
206
        // Assert that member belongs to the groups selected
207
        $this->assertTrue($joe->inGroup($this->objFromFixture('Group', 'test1')));
208
        $this->assertTrue($joe->inGroup($this->objFromFixture('Group', 'test2')));
209
    }
210
211
    /**
212
     * Tests that a login link is presented to the user
213
     */
214
    public function testSuccess()
215
    {
216
        $this->logoutMember();
217
        $response = $this->get($this->controller->Link('success'));
218
        $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...
219
        $this->assertContains('Congratulations!', $body);
220
        $this->assertContains('You are now registered member', $body);
221
        $baseURL = Director::absoluteBaseURL();
222
        $this->assertContains("<a href=\"{$baseURL}//Security/login?BackURL=/\">", $body);
223
    }
224
225
    /**
226
     * Tests that the expired action is shown
227
     */
228 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...
229
    {
230
        $this->logoutMember();
231
        $response = $this->get($this->controller->Link('expired'));
232
        $body = Convert::nl2os($response->getBody(), '');
233
        $this->assertContains('Invitation expired', $body);
234
        $this->assertContains('Oops, you took too long to accept this invitation', $body);
235
    }
236
237
    /**
238
     * Tests that the notfound action is shown.
239
     */
240 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...
241
    {
242
        $this->logoutMember();
243
        $response = $this->get($this->controller->Link('notfound'));
244
        $body = Convert::nl2os($response->getBody(), '');
245
        $this->assertContains('Invitation not found', $body);
246
        $this->assertContains('Oops, the invitation ID was not found.', $body);
247
    }
248
249
    /**
250
     * Tests whether links are correctly re-written.
251
     */
252
    public function testLink()
253
    {
254
        $this->assertEquals('user/accept', $this->controller->Link('accept'));
255
        $this->assertEquals('user/index', $this->controller->Link('index'));
256
    }
257
258
    /**
259
     * Tests that the array key is returned
260
     */
261
    public function testProvidePermissions()
262
    {
263
        $permissions = $this->controller->providePermissions();
264
        $this->assertArrayHasKey('ACCESS_USER_INVITATIONS', $permissions);
265
        $this->assertArrayHasKey('name', $permissions['ACCESS_USER_INVITATIONS']);
266
        $this->assertArrayHasKey('category', $permissions['ACCESS_USER_INVITATIONS']);
267
    }
268
}
269