ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
NativeMailerHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog\Handler;
13 
15 use Monolog\Logger;
17 
18 function mail($to, $subject, $message, $additional_headers = null, $additional_parameters = null)
19 {
20  $GLOBALS['mail'][] = func_get_args();
21 }
22 
24 {
25  protected function setUp()
26  {
27  $GLOBALS['mail'] = array();
28  }
29 
34  {
35  $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', "receiver@example.org\r\nFrom: faked@attacker.org");
36  }
37 
41  public function testSetterHeaderInjection()
42  {
43  $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
44  $mailer->addHeader("Content-Type: text/html\r\nFrom: faked@attacker.org");
45  }
46 
51  {
52  $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
53  $mailer->addHeader(array("Content-Type: text/html\r\nFrom: faked@attacker.org"));
54  }
55 
60  {
61  $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
62  $mailer->setContentType("text/html\r\nFrom: faked@attacker.org");
63  }
64 
68  public function testSetterEncodingInjection()
69  {
70  $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
71  $mailer->setEncoding("utf-8\r\nFrom: faked@attacker.org");
72  }
73 
74  public function testSend()
75  {
76  $to = 'spammer@example.org';
77  $subject = 'dear victim';
78  $from = 'receiver@example.org';
79 
80  $mailer = new NativeMailerHandler($to, $subject, $from);
81  $mailer->handleBatch(array());
82 
83  // batch is empty, nothing sent
84  $this->assertEmpty($GLOBALS['mail']);
85 
86  // non-empty batch
87  $mailer->handle($this->getRecord(Logger::ERROR, "Foo\nBar\r\n\r\nBaz"));
88  $this->assertNotEmpty($GLOBALS['mail']);
89  $this->assertInternalType('array', $GLOBALS['mail']);
90  $this->assertArrayHasKey('0', $GLOBALS['mail']);
91  $params = $GLOBALS['mail'][0];
92  $this->assertCount(5, $params);
93  $this->assertSame($to, $params[0]);
94  $this->assertSame($subject, $params[1]);
95  $this->assertStringEndsWith(" test.ERROR: Foo Bar Baz [] []\n", $params[2]);
96  $this->assertSame("From: $from\r\nContent-type: text/plain; charset=utf-8\r\n", $params[3]);
97  $this->assertSame('', $params[4]);
98  }
99 
101  {
102  $mailer = new NativeMailerHandler('to@example.org', 'Alert: %level_name% %message%', 'from@example.org');
103  $mailer->handle($this->getRecord(Logger::ERROR, "Foo\nBar\r\n\r\nBaz"));
104  $this->assertNotEmpty($GLOBALS['mail']);
105  $this->assertInternalType('array', $GLOBALS['mail']);
106  $this->assertArrayHasKey('0', $GLOBALS['mail']);
107  $params = $GLOBALS['mail'][0];
108  $this->assertCount(5, $params);
109  $this->assertSame('Alert: ERROR Foo Bar Baz', $params[1]);
110  }
111 }
$params
Definition: disable.php:11
const ERROR
Runtime errors.
Definition: Logger.php:57
testConstructorHeaderInjection()
InvalidArgumentException
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
$from
mail($to, $subject, $message, $additional_headers=null, $additional_parameters=null)
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
NativeMailerHandler uses the mail() function to send the emails.
catch(Exception $e) $message
Create styles array
The data for the language used.
testSetterContentTypeInjection()
InvalidArgumentException
testSetterArrayHeaderInjection()
InvalidArgumentException
testSetterHeaderInjection()
InvalidArgumentException
testSetterEncodingInjection()
InvalidArgumentException