Contao\CoreBundle\Test\Contao\ValidatorTest::testExtractEmail PHP Method

testExtractEmail() public method

Tests the StringUtil::extactEmail() method.
public testExtractEmail ( )
    public function testExtractEmail()
    {
        $text = <<<EOF
This is a [email protected] and this a [email protected]. Another [email protected] and also a [email protected] or an [email protected]. There are "[email protected]"@example.com and "very.(),:;<>[]\\".VERY.\\"very@\\ \\"very\\".unusual"@strange.example.com and even !#\$%&'*+-/=?^_`{}|[email protected] or "()<>[]:,;@\\"!#\$%&'*+-/=?^_`{}|~.a"@example.org but they are all valid.
IP addresses as in user@[255.255.255.255], user@[IPv6:2001:db8:1ff::a0b:dbd0], user@[IPv6:2001:0db8:85a3:08d3:1319:8a2e:0370:7344], user@[IPv6:2001::7344] or user@[IPv6:1111:2222:3333:4444:5555:6666:255.255.255.255] are valid, too.
We also support IDNA domains as in test@exämple.com, test@ä-.xe, [email protected], test@wähwähwäh.ümläüts.de or "tes@t"@wähwähwäh.ümläüts.de. And we support new TLDs as in [email protected] or [email protected].
And we support unicode characters in the local part (RFC 6531) as in niceä[email protected], véry.ç[email protected], a.lîttle.lengthy.but.fiñ[email protected], dîsposable.style.é[email protected], other.é[email protected], "verî.uñ[email protected]ñusual.com"@example.com, "verî.(),:;<>[]\\".VERÎ.\\"verî@\\ \\"verî\\".unüsual"@strange.example.com, üñîçøðé@example.com, "üñîçøðé"@example.com or DžǼ੧ఘⅧ⒇৪@example.com.
Of course also with IP addresses: üser@[255.255.255.255], üser@[IPv6:2001:db8:1ff::a0b:dbd0], üser@[IPv6:2001:0db8:85a3:08d3:1319:8a2e:0370:7344], üser@[IPv6:2001::7344] or üser@[IPv6:1111:2222:3333:4444:5555:6666:255.255.255.255] and unicode characters in the local part: tést@exämple.com, tést@ä-.xe, té[email protected], tést@wähwähwäh.ümläüts.de, "tés@t"@wähwähwäh.ümläüts.de. New TLDs? No problem: té[email protected] or té[email protected].
And hopefully we do not match invalid addresses such as [email protected], [email protected], test@_smtp_.example.com, [email protected], test@subexamplecom, Abc.example.com, a"b(c)d,e:f;gi[j\\k][email protected], this is"not\\[email protected], this\\ still\\"not\\[email protected], (comment)[email protected], test@[1.2.3.4, @ or test@.
Also, we should correctly parse <a href="mailto:[email protected]">[email protected]</a>, <a href="mailto:[email protected]">write us</a> and <strong>[email protected]</strong>.
EOF;
        $expected = ['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '"[email protected]"@example.com', '"very.(),:;<>[]\\".VERY.\\"very@\\ \\"very\\".unusual"@strange.example.com', '!#$%&\'*+-/=?^_`{}|[email protected]', '"()<>[]:,;@\\"!#$%&\'*+-/=?^_`{}|~.a"@example.org', 'user@[255.255.255.255]', 'user@[IPv6:2001:db8:1ff::a0b:dbd0]', 'user@[IPv6:2001:0db8:85a3:08d3:1319:8a2e:0370:7344]', 'user@[IPv6:2001::7344]', 'user@[IPv6:1111:2222:3333:4444:5555:6666:255.255.255.255]', 'test@exämple.com', 'test@ä-.xe', '[email protected]', 'test@wähwähwäh.ümläüts.de', '"tes@t"@wähwähwäh.ümläüts.de', '[email protected]', '[email protected]', 'niceä[email protected]', 'véry.ç[email protected]', 'a.lîttle.lengthy.but.fiñ[email protected]', 'dîsposable.style.é[email protected]', 'other.é[email protected]', '"verî.uñ[email protected]ñusual.com"@example.com', '"verî.(),:;<>[]\\".VERÎ.\\"verî@\\ \\"verî\\".unüsual"@strange.example.com', 'üñîçøðé@example.com', '"üñîçøðé"@example.com', 'DžǼ੧ఘⅧ⒇৪@example.com', 'üser@[255.255.255.255]', 'üser@[IPv6:2001:db8:1ff::a0b:dbd0]', 'üser@[IPv6:2001:0db8:85a3:08d3:1319:8a2e:0370:7344]', 'üser@[IPv6:2001::7344]', 'üser@[IPv6:1111:2222:3333:4444:5555:6666:255.255.255.255]', 'tést@exämple.com', 'tést@ä-.xe', 'té[email protected]', 'tést@wähwähwäh.ümläüts.de', '"tés@t"@wähwähwäh.ümläüts.de', 'té[email protected]', 'té[email protected]', '[email protected]', '[email protected]', '[email protected]'];
        $actual = StringUtil::extractEmail($text, '<a><strong>');
        sort($actual);
        sort($expected);
        $this->assertEquals($expected, $actual);
    }