]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/monolog/monolog/tests/Monolog/Handler/NativeMailerHandlerTest.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / monolog / monolog / tests / Monolog / Handler / NativeMailerHandlerTest.php
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
14 use Monolog\TestCase;
15 use Monolog\Logger;
16 use InvalidArgumentException;
17
18 function mail($to, $subject, $message, $additional_headers = null, $additional_parameters = null)
19 {
20     $GLOBALS['mail'][] = func_get_args();
21 }
22
23 class NativeMailerHandlerTest extends TestCase
24 {
25     protected function setUp()
26     {
27         $GLOBALS['mail'] = array();
28     }
29
30     /**
31      * @expectedException InvalidArgumentException
32      */
33     public function testConstructorHeaderInjection()
34     {
35         $mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', "receiver@example.org\r\nFrom: faked@attacker.org");
36     }
37
38     /**
39      * @expectedException InvalidArgumentException
40      */
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
47     /**
48      * @expectedException InvalidArgumentException
49      */
50     public function testSetterArrayHeaderInjection()
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
56     /**
57      * @expectedException InvalidArgumentException
58      */
59     public function testSetterContentTypeInjection()
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
65     /**
66      * @expectedException InvalidArgumentException
67      */
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
100     public function testMessageSubjectFormatting()
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 }