Completed
Push — master ( ef93a4...8c28c3 )
by Franco
12s
created

UserControllerTest::testGroupFieldNotRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 15
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
    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;
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 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
        $this->assertNotNull($form->Fields()->fieldByName('Groups'));
51
    }
52
53
    /**
54
     * Tests whether an email is sent and that an invitation record is created
55
     */
56
    public function testSendInvite()
57
    {
58
        $this->loginAsJane();
59
        /** @var Form $form */
60
        $data = array(
61
            'FirstName' => 'Joe',
62
            'Email' => '[email protected]',
63
            'Groups' => array('test1', 'test2')
64
        );
65
        $response = $this->controller->sendInvite($data, $this->controller->InvitationForm()->loadDataFrom($data));
66
        $invitation = UserInvitation::get()->filter('Email', $data['Email']);
67
        $this->assertCount(1, $invitation);
68
        /** @var UserInvitation $invitation */
69
        $joe = $invitation->first();
70
        $this->assertEquals('Joe', $joe->FirstName);
71
        $this->assertEquals('[email protected]', $joe->Email);
72
        $this->assertEquals('test1,test2', $joe->Groups);
73
        $this->assertEquals(302, $response->getStatusCode());
74
    }
75
76
    /**
77
     * Tests whether switching the UserInvitation::force_require_group has an effect or not
78
     */
79
    public function testGroupFieldNotRequired()
80
    {
81
        $this->loginAsJane();
82
        $data = array(
83
            'FirstName' => 'Joe',
84
            'Email' => '[email protected]',
85
            'Groups' => array('test1', 'test2')
86
        );
87
        $form = $this->controller->InvitationForm()->loadDataFrom($data);
88
        $this->assertTrue($form->validate());
89
90
        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...
91
        unset($data['Groups']);
92
        $form = $this->controller->InvitationForm()->loadDataFrom($data);
93
        $this->assertFalse($form->validate());
94
    }
95
96
    private function loginAsJane()
97
    {
98
        $this->logInAs($this->objFromFixture('Member', 'jane'));
99
    }
100
101
    /**
102
     * Tests for 403 if no ID parameter given
103
     */
104
    public function testAcceptForbiddenError()
105
    {
106
        $response = $this->get($this->controller->Link('accept'));
107
        $this->assertEquals(403, $response->getStatusCode());
108
    }
109
110
    /**
111
     * Tests for expired TempHash and that it redirects to the expired page
112
     */
113 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...
114
    {
115
        /** @var UserInvitation $invitation */
116
        $invitation = $this->objFromFixture('UserInvitation', 'expired');
117
        $response = $this->get($this->controller->Link('accept/' . $invitation->TempHash));
118
        $this->assertEquals(302, $response->getStatusCode());
119
        $this->assertEquals('/user/expired', $response->getHeader('Location'));
120
    }
121
122
    /**
123
     * Tests if a form is returned and that expected fields are present
124
     */
125
    public function testAcceptForm()
126
    {
127
        $form = $this->controller->AcceptForm();
128
        $this->assertInstanceOf('Form', $form);
129
        $this->assertNotNull($form->Fields()->fieldByName('FirstName'));
130
        $this->assertNull($form->Fields()->fieldByName('Email'));
131
        $this->assertNotNull($form->Fields()->fieldByName('HashID'));
132
        $this->assertNotNull($form->Fields()->fieldByName('Surname'));
133
    }
134
135
    /**
136
     * Tests that redirected to not found if has not found
137
     */
138 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...
139
    {
140
        $data = array(
141
            'HashID' => '432'
142
        );
143
        $response = $this->controller->saveInvite($data, $this->controller->AcceptForm()->loadDataFrom($data));
144
        $this->assertEquals(302, $response->getStatusCode());
145
        $this->assertEquals('/user/notfound', $response->getHeader('Location'));
146
    }
147
148
    public function testSaveInvite()
149
    {
150
        /** @var UserInvitation $invitation */
151
        $invitation = $this->objFromFixture('UserInvitation', 'joe');
152
        $data = array(
153
            'HashID' => $invitation->TempHash,
154
            'FirstName' => $invitation->FirstName,
155
            'Surname' => 'Soap',
156
            'Password' => array(
157
                '_Password' => 'password',
158
                '_ConfirmPassword' => 'password'
159
            )
160
        );
161
162
        $response = $this->controller->saveInvite($data, $this->controller->AcceptForm()->loadDataFrom($data));
163
        $this->assertEquals(302, $response->getStatusCode());
164
        $this->assertEquals('/user/success', $response->getHeader('Location'));
165
166
        // Assert that invitation is deleted
167
        $this->assertNull(UserInvitation::get()->filter('Email', $invitation->Email)->first());
168
169
        /** @var Member $joe */
170
        $joe = Member::get()->filter('Email', $invitation->Email)->first();
171
        // Assert that member is created
172
        $this->assertTrue($joe->exists());
173
174
        // Assert that member belongs to the groups selected
175
        $this->assertTrue($joe->inGroup($this->objFromFixture('Group', 'test1')));
176
        $this->assertTrue($joe->inGroup($this->objFromFixture('Group', 'test2')));
177
    }
178
179
    /**
180
     * Tests that a login link is presented to the user
181
     */
182
    public function testSuccess()
183
    {
184
        $this->logoutMember();
185
        $response = $this->get($this->controller->Link('success'));
186
        $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...
187
        $this->assertContains('Congratulations!', $body);
188
        $this->assertContains('You are now registered member', $body);
189
        $baseURL = Director::absoluteBaseURL();
190
        $this->assertContains("<a href=\"{$baseURL}//Security/login?BackURL=/\">", $body);
191
    }
192
193
    /**
194
     * Tests that the expired action is shown
195
     */
196 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...
197
    {
198
        $this->logoutMember();
199
        $response = $this->get($this->controller->Link('expired'));
200
        $body = Convert::nl2os($response->getBody(), '');
201
        $this->assertContains('Invitation expired', $body);
202
        $this->assertContains('Oops, you took too long to accept this invitation', $body);
203
    }
204
205
    /**
206
     * Tests that the notfound action is shown.
207
     */
208 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...
209
    {
210
        $this->logoutMember();
211
        $response = $this->get($this->controller->Link('notfound'));
212
        $body = Convert::nl2os($response->getBody(), '');
213
        $this->assertContains('Invitation not found', $body);
214
        $this->assertContains('Oops, the invitation ID was not found.', $body);
215
    }
216
217
    /**
218
     * Tests whether links are correctly re-written.
219
     */
220
    public function testLink()
221
    {
222
        $this->assertEquals('user/accept', $this->controller->Link('accept'));
223
        $this->assertEquals('user/index', $this->controller->Link('index'));
224
    }
225
}
226