From c06dfacc1bf34e485be39a5fe185b0ea5100642e Mon Sep 17 00:00:00 2001 From: Abood Afifi <38721478+AEM5299@users.noreply.github.com> Date: Wed, 22 Feb 2023 00:22:10 +1100 Subject: [PATCH] [10.x] Fix custom themes not reseting on Markdown renderer (#46200) * test failing * resets theme to default if Mailable theme is null --- src/Illuminate/Mail/Mailable.php | 4 +-- tests/Mail/MailMailableTest.php | 48 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Mail/Mailable.php b/src/Illuminate/Mail/Mailable.php index 8502f55ac49f..0910d4177a0f 100644 --- a/src/Illuminate/Mail/Mailable.php +++ b/src/Illuminate/Mail/Mailable.php @@ -324,9 +324,7 @@ protected function buildMarkdownView() { $markdown = Container::getInstance()->make(Markdown::class); - if (isset($this->theme)) { - $markdown->theme($this->theme); - } + $markdown->theme($this->theme ?? 'default'); $data = $this->buildViewData(); diff --git a/tests/Mail/MailMailableTest.php b/tests/Mail/MailMailableTest.php index ea4801a908d2..6e93d0a19240 100644 --- a/tests/Mail/MailMailableTest.php +++ b/tests/Mail/MailMailableTest.php @@ -7,8 +7,10 @@ use Illuminate\Contracts\View\Factory; use Illuminate\Mail\Attachment; use Illuminate\Mail\Mailable; +use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use Illuminate\Mail\Mailer; +use Illuminate\Mail\Markdown; use Illuminate\Mail\Transport\ArrayTransport; use Mockery as m; use PHPUnit\Framework\AssertionFailedError; @@ -437,6 +439,52 @@ public function testMailableSetsFromCorrectly() } } + public function testMailableSetsMarkdownThemeCorrectly() + { + $viewFactory = m::mock(Factory::class); + $viewFactory->shouldReceive('flushFinderCache'); + $viewFactory->shouldReceive('replaceNamespace')->andReturnSelf(); + $viewFactory->shouldReceive('make')->andReturnSelf(); + $viewFactory->shouldReceive('render')->andReturn('', 'body {}'); + $viewFactory->shouldReceive('exists')->andReturn(true); + + Container::getInstance()->instance(Factory::class, $viewFactory); + Container::getInstance()->singleton(Markdown::class); + Container::getInstance()->instance('mailer', new class + { + public function render() + { + // + } + }); + + (new class() extends Mailable + { + public $theme = 'custom-theme'; + + public function content() + { + return new Content( + markdown: 'mail.markdown', + ); + } + })->render(); + + $this->assertEquals('custom-theme', Container::getInstance()->make(Markdown::class)->getTheme()); + + (new class() extends Mailable + { + public function content() + { + return new Content( + markdown: 'mail.markdown', + ); + } + })->render(); + + $this->assertEquals('default', Container::getInstance()->make(Markdown::class)->getTheme()); + } + public function testMailableSetsSubjectCorrectly() { $mailable = new WelcomeMailableStub;