|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Tests\Unit\Domain\Messaging\Service; |
| 6 | + |
| 7 | +use PhpList\Core\Domain\Common\Html2Text; |
| 8 | +use PhpList\Core\Domain\Configuration\Model\ConfigOption; |
| 9 | +use PhpList\Core\Domain\Configuration\Service\Provider\ConfigProvider; |
| 10 | +use PhpList\Core\Domain\Messaging\Model\Template; |
| 11 | +use PhpList\Core\Domain\Messaging\Repository\TemplateRepository; |
| 12 | +use PhpList\Core\Domain\Messaging\Service\Manager\TemplateImageManager; |
| 13 | +use PhpList\Core\Domain\Messaging\Service\SystemMailConstructor; |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +class SystemMailConstructorTest extends TestCase |
| 18 | +{ |
| 19 | + private Html2Text&MockObject $html2Text; |
| 20 | + private ConfigProvider&MockObject $configProvider; |
| 21 | + private TemplateRepository&MockObject $templateRepository; |
| 22 | + private TemplateImageManager&MockObject $templateImageManager; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + $this->html2Text = $this->getMockBuilder(Html2Text::class) |
| 27 | + ->disableOriginalConstructor() |
| 28 | + ->onlyMethods(['__invoke']) |
| 29 | + ->getMock(); |
| 30 | + $this->configProvider = $this->createMock(ConfigProvider::class); |
| 31 | + $this->templateRepository = $this->createMock(TemplateRepository::class); |
| 32 | + $this->templateImageManager = $this->getMockBuilder(TemplateImageManager::class) |
| 33 | + ->disableOriginalConstructor() |
| 34 | + ->onlyMethods(['parseLogoPlaceholders']) |
| 35 | + ->getMock(); |
| 36 | + } |
| 37 | + |
| 38 | + private function createConstructor(bool $poweredByPhplist = false): SystemMailConstructor |
| 39 | + { |
| 40 | + // Defaults needed by constructor |
| 41 | + $this->configProvider->method('getValue')->willReturnMap([ |
| 42 | + [ConfigOption::PoweredByText, '<b>Powered</b> by phpList'], |
| 43 | + [ConfigOption::SystemMessageTemplate, null], |
| 44 | + ]); |
| 45 | + |
| 46 | + return new SystemMailConstructor( |
| 47 | + html2Text: $this->html2Text, |
| 48 | + configProvider: $this->configProvider, |
| 49 | + templateRepository: $this->templateRepository, |
| 50 | + templateImageManager: $this->templateImageManager, |
| 51 | + poweredByPhplist: $poweredByPhplist, |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + public function testPlainTextWithoutTemplateLinkifiedAndNl2br(): void |
| 56 | + { |
| 57 | + $constructor = $this->createConstructor(); |
| 58 | + |
| 59 | + // Html2Text is not used when source is plain text |
| 60 | + $this->html2Text->expects($this->never())->method('__invoke'); |
| 61 | + |
| 62 | + [$html, $text] = $constructor('Line1' . "\n" . 'Visit http://example.com', 'Subject'); |
| 63 | + |
| 64 | + $this->assertSame("Line1\nVisit http://example.com", $text); |
| 65 | + $this->assertStringContainsString('Line1<br', $html); |
| 66 | + $this->assertStringContainsString('<a href="http://example.com">http://example.com</a>', $html); |
| 67 | + } |
| 68 | + |
| 69 | + public function testHtmlSourceWithoutTemplateUsesHtml2Text(): void |
| 70 | + { |
| 71 | + $constructor = $this->createConstructor(); |
| 72 | + |
| 73 | + $this->html2Text->expects($this->once()) |
| 74 | + ->method('__invoke') |
| 75 | + ->with('<p><strong>Hello</strong></p>') |
| 76 | + ->willReturn('Hello'); |
| 77 | + |
| 78 | + [$html, $text] = $constructor('<p><strong>Hello</strong></p>', 'Subject'); |
| 79 | + |
| 80 | + $this->assertSame('<p><strong>Hello</strong></p>', $html); |
| 81 | + $this->assertSame('Hello', $text); |
| 82 | + } |
| 83 | + |
| 84 | + public function testTemplateWithSignaturePlaceholderUsesPoweredByImageWhenFlagFalse(): void |
| 85 | + { |
| 86 | + // Configure template usage |
| 87 | + $this->configProvider->method('getValue')->willReturnMap([ |
| 88 | + [ConfigOption::PoweredByText, '<b>Powered</b>'], |
| 89 | + [ConfigOption::SystemMessageTemplate, '10'], |
| 90 | + [ConfigOption::PoweredByImage, '<img alt="" src="/assets/power-phplist.png" />'], |
| 91 | + ]); |
| 92 | + |
| 93 | + $template = new Template('sys-template'); |
| 94 | + $template->setContent('<html><body>[SUBJECT]: [CONTENT] [SIGNATURE]</body></html>'); |
| 95 | + $template->setText("SUBJ: [SUBJECT]\n[BODY]\n[CONTENT]\n[SIGNATURE]"); |
| 96 | + |
| 97 | + $this->templateRepository->method('findOneById')->with(10)->willReturn($template); |
| 98 | + |
| 99 | + $this->templateImageManager->expects($this->once()) |
| 100 | + ->method('parseLogoPlaceholders') |
| 101 | + ->with($this->callback(fn ($html) => is_string($html))) |
| 102 | + ->willReturnArgument(0); |
| 103 | + |
| 104 | + // Plain text input so Html2Text is called only for powered by text when building text part |
| 105 | + $this->html2Text->expects($this->once()) |
| 106 | + ->method('__invoke') |
| 107 | + ->with('<b>Powered</b>') |
| 108 | + ->willReturn('Powered'); |
| 109 | + |
| 110 | + $constructor = new SystemMailConstructor( |
| 111 | + html2Text: $this->html2Text, |
| 112 | + configProvider: $this->configProvider, |
| 113 | + templateRepository: $this->templateRepository, |
| 114 | + templateImageManager: $this->templateImageManager, |
| 115 | + poweredByPhplist: false, |
| 116 | + ); |
| 117 | + |
| 118 | + [$html, $text] = $constructor('Body', 'Subject'); |
| 119 | + |
| 120 | + // HTML should contain processed powered-by image (src rewritten to powerphplist.png) in place of [SIGNATURE] |
| 121 | + $this->assertStringContainsString('Subject: Body', $html); |
| 122 | + $this->assertStringContainsString('src="powerphplist.png"', $html); |
| 123 | + |
| 124 | + // Text should include powered by text substituted into [SIGNATURE] |
| 125 | + $this->assertStringContainsString("SUBJ: Subject\n[BODY]\nBody\nPowered", $text); |
| 126 | + } |
| 127 | + |
| 128 | + public function testTemplateWithoutSignatureAppendsPoweredByTextAndBeforeBodyEndWhenHtml(): void |
| 129 | + { |
| 130 | + // Configure template usage with poweredByPhplist=true (use text snippet instead of image) |
| 131 | + $this->configProvider->method('getValue')->willReturnMap([ |
| 132 | + [ConfigOption::PoweredByText, '<i>PB</i>'], |
| 133 | + [ConfigOption::SystemMessageTemplate, '11'], |
| 134 | + ]); |
| 135 | + |
| 136 | + $template = new Template('sys-template'); |
| 137 | + $template->setContent('<html><body>[CONTENT]</body></html>'); |
| 138 | + $template->setText('[CONTENT]'); |
| 139 | + $this->templateRepository->method('findOneById')->with(11)->willReturn($template); |
| 140 | + |
| 141 | + $this->templateImageManager->method('parseLogoPlaceholders')->willReturnCallback(static fn ($h) => $h); |
| 142 | + |
| 143 | + // Html2Text is called twice: once for the HTML message -> text, and once for powered-by text |
| 144 | + $this->html2Text->expects($this->exactly(2)) |
| 145 | + ->method('__invoke') |
| 146 | + ->withConsecutive( |
| 147 | + ['Hello <b>World</b>'], |
| 148 | + ['<i>PB</i>'] |
| 149 | + ) |
| 150 | + ->willReturnOnConsecutiveCalls('Hello World', 'PB'); |
| 151 | + |
| 152 | + $constructor = new SystemMailConstructor( |
| 153 | + html2Text: $this->html2Text, |
| 154 | + configProvider: $this->configProvider, |
| 155 | + templateRepository: $this->templateRepository, |
| 156 | + templateImageManager: $this->templateImageManager, |
| 157 | + poweredByPhplist: true, |
| 158 | + ); |
| 159 | + |
| 160 | + [$html, $text] = $constructor('Hello <b>World</b>', 'Sub'); |
| 161 | + |
| 162 | + // HTML path: since poweredByPhplist=true, raw PoweredByText should be inserted before </body> |
| 163 | + $this->assertStringContainsString('Hello <b>World</b>', $html); |
| 164 | + $this->assertMatchesRegularExpression('~<i>PB</i></body>\s*</html>$~', $html); |
| 165 | + |
| 166 | + // TEXT path: PoweredByText (converted) appended with two newlines since no [SIGNATURE] |
| 167 | + $this->assertSame("Hello World\n\nPB", $text); |
| 168 | + } |
| 169 | +} |
0 commit comments