1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class UserControllerTest extends FunctionalTest |
|
|
|
|
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()) { |
|
|
|
|
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
|
|
|
$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); |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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(), ''); |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths