|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace site\tests\unit\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
|
|
7
|
|
|
class SignupFormTest extends \Codeception\Test\Unit |
|
8
|
|
|
{ |
|
9
|
|
|
use \Codeception\Specify; |
|
10
|
|
|
|
|
11
|
|
|
private $vals = [ |
|
12
|
|
|
'password' => 'password', |
|
13
|
|
|
'email' => '[email protected]', |
|
14
|
|
|
'timezone' => "America/Los_Angeles", |
|
15
|
|
|
'send_email' => true, |
|
16
|
|
|
'partner_email1' => '[email protected]', |
|
17
|
|
|
'partner_email2' => '[email protected]', |
|
18
|
|
|
'partner_email3' => '[email protected]', |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
private $whitelist = [ |
|
22
|
|
|
'email', |
|
23
|
|
|
'timezone', |
|
24
|
|
|
'send_email', |
|
25
|
|
|
'partner_email1', |
|
26
|
|
|
'partner_email2', |
|
27
|
|
|
'partner_email3' |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
public function setUp() { |
|
31
|
|
|
// define interfaces for injection |
|
32
|
|
|
$this->container = new \yii\di\Container; |
|
|
|
|
|
|
33
|
|
|
$this->container->set('common\interfaces\UserInterface', '\site\tests\_support\MockUser'); |
|
34
|
|
|
$this->container->set('common\interfaces\UserBehaviorInterface', '\site\tests\_support\MockUserBehavior'); |
|
35
|
|
|
$this->container->set('common\interfaces\TimeInterface', function () { |
|
36
|
|
|
return new \common\components\Time('America/Los_Angeles'); |
|
37
|
|
|
}); |
|
38
|
|
|
|
|
39
|
|
|
// instantiate mock objects |
|
40
|
|
|
$user_behavior = $this->container->get('common\interfaces\UserBehaviorInterface'); |
|
41
|
|
|
$time = $this->container->get('common\interfaces\TimeInterface'); |
|
42
|
|
|
|
|
43
|
|
|
$question = $this->getMockBuilder('\common\models\Question') |
|
44
|
|
|
->setMethods(['save', 'attributes']) |
|
45
|
|
|
->getMock(); |
|
46
|
|
|
|
|
47
|
|
|
$user_methods = ['save', 'attributes', 'findByEmail', 'sendSignupNotificationEmail', 'sendVerifyEmail']; |
|
48
|
|
|
$this->user = $this->getMockBuilder('\common\models\User') |
|
|
|
|
|
|
49
|
|
|
->setConstructorArgs([$user_behavior, $question, $time]) |
|
50
|
|
|
->setMethods($user_methods) |
|
51
|
|
|
->getMock(); |
|
52
|
|
|
$user_attrs = [ |
|
53
|
|
|
'id', |
|
54
|
|
|
'password_hash', |
|
55
|
|
|
'password_reset_token', |
|
56
|
|
|
'verify_email_token', |
|
57
|
|
|
'email', |
|
58
|
|
|
'auth_key', |
|
59
|
|
|
'role', |
|
60
|
|
|
'status', |
|
61
|
|
|
'created_at', |
|
62
|
|
|
'updated_at', |
|
63
|
|
|
'password', |
|
64
|
|
|
'timezone', |
|
65
|
|
|
'send_email', |
|
66
|
|
|
'email_category', |
|
67
|
|
|
'partner_email1', |
|
68
|
|
|
'partner_email2', |
|
69
|
|
|
'partner_email3', |
|
70
|
|
|
]; |
|
71
|
|
|
$this->user->method('attributes')->willReturn($user_attrs); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$this->existing_user = $this->getMockBuilder('\common\models\User') |
|
|
|
|
|
|
74
|
|
|
->setConstructorArgs([$user_behavior, $question, $time]) |
|
75
|
|
|
->setMethods($user_methods) |
|
76
|
|
|
->getMock(); |
|
77
|
|
|
$this->existing_user->method('attributes')->willReturn($user_attrs); |
|
78
|
|
|
|
|
79
|
|
|
$this->existing_user->id = 1; |
|
|
|
|
|
|
80
|
|
|
$this->existing_user->email = "[email protected]"; |
|
|
|
|
|
|
81
|
|
|
$this->existing_user->setIsNewRecord(false); |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
parent::setUp(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function tearDown() { |
|
87
|
|
|
$this->user = null; |
|
|
|
|
|
|
88
|
|
|
parent::tearDown(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testRules() { |
|
92
|
|
|
$form = $this->container->get('\site\models\SignupForm', [$this->user]); |
|
93
|
|
|
|
|
94
|
|
|
$form->attributes = $this->vals; // massive assignment |
|
95
|
|
|
expect('the form should pass validation with good values', $this->assertTrue($form->validate())); |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
$form->attributes = $this->vals; // massive assignment |
|
98
|
|
|
$form->partner_email1 = null; |
|
99
|
|
|
expect('the form should fail validation when send_email is true but no partner_emails are provided', $this->assertFalse($form->validate())); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
$form->attributes = $this->vals; // massive assignment |
|
102
|
|
|
$form->timezone = 'FakePlace/FakeCity'; |
|
103
|
|
|
expect('the form should fail validation when a bad timezone is provided', $this->assertFalse($form->validate())); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testSignupNewUser() { |
|
107
|
|
|
|
|
108
|
|
|
// set up a side effect to occur when the user's save() function is called |
|
109
|
|
|
$saved = false; |
|
110
|
|
|
$this->user->method('save')->will($this->returnCallback(function($runValidation=true, $attributeNames=null) use(&$saved) { |
|
|
|
|
|
|
111
|
|
|
$saved = true; |
|
112
|
|
|
return $this->user; |
|
113
|
|
|
})); |
|
114
|
|
|
|
|
115
|
|
|
$form = $this->getForm(); |
|
116
|
|
|
|
|
117
|
|
|
///////////////////////////////////////////////////////////////////// |
|
118
|
|
|
|
|
119
|
|
|
$this->specify('a brand-new user should be able to sign up', function() use ($form, &$saved) { |
|
120
|
|
|
expect('the form should validate correctly', $this->assertTrue($form->validate())); |
|
|
|
|
|
|
121
|
|
|
$user = $form->signup(); |
|
122
|
|
|
expect('signup() should return a newly-saved user when the user does not already exist with the submitted values', |
|
123
|
|
|
$this->assertEquals($user->getAttributes($this->whitelist), array_splice($this->vals, 1))); |
|
|
|
|
|
|
124
|
|
|
expect('the "Remember Me" auth_key value should be set to a random string (defaults to a 32 char length)', |
|
125
|
|
|
$this->assertEquals(32, strlen($user->auth_key))); |
|
|
|
|
|
|
126
|
|
|
expect('password_hash to be set to something like a hash', |
|
127
|
|
|
$this->assertStringStartsWith('$2y$13$', $user->password_hash)); |
|
|
|
|
|
|
128
|
|
|
expect('when the user is created, signup() should call save()', |
|
129
|
|
|
$this->assertTrue($saved)); |
|
|
|
|
|
|
130
|
|
|
expect('This user should be an isNewRecord -- they should NOT already exist', $this->assertTrue($user->getIsNewRecord())); |
|
|
|
|
|
|
131
|
|
|
}); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function testSignupExistingConfirmedUser() { |
|
135
|
|
|
$saved = false; |
|
136
|
|
|
$this->existing_user->method('save')->will($this->returnCallback(function($runValidation=true, $attributeNames=null) use(&$saved) { |
|
|
|
|
|
|
137
|
|
|
$saved = true; |
|
138
|
|
|
return $this->existing_user; |
|
139
|
|
|
})); |
|
140
|
|
|
|
|
141
|
|
|
$this->user->method('findByEmail')->willReturn($this->existing_user); |
|
142
|
|
|
$this->existing_user->verify_email_token = $this->getValidToken() . "_confirmed"; |
|
143
|
|
|
$form2 = $this->getForm(); |
|
144
|
|
|
|
|
145
|
|
|
$this->specify('an existing and confirmed user should not be signed up', function() use ($form2, &$saved) { |
|
146
|
|
|
expect('the form should validate correctly', $this->assertTrue($form2->validate())); |
|
|
|
|
|
|
147
|
|
|
$user = $form2->signup(); |
|
148
|
|
|
expect('when the user exists and is confirmed, signup() should return null', $this->assertNull($user)); |
|
|
|
|
|
|
149
|
|
|
expect('when the user exists and is confirmed, signup() should NOT call save()', $this->assertFalse($saved)); |
|
|
|
|
|
|
150
|
|
|
}); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function testSignupExistingUnconfirmedUser() { |
|
154
|
|
|
$saved = false; |
|
155
|
|
|
$this->existing_user->method('save')->will($this->returnCallback(function($runValidation=true, $attributeNames=null) use(&$saved) { |
|
|
|
|
|
|
156
|
|
|
$saved = true; |
|
157
|
|
|
return $this->existing_user; |
|
158
|
|
|
})); |
|
159
|
|
|
|
|
160
|
|
|
$this->existing_user->verify_email_token = $this->getExpiredToken(); |
|
161
|
|
|
// make sure our fake token actually _is_ expired |
|
162
|
|
|
expect('token is not confirmed', $this->assertFalse($this->existing_user->isTokenConfirmed())); |
|
|
|
|
|
|
163
|
|
|
expect('token is expired', $this->assertFalse($this->existing_user->isTokenCurrent($this->existing_user->verify_email_token, 'user.verifyAccountTokenExpire'))); |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
$this->user->method('findByEmail')->willReturn($this->existing_user); |
|
166
|
|
|
$form3 = $this->getForm(); |
|
167
|
|
|
|
|
168
|
|
|
$this->specify('an existing and unconfirmed user should be able to sign up again, but their information will be UPDATED', function() use ($form3, &$saved) { |
|
169
|
|
|
expect('the form should validate correctly', $this->assertTrue($form3->validate())); |
|
|
|
|
|
|
170
|
|
|
$user = $form3->signup(); |
|
171
|
|
|
expect('This user should not be an isNewRecord -- they should already exist', $this->assertFalse($user->getIsNewRecord())); |
|
|
|
|
|
|
172
|
|
|
expect('signup() should return a newly-saved user when the user does not already exist with the submitted values', |
|
173
|
|
|
$this->assertEquals($user->getAttributes($this->whitelist), array_splice($this->vals, 1))); |
|
|
|
|
|
|
174
|
|
|
expect('when the user exists and is NOT confirmed, signup() should call save() to UPDATE their info', $this->assertTrue($saved)); |
|
|
|
|
|
|
175
|
|
|
}); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function testSetFields() { |
|
179
|
|
|
$container = new yii\di\Container; |
|
180
|
|
|
$form = $container->get('\site\models\SignupForm', [$this->user]); |
|
181
|
|
|
|
|
182
|
|
|
$form = $this->setAttrs($form, $this->vals); |
|
183
|
|
|
|
|
184
|
|
|
$result = $form->setFields($this->user); |
|
185
|
|
|
$this->specify('populate should set the values of the user model from the form', function() use ($result) { |
|
186
|
|
|
foreach($this->whitelist as $attr) { |
|
187
|
|
|
$this->assertEquals($this->vals[$attr], $result->$attr); |
|
188
|
|
|
} |
|
189
|
|
|
}); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
private function setAttrs($form, Array $vals) { |
|
193
|
|
|
foreach($vals as $key => $val) { |
|
194
|
|
|
$form->$key = $val; |
|
195
|
|
|
} |
|
196
|
|
|
return $form; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
private function getExpiredToken() { |
|
200
|
|
|
// subtract the expiration time and a little more from the current time |
|
201
|
|
|
$expire = \Yii::$app->params['user.verifyAccountTokenExpire']; |
|
202
|
|
|
return \Yii::$app |
|
203
|
|
|
->getSecurity() |
|
204
|
|
|
->generateRandomString() . '_' . (time() - $expire - 1); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
private function getValidToken() { |
|
208
|
|
|
return \Yii::$app |
|
209
|
|
|
->getSecurity() |
|
210
|
|
|
->generateRandomString() . '_' . time(); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
private function getForm($user = null) { |
|
214
|
|
|
if(is_null($user)) $user = $this->user; |
|
215
|
|
|
$form = $this->container->get('\site\models\SignupForm', [$user]); |
|
216
|
|
|
$form->attributes = $this->vals; // massive assignment |
|
217
|
|
|
return $form; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|