Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/tests/MessageTest.php

raw symbols SCIP
1 <?php
2 /*
3 * File: MessageTest.php
4 * Category: -
5 * Author: M.Goldenbaum
6 * Created: 28.12.22 18:11
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Tests;
14
15 use PHPUnit\Framework\MockObject\MockObject;
16 use PHPUnit\Framework\TestCase;
17 use ReflectionException;
18 use Webklex\PHPIMAP\Attachment;
19 use Webklex\PHPIMAP\Attribute;
20 use Webklex\PHPIMAP\Client;
21 use Webklex\PHPIMAP\Config;
22 use Webklex\PHPIMAP\Connection\Protocols\Response;
23 use Webklex\PHPIMAP\Exceptions\EventNotFoundException;
24 use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException;
25 use Webklex\PHPIMAP\Exceptions\MessageContentFetchingException;
26 use Webklex\PHPIMAP\Exceptions\MessageFlagException;
27 use Webklex\PHPIMAP\Exceptions\MessageNotFoundException;
28 use Webklex\PHPIMAP\Exceptions\MessageSizeFetchingException;
29 use Webklex\PHPIMAP\Exceptions\ResponseException;
30 use Webklex\PHPIMAP\IMAP;
31 use Webklex\PHPIMAP\Message;
32 use Webklex\PHPIMAP\Connection\Protocols\ImapProtocol;
33 use Webklex\PHPIMAP\Exceptions\AuthFailedException;
34 use Webklex\PHPIMAP\Exceptions\ConnectionFailedException;
35 use Webklex\PHPIMAP\Exceptions\ImapBadRequestException;
36 use Webklex\PHPIMAP\Exceptions\ImapServerErrorException;
37 use Webklex\PHPIMAP\Exceptions\MaskNotFoundException;
38 use Webklex\PHPIMAP\Exceptions\RuntimeException;
39
40 class MessageTest extends TestCase {
41
42 /** @var Message $message */
43 protected Message $message;
44
45 /** @var Client $client */
46 protected Client $client;
47
48 /** @var MockObject ImapProtocol mockup */
49 protected MockObject $protocol;
50
51 /**
52 * Setup the test environment.
53 *
54 * @return void
55 */
56 public function setUp(): void {
57 $config = Config::make([
58 "accounts" => [
59 "default" => [
60 'protocol' => 'imap',
61 'encryption' => 'ssl',
62 'username' => '[email protected]',
63 'password' => 'bar',
64 'proxy' => [
65 'socket' => null,
66 'request_fulluri' => false,
67 'username' => null,
68 'password' => null,
69 ],
70 ]]
71 ]);
72 $this->client = new Client($config);
73 }
74
75 /**
76 * Message test
77 *
78 * @return void
79 * @throws AuthFailedException
80 * @throws ConnectionFailedException
81 * @throws EventNotFoundException
82 * @throws ImapBadRequestException
83 * @throws ImapServerErrorException
84 * @throws InvalidMessageDateException
85 * @throws MessageContentFetchingException
86 * @throws MessageFlagException
87 * @throws MessageNotFoundException
88 * @throws MessageSizeFetchingException
89 * @throws ReflectionException
90 * @throws ResponseException
91 * @throws RuntimeException
92 */
93 public function testMessage(): void {
94 $this->createNewProtocolMockup();
95
96 $email = file_get_contents(implode(DIRECTORY_SEPARATOR, [__DIR__, "messages", "[email protected]"]));
97 if(!str_contains($email, "\r\n")){
98 $email = str_replace("\n", "\r\n", $email);
99 }
100
101 $raw_header = substr($email, 0, strpos($email, "\r\n\r\n"));
102 $raw_body = substr($email, strlen($raw_header)+8);
103
104 $this->protocol->expects($this->any())->method('getUid')->willReturn(Response::empty()->setResult(22));
105 $this->protocol->expects($this->any())->method('getMessageNumber')->willReturn(Response::empty()->setResult(21));
106 $this->protocol->expects($this->any())->method('flags')->willReturn(Response::empty()->setResult([22 => [0 => "\\Seen"]]));
107
108 self::assertNotEmpty($this->client->openFolder("INBOX"));
109
110 $message = Message::make(22, null, $this->client, $raw_header, $raw_body, [0 => "\\Seen"], IMAP::ST_UID);
111
112 self::assertInstanceOf(Client::class, $message->getClient());
113 self::assertSame(22, $message->uid);
114 self::assertSame(21, $message->msgn);
115 self::assertContains("Seen", $message->flags()->toArray());
116
117 $subject = $message->get("subject");
118 $returnPath = $message->get("Return-Path");
119
120 self::assertInstanceOf(Attribute::class, $subject);
121 self::assertSame("Re: [Webklex/php-imap] Read all folders? (Issue #349)", $subject->toString());
122 self::assertSame("Re: [Webklex/php-imap] Read all folders? (Issue #349)", (string)$message->subject);
123 self::assertSame("[email protected]", $returnPath->toString());
124 self::assertSame("return_path", $returnPath->getName());
125 self::assertSame("-4.299", (string)$message->get("X-Spam-Score"));
126 self::assertSame("Webklex/php-imap/issues/349/[email protected]", (string)$message->get("Message-ID"));
127 self::assertSame(5, $message->get("received")->count());
128 self::assertSame(IMAP::MESSAGE_PRIORITY_UNKNOWN, (int)$message->get("priority")());
129 }
130
131 /**
132 * Test getMessageNumber
133 *
134 * @return void
135 * @throws AuthFailedException
136 * @throws ConnectionFailedException
137 * @throws ImapBadRequestException
138 * @throws ImapServerErrorException
139 * @throws MessageNotFoundException
140 * @throws ResponseException
141 * @throws RuntimeException
142 */
143 public function testGetMessageNumber(): void {
144 $this->createNewProtocolMockup();
145 $this->protocol->expects($this->any())->method('getMessageNumber')->willReturn(Response::empty()->setResult(""));
146
147 self::assertNotEmpty($this->client->openFolder("INBOX"));
148
149 try {
150 $this->client->getConnection()->getMessageNumber(21)->validatedData();
151 $this->fail("Message number should not exist");
152 } catch (ResponseException $e) {
153 self::assertTrue(true);
154 }
155
156 }
157
158 /**
159 * Test loadMessageFromFile
160 *
161 * @return void
162 * @throws AuthFailedException
163 * @throws ConnectionFailedException
164 * @throws ImapBadRequestException
165 * @throws ImapServerErrorException
166 * @throws InvalidMessageDateException
167 * @throws MaskNotFoundException
168 * @throws MessageContentFetchingException
169 * @throws MessageNotFoundException
170 * @throws ReflectionException
171 * @throws ResponseException
172 * @throws RuntimeException
173 * @throws MessageSizeFetchingException
174 */
175 public function testLoadMessageFromFile(): void {
176 $filename = implode(DIRECTORY_SEPARATOR, [__DIR__, "messages", "[email protected]"]);
177 $message = Message::fromFile($filename);
178
179 $subject = $message->get("subject");
180 $returnPath = $message->get("Return-Path");
181
182 self::assertInstanceOf(Attribute::class, $subject);
183 self::assertSame("Re: [Webklex/php-imap] Read all folders? (Issue #349)", $subject->toString());
184 self::assertSame("Re: [Webklex/php-imap] Read all folders? (Issue #349)", (string)$message->subject);
185 self::assertSame("[email protected]", $returnPath->toString());
186 self::assertSame("return_path", $returnPath->getName());
187 self::assertSame("-4.299", (string)$message->get("X-Spam-Score"));
188 self::assertSame("Webklex/php-imap/issues/349/[email protected]", (string)$message->get("Message-ID"));
189 self::assertSame(5, $message->get("received")->count());
190 self::assertSame(IMAP::MESSAGE_PRIORITY_UNKNOWN, (int)$message->get("priority")());
191
192 self::assertNull($message->getClient());
193 self::assertSame(0, $message->uid);
194
195 $filename = implode(DIRECTORY_SEPARATOR, [__DIR__, "messages", "example_attachment.eml"]);
196 $message = Message::fromFile($filename);
197
198 $subject = $message->get("subject");
199 $returnPath = $message->get("Return-Path");
200
201 self::assertInstanceOf(Attribute::class, $subject);
202 self::assertSame("ogqMVHhz7swLaq2PfSWsZj0k99w8wtMbrb4RuHdNg53i76B7icIIM0zIWpwGFtnk", $subject->toString());
203 self::assertSame("ogqMVHhz7swLaq2PfSWsZj0k99w8wtMbrb4RuHdNg53i76B7icIIM0zIWpwGFtnk", (string)$message->subject);
204 self::assertSame("[email protected]", $returnPath->toString());
205 self::assertSame("return_path", $returnPath->getName());
206 self::assertSame("1.103", (string)$message->get("X-Spam-Score"));
207 self::assertSame("[email protected]", (string)$message->get("Message-ID"));
208 self::assertSame(4, $message->get("received")->count());
209 self::assertSame(IMAP::MESSAGE_PRIORITY_HIGHEST, (int)$message->get("priority")());
210
211 self::assertNull($message->getClient());
212 self::assertSame(0, $message->uid);
213 self::assertSame(1, $message->getAttachments()->count());
214
215 /** @var Attachment $attachment */
216 $attachment = $message->getAttachments()->first();
217 self::assertSame("attachment", $attachment->disposition);
218 self::assertSame("znk551MP3TP3WPp9Kl1gnLErrWEgkJFAtvaKqkTgrk3dKI8dX38YT8BaVxRcOERN", $attachment->content);
219 self::assertSame("application/octet-stream", $attachment->content_type);
220 self::assertSame("6mfFxiU5Yhv9WYJx.txt", $attachment->name);
221 self::assertSame(2, $attachment->part_number);
222 self::assertSame("text", $attachment->type);
223 self::assertNotEmpty($attachment->id);
224 self::assertSame(90, $attachment->size);
225 self::assertSame("txt", $attachment->getExtension());
226 self::assertInstanceOf(Message::class, $attachment->getMessage());
227 self::assertSame("text/plain", $attachment->getMimeType());
228 }
229
230 /**
231 * Test issue #348
232 *
233 * @return void
234 * @throws AuthFailedException
235 * @throws ConnectionFailedException
236 * @throws ImapBadRequestException
237 * @throws ImapServerErrorException
238 * @throws InvalidMessageDateException
239 * @throws MaskNotFoundException
240 * @throws MessageContentFetchingException
241 * @throws ReflectionException
242 * @throws ResponseException
243 * @throws RuntimeException
244 */
245 public function testIssue348() {
246 $filename = implode(DIRECTORY_SEPARATOR, [__DIR__, "messages", "issue-348.eml"]);
247 $message = Message::fromFile($filename);
248
249 self::assertSame(1, $message->getAttachments()->count());
250
251 /** @var Attachment $attachment */
252 $attachment = $message->getAttachments()->first();
253
254 self::assertSame("attachment", $attachment->disposition);
255 self::assertSame("application/pdf", $attachment->content_type);
256 self::assertSame("Kelvinsong—Font_test_page_bold.pdf", $attachment->name);
257 self::assertSame(1, $attachment->part_number);
258 self::assertSame("text", $attachment->type);
259 self::assertNotEmpty($attachment->id);
260 self::assertSame(92384, $attachment->size);
261 self::assertSame("pdf", $attachment->getExtension());
262 self::assertInstanceOf(Message::class, $attachment->getMessage());
263 self::assertSame("application/pdf", $attachment->getMimeType());
264 }
265
266 /**
267 * Create a new protocol mockup
268 *
269 * @return void
270 */
271 protected function createNewProtocolMockup(): void {
272 $this->protocol = $this->createMock(ImapProtocol::class);
273
274 $this->protocol->expects($this->any())->method('createStream')->willReturn(true);
275 $this->protocol->expects($this->any())->method('connected')->willReturn(true);
276 $this->protocol->expects($this->any())->method('getConnectionTimeout')->willReturn(30);
277 $this->protocol->expects($this->any())->method('logout')->willReturn(Response::empty()->setResponse([
278 0 => "BYE Logging out\r\n",
279 1 => "OK Logout completed (0.001 + 0.000 secs).\r\n",
280 ]));
281 $this->protocol->expects($this->any())->method('selectFolder')->willReturn(Response::empty()->setResponse([
282 "flags" => [
283 0 => [
284 0 => "\Answered",
285 1 => "\Flagged",
286 2 => "\Deleted",
287 3 => "\Seen",
288 4 => "\Draft",
289 5 => "NonJunk",
290 6 => "unknown-1",
291 ],
292 ],
293 "exists" => 139,
294 "recent" => 0,
295 "unseen" => 94,
296 "uidvalidity" => 1488899637,
297 "uidnext" => 278,
298 ]));
299
300 $this->client->connection = $this->protocol;
301 }
302 }