|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for the behavior of `wp_application_password_created_notification()`. |
| 5 | + * |
| 6 | + * @group functions |
| 7 | + * @group auth |
| 8 | + * |
| 9 | + * @covers ::wp_application_password_created_notification |
| 10 | + */ |
| 11 | +class Tests_Functions_wpApplicationPasswordCreatedNotification extends WP_UnitTestCase { |
| 12 | + |
| 13 | + /** |
| 14 | + * Test user. |
| 15 | + * |
| 16 | + * @var WP_User |
| 17 | + */ |
| 18 | + protected static $user; |
| 19 | + |
| 20 | + /** |
| 21 | + * Creates the user shared by the test methods. |
| 22 | + * |
| 23 | + * @param WP_UnitTest_Factory $factory Factory. |
| 24 | + */ |
| 25 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 26 | + self::$user = $factory->user->create_and_get( |
| 27 | + array( |
| 28 | + 'user_email' => 'app-password-user@example.org', |
| 29 | + 'display_name' => 'App Password User', |
| 30 | + ) |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function set_up() { |
| 35 | + parent::set_up(); |
| 36 | + reset_phpmailer_instance(); |
| 37 | + } |
| 38 | + |
| 39 | + public function tear_down() { |
| 40 | + reset_phpmailer_instance(); |
| 41 | + parent::tear_down(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @ticket 63927 |
| 46 | + */ |
| 47 | + public function test_sends_email_when_application_password_is_created() { |
| 48 | + $created = WP_Application_Passwords::create_new_application_password( self::$user->ID, array( 'name' => 'My App' ) ); |
| 49 | + |
| 50 | + $this->assertNotWPError( $created, 'The application password should be created.' ); |
| 51 | + |
| 52 | + $mailer = tests_retrieve_phpmailer_instance(); |
| 53 | + $this->assertCount( 1, $mailer->mock_sent, 'Exactly one email should be sent.' ); |
| 54 | + $this->assertSame( self::$user->user_email, $mailer->get_recipient( 'to' )->address ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @ticket 63927 |
| 59 | + */ |
| 60 | + public function test_email_replaces_all_placeholders() { |
| 61 | + update_option( 'blogname', 'Test Blog' ); |
| 62 | + update_option( 'admin_email', 'site-admin@example.org' ); |
| 63 | + // Changing the admin email sends its own notification. |
| 64 | + reset_phpmailer_instance(); |
| 65 | + |
| 66 | + wp_application_password_created_notification( self::$user->ID, array( 'name' => 'My App' ) ); |
| 67 | + |
| 68 | + $mailer = tests_retrieve_phpmailer_instance(); |
| 69 | + $sent = $mailer->get_sent(); |
| 70 | + |
| 71 | + $this->assertSame( '[Test Blog] Application Password Created', $sent->subject ); |
| 72 | + $this->assertStringNotContainsString( '###', $sent->body, 'All placeholders should be replaced.' ); |
| 73 | + $this->assertStringContainsString( 'Hi App Password User,', $sent->body ); |
| 74 | + $this->assertStringContainsString( 'Application password name: My App', $sent->body ); |
| 75 | + $this->assertStringContainsString( 'site-admin@example.org', $sent->body ); |
| 76 | + $this->assertStringContainsString( 'This email has been sent to app-password-user@example.org', $sent->body ); |
| 77 | + $this->assertStringContainsString( 'All at Test Blog', $sent->body ); |
| 78 | + $this->assertStringContainsString( home_url(), $sent->body ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @ticket 63927 |
| 83 | + */ |
| 84 | + public function test_does_not_send_email_when_disabled_by_filter() { |
| 85 | + add_filter( 'wp_send_application_password_created_email', '__return_false' ); |
| 86 | + |
| 87 | + wp_application_password_created_notification( self::$user->ID, array( 'name' => 'My App' ) ); |
| 88 | + |
| 89 | + $this->assertEmpty( tests_retrieve_phpmailer_instance()->mock_sent ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @ticket 63927 |
| 94 | + */ |
| 95 | + public function test_send_filter_receives_user_and_application_password() { |
| 96 | + $filter = new MockAction(); |
| 97 | + add_filter( 'wp_send_application_password_created_email', array( $filter, 'filter' ), 10, 3 ); |
| 98 | + |
| 99 | + $new_item = array( 'name' => 'My App' ); |
| 100 | + wp_application_password_created_notification( self::$user->ID, $new_item ); |
| 101 | + |
| 102 | + $args = $filter->get_args(); |
| 103 | + $this->assertCount( 1, $args, 'The filter should run once.' ); |
| 104 | + $this->assertTrue( $args[0][0], 'The email should be sent by default.' ); |
| 105 | + $this->assertSame( self::$user->ID, $args[0][1]->ID ); |
| 106 | + $this->assertSame( $new_item, $args[0][2] ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @ticket 63927 |
| 111 | + */ |
| 112 | + public function test_email_can_be_changed_by_filter() { |
| 113 | + add_filter( |
| 114 | + 'wp_application_password_created_email', |
| 115 | + static function ( $email ) { |
| 116 | + $email['to'] = 'someone-else@example.org'; |
| 117 | + $email['subject'] = 'Custom subject for %s'; |
| 118 | + $email['message'] = 'Custom message for ###APPLICATION_PASSWORD_NAME###'; |
| 119 | + return $email; |
| 120 | + } |
| 121 | + ); |
| 122 | + update_option( 'blogname', 'Test Blog' ); |
| 123 | + |
| 124 | + wp_application_password_created_notification( self::$user->ID, array( 'name' => 'My App' ) ); |
| 125 | + |
| 126 | + $mailer = tests_retrieve_phpmailer_instance(); |
| 127 | + $sent = $mailer->get_sent(); |
| 128 | + |
| 129 | + $this->assertSame( 'someone-else@example.org', $mailer->get_recipient( 'to' )->address ); |
| 130 | + $this->assertSame( 'Custom subject for Test Blog', $sent->subject ); |
| 131 | + $this->assertStringContainsString( 'Custom message for My App', $sent->body, 'Placeholders in a filtered message should still be replaced.' ); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @ticket 63927 |
| 136 | + * @dataProvider data_does_not_send_email_for_invalid_input |
| 137 | + * |
| 138 | + * @param callable $get_args Returns the user ID and application password details to pass. |
| 139 | + */ |
| 140 | + public function test_does_not_send_email_for_invalid_input( $get_args ) { |
| 141 | + list( $user_id, $new_item ) = $get_args( self::$user->ID ); |
| 142 | + |
| 143 | + wp_application_password_created_notification( $user_id, $new_item ); |
| 144 | + |
| 145 | + $this->assertEmpty( tests_retrieve_phpmailer_instance()->mock_sent ); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Data provider for test_does_not_send_email_for_invalid_input(). |
| 150 | + * |
| 151 | + * @return array<string, array{0: callable}> |
| 152 | + */ |
| 153 | + public static function data_does_not_send_email_for_invalid_input() { |
| 154 | + return array( |
| 155 | + 'user does not exist' => array( |
| 156 | + static function () { |
| 157 | + return array( PHP_INT_MAX, array( 'name' => 'My App' ) ); |
| 158 | + }, |
| 159 | + ), |
| 160 | + 'application password unnamed' => array( |
| 161 | + static function ( $user_id ) { |
| 162 | + return array( $user_id, array( 'name' => '' ) ); |
| 163 | + }, |
| 164 | + ), |
| 165 | + 'name missing' => array( |
| 166 | + static function ( $user_id ) { |
| 167 | + return array( $user_id, array() ); |
| 168 | + }, |
| 169 | + ), |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @ticket 63927 |
| 175 | + */ |
| 176 | + public function test_does_not_send_email_when_user_email_is_invalid() { |
| 177 | + global $wpdb; |
| 178 | + |
| 179 | + $user_id = self::factory()->user->create(); |
| 180 | + $wpdb->update( $wpdb->users, array( 'user_email' => 'not-an-email' ), array( 'ID' => $user_id ) ); |
| 181 | + clean_user_cache( $user_id ); |
| 182 | + |
| 183 | + wp_application_password_created_notification( $user_id, array( 'name' => 'My App' ) ); |
| 184 | + |
| 185 | + $this->assertEmpty( tests_retrieve_phpmailer_instance()->mock_sent ); |
| 186 | + } |
| 187 | +} |
0 commit comments