From eedb4208928a3be6b7a90541ec188e1b434e4b45 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 13 Dec 2020 13:01:14 -0500 Subject: [PATCH 01/26] Restore full Laravel Mailer behavior --- modules/system/ServiceProvider.php | 4 +- modules/system/classes/MailManager.php | 102 ++++++++++++++++++++----- 2 files changed, 83 insertions(+), 23 deletions(-) diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index e184775654..47965b893b 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -360,9 +360,7 @@ protected function registerMailer() * Override standard Mailer content with template */ Event::listen('mailer.beforeAddContent', function ($mailer, $message, $view, $data, $raw, $plain) { - $method = $raw === null ? 'addContentToMailer' : 'addRawContentToMailer'; - $plainOnly = $view === null; // When "plain-text only" email is sent, $view is null, this sets the flag appropriately - return !MailManager::instance()->$method($message, $raw ?: $view ?: $plain, $data, $plainOnly); + return !MailManager::instance()->addContent($message, $view, $plain, $raw, $data); }); } diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index ab76bd5167..c1ce029d89 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -58,14 +58,15 @@ class MailManager * Same as `addContentToMailer` except with raw content. * * @return bool + * + * @deprecated Use addContent() instead */ public function addRawContentToMailer($message, $content, $data) { - $template = new MailTemplate; - - $template->fillFromContent($content); + $text = new MailTemplate; + $text->fillFromContent($content); - $this->addContentToMailerInternal($message, $template, $data); + $this->addContentToMailerInternal($message, null, $text, $data); return true; } @@ -79,6 +80,8 @@ public function addRawContentToMailer($message, $content, $data) * @param array $data * @param bool $plainOnly Add only plain text content to the message * @return bool + * + * @deprecated Use addContent() instead */ public function addContentToMailer($message, $code, $data, $plainOnly = false) { @@ -93,21 +96,74 @@ public function addContentToMailer($message, $code, $data, $plainOnly = false) return false; } - $this->addContentToMailerInternal($message, $template, $data, $plainOnly); + $html = $text = $template; + + if ($plainOnly) { + $html = null; + } + $this->addContentToMailerInternal($message, $html, $text, $data); return true; } /** - * Internal method used to share logic between `addRawContentToMailer` and `addContentToMailer` + * Restore proper behavior in-line with Laravel Mailer + * Replaces both `addContentToMailer` and `addRawContentToMailer` * * @param \Illuminate\Mail\Message $message - * @param string $template + * @param string $view + * @param string $plain + * @param string $raw * @param array $data - * @param bool $plainOnly Add only plain text content to the message * @return void */ - protected function addContentToMailerInternal($message, $template, $data, $plainOnly = false) + public function addContent($message, $view, $plain, $raw, $data) + { + $html = null; + $text = null; + + if (isset($view)) { + if (isset($this->templateCache[$view])) { + $html = $this->templateCache[$view]; + } + else { + $this->templateCache[$view] = $html = MailTemplate::findOrMakeTemplate($view); + } + } + + if (isset($plain)) { + if (isset($this->templateCache[$plain])) { + $text = $this->templateCache[$plain]; + } + else { + $this->templateCache[$plain] = $text = MailTemplate::findOrMakeTemplate($plain); + } + } + + if (isset($raw)) { + $text = new MailTemplate; + $text->fillFromContent($raw); + } + + if (!($html || $text)) { + return false; + } + + $this->addContentToMailerInternal($message, $html, $text, $data); + + return true; + } + + /** + * Internal method used to share logic between `addContent`, `addRawContentToMailer` and `addContentToMailer` + * + * @param \Illuminate\Mail\Message $message + * @param MailTemplate $html + * @param MailTemplate $text + * @param array $data + * @return void + */ + protected function addContentToMailerInternal($message, $html, $text, $data) { /* * Start twig transaction @@ -128,28 +184,34 @@ protected function addContentToMailerInternal($message, $template, $data, $plain $swiftMessage = $message->getSwiftMessage(); if (empty($swiftMessage->getSubject())) { - $message->subject(Twig::parse($template->subject, $data)); + if ($html) { + $message->subject(Twig::parse($html->subject, $data)); + } else { + $message->subject(Twig::parse($text->subject, $data)); + } } $data += [ 'subject' => $swiftMessage->getSubject() ]; - if (!$plainOnly) { - /* - * HTML contents - */ - $html = $this->renderTemplate($template, $data); - - $message->setBody($html, 'text/html'); + /* + * HTML content + */ + if ($html) { + $message->setBody($this->renderTemplate($html, $data), 'text/html'); + if (!$text && $html->content_text) { + $text = $html; + } } /* - * Text contents + * Text content */ - $text = $this->renderTextTemplate($template, $data); + if ($text) { + $message->addPart($this->renderTextTemplate($text, $data), 'text/plain'); + } - $message->addPart($text, 'text/plain'); /* * End twig transaction From f56c617dc2612784adbcf86095f326c28d52c70f Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 14 Dec 2020 12:07:08 -0500 Subject: [PATCH 02/26] add Mailer tests --- tests/unit/system/classes/MailManagerTest.php | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/unit/system/classes/MailManagerTest.php diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php new file mode 100644 index 0000000000..35e0d7a003 --- /dev/null +++ b/tests/unit/system/classes/MailManagerTest.php @@ -0,0 +1,64 @@ +to = 'myself@octobercms.com'; + $message->subject = 'test mail'; + }); + + Mail::assertSent('html-view', 1); + Mail::assertNotSent('plain-view'); + } + + public function testIndexedArrayViews() + { + $views = ['html-view', 'plain-view']; + + Mail::assertNothingSent(); + + Mail::send($views, [], function ($message) { + $message->to = 'myself@octobercms.com'; + $message->subject = 'test mail'; + }); + + Mail::assertSent('html-view', 1); + Mail::assertSent('plain-view', 1); + } + + public function testNamedArrayViews() + { + $views = [ + 'html' => 'html-view', + 'text' => 'plain-view', + ]; + + Mail::assertNothingSent(); + + Mail::send($views, [], function ($message) { + $message->to = 'myself@octobercms.com'; + $message->subject = 'test mail'; + }); + + Mail::assertSent('html-view', 1); + Mail::assertSent('plain-view', 1); + } +} From 7a935beae0ff64aeedcdd86649b2298bb2135bf1 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 14 Dec 2020 15:00:34 -0500 Subject: [PATCH 03/26] minor tweaks; add plethora of tests --- modules/system/classes/MailManager.php | 5 +- tests/unit/system/classes/MailManagerTest.php | 131 +++++++++++++----- 2 files changed, 103 insertions(+), 33 deletions(-) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index c1ce029d89..280ac28837 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -186,7 +186,7 @@ protected function addContentToMailerInternal($message, $html, $text, $data) if (empty($swiftMessage->getSubject())) { if ($html) { $message->subject(Twig::parse($html->subject, $data)); - } else { + } else if ($text) { $message->subject(Twig::parse($text->subject, $data)); } } @@ -209,7 +209,8 @@ protected function addContentToMailerInternal($message, $html, $text, $data) * Text content */ if ($text) { - $message->addPart($this->renderTextTemplate($text, $data), 'text/plain'); + $method = $html ? 'addPart' : 'setBody'; + $message->{$method}($this->renderTextTemplate($text, $data), 'text/plain'); } diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php index 35e0d7a003..e7b26dcdc8 100644 --- a/tests/unit/system/classes/MailManagerTest.php +++ b/tests/unit/system/classes/MailManagerTest.php @@ -1,5 +1,9 @@ message = new Message($swift->createMessage('message')); + + $template = new MailTemplate(); + $template->is_custom = true; + $template->code = 'html-view'; + $template->subject = 'html view [{{ mode }}]'; + $template->content_html = 'my html view content'; + $template->description = 'my html view description'; + $template->save(); + + $template = new MailTemplate(); + $template->is_custom = true; + $template->code = 'plain-view'; + $template->subject = 'plain view [{{ mode }}]'; + $template->content_html = 'my plain view content'; + $template->description = 'my plain view description'; + $template->save(); } // // Tests // - public function testStringViews() + public function testAddContent_Html() { - $views = 'html-view'; + $html = $plain = $raw = null; + $data = ['mode' => 'test']; - Mail::assertNothingSent(); + $html = 'html-view'; - Mail::send($views, [], function ($message) { - $message->to = 'myself@octobercms.com'; - $message->subject = 'test mail'; - }); + $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); - Mail::assertSent('html-view', 1); - Mail::assertNotSent('plain-view'); + $swiftMsg = $this->message->getSwiftMessage(); + $this->assertEquals('text/html', $swiftMsg->getBodyContentType()); + $this->assertEquals('html view [test]', $swiftMsg->getSubject()); } - public function testIndexedArrayViews() + public function testAddContent_Plain() { - $views = ['html-view', 'plain-view']; + $html = $plain = $raw = null; + $data = ['mode' => 'test']; + + $plain = 'plain-view'; - Mail::assertNothingSent(); + $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); - Mail::send($views, [], function ($message) { - $message->to = 'myself@octobercms.com'; - $message->subject = 'test mail'; - }); + $this->assertTrue($result); - Mail::assertSent('html-view', 1); - Mail::assertSent('plain-view', 1); + $swiftMsg = $this->message->getSwiftMessage(); + $this->assertEquals('text/plain', $swiftMsg->getBodyContentType()); + $this->assertEquals('plain view [test]', $swiftMsg->getSubject()); + $this->assertEquals('my plain view content', $swiftMsg->getBody()); } - public function testNamedArrayViews() + public function testAddContent_Raw() { - $views = [ - 'html' => 'html-view', - 'text' => 'plain-view', - ]; + $html = $plain = $raw = null; + $data = ['mode' => 'test']; + + $raw = 'my raw content'; + + $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); + + $this->assertTrue($result); + + $swiftMsg = $this->message->getSwiftMessage(); + $this->assertEquals('text/plain', $swiftMsg->getBodyContentType()); + $this->assertEquals('No subject', $swiftMsg->getSubject()); + $this->assertEquals($raw, $swiftMsg->getBody()); + } + + public function testAddContent_Html_Plain() + { + $html = $plain = $raw = null; + $data = ['mode' => 'test']; + + $html = 'html-view'; + $plain = 'plain-view'; + + $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); + + $this->assertTrue($result); + + $swiftMsg = $this->message->getSwiftMessage(); + + $this->assertEquals('text/html', $swiftMsg->getBodyContentType()); + $this->assertEquals('html view [test]', $swiftMsg->getSubject()); + $this->assertTrue(str_contains($swiftMsg->getBody(), 'my html view content')); + + $parts = $swiftMsg->getChildren(); + $this->assertEquals(1, count($parts)); + $this->assertEquals('text/plain', $parts[0]->getBodyContentType()); + $this->assertEquals('my plain view content', $parts[0]->getBody()); + } + + public function testAddContent_Html_Plain_Raw() + { + $html = $plain = $raw = null; + $data = ['mode' => 'test']; + + $html = 'html-view'; + $plain = 'plain-view'; + $raw = 'my raw content'; + + $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); + + $this->assertTrue($result); - Mail::assertNothingSent(); + $swiftMsg = $this->message->getSwiftMessage(); - Mail::send($views, [], function ($message) { - $message->to = 'myself@octobercms.com'; - $message->subject = 'test mail'; - }); + $this->assertEquals('text/html', $swiftMsg->getBodyContentType()); + $this->assertEquals('html view [test]', $swiftMsg->getSubject()); - Mail::assertSent('html-view', 1); - Mail::assertSent('plain-view', 1); + $parts = $swiftMsg->getChildren(); + $this->assertEquals(1, count($parts)); + $this->assertEquals('text/plain', $parts[0]->getBodyContentType()); + $this->assertEquals($raw, $parts[0]->getBody()); } } From 018b0b22fea771af0b4fd0675e9a383569b89f5e Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 14 Dec 2020 15:47:39 -0500 Subject: [PATCH 04/26] add a few comments --- modules/system/classes/MailManager.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index 280ac28837..49434f27f2 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -140,11 +140,13 @@ public function addContent($message, $view, $plain, $raw, $data) } } + // raw content will overwrite plain view content, as done in laravel if (isset($raw)) { $text = new MailTemplate; $text->fillFromContent($raw); } + // bailout if we have no content if (!($html || $text)) { return false; } From 7ba918def881bf1beb9252d88d86a793a68f1c97 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 14 Dec 2020 15:58:29 -0500 Subject: [PATCH 05/26] single line assignment --- modules/system/classes/MailManager.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index 49434f27f2..d0170c042e 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -119,8 +119,7 @@ public function addContentToMailer($message, $code, $data, $plainOnly = false) */ public function addContent($message, $view, $plain, $raw, $data) { - $html = null; - $text = null; + $html = $text = null; if (isset($view)) { if (isset($this->templateCache[$view])) { From d88f893b9b1f76ac420c95b5098248fe273dcca8 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 14 Dec 2020 15:59:54 -0500 Subject: [PATCH 06/26] no new line for else block --- modules/system/classes/MailManager.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index d0170c042e..ce02cdca75 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -124,8 +124,7 @@ public function addContent($message, $view, $plain, $raw, $data) if (isset($view)) { if (isset($this->templateCache[$view])) { $html = $this->templateCache[$view]; - } - else { + } else { $this->templateCache[$view] = $html = MailTemplate::findOrMakeTemplate($view); } } @@ -133,8 +132,7 @@ public function addContent($message, $view, $plain, $raw, $data) if (isset($plain)) { if (isset($this->templateCache[$plain])) { $text = $this->templateCache[$plain]; - } - else { + } else { $this->templateCache[$plain] = $text = MailTemplate::findOrMakeTemplate($plain); } } From 6bf22a6263180ebf80a04ad56ca3b780f345997b Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 14 Dec 2020 17:22:26 -0500 Subject: [PATCH 07/26] simplify setUp() --- tests/unit/system/classes/MailManagerTest.php | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php index e7b26dcdc8..c51db214fb 100644 --- a/tests/unit/system/classes/MailManagerTest.php +++ b/tests/unit/system/classes/MailManagerTest.php @@ -14,21 +14,15 @@ public function setUp() : void $swift = Mail::getSwiftMailer(); $this->message = new Message($swift->createMessage('message')); - $template = new MailTemplate(); - $template->is_custom = true; - $template->code = 'html-view'; - $template->subject = 'html view [{{ mode }}]'; - $template->content_html = 'my html view content'; - $template->description = 'my html view description'; - $template->save(); - - $template = new MailTemplate(); - $template->is_custom = true; - $template->code = 'plain-view'; - $template->subject = 'plain view [{{ mode }}]'; - $template->content_html = 'my plain view content'; - $template->description = 'my plain view description'; - $template->save(); + foreach (['html', 'plain'] as $view) { + $t = new MailTemplate(); + $t->is_custom = true; + $t->code = "$view-view"; + $t->subject = "$view view [{{ mode }}]"; + $t->content_html = "my $view view content"; + $t->description = "my $view view description"; + $t->save(); + } } // From 96ecbb05503675d45176a717f7d0a295794a0c27 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 14 Dec 2020 17:39:42 -0500 Subject: [PATCH 08/26] tidyup tests a bit --- tests/unit/system/classes/MailManagerTest.php | 37 +++++++------------ 1 file changed, 13 insertions(+), 24 deletions(-) diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php index c51db214fb..53c93176d6 100644 --- a/tests/unit/system/classes/MailManagerTest.php +++ b/tests/unit/system/classes/MailManagerTest.php @@ -45,16 +45,14 @@ public function testAddContent_Html() public function testAddContent_Plain() { - $html = $plain = $raw = null; - $data = ['mode' => 'test']; - + $html = $raw = null; $plain = 'plain-view'; + $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); + $swiftMsg = $this->message->getSwiftMessage(); $this->assertTrue($result); - - $swiftMsg = $this->message->getSwiftMessage(); $this->assertEquals('text/plain', $swiftMsg->getBodyContentType()); $this->assertEquals('plain view [test]', $swiftMsg->getSubject()); $this->assertEquals('my plain view content', $swiftMsg->getBody()); @@ -62,16 +60,14 @@ public function testAddContent_Plain() public function testAddContent_Raw() { - $html = $plain = $raw = null; - $data = ['mode' => 'test']; - + $html = $plain = null; $raw = 'my raw content'; + $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); + $swiftMsg = $this->message->getSwiftMessage(); $this->assertTrue($result); - - $swiftMsg = $this->message->getSwiftMessage(); $this->assertEquals('text/plain', $swiftMsg->getBodyContentType()); $this->assertEquals('No subject', $swiftMsg->getSubject()); $this->assertEquals($raw, $swiftMsg->getBody()); @@ -79,23 +75,20 @@ public function testAddContent_Raw() public function testAddContent_Html_Plain() { - $html = $plain = $raw = null; - $data = ['mode' => 'test']; - + $raw = null; $html = 'html-view'; $plain = 'plain-view'; + $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); - - $this->assertTrue($result); - $swiftMsg = $this->message->getSwiftMessage(); + $parts = $swiftMsg->getChildren(); + $this->assertTrue($result); $this->assertEquals('text/html', $swiftMsg->getBodyContentType()); $this->assertEquals('html view [test]', $swiftMsg->getSubject()); $this->assertTrue(str_contains($swiftMsg->getBody(), 'my html view content')); - $parts = $swiftMsg->getChildren(); $this->assertEquals(1, count($parts)); $this->assertEquals('text/plain', $parts[0]->getBodyContentType()); $this->assertEquals('my plain view content', $parts[0]->getBody()); @@ -103,23 +96,19 @@ public function testAddContent_Html_Plain() public function testAddContent_Html_Plain_Raw() { - $html = $plain = $raw = null; - $data = ['mode' => 'test']; - $html = 'html-view'; $plain = 'plain-view'; $raw = 'my raw content'; + $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); - - $this->assertTrue($result); - $swiftMsg = $this->message->getSwiftMessage(); + $parts = $swiftMsg->getChildren(); + $this->assertTrue($result); $this->assertEquals('text/html', $swiftMsg->getBodyContentType()); $this->assertEquals('html view [test]', $swiftMsg->getSubject()); - $parts = $swiftMsg->getChildren(); $this->assertEquals(1, count($parts)); $this->assertEquals('text/plain', $parts[0]->getBodyContentType()); $this->assertEquals($raw, $parts[0]->getBody()); From 5a934116c36b7a7ec7219f51aa027b10ceff468f Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 14 Dec 2020 17:42:31 -0500 Subject: [PATCH 09/26] missed one --- tests/unit/system/classes/MailManagerTest.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php index 53c93176d6..e16aeb4fa8 100644 --- a/tests/unit/system/classes/MailManagerTest.php +++ b/tests/unit/system/classes/MailManagerTest.php @@ -31,14 +31,13 @@ public function setUp() : void public function testAddContent_Html() { - $html = $plain = $raw = null; - $data = ['mode' => 'test']; - + $plain = $raw = null; $html = 'html-view'; + $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); - $swiftMsg = $this->message->getSwiftMessage(); + $this->assertEquals('text/html', $swiftMsg->getBodyContentType()); $this->assertEquals('html view [test]', $swiftMsg->getSubject()); } From b9d976c707930a543e517daf78ca6c52583e1afb Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 16 Dec 2020 22:57:17 -0500 Subject: [PATCH 10/26] update return type Co-authored-by: Ben Thomson --- modules/system/classes/MailManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index ce02cdca75..a6e9615230 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -115,7 +115,7 @@ public function addContentToMailer($message, $code, $data, $plainOnly = false) * @param string $plain * @param string $raw * @param array $data - * @return void + * @return bool */ public function addContent($message, $view, $plain, $raw, $data) { From 8ce5c351c1a3cf7677ea2ea6ae12dd7379382645 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 16 Dec 2020 22:57:57 -0500 Subject: [PATCH 11/26] use verbose code Co-authored-by: Ben Thomson --- modules/system/classes/MailManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index a6e9615230..e9ed54d8d8 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -144,7 +144,7 @@ public function addContent($message, $view, $plain, $raw, $data) } // bailout if we have no content - if (!($html || $text)) { + if (!isset($html) && !isset($text)) { return false; } From 7a823e3759882c0aa660482cefa2d158635a0114 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 16 Dec 2020 23:54:45 -0500 Subject: [PATCH 12/26] Apply suggestions from code review Co-authored-by: Ben Thomson --- modules/system/classes/MailManager.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index e9ed54d8d8..1771d2d64a 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -111,9 +111,9 @@ public function addContentToMailer($message, $code, $data, $plainOnly = false) * Replaces both `addContentToMailer` and `addRawContentToMailer` * * @param \Illuminate\Mail\Message $message - * @param string $view - * @param string $plain - * @param string $raw + * @param string|null $view + * @param string|null $plain + * @param string|null $raw * @param array $data * @return bool */ @@ -157,12 +157,12 @@ public function addContent($message, $view, $plain, $raw, $data) * Internal method used to share logic between `addContent`, `addRawContentToMailer` and `addContentToMailer` * * @param \Illuminate\Mail\Message $message - * @param MailTemplate $html - * @param MailTemplate $text + * @param MailTemplate|null $html + * @param MailTemplate|null $text * @param array $data * @return void */ - protected function addContentToMailerInternal($message, $html, $text, $data) + protected function addContentToMailerInternal($message, $html = null, $text = null, array $data = []) { /* * Start twig transaction From 0fb2bcd3b54e12a4b17921c7d1b853bd1ac0953b Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 4 Feb 2021 14:37:49 -0500 Subject: [PATCH 13/26] use !is_null() instead of isset() --- modules/system/classes/MailManager.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index 1771d2d64a..1e20d95e59 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -121,7 +121,7 @@ public function addContent($message, $view, $plain, $raw, $data) { $html = $text = null; - if (isset($view)) { + if (!is_null($view)) { if (isset($this->templateCache[$view])) { $html = $this->templateCache[$view]; } else { @@ -129,7 +129,7 @@ public function addContent($message, $view, $plain, $raw, $data) } } - if (isset($plain)) { + if (!is_null($plain)) { if (isset($this->templateCache[$plain])) { $text = $this->templateCache[$plain]; } else { @@ -138,13 +138,13 @@ public function addContent($message, $view, $plain, $raw, $data) } // raw content will overwrite plain view content, as done in laravel - if (isset($raw)) { + if (!is_null($raw)) { $text = new MailTemplate; $text->fillFromContent($raw); } // bailout if we have no content - if (!isset($html) && !isset($text)) { + if (is_null($html) && is_null($text)) { return false; } From 3ee9045127538317813c3acdc383e4fb3e3b0ebc Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 5 Feb 2021 14:35:36 -0500 Subject: [PATCH 14/26] add default argument values --- modules/system/classes/MailManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index 1e20d95e59..fa816443c3 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -117,7 +117,7 @@ public function addContentToMailer($message, $code, $data, $plainOnly = false) * @param array $data * @return bool */ - public function addContent($message, $view, $plain, $raw, $data) + public function addContent($message, $view=null, $plain=null, $raw=null, $data=[]) { $html = $text = null; From afb978c09846fe9ecd13cf3632d08695fd64ca4c Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 5 Feb 2021 14:37:45 -0500 Subject: [PATCH 15/26] add missing spaces around = --- modules/system/classes/MailManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index fa816443c3..bbf1dc8d3a 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -117,7 +117,7 @@ public function addContentToMailer($message, $code, $data, $plainOnly = false) * @param array $data * @return bool */ - public function addContent($message, $view=null, $plain=null, $raw=null, $data=[]) + public function addContent($message, $view = null, $plain = null, $raw = null, $data = []) { $html = $text = null; From d386a4138dc0c410ea7571e4f97052498c771b17 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Thu, 25 Mar 2021 12:29:10 +0800 Subject: [PATCH 16/26] Add version.yaml processor, update versioning to use this processor --- modules/system/classes/VersionManager.php | 4 +- .../system/classes/VersionYamlProcessor.php | 56 +++++++++++++++++++ modules/system/controllers/Updates.php | 9 ++- 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 modules/system/classes/VersionYamlProcessor.php diff --git a/modules/system/classes/VersionManager.php b/modules/system/classes/VersionManager.php index 8bb56d3881..cce32fa958 100644 --- a/modules/system/classes/VersionManager.php +++ b/modules/system/classes/VersionManager.php @@ -279,7 +279,9 @@ protected function getFileVersions($code) } $versionFile = $this->getVersionFile($code); - $versionInfo = Yaml::parseFile($versionFile); + $versionInfo = Yaml::withProcessor(new VersionYamlProcessor, function ($yaml) use ($versionFile) { + return $yaml->parseFile($versionFile); + }); if (!is_array($versionInfo)) { $versionInfo = []; diff --git a/modules/system/classes/VersionYamlProcessor.php b/modules/system/classes/VersionYamlProcessor.php new file mode 100644 index 0000000000..8ca2fb8651 --- /dev/null +++ b/modules/system/classes/VersionYamlProcessor.php @@ -0,0 +1,56 @@ + &$line) { + // Surround array keys with quotes if not already + $line = preg_replace_callback('/^\s*([\'"]{0}[^\'"\n\r:]+[\'"]{0})\s*:/m', function ($matches) { + return '"' . trim($matches[1]) . '":'; + }, rtrim($line)); + + // Add quotes around any unquoted text proceeding an array key + $line = preg_replace('/^\s*([^\n\r\-:]+)\s*: +(?![\'"\s])(.*)/m', '$1: "$2"', $line); + + // If this line is the continuance of a multi-line string, remove the quote from the previous line and + // continue the quote + if ( + preg_match('/^(?!\s*(-|(.*?):\s*))([^\n\r]+)([^"]$)/m', $line) + && substr($lines[$num - 1], -1) === '"' + ) { + $lines[$num - 1] = substr($lines[$num - 1], 0, -1); + $line .= '"'; + } + + // Add quotes around any unquoted array items + $line = preg_replace('/^(\s*-\s*)(?![\'" ])(.*)/m', '$1"$2"', $line); + } + + return implode("\n", $lines); + } + + /** + * @inheritDoc + */ + public function process($parsed) + { + return $parsed; + } +} diff --git a/modules/system/controllers/Updates.php b/modules/system/controllers/Updates.php index b6ad688365..e2aeeb739d 100644 --- a/modules/system/controllers/Updates.php +++ b/modules/system/controllers/Updates.php @@ -17,6 +17,7 @@ use System\Classes\UpdateManager; use System\Classes\PluginManager; use System\Classes\SettingsManager; +use System\Classes\VersionYamlProcessor; use ApplicationException; use Exception; @@ -184,7 +185,9 @@ protected function getPluginVersionFile($path, $filename) $contents = []; try { - $updates = (array) Yaml::parseFile($path.'/'.$filename); + $updates = Yaml::withProcessor(new VersionYamlProcessor, function ($yaml) use ($path, $filename) { + return (array) $yaml->parseFile($path.'/'.$filename); + }); foreach ($updates as $version => $details) { if (!is_array($details)) { @@ -192,9 +195,9 @@ protected function getPluginVersionFile($path, $filename) } //Filter out update scripts - $details = array_filter($details, function ($string) use ($path) { + $details = array_values(array_filter($details, function ($string) use ($path) { return !preg_match('/^[a-z_\-0-9]*\.php$/i', $string) || !File::exists($path . '/updates/' . $string); - }); + })); $contents[$version] = $details; } From b736df2b88d92f669c7c8b193b8a9d5781f803a7 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Thu, 25 Mar 2021 12:29:25 +0800 Subject: [PATCH 17/26] Tweaks to version/update unit tests --- ...table.php => drop_blog_settings_table.php} | 0 .../winter/tester/updates/fix_database.php | 0 .../tester/updates/some_upgrade_file.php | 0 .../winter/tester/updates/version.yaml | 23 ++++++++++------ .../system/classes/UpdatesControllerTest.php | 14 +++++++--- .../system/classes/VersionManagerTest.php | 27 ++++++++++++++++--- 6 files changed, 48 insertions(+), 16 deletions(-) rename tests/fixtures/plugins/winter/tester/updates/{create_comments_table.php => drop_blog_settings_table.php} (100%) create mode 100644 tests/fixtures/plugins/winter/tester/updates/fix_database.php create mode 100644 tests/fixtures/plugins/winter/tester/updates/some_upgrade_file.php diff --git a/tests/fixtures/plugins/winter/tester/updates/create_comments_table.php b/tests/fixtures/plugins/winter/tester/updates/drop_blog_settings_table.php similarity index 100% rename from tests/fixtures/plugins/winter/tester/updates/create_comments_table.php rename to tests/fixtures/plugins/winter/tester/updates/drop_blog_settings_table.php diff --git a/tests/fixtures/plugins/winter/tester/updates/fix_database.php b/tests/fixtures/plugins/winter/tester/updates/fix_database.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fixtures/plugins/winter/tester/updates/some_upgrade_file.php b/tests/fixtures/plugins/winter/tester/updates/some_upgrade_file.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fixtures/plugins/winter/tester/updates/version.yaml b/tests/fixtures/plugins/winter/tester/updates/version.yaml index 571298ebab..f1820e2475 100644 --- a/tests/fixtures/plugins/winter/tester/updates/version.yaml +++ b/tests/fixtures/plugins/winter/tester/updates/version.yaml @@ -1,19 +1,26 @@ +1.3.2: + Added support for Translate plugin. + Added some new languages. +'1.3.1' : + - 'Minor bug fix + Please see changelog' + - 'fix_database.php' +"1.3.0" : !!! We've refactored major parts + of this plugin. Please see the + website for more information. 1.2.0: - "!!! Security update - see: https://wintercms.com" -1.1.0: +1.1.0 : - !!! Drop support for blog settings - drop_blog_settings_table.php -1.0.5: +"1.0.5": - Create blog settings table - Another update message - Yet one more update message - create_blog_settings_table.php -1.0.4: Another fix -1.0.3: Bug fix update that uses no scripts -1.0.2: - - Create blog post comments table - - Multiple update messages are allowed - - create_comments_table.php +"1.0.4": "Another fix" +"1.0.3": Bug fix update that uses no scripts +1.0.2: Added some stuff 1.0.1: - Added some upgrade file and some seeding - some_upgrade_file.php diff --git a/tests/unit/system/classes/UpdatesControllerTest.php b/tests/unit/system/classes/UpdatesControllerTest.php index f75c8f5483..e5fcefff8a 100644 --- a/tests/unit/system/classes/UpdatesControllerTest.php +++ b/tests/unit/system/classes/UpdatesControllerTest.php @@ -14,12 +14,20 @@ public function testGetPluginVersionFile() $controller = $this->getMockBuilder(Updates::class)->disableOriginalConstructor()->getMock(); $expectedVersions = [ + '1.3.2' => [ + 'Added support for Translate plugin. Added some new languages.', + ], + '1.3.1' => [ + 'Minor bug fix Please see changelog', + ], + '1.3.0' => [ + '!!! We\'ve refactored major parts of this plugin. Please see the website for more information.', + ], '1.2.0' => [ '!!! Security update - see: https://wintercms.com', ], '1.1.0' => [ '!!! Drop support for blog settings', - 'drop_blog_settings_table.php', ], '1.0.5' => [ 'Create blog settings table', @@ -33,12 +41,10 @@ public function testGetPluginVersionFile() 'Bug fix update that uses no scripts' ], '1.0.2' => [ - 'Create blog post comments table', - 'Multiple update messages are allowed', + 'Added some stuff', ], '1.0.1' => [ 'Added some upgrade file and some seeding', - 'some_upgrade_file.php', //does not exist 'some_seeding_file.php' //does not exist ], ]; diff --git a/tests/unit/system/classes/VersionManagerTest.php b/tests/unit/system/classes/VersionManagerTest.php index 8f0d868ee4..46a992022b 100644 --- a/tests/unit/system/classes/VersionManagerTest.php +++ b/tests/unit/system/classes/VersionManagerTest.php @@ -24,7 +24,7 @@ public function testGetLatestFileVersion() $result = self::callProtectedMethod($manager, 'getLatestFileVersion', ['\Winter\\Tester']); $this->assertNotNull($result); - $this->assertEquals('1.2.0', $result); + $this->assertEquals('1.3.2', $result); } public function testGetFileVersions() @@ -32,7 +32,7 @@ public function testGetFileVersions() $manager = VersionManager::instance(); $result = self::callProtectedMethod($manager, 'getFileVersions', ['\Winter\\Tester']); - $this->assertCount(7, $result); + $this->assertCount(10, $result); $this->assertArrayHasKey('1.0.1', $result); $this->assertArrayHasKey('1.0.2', $result); $this->assertArrayHasKey('1.0.3', $result); @@ -40,6 +40,9 @@ public function testGetFileVersions() $this->assertArrayHasKey('1.0.5', $result); $this->assertArrayHasKey('1.1.0', $result); $this->assertArrayHasKey('1.2.0', $result); + $this->assertArrayHasKey('1.3.0', $result); + $this->assertArrayHasKey('1.3.1', $result); + $this->assertArrayHasKey('1.3.2', $result); $sample = $result['1.0.1']; $this->assertEquals('Added some upgrade file and some seeding', $sample[0]); @@ -51,6 +54,16 @@ public function testGetFileVersions() $sample = $result['1.2.0']; $this->assertEquals('!!! Security update - see: https://wintercms.com', $sample[0]); + $sample = $result['1.3.0']; + $this->assertEquals('!!! We\'ve refactored major parts of this plugin. Please see the website for more information.', $sample); + + $sample = $result['1.3.1']; + $this->assertEquals('Minor bug fix Please see changelog', $sample[0]); + $this->assertEquals('fix_database.php', $sample[1]); + + $sample = $result['1.3.2']; + $this->assertEquals('Added support for Translate plugin. Added some new languages.', $sample); + /* * Test junk file */ @@ -78,11 +91,14 @@ public function testGetNewFileVersions() $manager = VersionManager::instance(); $result = self::callProtectedMethod($manager, 'getNewFileVersions', ['\Winter\\Tester', '1.0.3']); - $this->assertCount(4, $result); + $this->assertCount(7, $result); $this->assertArrayHasKey('1.0.4', $result); $this->assertArrayHasKey('1.0.5', $result); $this->assertArrayHasKey('1.1.0', $result); $this->assertArrayHasKey('1.2.0', $result); + $this->assertArrayHasKey('1.3.0', $result); + $this->assertArrayHasKey('1.3.1', $result); + $this->assertArrayHasKey('1.3.2', $result); /* * When at version 0, should return everything @@ -90,7 +106,7 @@ public function testGetNewFileVersions() $manager = VersionManager::instance(); $result = self::callProtectedMethod($manager, 'getNewFileVersions', ['\Winter\\Tester']); - $this->assertCount(7, $result); + $this->assertCount(10, $result); $this->assertArrayHasKey('1.0.1', $result); $this->assertArrayHasKey('1.0.2', $result); $this->assertArrayHasKey('1.0.3', $result); @@ -98,6 +114,9 @@ public function testGetNewFileVersions() $this->assertArrayHasKey('1.0.5', $result); $this->assertArrayHasKey('1.1.0', $result); $this->assertArrayHasKey('1.2.0', $result); + $this->assertArrayHasKey('1.3.0', $result); + $this->assertArrayHasKey('1.3.1', $result); + $this->assertArrayHasKey('1.3.2', $result); } /** From ec3d47710699d1e8f19bf3e7ae8cb6189651e4d2 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 25 Mar 2021 10:38:54 -0400 Subject: [PATCH 18/26] bail out if the view is not a string template name --- modules/system/classes/MailManager.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index e704c3c176..2b515a5746 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -121,6 +121,11 @@ public function addContent($message, $view = null, $plain = null, $raw = null, $ { $html = $text = null; + // We only handle mail template names as a string, let the caller handle the content if we receive anything else + if (!is_string($view)) { + return false; + } + if (!is_null($view)) { if (isset($this->templateCache[$view])) { $html = $this->templateCache[$view]; From d2f3acf3210ba92926371296505ca30d4594f051 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 25 Mar 2021 11:25:10 -0400 Subject: [PATCH 19/26] only work with string template names --- modules/system/classes/MailManager.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/modules/system/classes/MailManager.php b/modules/system/classes/MailManager.php index 2b515a5746..8080849186 100644 --- a/modules/system/classes/MailManager.php +++ b/modules/system/classes/MailManager.php @@ -122,11 +122,8 @@ public function addContent($message, $view = null, $plain = null, $raw = null, $ $html = $text = null; // We only handle mail template names as a string, let the caller handle the content if we receive anything else - if (!is_string($view)) { - return false; - } - if (!is_null($view)) { + if (is_string($view)) { if (isset($this->templateCache[$view])) { $html = $this->templateCache[$view]; } else { @@ -134,7 +131,7 @@ public function addContent($message, $view = null, $plain = null, $raw = null, $ } } - if (!is_null($plain)) { + if (is_string($plain)) { if (isset($this->templateCache[$plain])) { $text = $this->templateCache[$plain]; } else { @@ -143,7 +140,7 @@ public function addContent($message, $view = null, $plain = null, $raw = null, $ } // raw content will overwrite plain view content, as done in laravel - if (!is_null($raw)) { + if (is_string($raw)) { $text = new MailTemplate; $text->fillFromContent($raw); } From ad9063ca69482e9f655b9eaa0b08a935967cf698 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 17 Mar 2022 22:30:40 -0400 Subject: [PATCH 20/26] fix tests --- tests/unit/system/classes/MailManagerTest.php | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php index e16aeb4fa8..7654cd5bcd 100644 --- a/tests/unit/system/classes/MailManagerTest.php +++ b/tests/unit/system/classes/MailManagerTest.php @@ -11,8 +11,8 @@ public function setUp() : void { parent::setUp(); - $swift = Mail::getSwiftMailer(); - $this->message = new Message($swift->createMessage('message')); + $symfonyMessage = Mail::getSymfonyMessage(); + $this->message = new Message($symfonyMessage->createMessage('message')); foreach (['html', 'plain'] as $view) { $t = new MailTemplate(); @@ -36,10 +36,10 @@ public function testAddContent_Html() $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); - $swiftMsg = $this->message->getSwiftMessage(); + $symfonyMessage = $this->message->getSymfonyMessage(); - $this->assertEquals('text/html', $swiftMsg->getBodyContentType()); - $this->assertEquals('html view [test]', $swiftMsg->getSubject()); + $this->assertEquals('text/html', $symfonyMessage->getContentType()); + $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); } public function testAddContent_Plain() @@ -49,12 +49,12 @@ public function testAddContent_Plain() $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); - $swiftMsg = $this->message->getSwiftMessage(); + $symfonyMessage = $this->message->getSymfonyMessage(); $this->assertTrue($result); - $this->assertEquals('text/plain', $swiftMsg->getBodyContentType()); - $this->assertEquals('plain view [test]', $swiftMsg->getSubject()); - $this->assertEquals('my plain view content', $swiftMsg->getBody()); + $this->assertEquals('text/plain', $symfonyMessage->getContentType()); + $this->assertEquals('plain view [test]', $symfonyMessage->getSubject()); + $this->assertEquals('my plain view content', $symfonyMessage->getBody()); } public function testAddContent_Raw() @@ -64,12 +64,12 @@ public function testAddContent_Raw() $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); - $swiftMsg = $this->message->getSwiftMessage(); + $symfonyMessage = $this->message->getSymfonyMessage(); $this->assertTrue($result); - $this->assertEquals('text/plain', $swiftMsg->getBodyContentType()); - $this->assertEquals('No subject', $swiftMsg->getSubject()); - $this->assertEquals($raw, $swiftMsg->getBody()); + $this->assertEquals('text/plain', $symfonyMessage->getContentType()); + $this->assertEquals('No subject', $symfonyMessage->getSubject()); + $this->assertEquals($raw, $symfonyMessage->getBody()); } public function testAddContent_Html_Plain() @@ -80,16 +80,16 @@ public function testAddContent_Html_Plain() $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); - $swiftMsg = $this->message->getSwiftMessage(); - $parts = $swiftMsg->getChildren(); + $symfonyMessage = $this->message->getSymfonyMessage(); + $parts = $symfonyMessage->getChildren(); $this->assertTrue($result); - $this->assertEquals('text/html', $swiftMsg->getBodyContentType()); - $this->assertEquals('html view [test]', $swiftMsg->getSubject()); - $this->assertTrue(str_contains($swiftMsg->getBody(), 'my html view content')); + $this->assertEquals('text/html', $symfonyMessage->getContentType()); + $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); + $this->assertTrue(str_contains($symfonyMessage->getBody(), 'my html view content')); $this->assertEquals(1, count($parts)); - $this->assertEquals('text/plain', $parts[0]->getBodyContentType()); + $this->assertEquals('text/plain', $parts[0]->getContentType()); $this->assertEquals('my plain view content', $parts[0]->getBody()); } @@ -101,15 +101,15 @@ public function testAddContent_Html_Plain_Raw() $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); - $swiftMsg = $this->message->getSwiftMessage(); - $parts = $swiftMsg->getChildren(); + $symfonyMessage = $this->message->getSymfonyMessage(); + $parts = $symfonyMessage->getAttachments(); $this->assertTrue($result); - $this->assertEquals('text/html', $swiftMsg->getBodyContentType()); - $this->assertEquals('html view [test]', $swiftMsg->getSubject()); + $this->assertEquals('text/html', $symfonyMessage->getContentType()); + $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); $this->assertEquals(1, count($parts)); - $this->assertEquals('text/plain', $parts[0]->getBodyContentType()); + $this->assertEquals('text/plain', $parts[0]->getContentType()); $this->assertEquals($raw, $parts[0]->getBody()); } } From 0d76369bb1384bd6a988b09c514799646803ee60 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 17 Mar 2022 22:55:32 -0400 Subject: [PATCH 21/26] properly create new Message --- tests/unit/system/classes/MailManagerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php index 7654cd5bcd..31dc52bf22 100644 --- a/tests/unit/system/classes/MailManagerTest.php +++ b/tests/unit/system/classes/MailManagerTest.php @@ -3,6 +3,7 @@ use Illuminate\Mail\Message; use System\Classes\MailManager; use System\Models\MailTemplate; +use Symfony\Component\Mime\Email; class MailManagerTest extends PluginTestCase { @@ -11,8 +12,7 @@ public function setUp() : void { parent::setUp(); - $symfonyMessage = Mail::getSymfonyMessage(); - $this->message = new Message($symfonyMessage->createMessage('message')); + $this->message = new Message(new Email()); foreach (['html', 'plain'] as $view) { $t = new MailTemplate(); From e564aa72ee336027be04c7b8e531bed140c37356 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 17 Mar 2022 23:13:48 -0400 Subject: [PATCH 22/26] must use AbstractPart interface --- tests/unit/system/classes/MailManagerTest.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php index 31dc52bf22..83db1d5df8 100644 --- a/tests/unit/system/classes/MailManagerTest.php +++ b/tests/unit/system/classes/MailManagerTest.php @@ -4,6 +4,7 @@ use System\Classes\MailManager; use System\Models\MailTemplate; use Symfony\Component\Mime\Email; +use Symfony\Component\Mime\Part\TextPart; class MailManagerTest extends PluginTestCase { @@ -32,7 +33,7 @@ public function setUp() : void public function testAddContent_Html() { $plain = $raw = null; - $html = 'html-view'; + $html = new TextPart('html-view'); $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); @@ -45,7 +46,7 @@ public function testAddContent_Html() public function testAddContent_Plain() { $html = $raw = null; - $plain = 'plain-view'; + $plain = new TextPart('plain-view'); $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); @@ -60,7 +61,7 @@ public function testAddContent_Plain() public function testAddContent_Raw() { $html = $plain = null; - $raw = 'my raw content'; + $raw = new TextPart('my raw content'); $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); @@ -75,8 +76,8 @@ public function testAddContent_Raw() public function testAddContent_Html_Plain() { $raw = null; - $html = 'html-view'; - $plain = 'plain-view'; + $html = new TextPart('html-view'); + $plain = new TextPart('plain-view'); $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); @@ -95,9 +96,9 @@ public function testAddContent_Html_Plain() public function testAddContent_Html_Plain_Raw() { - $html = 'html-view'; - $plain = 'plain-view'; - $raw = 'my raw content'; + $html = new TextPart('html-view'); + $plain = new TextPart('plain-view'); + $raw = new TextPart('my raw content'); $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); From 2fdef70720b81ff3d85d1119635daadf316f8f8b Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 17 Mar 2022 23:52:43 -0400 Subject: [PATCH 23/26] get content-type header --- tests/unit/system/classes/MailManagerTest.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php index 83db1d5df8..8e7c8e0b40 100644 --- a/tests/unit/system/classes/MailManagerTest.php +++ b/tests/unit/system/classes/MailManagerTest.php @@ -33,13 +33,13 @@ public function setUp() : void public function testAddContent_Html() { $plain = $raw = null; - $html = new TextPart('html-view'); + $html = new TextPart('html-view', $subtype="html"); $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); $symfonyMessage = $this->message->getSymfonyMessage(); - $this->assertEquals('text/html', $symfonyMessage->getContentType()); + $this->assertEquals('text/html', $symfonyMessage->getPreparedHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); } @@ -53,7 +53,7 @@ public function testAddContent_Plain() $symfonyMessage = $this->message->getSymfonyMessage(); $this->assertTrue($result); - $this->assertEquals('text/plain', $symfonyMessage->getContentType()); + $this->assertEquals('text/plain', $symfonyMessage->getPreparedHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('plain view [test]', $symfonyMessage->getSubject()); $this->assertEquals('my plain view content', $symfonyMessage->getBody()); } @@ -68,7 +68,7 @@ public function testAddContent_Raw() $symfonyMessage = $this->message->getSymfonyMessage(); $this->assertTrue($result); - $this->assertEquals('text/plain', $symfonyMessage->getContentType()); + $this->assertEquals('text/plain', $symfonyMessage->getPreparedHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('No subject', $symfonyMessage->getSubject()); $this->assertEquals($raw, $symfonyMessage->getBody()); } @@ -76,27 +76,27 @@ public function testAddContent_Raw() public function testAddContent_Html_Plain() { $raw = null; - $html = new TextPart('html-view'); + $html = new TextPart('html-view', $subtype="html"); $plain = new TextPart('plain-view'); $data = ['mode' => 'test']; $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); $symfonyMessage = $this->message->getSymfonyMessage(); - $parts = $symfonyMessage->getChildren(); + $parts = $symfonyMessage->getAttachments(); $this->assertTrue($result); - $this->assertEquals('text/html', $symfonyMessage->getContentType()); + $this->assertEquals('text/html', $symfonyMessage->getPreparedHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); $this->assertTrue(str_contains($symfonyMessage->getBody(), 'my html view content')); $this->assertEquals(1, count($parts)); - $this->assertEquals('text/plain', $parts[0]->getContentType()); + $this->assertEquals('text/plain', $parts[0]->getPreparedHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('my plain view content', $parts[0]->getBody()); } public function testAddContent_Html_Plain_Raw() { - $html = new TextPart('html-view'); + $html = new TextPart('html-view', $subtype="html"); $plain = new TextPart('plain-view'); $raw = new TextPart('my raw content'); $data = ['mode' => 'test']; @@ -106,11 +106,11 @@ public function testAddContent_Html_Plain_Raw() $parts = $symfonyMessage->getAttachments(); $this->assertTrue($result); - $this->assertEquals('text/html', $symfonyMessage->getContentType()); + $this->assertEquals('text/html', $symfonyMessage->getPreparedHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); $this->assertEquals(1, count($parts)); - $this->assertEquals('text/plain', $parts[0]->getContentType()); + $this->assertEquals('text/plain', $parts[0]->getPreparedHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals($raw, $parts[0]->getBody()); } } From 0db30712c0095abceac9b55faddd162921ab17b9 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 18 Mar 2022 00:00:13 -0400 Subject: [PATCH 24/26] use getHeaders() instead --- tests/unit/system/classes/MailManagerTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php index 8e7c8e0b40..ea44de0447 100644 --- a/tests/unit/system/classes/MailManagerTest.php +++ b/tests/unit/system/classes/MailManagerTest.php @@ -39,7 +39,7 @@ public function testAddContent_Html() $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); $symfonyMessage = $this->message->getSymfonyMessage(); - $this->assertEquals('text/html', $symfonyMessage->getPreparedHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/html', $symfonyMessage->getHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); } @@ -53,7 +53,7 @@ public function testAddContent_Plain() $symfonyMessage = $this->message->getSymfonyMessage(); $this->assertTrue($result); - $this->assertEquals('text/plain', $symfonyMessage->getPreparedHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/plain', $symfonyMessage->getHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('plain view [test]', $symfonyMessage->getSubject()); $this->assertEquals('my plain view content', $symfonyMessage->getBody()); } @@ -68,7 +68,7 @@ public function testAddContent_Raw() $symfonyMessage = $this->message->getSymfonyMessage(); $this->assertTrue($result); - $this->assertEquals('text/plain', $symfonyMessage->getPreparedHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/plain', $symfonyMessage->getHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('No subject', $symfonyMessage->getSubject()); $this->assertEquals($raw, $symfonyMessage->getBody()); } @@ -85,12 +85,12 @@ public function testAddContent_Html_Plain() $parts = $symfonyMessage->getAttachments(); $this->assertTrue($result); - $this->assertEquals('text/html', $symfonyMessage->getPreparedHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/html', $symfonyMessage->getHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); $this->assertTrue(str_contains($symfonyMessage->getBody(), 'my html view content')); $this->assertEquals(1, count($parts)); - $this->assertEquals('text/plain', $parts[0]->getPreparedHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/plain', $parts[0]->getHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('my plain view content', $parts[0]->getBody()); } @@ -106,11 +106,11 @@ public function testAddContent_Html_Plain_Raw() $parts = $symfonyMessage->getAttachments(); $this->assertTrue($result); - $this->assertEquals('text/html', $symfonyMessage->getPreparedHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/html', $symfonyMessage->getHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); $this->assertEquals(1, count($parts)); - $this->assertEquals('text/plain', $parts[0]->getPreparedHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/plain', $parts[0]->getHeaders()->getHeaderParameter('Content-Type')); $this->assertEquals($raw, $parts[0]->getBody()); } } From d6df6d31b3879ba027fe05260517c2ecf8dd36b5 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 18 Mar 2022 00:11:07 -0400 Subject: [PATCH 25/26] use Header::get --- tests/unit/system/classes/MailManagerTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php index ea44de0447..9be4ac9471 100644 --- a/tests/unit/system/classes/MailManagerTest.php +++ b/tests/unit/system/classes/MailManagerTest.php @@ -39,7 +39,7 @@ public function testAddContent_Html() $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); $symfonyMessage = $this->message->getSymfonyMessage(); - $this->assertEquals('text/html', $symfonyMessage->getHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/html', $symfonyMessage->getHeaders()->get('Content-Type')); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); } @@ -53,7 +53,7 @@ public function testAddContent_Plain() $symfonyMessage = $this->message->getSymfonyMessage(); $this->assertTrue($result); - $this->assertEquals('text/plain', $symfonyMessage->getHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/plain', $symfonyMessage->getHeaders()->get('Content-Type')); $this->assertEquals('plain view [test]', $symfonyMessage->getSubject()); $this->assertEquals('my plain view content', $symfonyMessage->getBody()); } @@ -68,7 +68,7 @@ public function testAddContent_Raw() $symfonyMessage = $this->message->getSymfonyMessage(); $this->assertTrue($result); - $this->assertEquals('text/plain', $symfonyMessage->getHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/plain', $symfonyMessage->getHeaders()->get('Content-Type')); $this->assertEquals('No subject', $symfonyMessage->getSubject()); $this->assertEquals($raw, $symfonyMessage->getBody()); } @@ -85,12 +85,12 @@ public function testAddContent_Html_Plain() $parts = $symfonyMessage->getAttachments(); $this->assertTrue($result); - $this->assertEquals('text/html', $symfonyMessage->getHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/html', $symfonyMessage->getHeaders()->get('Content-Type')); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); $this->assertTrue(str_contains($symfonyMessage->getBody(), 'my html view content')); $this->assertEquals(1, count($parts)); - $this->assertEquals('text/plain', $parts[0]->getHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/plain', $parts[0]->getHeaders()->get('Content-Type')); $this->assertEquals('my plain view content', $parts[0]->getBody()); } @@ -106,11 +106,11 @@ public function testAddContent_Html_Plain_Raw() $parts = $symfonyMessage->getAttachments(); $this->assertTrue($result); - $this->assertEquals('text/html', $symfonyMessage->getHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/html', $symfonyMessage->getHeaders()->get('Content-Type')); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); $this->assertEquals(1, count($parts)); - $this->assertEquals('text/plain', $parts[0]->getHeaders()->getHeaderParameter('Content-Type')); + $this->assertEquals('text/plain', $parts[0]->getHeaders()->get('Content-Type')); $this->assertEquals($raw, $parts[0]->getBody()); } } From 1703596eb9f12a70aaa526f900bc9c14c04f9ee8 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 18 Mar 2022 00:48:58 -0400 Subject: [PATCH 26/26] compare body instead --- tests/unit/system/classes/MailManagerTest.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/tests/unit/system/classes/MailManagerTest.php b/tests/unit/system/classes/MailManagerTest.php index 9be4ac9471..364cb47402 100644 --- a/tests/unit/system/classes/MailManagerTest.php +++ b/tests/unit/system/classes/MailManagerTest.php @@ -39,7 +39,7 @@ public function testAddContent_Html() $result = MailManager::instance()->addContent($this->message, $html, $plain, $raw, $data); $symfonyMessage = $this->message->getSymfonyMessage(); - $this->assertEquals('text/html', $symfonyMessage->getHeaders()->get('Content-Type')); + $this->assertEquals($html, $symfonyMessage->getHtmlBody()); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); } @@ -53,9 +53,8 @@ public function testAddContent_Plain() $symfonyMessage = $this->message->getSymfonyMessage(); $this->assertTrue($result); - $this->assertEquals('text/plain', $symfonyMessage->getHeaders()->get('Content-Type')); + $this->assertEquals($plain, $symfonyMessage->getTextBody()); $this->assertEquals('plain view [test]', $symfonyMessage->getSubject()); - $this->assertEquals('my plain view content', $symfonyMessage->getBody()); } public function testAddContent_Raw() @@ -68,7 +67,6 @@ public function testAddContent_Raw() $symfonyMessage = $this->message->getSymfonyMessage(); $this->assertTrue($result); - $this->assertEquals('text/plain', $symfonyMessage->getHeaders()->get('Content-Type')); $this->assertEquals('No subject', $symfonyMessage->getSubject()); $this->assertEquals($raw, $symfonyMessage->getBody()); } @@ -85,13 +83,11 @@ public function testAddContent_Html_Plain() $parts = $symfonyMessage->getAttachments(); $this->assertTrue($result); - $this->assertEquals('text/html', $symfonyMessage->getHeaders()->get('Content-Type')); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); - $this->assertTrue(str_contains($symfonyMessage->getBody(), 'my html view content')); + $this->assertEquals($html, $symfonyMessage->getHtmlBody()); $this->assertEquals(1, count($parts)); - $this->assertEquals('text/plain', $parts[0]->getHeaders()->get('Content-Type')); - $this->assertEquals('my plain view content', $parts[0]->getBody()); + $this->assertEquals($plain, $symfonyMessage->getTextBody()); } public function testAddContent_Html_Plain_Raw() @@ -106,11 +102,11 @@ public function testAddContent_Html_Plain_Raw() $parts = $symfonyMessage->getAttachments(); $this->assertTrue($result); - $this->assertEquals('text/html', $symfonyMessage->getHeaders()->get('Content-Type')); $this->assertEquals('html view [test]', $symfonyMessage->getSubject()); + $this->assertEquals($html, $symfonyMessage->getHtmlBody()); $this->assertEquals(1, count($parts)); - $this->assertEquals('text/plain', $parts[0]->getHeaders()->get('Content-Type')); - $this->assertEquals($raw, $parts[0]->getBody()); + $this->assertEquals($plain, $symfonyMessage->getTextBody()); + $this->assertEquals($raw, $symfonyMessage->getBody()); } }