Make WordPress Core


Ignore:
Timestamp:
07/12/2022 09:03:39 PM (4 years ago)
Author:
johnbillion
Message:

Users: Allow conditional supression of the email notifications that are sent when a new user account is registered.

This introduces the following new filters:

  • wp_send_new_user_notification_to_admin
  • wp_send_new_user_notification_to_user

Props janthiel, costdev, audrasjb, peterwilsoncc

Fixes #54874

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user.php

    r53408 r53698  
    13451345
    13461346        /**
     1347         * Test that admin notification of a new user registration is dependent
     1348         * on the 'wp_send_new_user_notification_to_admin' filter.
     1349         *
     1350         * @dataProvider data_wp_send_new_user_notification_filters
     1351         *
     1352         * @ticket 54874
     1353         *
     1354         * @covers ::wp_new_user_notification
     1355         *
     1356         * @param bool   $expected Whether the email should be sent.
     1357         * @param string $callback The callback to pass to the filter.
     1358         */
     1359        public function test_wp_send_new_user_notification_to_admin_filter( $expected, $callback ) {
     1360                reset_phpmailer_instance();
     1361
     1362                add_filter( 'wp_send_new_user_notification_to_admin', $callback );
     1363
     1364                wp_new_user_notification( self::$contrib_id, null, 'admin' );
     1365
     1366                $mailer    = tests_retrieve_phpmailer_instance();
     1367                $recipient = $mailer->get_recipient( 'to' );
     1368                $actual    = $recipient ? WP_TESTS_EMAIL === $recipient->address : false;
     1369
     1370                $this->assertSame( $expected, $actual, 'Admin email result was not as expected in test_wp_send_new_user_notification_to_admin_filter' );
     1371        }
     1372
     1373        /**
     1374         * Test that user notification of a new user registration is dependent
     1375         * on the 'wp_send_new_user_notification_to_user' filter.
     1376         *
     1377         * @dataProvider data_wp_send_new_user_notification_filters
     1378         *
     1379         * @ticket 54874
     1380         *
     1381         * @covers ::wp_new_user_notification
     1382         *
     1383         * @param bool   $expected Whether the email should be sent.
     1384         * @param string $callback The callback to pass to the filter.
     1385         */
     1386        public function test_wp_send_new_user_notification_to_user_filter( $expected, $callback ) {
     1387                reset_phpmailer_instance();
     1388
     1389                add_filter( 'wp_send_new_user_notification_to_user', $callback );
     1390
     1391                wp_new_user_notification( self::$contrib_id, null, 'user' );
     1392
     1393                $mailer    = tests_retrieve_phpmailer_instance();
     1394                $recipient = $mailer->get_recipient( 'to' );
     1395                $actual    = $recipient ? 'blackburn@battlefield3.com' === $recipient->address : false;
     1396
     1397                $this->assertSame( $expected, $actual, 'User email result was not as expected in test_wp_send_new_user_notification_to_user_filter' );
     1398        }
     1399
     1400        /**
     1401         * Data provider.
     1402         *
     1403         * @return array
     1404         */
     1405        public function data_wp_send_new_user_notification_filters() {
     1406                return array(
     1407                        'true'          => array(
     1408                                'expected' => true,
     1409                                'callback' => '__return_true',
     1410                        ),
     1411                        'false'         => array(
     1412                                'expected' => false,
     1413                                'callback' => '__return_false',
     1414                        ),
     1415                        'null'          => array(
     1416                                'expected' => false,
     1417                                'callback' => '__return_null',
     1418                        ),
     1419                        'empty array'   => array(
     1420                                'expected' => false,
     1421                                'callback' => '__return_empty_array',
     1422                        ),
     1423                        'zero int'      => array(
     1424                                'expected' => false,
     1425                                'callback' => '__return_zero',
     1426                        ),
     1427                        'zero float'    => array(
     1428                                'expected' => false,
     1429                                'callback' => array( $this, 'cb_return_zero_float' ),
     1430                        ),
     1431                        'zero string'   => array(
     1432                                'expected' => false,
     1433                                'callback' => array( $this, 'cb_return_zero_string' ),
     1434                        ),
     1435                        'array( true )' => array(
     1436                                'expected' => false,
     1437                                'callback' => array( $this, 'cb_return_array_true' ),
     1438                        ),
     1439                );
     1440        }
     1441
     1442        /**
     1443         * Callback that returns 0.0.
     1444         *
     1445         * @return float 0.0.
     1446         */
     1447        public function cb_return_zero_float() {
     1448                return 0.0;
     1449        }
     1450
     1451        /**
     1452         * Callback that returns '0'.
     1453         *
     1454         * @return string '0'.
     1455         */
     1456        public function cb_return_zero_string() {
     1457                return '0';
     1458        }
     1459
     1460        /**
     1461         * Callback that returns array( true ).
     1462         *
     1463         * @return array array( true )
     1464         */
     1465        public function cb_return_array_true() {
     1466                return array( true );
     1467        }
     1468
     1469        /**
    13471470         * Ensure blog's admin email change notification emails do not contain encoded HTML entities
    13481471         *
Note: See TracChangeset for help on using the changeset viewer.