]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/UserBundle/Tests/Mailer/AuthCodeMailerTest.php
Use HTML email for 2FA
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Tests / Mailer / AuthCodeMailerTest.php
CommitLineData
23ff8d36
JB
1<?php
2
3namespace Wallabag\UserBundle\Tests\Mailer;
4
5use Wallabag\UserBundle\Entity\User;
6use Wallabag\UserBundle\Mailer\AuthCodeMailer;
7use Symfony\Component\Translation\Translator;
8use Symfony\Component\Translation\Loader\ArrayLoader;
23ff8d36
JB
9
10/**
11 * @see https://www.pmg.com/blog/integration-testing-swift-mailer/
12 */
13final 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
26class AuthCodeMailerTest extends \PHPUnit_Framework_TestCase
27{
28 protected $mailer;
29 protected $spool;
7ce895bf 30 protected $twig;
23ff8d36
JB
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
7ce895bf
JB
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')));
23ff8d36
JB
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,
7ce895bf 58 $this->twig,
23ff8d36
JB
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());
7ce895bf
JB
71 $this->assertEquals('subject', $msg->getSubject());
72 $this->assertContains('text body', $msg->toString());
73 $this->assertContains('html body', $msg->toString());
23ff8d36
JB
74 }
75}