diff --git a/test/PHPMailer/ParseAddressesTest.php b/test/PHPMailer/ParseAddressesTest.php index f6188b90..707355c4 100644 --- a/test/PHPMailer/ParseAddressesTest.php +++ b/test/PHPMailer/ParseAddressesTest.php @@ -30,17 +30,16 @@ final class ParseAddressesTest extends TestCase { /** * Test RFC822 address splitting using the PHPMailer native implementation - * with the Mbstring extension available. * * @requires extension mbstring - * + * @group mbstringRequired * @dataProvider dataAddressSplitting * * @param string $addrstr The address list string. * @param array $expected The expected function output. * @param string $charset Optional. The charset to use. */ - public function testAddressSplittingNative($addrstr, $expected, $charset = null) + public function testAddressSplitting($addrstr, $expected, $charset = null) { if (isset($charset)) { $parsed = PHPMailer::parseAddresses($addrstr, false, $charset); @@ -48,111 +47,46 @@ final class ParseAddressesTest extends TestCase $parsed = PHPMailer::parseAddresses($addrstr, false); } - $expectedOutput = $expected['default']; - if (empty($expected['native+mbstring']) === false) { - $expectedOutput = $expected['native+mbstring']; - } elseif (empty($expected['native']) === false) { - $expectedOutput = $expected['native']; - } - - $this->verifyExpectations($parsed, $expectedOutput); + $this->verifyExpectations($parsed, $expected); } /** - * Test RFC822 address splitting using the IMAP implementation + * Test decodeHeader using the PHPMailer * with the Mbstring extension available. * - * @requires extension imap * @requires extension mbstring + * @group mbstringExtRequired + * @dataProvider dataDecodeHeader * - * @dataProvider dataAddressSplitting - * - * @param string $addrstr The address list string. + * @param string $addrstr The header string. * @param array $expected The expected function output. - * @param string $charset Optional. The charset to use. */ - public function testAddressSplittingImap($addrstr, $expected, $charset = null) + public function testDecodeHeaderMbstring($addrstr, $expected) { - if (isset($charset)) { - $parsed = PHPMailer::parseAddresses($addrstr, true, $charset); - } else { - $parsed = PHPMailer::parseAddresses($addrstr, true); - } - $expectedOutput = $expected['default']; - if (empty($expected['imap+mbstring']) === false) { - $expectedOutput = $expected['imap+mbstring']; - } elseif (empty($expected['imap']) === false) { - $expectedOutput = $expected['imap']; - } + $parsed = PHPMailer::decodeHeader($addrstr); - $this->verifyExpectations($parsed, $expectedOutput); + $this->assertEquals($parsed, $expected['mbstring']); } - + /** - * Test RFC822 address splitting using the PHPMailer native implementation + * Test decodeHeader using the PHPMailer native implementation * without the Mbstring extension. * - * @dataProvider dataAddressSplitting + * @group mbstringExtDisabled + * @dataProvider dataDecodeHeader * - * @param string $addrstr The address list string. + * @param string $addrstr The header string. * @param array $expected The expected function output. - * @param string $charset Optional. The charset to use. */ - public function testAddressSplittingNativeNoMbstring($addrstr, $expected, $charset = null) + public function testDecodeHeaderNative($addrstr, $expected) { if (extension_loaded('mbstring')) { self::markTestSkipped('Test requires MbString *not* to be available'); } + $parsed = PHPMailer::decodeHeader($addrstr); - if (isset($charset)) { - $parsed = PHPMailer::parseAddresses($addrstr, false, $charset); - } else { - $parsed = PHPMailer::parseAddresses($addrstr, false); - } - - $expectedOutput = $expected['default']; - if (empty($expected['native--mbstring']) === false) { - $expectedOutput = $expected['native--mbstring']; - } elseif (empty($expected['native']) === false) { - $expectedOutput = $expected['native']; - } - - $this->verifyExpectations($parsed, $expectedOutput); - } - - /** - * Test RFC822 address splitting using the IMAP implementation - * without the Mbstring extension. - * - * @requires extension imap - * - * @dataProvider dataAddressSplitting - * - * @param string $addrstr The address list string. - * @param array $expected The expected function output. - * @param string $charset Optional. The charset to use. - */ - public function testAddressSplittingImapNoMbstring($addrstr, $expected, $charset = null) - { - if (extension_loaded('mbstring')) { - self::markTestSkipped('Test requires MbString *not* to be available'); - } - - if (isset($charset)) { - $parsed = PHPMailer::parseAddresses($addrstr, true, $charset); - } else { - $parsed = PHPMailer::parseAddresses($addrstr, true); - } - - $expectedOutput = $expected['default']; - if (empty($expected['imap--mbstring']) === false) { - $expectedOutput = $expected['imap--mbstring']; - } elseif (empty($expected['imap']) === false) { - $expectedOutput = $expected['imap']; - } - - $this->verifyExpectations($parsed, $expectedOutput); + $this->assertEquals($parsed, $expected['native']); } /** @@ -177,15 +111,7 @@ final class ParseAddressesTest extends TestCase * Data provider. * * @return array The array is expected to have an `addrstr` and an `expected` key. - * The `expected` key should - as a minimum - have a `default` key. - * Optionally, the following extra keys are supported: - * - `native` Expected output from the native implementation with or without Mbstring. - * - `native+mbstring` Expected output from the native implementation with Mbstring. - * - `native--mbstring` Expected output from the native implementation without Mbstring. - * - `imap` Expected output from the IMAP implementation with or without Mbstring. - * - `imap+mbstring` Expected output from the IMAP implementation with Mbstring. - * - `imap--mbstring` Expected output from the IMAP implementation without Mbstring. - * Also optionally, an additional `charset` key can be passed, + * The `expected` key should - as a minimum. */ public function dataAddressSplitting() { @@ -194,54 +120,39 @@ final class ParseAddressesTest extends TestCase 'Valid address: single address without name' => [ 'addrstr' => 'joe@example.com', 'expected' => [ - 'default' => [ ['name' => '', 'address' => 'joe@example.com'], - ], ], ], 'Valid address: single address with name' => [ 'addrstr' => 'Joe User ', 'expected' => [ - 'default' => [ - ['name' => 'Joe User', 'address' => 'joe@example.com'], - ], + ['name' => 'Joe User', 'address' => 'joe@example.com'], ], ], 'Valid address: single RFC2047 address folded onto multiple lines' => [ 'addrstr' => "=?iso-8859-1?B?QWJjZGVmZ2ggSWprbG3DsSDmnIPorbDlrqTpoJDntITn?=\r\n" . ' =?iso-8859-1?B?s7vntbE=?= ', 'expected' => [ - 'default' => [ - ['name' => 'Abcdefgh Ijklmñ 會議室預約系統', 'address' => 'xyz@example.com'], - ], + ['name' => 'Abcdefgh Ijklmñ 會議室預約系統', 'address' => 'xyz@example.com'], ], ], 'Valid address: single RFC2047 address with space encoded as _' => [ 'addrstr' => '=?iso-8859-1?Q?Abcdefgh_ijklm=C3=B1?= ', 'expected' => [ - 'default' => [ - ['name' => 'Abcdefgh ijklmñ', 'address' => 'xyz@example.com'], - ], + ['name' => 'Abcdefgh ijklmñ', 'address' => 'xyz@example.com'], ], ], 'Valid address: single address, quotes within name' => [ 'addrstr' => 'Tim "The Book" O\'Reilly ', 'expected' => [ - 'default' => [ - ['name' => 'Tim "The Book" O\'Reilly', 'address' => 'foo@example.com'], - ], - 'imap' => [ - ['name' => 'Tim The Book O\'Reilly', 'address' => 'foo@example.com'], - ], + ['name' => 'Tim "The Book" O\'Reilly', 'address' => 'foo@example.com'], ], ], 'Valid address: two addresses with names' => [ 'addrstr' => 'Joe User , Jill User ', 'expected' => [ - 'default' => [ - ['name' => 'Joe User', 'address' => 'joe@example.com'], - ['name' => 'Jill User', 'address' => 'jill@example.net'], - ], + ['name' => 'Joe User', 'address' => 'joe@example.com'], + ['name' => 'Jill User', 'address' => 'jill@example.net'], ], ], 'Valid address: two addresses with names, one without' => [ @@ -249,84 +160,23 @@ final class ParseAddressesTest extends TestCase . 'Jill User ,' . 'frank@example.com,', 'expected' => [ - 'default' => [ - ['name' => 'Joe User', 'address' => 'joe@example.com'], - ['name' => 'Jill User', 'address' => 'jill@example.net'], - ['name' => '', 'address' => 'frank@example.com'], - ], + ['name' => 'Joe User', 'address' => 'joe@example.com'], + ['name' => 'Jill User', 'address' => 'jill@example.net'], + ['name' => '', 'address' => 'frank@example.com'], ], ], - 'Valid address: multiple address, various formats, including one utf8-encoded name' => [ + 'Valid address: multiple address, various formats, including one utf8-encoded names' => [ 'addrstr' => 'joe@example.com, , Joe Doe ,' . ' "John O\'Groats" ,' . - ' =?utf-8?B?0J3QsNC30LLQsNC90LjQtSDRgtC10YHRgtCw?= ', + ' =?utf-8?B?0J3QsNC30LLQsNC90LjQtSDRgtC10YHRgtCw?= ,' . + ' =?UTF-8?Q?Welcome_to_our_caf=C3=A9!?= =?ISO-8859-1?Q?_Willkommen_in_unserem_Caf=E9!?= =?KOI8-R?Q?_=F0=D2=C9=D7=C5=D4_=D7_=CE=C1=DB=C5_=CB=C1=C6=C5!?= ', 'expected' => [ - 'default' => [ - [ - 'name' => '', - 'address' => 'joe@example.com', - ], - [ - 'name' => '', - 'address' => 'me@example.com', - ], - [ - 'name' => 'Joe Doe', - 'address' => 'doe@example.com', - ], - [ - 'name' => "John O'Groats", - 'address' => 'johnog@example.net', - ], - [ - 'name' => 'Название теста', - 'address' => 'encoded@example.org', - ], - ], - 'native--mbstring' => [ - [ - 'name' => '', - 'address' => 'joe@example.com', - ], - [ - 'name' => '', - 'address' => 'me@example.com', - ], - [ - 'name' => 'Joe Doe', - 'address' => 'doe@example.com', - ], - [ - 'name' => "John O'Groats", - 'address' => 'johnog@example.net', - ], - [ - 'name' => '=?utf-8?B?0J3QsNC30LLQsNC90LjQtSDRgtC10YHRgtCw?=', - 'address' => 'encoded@example.org', - ], - ], - 'imap--mbstring' => [ - [ - 'name' => '', - 'address' => 'joe@example.com', - ], - [ - 'name' => '', - 'address' => 'me@example.com', - ], - [ - 'name' => 'Joe Doe', - 'address' => 'doe@example.com', - ], - [ - 'name' => "John O'Groats", - 'address' => 'johnog@example.net', - ], - [ - 'name' => '=?utf-8?B?0J3QsNC30LLQsNC90LjQtSDRgtC10YHRgtCw?=', - 'address' => 'encoded@example.org', - ], - ], + ['name' => '', 'address' => 'joe@example.com'], + ['name' => '', 'address' => 'me@example.com'], + ['name' => 'Joe Doe', 'address' => 'doe@example.com'], + ['name' => "John O'Groats", 'address' => 'johnog@example.net'], + ['name' => 'Название теста', 'address' => 'encoded@example.org'], + ['name' => 'Welcome to our café! Willkommen in unserem Café! Привет в наше кафе!', 'address' => 'encoded3@example.org'], ], 'charset' => PHPMailer::CHARSET_UTF8, ], @@ -334,22 +184,66 @@ final class ParseAddressesTest extends TestCase // Test cases with invalid addresses. 'Invalid address: single address, incomplete email' => [ 'addrstr' => 'Jill User ', - 'expected' => [ - 'default' => [], - ], + 'expected' => [], ], 'Invalid address: single address, invalid characters in email' => [ 'addrstr' => 'Joe User <{^c\@**Dog^}@cartoon.com>', - 'expected' => [ - 'default' => [], - ], + 'expected' => [], ], 'Invalid address: multiple addresses, invalid periods' => [ 'addrstr' => 'Joe User , Jill User ', + 'expected' => [], + ], + ]; + } + + /** + * Data provider for decodeHeader. + * + * @return array The array is expected to have an `addrstr` and an `expected` key. + * The `expected` key should - as a minimum - have a `mbstring` and `native` key. + * - `mbstring` Expected output from the native implementation with Mbstring. + * - `native` Expected output from the native implementation without Mbstring. + */ + public function dataDecodeHeader() + { + return [ + 'UTF-8' => [ + 'name' => '=?utf-8?B?0J3QsNC30LLQsNC90LjQtSDRgtC10YHRgtCw?=', 'expected' => [ - 'default' => [], + 'mbstring' => 'Название теста', + 'native' => '=?utf-8?B?0J3QsNC30LLQsNC90LjQtSDRgtC10YHRgtCw?=', + ], + ], + 'KOI8-R' => [ + 'name' => '=?koi8-r?B?0J3QsNC30LLQsNC90LjQtSDRgtC10YHRgtCw?=', + 'expected' => [ + 'mbstring' => 'Название теста', + 'native' => '=?koi8-r?B?0J3QsNC30LLQsNC90LjQtSDRgtC10YHRgtCw?=', + ], + ], + 'Simple ISO-8859-1' => [ + 'name' => '=?ISO-8859-1?Q?_Willkommen_in_unserem_Caf=E9!?=', + 'expected' => [ + 'mbstring' => 'Willkommen in unserem Café!', + 'native' => 'Willkommen in unserem Café!', + ], + ], + 'Wrongly labeled ISO-8859-1' => [ + 'name' => '=?iso-8859-1?B?QWJjZGVmZ2ggSWprbG3DsSDmnIPorbDlrqTpoJDntITns7vntbE=?=', + 'expected' => [ + 'mbstring' => 'Abcdefgh Ijklmñ 會議室預約系統', + 'native' => '=?iso-8859-1?B?QWJjZGVmZ2ggSWprbG3DsSDmnIPorbDlrqTpoJDntITn?=', + ], + ], + 'UTF-8' => [ + 'name' => '=?UTF-8?B?SGVsbG8g8J+MjSDkuJbnlYwgY2Fmw6k=?=', + 'expected' => [ + 'mbstring' => 'Hello 🌍 世界 café', + 'native' => '=?UTF-8?B?SGVsbG8g8J+MjSDkuJbnlYwgY2Fmw6k=?=', ], ], ]; } + }