1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace common\unit\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use Codeception\Specify; |
7
|
|
|
use common\models\User; |
8
|
|
|
|
9
|
|
|
date_default_timezone_set('UTC'); |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* User test |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
class UserTest extends \Codeception\Test\Unit { |
16
|
|
|
use Specify; |
17
|
|
|
|
18
|
|
|
private $user; |
19
|
|
|
private $time; |
20
|
|
|
private $question; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
public $exportData = [ |
24
|
|
|
[ |
25
|
|
|
'id' => 485, |
26
|
|
|
'date' => '2017-07-29 10:40:29', |
27
|
|
|
'behavior_id' => 59, |
28
|
|
|
'question1' => 'q1', |
29
|
|
|
'question2' => 'q2', |
30
|
|
|
'question3' => 'q3', |
31
|
|
|
'behavior' => [ |
32
|
|
|
'id' => 59, |
33
|
|
|
'name' => 'repetitive, negative thoughts', |
34
|
|
|
], |
35
|
|
|
'category' => [ |
36
|
|
|
'id' => 4, |
37
|
|
|
'name' => 'Speeding Up', |
38
|
|
|
], |
39
|
|
|
], [ |
40
|
|
|
'id' => 487, |
41
|
|
|
'date' => '2017-07-29 10:40:29', |
42
|
|
|
'behavior_id' => 106, |
43
|
|
|
'question1' => 'q1', |
44
|
|
|
'question2' => 'q2', |
45
|
|
|
'question3' => 'q3', |
46
|
|
|
'behavior' => [ |
47
|
|
|
'id' => 106, |
48
|
|
|
'name' => 'tired', |
49
|
|
|
], |
50
|
|
|
'category' => [ |
51
|
|
|
'id' => 6, |
52
|
|
|
'name' => 'Exhausted', |
53
|
|
|
], |
54
|
|
|
], [ |
55
|
|
|
'id' => 488, |
56
|
|
|
'date' => '2017-07-29 10:40:29', |
57
|
|
|
'behavior_id' => 125, |
58
|
|
|
'question1' => 'q1', |
59
|
|
|
'question2' => 'q2', |
60
|
|
|
'question3' => 'q3', |
61
|
|
|
'behavior' => [ |
62
|
|
|
'id' => 125, |
63
|
|
|
'name' => 'out of control', |
64
|
|
|
], |
65
|
|
|
'category' => [ |
66
|
|
|
'id' => 7, |
67
|
|
|
'name' => 'Relapse/Moral Failure', |
68
|
|
|
], |
69
|
|
|
], [ |
70
|
|
|
'id' => 486, |
71
|
|
|
'date' => '2017-07-29 10:40:29', |
72
|
|
|
'behavior_id' => 89, |
73
|
|
|
'question1' => 'q1', |
74
|
|
|
'question2' => 'q2', |
75
|
|
|
'question3' => 'q3', |
76
|
|
|
'behavior' => [ |
77
|
|
|
'id' => 89, |
78
|
|
|
'name' => 'obsessive (stuck) thoughts', |
79
|
|
|
], |
80
|
|
|
'category' => [ |
81
|
|
|
'id' => 5, |
82
|
|
|
'name' => 'Ticked Off', |
83
|
|
|
], |
84
|
|
|
] |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
public function setUp() { |
88
|
|
|
$this->container = new \yii\di\Container; |
|
|
|
|
89
|
|
|
$this->container->set('common\interfaces\UserInterface', '\site\tests\_support\MockUser'); |
90
|
|
|
$this->container->set('common\interfaces\UserBehaviorInterface', '\site\tests\_support\MockUserBehavior'); |
91
|
|
|
$this->container->set('common\interfaces\TimeInterface', function () { |
92
|
|
|
return new \common\components\Time('America/Los_Angeles'); |
93
|
|
|
}); |
94
|
|
|
|
95
|
|
|
$user_behavior = $this->container->get('common\interfaces\UserBehaviorInterface'); |
96
|
|
|
$this->time = $this->container->get('common\interfaces\TimeInterface'); |
97
|
|
|
|
98
|
|
|
$this->question = $this->getMockBuilder('\common\models\Question') |
99
|
|
|
->setMethods(['save', 'attributes']) |
100
|
|
|
->getMock(); |
101
|
|
|
|
102
|
|
|
$this->user = $this->getMockBuilder(User::class) |
103
|
|
|
->setConstructorArgs([$user_behavior, $this->time]) |
104
|
|
|
->setMethods(['save', 'attributes']) |
105
|
|
|
->getMock(); |
106
|
|
|
$this->user->method('save')->willReturn(true); |
107
|
|
|
$this->user->method('attributes')->willReturn([ |
108
|
|
|
'id', |
109
|
|
|
'password_hash', |
110
|
|
|
'password_reset_token', |
111
|
|
|
'verify_email_token', |
112
|
|
|
'change_email_token', |
113
|
|
|
'email', |
114
|
|
|
'auth_key', |
115
|
|
|
'role', |
116
|
|
|
'status', |
117
|
|
|
'created_at', |
118
|
|
|
'updated_at', |
119
|
|
|
'password', |
120
|
|
|
'timezone', |
121
|
|
|
'send_email', |
122
|
|
|
'email_category', |
123
|
|
|
'partner_email1', |
124
|
|
|
'partner_email2', |
125
|
|
|
'partner_email3', |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
parent::setUp(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function tearDown() { |
132
|
|
|
$this->user = null; |
133
|
|
|
parent::tearDown(); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testIsTokenCurrent() { |
137
|
|
|
$this->specify('isTokenCurrent should function correctly', function () { |
138
|
|
|
$good_token = \Yii::$app |
139
|
|
|
->getSecurity() |
140
|
|
|
->generateRandomString() . '_' . time(); |
141
|
|
|
expect('isTokenCurrent should return true if the token is still current/alive', $this->assertTrue($this->user->isTokenCurrent($good_token))); |
|
|
|
|
142
|
|
|
$expire = \Yii::$app->params['user.passwordResetTokenExpire']; |
143
|
|
|
$bad_token = \Yii::$app |
144
|
|
|
->getSecurity() |
145
|
|
|
->generateRandomString() . '_' . (time() - $expire - 1); // subtract the expiration time and a little more from the current time |
146
|
|
|
expect('isTokenCurrent should return false if the token is expired', $this->assertFalse($this->user->isTokenCurrent($bad_token))); |
|
|
|
|
147
|
|
|
}); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testIsTokenConfirmed() { |
151
|
|
|
expect('isTokenConfirmed should return true if the token has been confirmed', $this->assertTrue($this->user->isTokenConfirmed('token123_confirmed'))); |
|
|
|
|
152
|
|
|
expect('isTokenConfirmed should return false if the token has not been confirmed', $this->assertFalse($this->user->isTokenConfirmed('token123'))); |
|
|
|
|
153
|
|
|
expect('isTokenConfirmed should return false if the token has not been confirmed', $this->assertFalse($this->user->isTokenConfirmed('token123_not_blah'))); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testGeneratePasswordResetToken() { |
157
|
|
|
expect('password_reset_token should be null by default', $this->assertNull($this->user->password_reset_token)); |
|
|
|
|
158
|
|
|
$this->user->generatePasswordResetToken(); |
159
|
|
|
expect('password_reset_token should now have a verification token set', $this->assertRegExp('/.*_[0-9]+/', $this->user->password_reset_token)); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testGenerateVerifyEmailToken() { |
163
|
|
|
expect('verify_email_token should be null by default', $this->assertNull($this->user->verify_email_token)); |
|
|
|
|
164
|
|
|
$this->user->generateVerifyEmailToken(); |
165
|
|
|
expect('verify_email_token should now have a verification token set', $this->assertRegExp('/.*_[0-9]+/', $this->user->verify_email_token)); |
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function testConfirmVerifyEmailToken() { |
169
|
|
|
$this->user->verify_email_token = 'hello_world'; |
170
|
|
|
$this->user->confirmVerifyEmailToken(); |
171
|
|
|
expect('confirmVerifyEmailToken should append User::CONFIRMED_STRING to the end of the verify_email_token property', $this->assertEquals($this->user->verify_email_token, 'hello_world'.$this->user::CONFIRMED_STRING)); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testIsVerified() { |
175
|
|
|
$this->user->verify_email_token = null; |
176
|
|
|
expect('isVerified should return true if the token is null', $this->assertTrue($this->user->isVerified())); |
|
|
|
|
177
|
|
|
$this->user->verify_email_token = ''; |
178
|
|
|
expect('isVerified should return false if the token is the empty string', $this->assertFalse($this->user->isVerified())); |
|
|
|
|
179
|
|
|
$this->user->verify_email_token = 'this_looks_truthy'; |
180
|
|
|
expect('isVerified should return false if the token is still present', $this->assertFalse($this->user->isVerified())); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testRemoveVerifyEmailToken() { |
184
|
|
|
$this->user->verify_email_token = 'faketoken_1234'; |
185
|
|
|
$this->user->removeVerifyEmailToken(); |
186
|
|
|
expect('removeVerifyEmailToken should set the verify_email_token to be null', $this->assertNull($this->user->verify_email_token)); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testGetIdHash() { |
190
|
|
|
$hash = 'iegYQgiPZUF48kk5bneuPn9_6ZOZhkMEGJ6Y8yICgKc'; |
191
|
|
|
|
192
|
|
|
$this->user->id = 12345; |
193
|
|
|
$this->user->created_at = "2017-12-31 23:59:59"; |
194
|
|
|
expect('getIdHash should return a url-safe string', $this->assertEquals($this->user->getidHash(), $hash)); |
|
|
|
|
195
|
|
|
|
196
|
|
|
$this->user->created_at = "2018-01-01 00:00:00"; |
197
|
|
|
expect('getIdHash should return a DIFFERENT url-safe string for different params', $this->assertNotEquals($this->user->getidHash(), $hash)); |
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function testCleanExportRow() { |
201
|
|
|
// need this for the convertUTCToLocal call |
202
|
|
|
Yii::configure(Yii::$app, [ |
203
|
|
|
'components' => [ |
204
|
|
|
'user' => [ |
205
|
|
|
'class' => 'yii\web\User', |
206
|
|
|
'identityClass' => 'common\tests\unit\FakeUser', |
207
|
|
|
], |
208
|
|
|
], |
209
|
|
|
]); |
210
|
|
|
$identity = new \common\tests\unit\FakeUser(); |
211
|
|
|
$identity->timezone = "America/Los_Angeles"; |
212
|
|
|
// logs in the user |
213
|
|
|
Yii::$app->user->setIdentity($identity); |
|
|
|
|
214
|
|
|
|
215
|
|
|
expect('cleanExportRow should clean and mutate the queried data to be suitable for downloading', $this->assertEquals([ |
|
|
|
|
216
|
|
|
[ |
217
|
|
|
'date' => '2017-07-29 03:40:29', |
218
|
|
|
'behavior' => 'repetitive, negative thoughts', |
219
|
|
|
'category' => 'Speeding Up', |
220
|
|
|
'question1' => 'q1', |
221
|
|
|
'question2' => 'q2', |
222
|
|
|
'question3' => 'q3', |
223
|
|
|
], [ |
224
|
|
|
'date' => '2017-07-29 03:40:29', |
225
|
|
|
'behavior' => 'tired', |
226
|
|
|
'category' => 'Exhausted', |
227
|
|
|
'question1' => 'q1', |
228
|
|
|
'question2' => 'q2', |
229
|
|
|
'question3' => 'q3', |
230
|
|
|
], [ |
231
|
|
|
'date' => '2017-07-29 03:40:29', |
232
|
|
|
'behavior' => 'out of control', |
233
|
|
|
'category' => 'Relapse/Moral Failure', |
234
|
|
|
'question1' => 'q1', |
235
|
|
|
'question2' => 'q2', |
236
|
|
|
'question3' => 'q3', |
237
|
|
|
], [ |
238
|
|
|
'date' => '2017-07-29 03:40:29', |
239
|
|
|
'behavior' => 'obsessive (stuck) thoughts', |
240
|
|
|
'category' => 'Ticked Off', |
241
|
|
|
'question1' => 'q1', |
242
|
|
|
'question2' => 'q2', |
243
|
|
|
'question3' => 'q3', |
244
|
|
|
] |
245
|
|
|
], array_map(function($row) { |
246
|
|
|
return $this->user->cleanExportRow($row); |
247
|
|
|
}, $this->exportData))); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function testGenerateChangeEmailToken() { |
251
|
|
|
expect('change_email_token should be null by default', $this->assertNull($this->user->change_email_token)); |
|
|
|
|
252
|
|
|
$this->user->generateChangeEmailToken(); |
253
|
|
|
expect('change_email_token should now have a verification token set', $this->assertRegExp('/.*_[0-9]+/', $this->user->change_email_token)); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function testRemoveChangeEmailToken() { |
257
|
|
|
$this->user->change_email_token = 'faketoken_1234'; |
258
|
|
|
$this->user->removeChangeEmailToken(); |
259
|
|
|
expect('removeChangeEmailToken should set the change_email_token to be null', $this->assertNull($this->user->change_email_token)); |
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
public function testSendEmailReport() { |
263
|
|
|
$user_behavior = $this->getMockBuilder(\common\models\UserBehavior::class) |
264
|
|
|
->disableOriginalConstructor() |
265
|
|
|
->setMethods(['save', 'attributes', 'getCheckinBreakdown']) |
266
|
|
|
->getMock(); |
267
|
|
|
$expected = require(__DIR__.'/../data/expected_getCheckinBreakdown.php'); |
268
|
|
|
$user_behavior->method('getCheckinBreakdown')->willReturn($expected); |
269
|
|
|
|
270
|
|
|
$user = $this->getMockBuilder(User::class) |
271
|
|
|
->setConstructorArgs([$user_behavior, $this->time]) |
272
|
|
|
->setMethods(['save', 'attributes']) |
273
|
|
|
->getMock(); |
274
|
|
|
$user->method('save')->willReturn(true); |
275
|
|
|
$user->method('attributes')->willReturn([ |
276
|
|
|
'send_email', |
277
|
|
|
'email_category', |
278
|
|
|
]); |
279
|
|
|
|
280
|
|
|
$user->send_email = false; |
|
|
|
|
281
|
|
|
$user->email_category = 6; |
|
|
|
|
282
|
|
|
expect('it should not send any emails if the user has disabled send_email', $this->assertFalse($user->sendEmailReport('2019-01-01'))); |
|
|
|
|
283
|
|
|
expect('it should not send any emails if the user did not select any behaviors for the given day', $this->assertFalse($user->sendEmailReport('2018-01-01'))); |
|
|
|
|
284
|
|
|
expect('it should not send any emails if the user did not select any behaviors for the given day', $this->assertFalse($user->sendEmailReport('2018-03-02'))); |
|
|
|
|
285
|
|
|
expect('it should not send any emails if the user did not meet or exceed their email_category', $this->assertFalse($user->sendEmailReport('2019-03-01'))); |
|
|
|
|
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|