]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php
Use HTML email for 2FA
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Tests / Mailer / AuthCodeMailerTest.php
1 <?php
2
3 namespace Wallabag\UserBundle\Tests\Mailer;
4
5 use Wallabag\UserBundle\Entity\User;
6 use Wallabag\UserBundle\Mailer\AuthCodeMailer;
7 use Symfony\Component\Translation\Translator;
8 use Symfony\Component\Translation\Loader\ArrayLoader;
9
10 /**
11 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
12 */
13 final class CountableMemorySpool extends \Swift_MemorySpool implements \Countable
14 {
15 public function count()
16 {
17 return count($this->messages);
18 }
19
20 public function getMessages()
21 {
22 return $this->messages;
23 }
24 }
25
26 class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase
27 {
28 protected $mailer;
29 protected $spool;
30 protected $twig;
31
32 protected function setUp()
33 {
34 $this->spool = new CountableMemorySpool();
35 $transport = new \Swift_Transport_SpoolTransport(
36 new \Swift_Events_SimpleEventDispatcher(),
37 $this->spool
38 );
39 $this->mailer = new \Swift_Mailer($transport);
40
41 $this->twig = new \Twig_Environment(new \Twig_Loader_Array(array('@WallabagUserBundle/Resources/views/TwoFactor/email_auth_code.html.twig' => '
42 {% block subject %}subject{% endblock %}
43 {% block body_html %}html body{% endblock %}
44 {% block body_text %}text body{% endblock %}
45 ')));
46 }
47
48 public function testSendEmail()
49 {
50 $user = new User();
51 $user->setTwoFactorAuthentication(true);
52 $user->setEmailAuthCode(666666);
53 $user->setEmail('test@wallabag.io');
54 $user->setName('Bob');
55
56 $authCodeMailer = new AuthCodeMailer(
57 $this->mailer,
58 $this->twig,
59 'nobody@test.io',
60 'wallabag test',
61 'http://0.0.0.0'
62 );
63
64 $authCodeMailer->sendAuthCode($user);
65
66 $this->assertCount(1, $this->spool);
67
68 $msg = $this->spool->getMessages()[0];
69 $this->assertArrayHasKey('test@wallabag.io', $msg->getTo());
70 $this->assertEquals(array('nobody@test.io' => 'wallabag test'), $msg->getFrom());
71 $this->assertEquals('subject', $msg->getSubject());
72 $this->assertContains('text body', $msg->toString());
73 $this->assertContains('html body', $msg->toString());
74 }
75 }