|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Live Mailbox - PHPUnit tests. |
| 4 | + * |
| 5 | + * Runs tests on a live mailbox |
| 6 | + * |
| 7 | + * @author BAPCLTD-Marv |
| 8 | + */ |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace PhpImap; |
| 12 | + |
| 13 | +use const ENCBASE64; |
| 14 | +use ParagonIE\HiddenString\HiddenString; |
| 15 | +use Throwable; |
| 16 | +use const TYPEIMAGE; |
| 17 | +use const TYPEMULTIPART; |
| 18 | +use const TYPETEXT; |
| 19 | + |
| 20 | +/** |
| 21 | + * @psalm-import-type COMPOSE_ENVELOPE from AbstractLiveMailboxTest |
| 22 | + */ |
| 23 | +class LiveMailboxIssue514Test extends AbstractLiveMailboxTest |
| 24 | +{ |
| 25 | + /** |
| 26 | + * @dataProvider MailBoxProvider |
| 27 | + * |
| 28 | + * @group live |
| 29 | + * @group issue-514 |
| 30 | + */ |
| 31 | + public function testEmbed( |
| 32 | + HiddenString $imapPath, |
| 33 | + HiddenString $login, |
| 34 | + HiddenString $password, |
| 35 | + string $attachmentsDir, |
| 36 | + string $serverEncoding = 'UTF-8' |
| 37 | + ): void { |
| 38 | + /** @var Throwable|null */ |
| 39 | + $exception = null; |
| 40 | + |
| 41 | + $mailboxDeleted = false; |
| 42 | + |
| 43 | + /** @psalm-var COMPOSE_ENVELOPE */ |
| 44 | + $envelope = [ |
| 45 | + 'subject' => 'barbushin/php-imap#514--'.\bin2hex(\random_bytes(16)), |
| 46 | + ]; |
| 47 | + |
| 48 | + list($search_criteria) = $this->SubjectSearchCriteriaAndSubject($envelope); |
| 49 | + |
| 50 | + $body = [ |
| 51 | + [ |
| 52 | + 'type' => TYPEMULTIPART, |
| 53 | + ], |
| 54 | + [ |
| 55 | + 'type' => TYPETEXT, |
| 56 | + 'subtype' => 'plain', |
| 57 | + 'contents.data' => 'foo', |
| 58 | + ], |
| 59 | + [ |
| 60 | + 'type' => TYPETEXT, |
| 61 | + 'subtype' => 'html', |
| 62 | + 'contents.data' => \implode('', [ |
| 63 | + '<img alt="png" width="5" height="1" src="cid:foo.png">', |
| 64 | + '<img alt="webp" width="5" height="1" src="cid:foo.webp">', |
| 65 | + ]), |
| 66 | + ], |
| 67 | + [ |
| 68 | + 'type' => TYPEIMAGE, |
| 69 | + 'subtype' => 'png', |
| 70 | + 'encoding' => ENCBASE64, |
| 71 | + 'id' => 'foo.png', |
| 72 | + 'description' => 'foo.png', |
| 73 | + 'disposition' => ['filename' => 'foo.png'], |
| 74 | + 'disposition.type' => 'inline', |
| 75 | + 'type.parameters' => ['name' => 'foo.png'], |
| 76 | + 'contents.data' => \base64_encode( |
| 77 | + \file_get_contents(__DIR__.'/Fixtures/rgbkw5x1.png') |
| 78 | + ), |
| 79 | + ], |
| 80 | + [ |
| 81 | + 'type' => TYPEIMAGE, |
| 82 | + 'subtype' => 'webp', |
| 83 | + 'encoding' => ENCBASE64, |
| 84 | + 'id' => 'foo.webp', |
| 85 | + 'description' => 'foo.webp', |
| 86 | + 'disposition' => ['filename' => 'foo.webp'], |
| 87 | + 'disposition.type' => 'inline', |
| 88 | + 'type.parameters' => ['name' => 'foo.webp'], |
| 89 | + 'contents.data' => \base64_encode( |
| 90 | + \file_get_contents(__DIR__.'/Fixtures/rgbkw5x1.webp') |
| 91 | + ), |
| 92 | + ], |
| 93 | + ]; |
| 94 | + |
| 95 | + $message = Imap::mail_compose( |
| 96 | + $envelope, |
| 97 | + $body |
| 98 | + ); |
| 99 | + |
| 100 | + list($mailbox, $remove_mailbox, $path) = $this->getMailboxFromArgs([ |
| 101 | + $imapPath, |
| 102 | + $login, |
| 103 | + $password, |
| 104 | + $attachmentsDir, |
| 105 | + $serverEncoding, |
| 106 | + ]); |
| 107 | + |
| 108 | + $result = null; |
| 109 | + |
| 110 | + try { |
| 111 | + $search = $mailbox->searchMailbox($search_criteria); |
| 112 | + |
| 113 | + $this->assertCount( |
| 114 | + 0, |
| 115 | + $search, |
| 116 | + ( |
| 117 | + 'If a subject was found,'. |
| 118 | + ' then the message is insufficiently unique to assert that'. |
| 119 | + ' a newly-appended message was actually created.' |
| 120 | + ) |
| 121 | + ); |
| 122 | + |
| 123 | + $mailbox->appendMessageToMailbox($message); |
| 124 | + |
| 125 | + $search = $mailbox->searchMailbox($search_criteria); |
| 126 | + |
| 127 | + $this->assertCount( |
| 128 | + 1, |
| 129 | + $search, |
| 130 | + ( |
| 131 | + 'If a subject was not found, '. |
| 132 | + ' then Mailbox::appendMessageToMailbox() failed'. |
| 133 | + ' despite not throwing an exception.' |
| 134 | + ) |
| 135 | + ); |
| 136 | + |
| 137 | + $result = $mailbox->getMail($search[0], false); |
| 138 | + |
| 139 | + /** @var array<string, int> */ |
| 140 | + $counts = []; |
| 141 | + |
| 142 | + foreach ($result->getAttachments() as $attachment) { |
| 143 | + if (!isset($counts[(string) $attachment->contentId])) { |
| 144 | + $counts[(string) $attachment->contentId] = 0; |
| 145 | + } |
| 146 | + |
| 147 | + ++$counts[(string) $attachment->contentId]; |
| 148 | + } |
| 149 | + |
| 150 | + $this->assertCount( |
| 151 | + 2, |
| 152 | + $counts, |
| 153 | + ( |
| 154 | + 'counts should only contain foo.png and foo.webp, found: '. |
| 155 | + \implode( |
| 156 | + ', ', |
| 157 | + \array_keys($counts) |
| 158 | + ) |
| 159 | + ) |
| 160 | + ); |
| 161 | + |
| 162 | + foreach ($counts as $cid => $count) { |
| 163 | + $this->assertSame( |
| 164 | + 1, |
| 165 | + $count, |
| 166 | + $cid.' had '.(string) $count.', expected 1.' |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + $this->assertSame( |
| 171 | + 'foo', |
| 172 | + $result->textPlain, |
| 173 | + 'plain text body did not match expected result!' |
| 174 | + ); |
| 175 | + |
| 176 | + $embedded = \implode('', [ |
| 177 | + '<img alt="png" width="5" height="1" src="', |
| 178 | + 'data:image/png; charset=binary;base64, ', |
| 179 | + $body[3]['contents.data'], |
| 180 | + '">', |
| 181 | + '<img alt="webp" width="5" height="1" src="', |
| 182 | + 'data:image/webp; charset=binary;base64, ', |
| 183 | + $body[4]['contents.data'], |
| 184 | + '">', |
| 185 | + ]); |
| 186 | + |
| 187 | + $this->assertSame( |
| 188 | + [ |
| 189 | + 'foo.png' => 'cid:foo.png', |
| 190 | + 'foo.webp' => 'cid:foo.webp', |
| 191 | + ], |
| 192 | + $result->getInternalLinksPlaceholders(), |
| 193 | + 'Internal link placeholders did not match expected result!' |
| 194 | + ); |
| 195 | + |
| 196 | + $replaced = \implode('', [ |
| 197 | + '<img alt="png" width="5" height="1" src="', |
| 198 | + 'foo.png', |
| 199 | + '">', |
| 200 | + '<img alt="webp" width="5" height="1" src="', |
| 201 | + 'foo.webp', |
| 202 | + '">', |
| 203 | + ]); |
| 204 | + |
| 205 | + foreach ($result->getAttachments() as $attachment) { |
| 206 | + if ('foo.png' === $attachment->contentId) { |
| 207 | + $replaced = \str_replace( |
| 208 | + 'foo.png', |
| 209 | + '/'.\basename($attachment->filePath), |
| 210 | + $replaced |
| 211 | + ); |
| 212 | + } elseif ('foo.webp' === $attachment->contentId) { |
| 213 | + $replaced = \str_replace( |
| 214 | + 'foo.webp', |
| 215 | + '/'.\basename($attachment->filePath), |
| 216 | + $replaced |
| 217 | + ); |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + $this->assertSame( |
| 222 | + $replaced, |
| 223 | + $result->replaceInternalLinks(''), |
| 224 | + 'replaced html body did not match expected result!' |
| 225 | + ); |
| 226 | + |
| 227 | + $this->assertSame( |
| 228 | + $body[2]['contents.data'], |
| 229 | + $result->textHtml, |
| 230 | + 'unembeded html body did not match expected result!' |
| 231 | + ); |
| 232 | + |
| 233 | + $result->embedImageAttachments(); |
| 234 | + |
| 235 | + $this->assertSame( |
| 236 | + $embedded, |
| 237 | + $result->textHtml, |
| 238 | + 'embeded html body did not match expected result!' |
| 239 | + ); |
| 240 | + |
| 241 | + $mailbox->deleteMail($search[0]); |
| 242 | + } catch (Throwable $ex) { |
| 243 | + $exception = $ex; |
| 244 | + } finally { |
| 245 | + $mailbox->switchMailbox($path->getString()); |
| 246 | + |
| 247 | + if (!$mailboxDeleted) { |
| 248 | + $mailbox->deleteMailbox($remove_mailbox); |
| 249 | + } |
| 250 | + |
| 251 | + $mailbox->disconnect(); |
| 252 | + } |
| 253 | + |
| 254 | + if (null !== $exception) { |
| 255 | + throw $exception; |
| 256 | + } |
| 257 | + } |
| 258 | +} |
0 commit comments