You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.5KB

  1. <?php
  2. namespace tests\codeception\frontend\models;
  3. use Yii;
  4. use tests\codeception\frontend\unit\DbTestCase;
  5. use frontend\models\PasswordResetRequestForm;
  6. use tests\codeception\common\fixtures\UserFixture;
  7. use common\models\User;
  8. use Codeception\Specify;
  9. class PasswordResetRequestFormTest extends DbTestCase
  10. {
  11. use Specify;
  12. protected function setUp()
  13. {
  14. parent::setUp();
  15. Yii::$app->mailer->fileTransportCallback = function ($mailer, $message) {
  16. return 'testing_message.eml';
  17. };
  18. }
  19. protected function tearDown()
  20. {
  21. @unlink($this->getMessageFile());
  22. parent::tearDown();
  23. }
  24. public function testSendEmailWrongUser()
  25. {
  26. $this->specify('no user with such email, message should not be send', function () {
  27. $model = new PasswordResetRequestForm();
  28. $model->email = 'not-existing-email@example.com';
  29. expect('email not send', $model->sendEmail())->false();
  30. });
  31. $this->specify('user is not active, message should not be send', function () {
  32. $model = new PasswordResetRequestForm();
  33. $model->email = $this->user[1]['email'];
  34. expect('email not send', $model->sendEmail())->false();
  35. });
  36. }
  37. public function testSendEmailCorrectUser()
  38. {
  39. $model = new PasswordResetRequestForm();
  40. $model->email = $this->user[0]['email'];
  41. $user = User::findOne(['password_reset_token' => $this->user[0]['password_reset_token']]);
  42. expect('email sent', $model->sendEmail())->true();
  43. expect('user has valid token', $user->password_reset_token)->notNull();
  44. $this->specify('message has correct format', function () use ($model) {
  45. expect('message file exists', file_exists($this->getMessageFile()))->true();
  46. $message = file_get_contents($this->getMessageFile());
  47. expect('message "from" is correct', $message)->contains(Yii::$app->params['supportEmail']);
  48. expect('message "to" is correct', $message)->contains($model->email);
  49. });
  50. }
  51. public function fixtures()
  52. {
  53. return [
  54. 'user' => [
  55. 'class' => UserFixture::className(),
  56. 'dataFile' => '@tests/codeception/frontend/unit/fixtures/data/models/user.php'
  57. ],
  58. ];
  59. }
  60. private function getMessageFile()
  61. {
  62. return Yii::getAlias(Yii::$app->mailer->fileTransportPath) . '/testing_message.eml';
  63. }
  64. }