| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Tests; |
| 14 |
|
| 15 |
use Carbon\Carbon; |
| 16 |
use PHPUnit\Framework\TestCase; |
| 17 |
use Webklex\PHPIMAP\Address; |
| 18 |
use Webklex\PHPIMAP\Attribute; |
| 19 |
use Webklex\PHPIMAP\Config; |
| 20 |
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException; |
| 21 |
use Webklex\PHPIMAP\Header; |
| 22 |
use Webklex\PHPIMAP\IMAP; |
| 23 |
|
| 24 |
class HeaderTest extends TestCase { |
| 25 |
|
| 26 |
|
| 27 |
protected Config $config; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public function setUp(): void { |
| 35 |
$this->config = Config::make(); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function testHeaderParsing(): void { |
| 44 |
$email = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, "messages", "[email protected]"])); |
| 45 |
if (!str_contains($email, "\r\n")) { |
| 46 |
$email = str_replace("\n", "\r\n", $email); |
| 47 |
} |
| 48 |
|
| 49 |
$raw_header = substr($email, 0, strpos($email, "\r\n\r\n")); |
| 50 |
|
| 51 |
$header = new Header($raw_header, $this->config); |
| 52 |
$subject = $header->get("subject"); |
| 53 |
$returnPath = $header->get("return_path"); |
| 54 |
|
| 55 |
$date = $header->get("date")->first(); |
| 56 |
|
| 57 |
$from = $header->get("from")->first(); |
| 58 |
|
| 59 |
$to = $header->get("to")->first(); |
| 60 |
|
| 61 |
self::assertSame($raw_header, $header->raw); |
| 62 |
self::assertSame([ |
| 63 |
0 => 'from mx.domain.tld by localhost with LMTP id SABVMNfGqWP+PAAA0J78UA (envelope-from <[email protected]>) for <[email protected]>; Mon, 26 Dec 2022 17:07:51 +0100', |
| 64 |
1 => 'from localhost (localhost [127.0.0.1]) by mx.domain.tld (Postfix) with ESMTP id C3828140227 for <[email protected]>; Mon, 26 Dec 2022 17:07:51 +0100 (CET)', |
| 65 |
2 => 'from mx.domain.tld ([127.0.0.1]) by localhost (mx.domain.tld [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id JcIS9RuNBTNx for <[email protected]>; Mon, 26 Dec 2022 17:07:21 +0100 (CET)', |
| 66 |
3 => 'from smtp.github.com (out-26.smtp.github.com [192.30.252.209]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx.domain.tld (Postfix) with ESMTPS id 6410B13FEB2 for <[email protected]>; Mon, 26 Dec 2022 17:07:21 +0100 (CET)', |
| 67 |
4 => 'from github-lowworker-891b8d2.va3-iad.github.net (github-lowworker-891b8d2.va3-iad.github.net [10.48.109.104]) by smtp.github.com (Postfix) with ESMTP id 176985E0200 for <[email protected]>; Mon, 26 Dec 2022 08:07:14 -0800 (PST)', |
| 68 |
], $header->get("received")->toArray()); |
| 69 |
self::assertInstanceOf(Attribute::class, $subject); |
| 70 |
self::assertSame("Re: [Webklex/php-imap] Read all folders? (Issue #349)", $subject->toString()); |
| 71 |
self::assertSame("Re: [Webklex/php-imap] Read all folders? (Issue #349)", (string)$header->subject); |
| 72 |
self::assertSame("[email protected]", $returnPath->toString()); |
| 73 |
self::assertSame("return_path", $returnPath->getName()); |
| 74 |
self::assertSame("-4.299", (string)$header->get("X-Spam-Score")); |
| 75 |
self::assertSame("Webklex/php-imap/issues/349/[email protected]", (string)$header->get("Message-ID")); |
| 76 |
self::assertSame(5, $header->get("received")->count()); |
| 77 |
self::assertSame(IMAP::MESSAGE_PRIORITY_UNKNOWN, (int)$header->get("priority")()); |
| 78 |
|
| 79 |
self::assertSame("Username", $from->personal); |
| 80 |
self::assertSame("notifications", $from->mailbox); |
| 81 |
self::assertSame("github.com", $from->host); |
| 82 |
self::assertSame("[email protected]", $from->mail); |
| 83 |
self::assertSame("Username <[email protected]>", $from->full); |
| 84 |
|
| 85 |
self::assertSame("Webklex/php-imap", $to->personal); |
| 86 |
self::assertSame("php-imap", $to->mailbox); |
| 87 |
self::assertSame("noreply.github.com", $to->host); |
| 88 |
self::assertSame("[email protected]", $to->mail); |
| 89 |
self::assertSame("Webklex/php-imap <[email protected]>", $to->full); |
| 90 |
|
| 91 |
self::assertInstanceOf(Carbon::class, $date); |
| 92 |
self::assertSame("2022-12-26 08:07:14 GMT-0800", $date->format("Y-m-d H:i:s T")); |
| 93 |
|
| 94 |
self::assertSame(51, count($header->getAttributes())); |
| 95 |
} |
| 96 |
|
| 97 |
public function testRfc822ParseHeaders() { |
| 98 |
$mock = $this->getMockBuilder(Header::class) |
| 99 |
->disableOriginalConstructor() |
| 100 |
->onlyMethods([]) |
| 101 |
->getMock(); |
| 102 |
|
| 103 |
$config = new \ReflectionProperty($mock, 'options'); |
| 104 |
$config->setAccessible(true); |
| 105 |
$config->setValue($mock, $this->config->get("options")); |
| 106 |
|
| 107 |
$mockHeader = "Content-Type: text/csv; charset=WINDOWS-1252; name*0=\"TH_Is_a_F ile name example 20221013.c\"; name*1=sv\r\nContent-Transfer-Encoding: quoted-printable\r\nContent-Disposition: attachment; filename*0=\"TH_Is_a_F ile name example 20221013.c\"; filename*1=\"sv\"\r\n"; |
| 108 |
|
| 109 |
$expected = new \stdClass(); |
| 110 |
$expected->content_type = 'text/csv; charset=WINDOWS-1252; name*0="TH_Is_a_F ile name example 20221013.c"; name*1=sv'; |
| 111 |
$expected->content_transfer_encoding = 'quoted-printable'; |
| 112 |
$expected->content_disposition = 'attachment; filename*0="TH_Is_a_F ile name example 20221013.c"; filename*1="sv"'; |
| 113 |
|
| 114 |
$this->assertEquals($expected, $mock->rfc822_parse_headers($mockHeader)); |
| 115 |
} |
| 116 |
|
| 117 |
public function testExtractHeaderExtensions() { |
| 118 |
$mock = $this->getMockBuilder(Header::class) |
| 119 |
->disableOriginalConstructor() |
| 120 |
->onlyMethods([]) |
| 121 |
->getMock(); |
| 122 |
|
| 123 |
$method = new \ReflectionMethod($mock, 'extractHeaderExtensions'); |
| 124 |
$method->setAccessible(true); |
| 125 |
|
| 126 |
$mockAttributes = [ |
| 127 |
'content_type' => new Attribute('content_type', 'text/csv; charset=WINDOWS-1252; name*0="TH_Is_a_F ile name example 20221013.c"; name*1=sv'), |
| 128 |
'content_transfer_encoding' => new Attribute('content_transfer_encoding', 'quoted-printable'), |
| 129 |
'content_disposition' => new Attribute('content_disposition', 'attachment; filename*0="TH_Is_a_F ile name example 20221013.c"; filename*1="sv"; attribute_test=attribute_test_value'), |
| 130 |
]; |
| 131 |
|
| 132 |
$attributes = new \ReflectionProperty($mock, 'attributes'); |
| 133 |
$attributes->setAccessible(true); |
| 134 |
$attributes->setValue($mock, $mockAttributes); |
| 135 |
|
| 136 |
$method->invoke($mock); |
| 137 |
|
| 138 |
$this->assertArrayHasKey('filename', $mock->getAttributes()); |
| 139 |
$this->assertArrayNotHasKey('filename*0', $mock->getAttributes()); |
| 140 |
$this->assertEquals('TH_Is_a_F ile name example 20221013.csv', $mock->get('filename')); |
| 141 |
|
| 142 |
$this->assertArrayHasKey('name', $mock->getAttributes()); |
| 143 |
$this->assertArrayNotHasKey('name*0', $mock->getAttributes()); |
| 144 |
$this->assertEquals('TH_Is_a_F ile name example 20221013.csv', $mock->get('name')); |
| 145 |
|
| 146 |
$this->assertArrayHasKey('content_type', $mock->getAttributes()); |
| 147 |
$this->assertEquals('text/csv', $mock->get('content_type')->last()); |
| 148 |
|
| 149 |
$this->assertArrayHasKey('charset', $mock->getAttributes()); |
| 150 |
$this->assertEquals('WINDOWS-1252', $mock->get('charset')->last()); |
| 151 |
|
| 152 |
$this->assertArrayHasKey('content_transfer_encoding', $mock->getAttributes()); |
| 153 |
$this->assertEquals('quoted-printable', $mock->get('content_transfer_encoding')); |
| 154 |
|
| 155 |
$this->assertArrayHasKey('content_disposition', $mock->getAttributes()); |
| 156 |
$this->assertEquals('attachment', $mock->get('content_disposition')->last()); |
| 157 |
$this->assertEquals('quoted-printable', $mock->get('content_transfer_encoding')); |
| 158 |
|
| 159 |
$this->assertArrayHasKey('attribute_test', $mock->getAttributes()); |
| 160 |
$this->assertEquals('attribute_test_value', $mock->get('attribute_test')); |
| 161 |
} |
| 162 |
|
| 163 |
public function testExtractHeaderExtensions2() { |
| 164 |
$mock = $this->getMockBuilder(Header::class) |
| 165 |
->disableOriginalConstructor() |
| 166 |
->onlyMethods([]) |
| 167 |
->getMock(); |
| 168 |
|
| 169 |
$method = new \ReflectionMethod($mock, 'extractHeaderExtensions'); |
| 170 |
$method->setAccessible(true); |
| 171 |
|
| 172 |
$mockAttributes = [ |
| 173 |
'content_type' => new Attribute('content_type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; name="=?utf-8?Q?=D0=A2=D0=B8=D0=BF=D0=BE=D0=B2=D0=BE=D0=B9_?= =?utf-8?Q?=D1=80=D0=B0=D1=81=D1=87=D0=B5=D1=82_=D0=BF?= =?utf-8?Q?=D0=BE=D1=82=D1=80=D0=B5=D0=B1=D0=BB=D0=B5=D0=BD?= =?utf-8?Q?=D0=B8=D1=8F_=D1=8D=D0=BB=D0=B5=D0=BA=D1=82?= =?utf-8?Q?=D1=80=D0=BE=D1=8D=D0=BD=D0=B5=D1=80=D0=B3=D0=B8=D0=B8_=D0=B2_?= =?utf-8?Q?=D0=9A=D0=9F_=D0=97=D0=B2=D0=B5=D0=B7=D0=B4?= =?utf-8?Q?=D0=BD=D1=8B=D0=B9=2Exlsx?="'), |
| 174 |
'content_transfer_encoding' => new Attribute('content_transfer_encoding', 'base64'), |
| 175 |
'content_disposition' => new Attribute('content_disposition', 'attachment; name*0*=utf-8\'\'%D0%A2%D0%B8%D0%BF%D0%BE%D0%B2%D0%BE%D0%B9%20; name*1*=%D1%80%D0%B0%D1%81%D1%87%D0%B5%D1%82%20%D0%BF; name*2*=%D0%BE%D1%82%D1%80%D0%B5%D0%B1%D0%BB%D0%B5%D0%BD; name*3*=%D0%B8%D1%8F%20%D1%8D%D0%BB%D0%B5%D0%BA%D1%82; name*4*=%D1%80%D0%BE%D1%8D%D0%BD%D0%B5%D1%80%D0%B3%D0%B8; name*5*=%D0%B8%20%D0%B2%20%D0%9A%D0%9F%20%D0%97%D0%B2%D0%B5%D0%B7%D0%B4; name*6*=%D0%BD%D1%8B%D0%B9.xlsx; filename*0*=utf-8\'\'%D0%A2%D0%B8%D0%BF%D0%BE%D0%B2%D0%BE%D0%B9%20; filename*1*=%D1%80%D0%B0%D1%81%D1%87%D0%B5%D1%82%20%D0%BF; filename*2*=%D0%BE%D1%82%D1%80%D0%B5%D0%B1%D0%BB%D0%B5%D0%BD; filename*3*=%D0%B8%D1%8F%20%D1%8D%D0%BB%D0%B5%D0%BA%D1%82; filename*4*=%D1%80%D0%BE%D1%8D%D0%BD%D0%B5%D1%80%D0%B3%D0%B8; filename*5*=%D0%B8%20%D0%B2%20%D0%9A%D0%9F%20%D0%97; filename*6*=%D0%B2%D0%B5%D0%B7%D0%B4%D0%BD%D1%8B%D0%B9.xlsx; attribute_test=attribute_test_value'), |
| 176 |
]; |
| 177 |
|
| 178 |
$attributes = new \ReflectionProperty($mock, 'attributes'); |
| 179 |
$attributes->setAccessible(true); |
| 180 |
$attributes->setValue($mock, $mockAttributes); |
| 181 |
|
| 182 |
$method->invoke($mock); |
| 183 |
|
| 184 |
$this->assertArrayHasKey('filename', $mock->getAttributes()); |
| 185 |
$this->assertArrayNotHasKey('filename*0', $mock->getAttributes()); |
| 186 |
$this->assertEquals('utf-8\'\'%D0%A2%D0%B8%D0%BF%D0%BE%D0%B2%D0%BE%D0%B9%20%D1%80%D0%B0%D1%81%D1%87%D0%B5%D1%82%20%D0%BF%D0%BE%D1%82%D1%80%D0%B5%D0%B1%D0%BB%D0%B5%D0%BD%D0%B8%D1%8F%20%D1%8D%D0%BB%D0%B5%D0%BA%D1%82%D1%80%D0%BE%D1%8D%D0%BD%D0%B5%D1%80%D0%B3%D0%B8%D0%B8%20%D0%B2%20%D0%9A%D0%9F%20%D0%97%D0%B2%D0%B5%D0%B7%D0%B4%D0%BD%D1%8B%D0%B9.xlsx', $mock->get('filename')); |
| 187 |
|
| 188 |
$this->assertArrayHasKey('name', $mock->getAttributes()); |
| 189 |
$this->assertArrayNotHasKey('name*0', $mock->getAttributes()); |
| 190 |
$this->assertEquals('=?utf-8?Q?=D0=A2=D0=B8=D0=BF=D0=BE=D0=B2=D0=BE=D0=B9_?= =?utf-8?Q?=D1=80=D0=B0=D1=81=D1=87=D0=B5=D1=82_=D0=BF?= =?utf-8?Q?=D0=BE=D1=82=D1=80=D0=B5=D0=B1=D0=BB=D0=B5=D0=BD?= =?utf-8?Q?=D0=B8=D1=8F_=D1=8D=D0=BB=D0=B5=D0=BA=D1=82?= =?utf-8?Q?=D1=80=D0=BE=D1=8D=D0=BD=D0=B5=D1=80=D0=B3=D0=B8=D0=B8_=D0=B2_?= =?utf-8?Q?=D0=9A=D0=9F_=D0=97=D0=B2=D0=B5=D0=B7=D0=B4?= =?utf-8?Q?=D0=BD=D1=8B=D0=B9=2Exlsx?=', $mock->get('name')); |
| 191 |
|
| 192 |
$this->assertArrayHasKey('content_type', $mock->getAttributes()); |
| 193 |
$this->assertEquals('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', $mock->get('content_type')->last()); |
| 194 |
|
| 195 |
$this->assertArrayHasKey('content_transfer_encoding', $mock->getAttributes()); |
| 196 |
$this->assertEquals('base64', $mock->get('content_transfer_encoding')); |
| 197 |
|
| 198 |
$this->assertArrayHasKey('content_disposition', $mock->getAttributes()); |
| 199 |
$this->assertEquals('attachment', $mock->get('content_disposition')->last()); |
| 200 |
|
| 201 |
$this->assertArrayHasKey('attribute_test', $mock->getAttributes()); |
| 202 |
$this->assertEquals('attribute_test_value', $mock->get('attribute_test')); |
| 203 |
} |
| 204 |
} |