Make WordPress Core

Changeset 62225


Ignore:
Timestamp:
04/11/2026 02:22:15 PM (6 months ago)
Author:
dmsnell
Message:

Email: Add unit tests covering email validation and sanitization.

In preparation for later work to allow non-US-ASCII email addresses, this change extends the unit test suite for is_email() and adds new tests covering antispambot() and sanitize_email().

This work was done collaboratively during WordCamp Vienna, 2026 as a Contributor Challenge in cooperation with and support from ICANN and also GeoTLDs Universal Acceptance Local Initiative.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/11552
Discussed in: https://core.trac.wordpress.org/ticket/31992

Props agulbra, akirk, benniledl, dmsnell.
See #31992.

Location:
trunk/tests/phpunit/tests/formatting
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/formatting/isEmail.php

    r61451 r62225  
    11<?php
    2 
    32/**
     3 * Tests for the is_email() function.
     4 *
    45 * @group formatting
    56 *
    … …  
    78 */
    89class Tests_Formatting_IsEmail extends WP_UnitTestCase {
    9 
    1010        /**
    11          * @dataProvider valid_email_provider
     11         * Ensures that valid emails are returned unchanged.
     12         *
     13         * @ticket 31992
     14         *
     15         * @dataProvider data_valid_email_provider
     16         *
     17         * @param string $email Valid email address.
    1218         */
    1319        public function test_returns_the_email_address_if_it_is_valid( $email ) {
    14                 $this->assertSame( $email, is_email( $email ), "is_email() should return the email address for $email." );
     20                $this->assertSame(
     21                        $email,
     22                        is_email( $email ),
     23                        'Should return the given email address unchanged when valid.'
     24                );
    1525        }
    1626
    1727        /**
    18          * Data provider for valid email addresses.
     28         * Data provider.
    1929         *
    20          * @return array
     30         * @return Generator
    2131         */
    22         public static function valid_email_provider() {
     32        public static function data_valid_email_provider() {
    2333                $valid_emails = array(
    2434                        'bob@example.com',
    2535                        'phil@example.info',
     36                        'phil@TLA.example',
    2637                        'ace@204.32.222.14',
    2738                        'kevin@many.subdomains.make.a.happy.man.edu',
    2839                        'a@b.co',
    2940                        'bill+ted@example.com',
     41                        '..@example.com',
    3042                );
    3143
    … …  
    3648
    3749        /**
    38          * @dataProvider invalid_email_provider
     50         * Ensures that unrecognized email addresses are rejected.
     51         *
     52         * @ticket 31992
     53         *
     54         * @dataProvider data_invalid_email_provider
     55         *
     56         * @param string $email Invalid or unrecognized-to-WordPress email address.
    3957         */
    4058        public function test_returns_false_if_given_an_invalid_email_address( $email ) {
    41                 $this->assertFalse( is_email( $email ), "is_email() should return false for $email." );
     59                $this->assertFalse(
     60                        is_email( $email ),
     61                        'Should have rejected the email as invalid.'
     62                );
    4263        }
    4364
    4465        /**
    45          * Data provider for invalid email addresses.
     66         * Data provider.
    4667         *
    47          * @return array
     68         * @return Generator
    4869         */
    49         public static function invalid_email_provider() {
     70        public static function data_invalid_email_provider() {
    5071                $invalid_emails = array(
    5172                        'khaaaaaaaaaaaaaaan!',
    … …  
    5576                        'bob@your mom',
    5677                        'a@b.c',
     78                        '" "@b.c',
     79                        '"@"@b.c',
     80                        'a@route.org@b.c',
     81                        'h(aj@couc.ou', // bad comment.
     82                        'hi@',
     83                        'hi@hi@couc.ou', // double @.
     84
     85                        /*
     86                         * The next address is not deliverable as described,
     87                         * SMTP servers should strip the (ab), so it is very
     88                         * likely a source of confusion or a typo.
     89                         * Best rejected.
     90                         */
     91                        '(ab)cd@couc.ou',
     92
     93                        /*
     94                         * The next address is not globally deliverable,
     95                         * so it may work with PHPMailer and break with
     96                         * mail sending services. Best not allow users
     97                         * to paint themselves into that corner. This also
     98                         * avoids security problems like those that were
     99                         * used to probe the WordPress server's local
     100                         * network.
     101                        */
     102                        'toto@to',
     103
     104                        /*
     105                         * Several addresses are best rejected because
     106                         * we don't want to allow sending to fe80::, 192.168
     107                         * and other special addresses; that too might
     108                         * be used to probe the WordPress server's local
     109                         * network.
     110                         */
     111                        'to@[2001:db8::1]',
     112                        'to@[IPv6:2001:db8::1]',
     113                        'to@[192.168.1.1]',
     114
     115                        /*
     116                         * Ill-formed UTF-8 byte sequences must be rejected.
     117                         * A lone continuation byte (0x80) is not valid UTF-8
     118                         * whether it appears in the local part or the domain.
     119                         */
     120                        "a\x80b@example.com",  // invalid UTF-8 in local part.
     121                        "abc@\x80.org",        // invalid UTF-8 in domain subdomain.
    57122                );
    58123
Note: See TracChangeset for help on using the changeset viewer.