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
|
|
|
} |
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() |
|
|
|
|
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() |
|
|
|
|
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(), ''); |
|
|
|
|
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() |
|
|
|
|
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() |
|
|
|
|
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
|
|
|
|
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