MailUtilityTest.php 5.88 KB
Newer Older
1
<?php
2
3
namespace TYPO3\CMS\Core\Tests\Unit\Utility;

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
8
9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11
12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
15
 * The TYPO3 project - inspiring people to share!
 */
16

17
18
use TYPO3\CMS\Core\Utility\MailUtility;

19
/**
20
 * Testcase for the \TYPO3\CMS\Core\Utility\MailUtility class.
21
 */
22
class MailUtilityTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23
24
25
26
{
    /**
     * @var array A backup of registered singleton instances
     */
27
    protected $singletonInstances = [];
28

29
30
31
32
    protected function setUp()
    {
        $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
    }
33

34
35
36
37
38
    protected function tearDown()
    {
        \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
        parent::tearDown();
    }
39

40
41
42
43
44
    /**
     * @test
     */
    public function breakLinesForEmailReturnsEmptyStringIfEmptryStringIsGiven()
    {
45
        $this->assertEmpty(MailUtility::breakLinesForEmail(''));
46
    }
47

48
49
50
51
52
53
54
55
    /**
     * @test
     */
    public function breakLinesForEmailReturnsOneLineIfCharWithIsNotExceeded()
    {
        $newlineChar = LF;
        $lineWidth = 76;
        $str = 'This text is not longer than 76 chars and therefore will not be broken.';
56
        $returnString = MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
57
58
        $this->assertEquals(1, count(explode($newlineChar, $returnString)));
    }
59

60
61
62
63
64
65
66
67
    /**
     * @test
     */
    public function breakLinesForEmailBreaksTextIfCharWithIsExceeded()
    {
        $newlineChar = LF;
        $lineWidth = 50;
        $str = 'This text is longer than 50 chars and therefore will be broken.';
68
        $returnString = MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
69
70
        $this->assertEquals(2, count(explode($newlineChar, $returnString)));
    }
71

72
73
74
75
76
77
78
79
80
    /**
     * @test
     */
    public function breakLinesForEmailBreaksTextWithNoSpaceFoundBeforeLimit()
    {
        $newlineChar = LF;
        $lineWidth = 10;
        // first space after 20 chars (more than $lineWidth)
        $str = 'abcdefghijklmnopqrst uvwxyz 123456';
81
        $returnString = MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
82
83
        $this->assertEquals($returnString, 'abcdefghijklmnopqrst' . LF . 'uvwxyz' . LF . '123456');
    }
84

85
86
87
88
89
90
    /**
     * @test
     */
    public function breakLinesForEmailBreaksTextIfLineIsLongerThanTheLineWidth()
    {
        $str = 'Mein Link auf eine News (Link: http://zzzzzzzzzzzzz.xxxxxxxxx.de/index.php?id=10&tx_ttnews%5Btt_news%5D=1&cHash=66f5af320da29b7ae1cda49047ca7358)';
91
        $returnString = MailUtility::breakLinesForEmail($str);
92
93
        $this->assertEquals($returnString, 'Mein Link auf eine News (Link:' . LF . 'http://zzzzzzzzzzzzz.xxxxxxxxx.de/index.php?id=10&tx_ttnews%5Btt_news%5D=1&cHash=66f5af320da29b7ae1cda49047ca7358)');
    }
94

95
96
97
98
99
100
101
    /**
     * Data provider for parseAddressesTest
     *
     * @return array Data sets
     */
    public function parseAddressesProvider()
    {
102
103
104
105
106
107
108
109
110
111
        return [
            'name &ltemail&gt;' => ['name <email@example.org>', ['email@example.org' => 'name']],
            '&lt;email&gt;' => ['<email@example.org>', ['email@example.org']],
            '@localhost' => ['@localhost', []],
            '000@example.com' => ['000@example.com', ['000@example.com']],
            'email' => ['email@example.org', ['email@example.org']],
            'email1,email2' => ['email1@example.org,email2@example.com', ['email1@example.org', 'email2@example.com']],
            'name &ltemail&gt;,email2' => ['name <email1@example.org>,email2@example.com', ['email1@example.org' => 'name', 'email2@example.com']],
            '"last, first" &lt;name@example.org&gt;' => ['"last, first" <email@example.org>', ['email@example.org' => '"last, first"']],
            'email,name &ltemail&gt;,"last, first" &lt;name@example.org&gt;' => [
112
                'email1@example.org, name <email2@example.org>, "last, first" <email3@example.org>',
113
                [
114
115
116
                    'email1@example.org',
                    'email2@example.org' => 'name',
                    'email3@example.org' => '"last, first"'
117
118
119
                ]
            ]
        ];
120
    }
121

122
123
124
125
126
127
    /**
     * @test
     * @dataProvider parseAddressesProvider
     */
    public function parseAddressesTest($source, $addressList)
    {
128
        $returnArray = MailUtility::parseAddresses($source);
129
130
        $this->assertEquals($addressList, $returnArray);
    }
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

    /**
     * @return array
     */
    public function replyToProvider(): array
    {
        return [
            'only address' => [
                ['defaultMailReplyToAddress' => 'noreply@example.org', 'defaultMailReplyToName' => ''],
                ['noreply@example.org'],
            ],
            'name and address' => [
                ['defaultMailReplyToAddress' => 'noreply@example.org', 'defaultMailReplyToName' => 'John Doe'],
                ['noreply@example.org' => 'John Doe'],
            ],
            'no address' => [
                ['defaultMailReplyToAddress' => '', 'defaultMailReplyToName' => ''],
                [],
            ],
            'invalid address' => [
                ['defaultMailReplyToAddress' => 'foo', 'defaultMailReplyToName' => ''],
                [],
            ],
        ];
    }

    /**
     * @test
     * @dataProvider replyToProvider
     * @param array $configuration
     * @param array $expectedReplyTo
     */
    public function getSystemReplyToTest(array $configuration, array $expectedReplyTo)
    {
        $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $configuration;
        $returnArray = MailUtility::getSystemReplyTo();
        $this->assertSame($expectedReplyTo, $returnArray);
    }
169
}