|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace tests\codeception\frontend\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use tests\codeception\frontend\unit\DbTestCase; |
|
7
|
|
|
use frontend\models\PasswordResetRequestForm; |
|
8
|
|
|
use tests\codeception\common\fixtures\UserFixture; |
|
9
|
|
|
use common\models\User; |
|
10
|
|
|
use Codeception\Specify; |
|
11
|
|
|
|
|
12
|
|
|
class PasswordResetRequestFormTest extends DbTestCase |
|
13
|
|
|
{ |
|
14
|
|
|
use Specify; |
|
15
|
|
|
|
|
16
|
|
|
protected function setUp() |
|
17
|
|
|
{ |
|
18
|
|
|
parent::setUp(); |
|
19
|
|
|
|
|
20
|
|
|
Yii::$app->mailer->fileTransportCallback = function ($mailer, $message) { |
|
|
|
|
|
|
21
|
|
|
return 'testing_message.eml'; |
|
22
|
|
|
}; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
protected function tearDown() |
|
26
|
|
|
{ |
|
27
|
|
|
@unlink($this->getMessageFile()); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
parent::tearDown(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function testSendEmailWrongUser() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->specify('no user with such email, message should not be sent', function () { |
|
35
|
|
|
|
|
36
|
|
|
$model = new PasswordResetRequestForm(); |
|
37
|
|
|
$model->email = '[email protected]'; |
|
38
|
|
|
|
|
39
|
|
|
expect('email not sent', $model->sendEmail())->false(); |
|
40
|
|
|
|
|
41
|
|
|
}); |
|
42
|
|
|
|
|
43
|
|
|
$this->specify('user is not active, message should not be sent', function () { |
|
44
|
|
|
|
|
45
|
|
|
$model = new PasswordResetRequestForm(); |
|
46
|
|
|
$model->email = $this->user[1]['email']; |
|
47
|
|
|
|
|
48
|
|
|
expect('email not sent', $model->sendEmail())->false(); |
|
49
|
|
|
|
|
50
|
|
|
}); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testSendEmailCorrectUser() |
|
54
|
|
|
{ |
|
55
|
|
|
$model = new PasswordResetRequestForm(); |
|
56
|
|
|
$model->email = $this->user[0]['email']; |
|
57
|
|
|
$user = User::findOne(['password_reset_token' => $this->user[0]['password_reset_token']]); |
|
58
|
|
|
|
|
59
|
|
|
expect('email sent', $model->sendEmail())->true(); |
|
60
|
|
|
expect('user has valid token', $user->password_reset_token)->notNull(); |
|
61
|
|
|
|
|
62
|
|
|
$this->specify('message has correct format', function () use ($model) { |
|
63
|
|
|
|
|
64
|
|
|
expect('message file exists', file_exists($this->getMessageFile()))->true(); |
|
65
|
|
|
|
|
66
|
|
|
$message = file_get_contents($this->getMessageFile()); |
|
67
|
|
|
expect('message "from" is correct', $message)->contains(Yii::$app->params['supportEmail']); |
|
68
|
|
|
expect('message "to" is correct', $message)->contains($model->email); |
|
69
|
|
|
|
|
70
|
|
|
}); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function fixtures() |
|
74
|
|
|
{ |
|
75
|
|
|
return [ |
|
76
|
|
|
'user' => [ |
|
77
|
|
|
'class' => UserFixture::className(), |
|
78
|
|
|
'dataFile' => '@tests/codeception/frontend/unit/fixtures/data/models/user.php' |
|
79
|
|
|
], |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
private function getMessageFile() |
|
84
|
|
|
{ |
|
85
|
|
|
return Yii::getAlias(Yii::$app->mailer->fileTransportPath) . '/testing_message.eml'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.