Skip to content

Commit 2a7e69d

Browse files
Tests: Add REST API user creation email notification tests.
1 parent 8ddfe96 commit 2a7e69d

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

tests/phpunit/tests/rest-api/rest-users-controller.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,52 @@ public function test_create_item() {
13741374
$this->check_add_edit_user_response( $response );
13751375
}
13761376

1377+
/**
1378+
* @ticket 40477
1379+
*/
1380+
public function test_create_user_sends_admin_notification() {
1381+
wp_set_current_user( self::$user );
1382+
reset_phpmailer_instance();
1383+
1384+
$request = new WP_REST_Request( 'POST', '/wp/v2/users' );
1385+
$request->set_param( 'username', 'testuser' );
1386+
$request->set_param( 'email', '[email protected]' );
1387+
$request->set_param( 'password', 'testpassword' );
1388+
$response = rest_get_server()->dispatch( $request );
1389+
1390+
$this->assertSame( 201, $response->get_status() );
1391+
1392+
$mailer = tests_retrieve_phpmailer_instance();
1393+
$this->assertNotEmpty( $mailer->mock_sent, 'No emails were sent' );
1394+
$this->assertSame( get_option( 'admin_email' ), $mailer->mock_sent[0]['to'][0][0] );
1395+
}
1396+
1397+
/**
1398+
* @ticket 40477
1399+
*/
1400+
public function test_create_user_notification_respects_filter() {
1401+
wp_set_current_user( self::$user );
1402+
add_filter( 'rest_wp_user_created_notification', function() {
1403+
return 'both';
1404+
});
1405+
1406+
reset_phpmailer_instance();
1407+
1408+
$user_email = '[email protected]';
1409+
$request = new WP_REST_Request( 'POST', '/wp/v2/users' );
1410+
$request->set_param( 'username', 'testuser2' );
1411+
$request->set_param( 'email', $user_email );
1412+
$request->set_param( 'password', 'testpassword2' );
1413+
rest_get_server()->dispatch( $request );
1414+
1415+
$mailer = tests_retrieve_phpmailer_instance();
1416+
$this->assertCount( 2, $mailer->mock_sent, 'Expected 2 emails (admin + user)' );
1417+
$this->assertSame( get_option( 'admin_email' ), $mailer->mock_sent[0]['to'][0][0] );
1418+
$this->assertSame( $user_email, $mailer->mock_sent[1]['to'][0][0] );
1419+
1420+
remove_all_filters( 'rest_wp_user_created_notification' );
1421+
}
1422+
13771423
public function test_create_item_invalid_username() {
13781424
$this->allow_user_to_manage_multisite();
13791425

0 commit comments

Comments
 (0)