PasswordResetRequestFormTest.php 2.42 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

namespace frontend\tests\unit\models;

use Yii;
use frontend\tests\unit\DbTestCase;
use frontend\models\PasswordResetRequestForm;
use common\tests\fixtures\UserFixture;
use common\models\User;
10
use Codeception\Specify;
11 12 13

class PasswordResetRequestFormTest extends DbTestCase
{
14
    use Specify;
15

16 17 18
    protected function setUp()
    {
        parent::setUp();
19

20
        Yii::$app->mailer->fileTransportCallback = function ($mailer, $message) {
21 22 23
            return 'testing_message.eml';
        };
    }
24

25 26 27
    protected function tearDown()
    {
        @unlink($this->getMessageFile());
28

29 30
        parent::tearDown();
    }
31

32 33 34
    public function testSendEmailWrongUser()
    {
        $this->specify('no user with such email, message should not be send', function () {
35

36 37
            $model = new PasswordResetRequestForm();
            $model->email = 'not-existing-email@example.com';
38

39
            expect('email not send', $model->sendEmail())->false();
40

41
        });
42

43
        $this->specify('user is not active, message should not be send', function () {
44

45 46
            $model = new PasswordResetRequestForm();
            $model->email = $this->user[1]['email'];
47

48
            expect('email not send', $model->sendEmail())->false();
49

50 51
        });
    }
52

53 54 55 56
    public function testSendEmailCorrectUser()
    {
        $model = new PasswordResetRequestForm();
        $model->email = $this->user[0]['email'];
Alexander Makarov committed
57
        $user = User::findOne(['password_reset_token' => $this->user[0]['password_reset_token']]);
58

59 60 61 62
        expect('email sent', $model->sendEmail())->true();
        expect('user has valid token', $user->password_reset_token)->notNull();

        $this->specify('message has correct format', function () use ($model) {
63

64 65 66 67 68
            expect('message file exists', file_exists($this->getMessageFile()))->true();

            $message = file_get_contents($this->getMessageFile());
            expect('message "from" is correct', $message)->contains(Yii::$app->params['supportEmail']);
            expect('message "to" is correct', $message)->contains($model->email);
69

70 71 72 73 74 75 76 77
        });
    }

    public function fixtures()
    {
        return [
            'user' => [
                'class' => UserFixture::className(),
78
                'dataFile' => '@frontend/tests/unit/fixtures/data/models/user.php'
79 80 81 82 83 84
            ],
        ];
    }

    private function getMessageFile()
    {
85
        return Yii::getAlias(Yii::$app->mailer->fileTransportPath) . '/testing_message.eml';
86
    }
87

88
}