NativeMailerHandlerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Test\TestCase;
  12. use Monolog\Logger;
  13. use InvalidArgumentException;
  14. function mail($to, $subject, $message, $additional_headers = null, $additional_parameters = null)
  15. {
  16. $GLOBALS['mail'][] = func_get_args();
  17. }
  18. class NativeMailerHandlerTest extends TestCase
  19. {
  20. protected function setUp()
  21. {
  22. $GLOBALS['mail'] = [];
  23. }
  24. /**
  25. * @expectedException InvalidArgumentException
  26. */
  27. public function testConstructorHeaderInjection()
  28. {
  29. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', "receiver@example.org\r\nFrom: faked@attacker.org");
  30. }
  31. /**
  32. * @expectedException InvalidArgumentException
  33. */
  34. public function testSetterHeaderInjection()
  35. {
  36. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  37. $mailer->addHeader("Content-Type: text/html\r\nFrom: faked@attacker.org");
  38. }
  39. /**
  40. * @expectedException InvalidArgumentException
  41. */
  42. public function testSetterArrayHeaderInjection()
  43. {
  44. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  45. $mailer->addHeader(["Content-Type: text/html\r\nFrom: faked@attacker.org"]);
  46. }
  47. /**
  48. * @expectedException InvalidArgumentException
  49. */
  50. public function testSetterContentTypeInjection()
  51. {
  52. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  53. $mailer->setContentType("text/html\r\nFrom: faked@attacker.org");
  54. }
  55. /**
  56. * @expectedException InvalidArgumentException
  57. */
  58. public function testSetterEncodingInjection()
  59. {
  60. $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
  61. $mailer->setEncoding("utf-8\r\nFrom: faked@attacker.org");
  62. }
  63. public function testSend()
  64. {
  65. $to = 'spammer@example.org';
  66. $subject = 'dear victim';
  67. $from = 'receiver@example.org';
  68. $mailer = new NativeMailerHandler($to, $subject, $from);
  69. $mailer->setFormatter(new \Monolog\Formatter\LineFormatter);
  70. $mailer->handleBatch([]);
  71. // batch is empty, nothing sent
  72. $this->assertEmpty($GLOBALS['mail']);
  73. // non-empty batch
  74. $mailer->handle($this->getRecord(Logger::ERROR, "Foo\nBar\r\n\r\nBaz"));
  75. $this->assertNotEmpty($GLOBALS['mail']);
  76. $this->assertInternalType('array', $GLOBALS['mail']);
  77. $this->assertArrayHasKey('0', $GLOBALS['mail']);
  78. $params = $GLOBALS['mail'][0];
  79. $this->assertCount(5, $params);
  80. $this->assertSame($to, $params[0]);
  81. $this->assertSame($subject, $params[1]);
  82. $this->assertStringEndsWith(" test.ERROR: Foo Bar Baz [] []\n", $params[2]);
  83. $this->assertSame("From: $from\r\nContent-type: text/plain; charset=utf-8\r\n", $params[3]);
  84. $this->assertSame('', $params[4]);
  85. }
  86. public function testMessageSubjectFormatting()
  87. {
  88. $mailer = new NativeMailerHandler('to@example.org', 'Alert: %level_name% %message%', 'from@example.org');
  89. $mailer->handle($this->getRecord(Logger::ERROR, "Foo\nBar\r\n\r\nBaz"));
  90. $this->assertNotEmpty($GLOBALS['mail']);
  91. $this->assertInternalType('array', $GLOBALS['mail']);
  92. $this->assertArrayHasKey('0', $GLOBALS['mail']);
  93. $params = $GLOBALS['mail'][0];
  94. $this->assertCount(5, $params);
  95. $this->assertSame('Alert: ERROR Foo Bar Baz', $params[1]);
  96. }
  97. }