File size: 3,313 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

declare(strict_types=1);

namespace Mautic\EmailBundle\Tests\Helper;

use Mautic\EmailBundle\Helper\PlainTextHelper;
use PHPUnit\Framework\TestCase;

class PlainTextHelperTest extends TestCase
{
    /**
     * @dataProvider emailContentProvider
     */
    public function testGetText(string $htmlContent, string $expectedPlainText): void
    {
        $plainTextHelper = new PlainTextHelper();
        $plainTextHelper->setHtml($htmlContent);
        $actualPlainText = $plainTextHelper->getText();

        $this->assertEquals($expectedPlainText, $actualPlainText);
    }

    /**
     * @return array<int, array<int, string>>
     */
    public function emailContentProvider(): array
    {
        return [
            // Test case 1: Simple paragraph
            [
                '<p>This is a simple paragraph.</p>',
                'This is a simple paragraph.',
            ],
            // Test case 2: Line breaks
            [
                '<p>This is line one.<br>This is line two.</p>',
                "This is line one.\nThis is line two.",
            ],
            // Test case 3: Links
            [
                '<p>Check this <a href="http://example.com">link</a>.</p>',
                'Check this link [http://example.com].',
            ],
            // Test case 4: Bold text
            [
                '<p>This is <strong>bold</strong> text.</p>',
                'This is bold text.',
            ],
            // Test case 5: Full html body
            [
                '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test Email</title>
</head>
<body>
    <h1>Welcome to Our Newsletter</h1>
    <p>This is an example paragraph in our email content.</p>
    <!-- More HTML content here -->
</body>
</html>',
                "WELCOME TO OUR NEWSLETTER\n\nThis is an example paragraph in our email content.",
            ],
        ];
    }

    /**
     * @dataProvider getPreviewProvider
     */
    public function testGetPreview(?int $previewLength, string $htmlContent, string $expectedPlainText): void
    {
        $options = [];
        if ($previewLength) {
            $options['preview_length'] = $previewLength;
        }
        $plainTextHelper = new PlainTextHelper($options);
        $plainTextHelper->setHtml($htmlContent);
        $actualPlainText = $plainTextHelper->getPreview();

        $this->assertEquals($expectedPlainText, $actualPlainText);
    }

    /**
     * @return array<int, array<int, string|int|null>>
     */
    public function getPreviewProvider(): array
    {
        return [
            // Test case 1: Simple paragraph, with default options
            [
                null,
                '<p>This is a simple paragraph.</p>',
                'This is a simple paragraph.',
            ],
            // Test case 2: Simple paragraph, with length set to 10 (whitespace truncated)
            [
                10,
                '<p>This is a simple paragraph.</p>',
                'This is a...',
            ],
            // Test case 3: Full html body
            [
                25,
                '<h1>Welcome to Our Newsletter</h1>
    <p>This is an example paragraph in our email content.</p>',
                'WELCOME TO OUR NEWSLETTER...',
            ],
        ];
    }
}