| | 1 | <?php |
| | 2 | /** |
| | 3 | * Test cases for the `_wp_privacy_send_erasure_fulfillment_notification()` function. |
| | 4 | * |
| | 5 | * @package WordPress |
| | 6 | * @subpackage UnitTests |
| | 7 | * @since 4.9.9 |
| | 8 | */ |
| | 9 | |
| | 10 | /** |
| | 11 | * Tests_Privacy_WpPrivacySendErasureFulfillmentNotification class. |
| | 12 | * |
| | 13 | * @group privacy |
| | 14 | * @covers _wp_privacy_send_erasure_fulfillment_notification |
| | 15 | * |
| | 16 | * @since 4.9.9 |
| | 17 | */ |
| | 18 | class Tests_Privacy_WpPrivacySendErasureFulfillmentNotification extends WP_UnitTestCase { |
| | 19 | /** |
| | 20 | * Request ID. |
| | 21 | * |
| | 22 | * @since 4.9.9 |
| | 23 | * |
| | 24 | * @var int $request_id |
| | 25 | */ |
| | 26 | protected static $request_id; |
| | 27 | |
| | 28 | /** |
| | 29 | * Requester Email. |
| | 30 | * |
| | 31 | * @since 4.9.9 |
| | 32 | * |
| | 33 | * @var string $requester_email |
| | 34 | */ |
| | 35 | protected static $requester_email; |
| | 36 | |
| | 37 | /** |
| | 38 | * Create user request fixtures shared by test methods. |
| | 39 | * |
| | 40 | * @since 4.9.9 |
| | 41 | * |
| | 42 | * @param WP_UnitTest_Factory $factory Factory. |
| | 43 | */ |
| | 44 | public static function wpSetUpBeforeClass( $factory ) { |
| | 45 | self::$requester_email = 'erase-my-data@local.dev'; |
| | 46 | self::$request_id = wp_create_user_request( self::$requester_email, 'erase_personal_data' ); |
| | 47 | } |
| | 48 | |
| | 49 | /** |
| | 50 | * Reset the mocked PHPMailer instance before each test method. |
| | 51 | * |
| | 52 | * @since 4.9.9 |
| | 53 | */ |
| | 54 | public function setUp() { |
| | 55 | parent::setUp(); |
| | 56 | |
| | 57 | reset_phpmailer_instance(); |
| | 58 | } |
| | 59 | |
| | 60 | /** |
| | 61 | * Reset the mocked PHPMailer instance after each test method. |
| | 62 | * |
| | 63 | * @since 4.9.9 |
| | 64 | */ |
| | 65 | public function tearDown() { |
| | 66 | reset_phpmailer_instance(); |
| | 67 | |
| | 68 | parent::tearDown(); |
| | 69 | } |
| | 70 | |
| | 71 | /** |
| | 72 | * The function should not send emails when the request ID does not exist. |
| | 73 | * |
| | 74 | * @ticket 44234 |
| | 75 | */ |
| | 76 | public function test_should_not_send_email_when_passed_invalid_request_id() { |
| | 77 | _wp_privacy_send_erasure_fulfillment_notification( 1234567890 ); |
| | 78 | |
| | 79 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 80 | |
| | 81 | $this->assertEmpty( $mailer->mock_sent ); |
| | 82 | } |
| | 83 | |
| | 84 | /** |
| | 85 | * The function should not send emails when the ID passed is not a WP_User_Request. |
| | 86 | * |
| | 87 | * @ticket 44234 |
| | 88 | */ |
| | 89 | public function test_should_not_send_email_when_not_a_wp_user_request() { |
| | 90 | $post_id = $this->factory->post->create( |
| | 91 | array( |
| | 92 | 'post_type' => 'post', |
| | 93 | ) |
| | 94 | ); |
| | 95 | |
| | 96 | _wp_privacy_send_erasure_fulfillment_notification( $post_id ); |
| | 97 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 98 | |
| | 99 | $this->assertEmpty( $mailer->mock_sent ); |
| | 100 | } |
| | 101 | |
| | 102 | /** |
| | 103 | * The function should not send emails when the request is not completed. |
| | 104 | * |
| | 105 | * @ticket 44234 |
| | 106 | */ |
| | 107 | public function test_should_not_send_email_when_request_not_completed() { |
| | 108 | wp_update_post( |
| | 109 | array( |
| | 110 | 'ID' => self::$request_id, |
| | 111 | 'post_status' => 'request-confirmed', |
| | 112 | ) |
| | 113 | ); |
| | 114 | |
| | 115 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 116 | |
| | 117 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 118 | |
| | 119 | $this->assertEmpty( $mailer->mock_sent ); |
| | 120 | $this->assertFalse( metadata_exists( 'post', self::$request_id, '_wp_user_notified' ) ); |
| | 121 | } |
| | 122 | |
| | 123 | /** |
| | 124 | * The function should send an email when a valid request ID is passed. |
| | 125 | * |
| | 126 | * @ticket 44234 |
| | 127 | */ |
| | 128 | public function test_should_send_email_no_privacy_policy() { |
| | 129 | wp_update_post( |
| | 130 | array( |
| | 131 | 'ID' => self::$request_id, |
| | 132 | 'post_status' => 'request-completed', |
| | 133 | ) |
| | 134 | ); |
| | 135 | |
| | 136 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 137 | |
| | 138 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 139 | |
| | 140 | $this->assertContains( self::$requester_email, $mailer->get_sent()->to[0] ); |
| | 141 | $this->assertContains( 'Erasure Request Fulfilled', $mailer->get_sent()->subject ); |
| | 142 | $this->assertContains( 'Your request to erase your personal data', $mailer->get_sent()->body ); |
| | 143 | $this->assertContains( 'has been completed.', $mailer->get_sent()->body ); |
| | 144 | $this->assertContains( wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), $mailer->get_sent()->body ); |
| | 145 | $this->assertContains( home_url(), $mailer->get_sent()->body ); |
| | 146 | |
| | 147 | $this->assertNotContains( 'you can also read our privacy policy', $mailer->get_sent()->body ); |
| | 148 | $this->assertTrue( (bool) get_post_meta( self::$request_id, '_wp_user_notified', true ) ); |
| | 149 | } |
| | 150 | |
| | 151 | /** |
| | 152 | * The email should include a link to the site's privacy policy when set. |
| | 153 | * |
| | 154 | * @ticket 44234 |
| | 155 | */ |
| | 156 | public function test_should_send_email_with_privacy_policy() { |
| | 157 | $privacy_policy = $this->factory->post->create( |
| | 158 | array( |
| | 159 | 'post_type' => 'page', |
| | 160 | 'title' => 'Site Privacy Policy', |
| | 161 | 'post_status' => 'publish', |
| | 162 | ) |
| | 163 | ); |
| | 164 | update_option( 'wp_page_for_privacy_policy', $privacy_policy ); |
| | 165 | |
| | 166 | wp_update_post( |
| | 167 | array( |
| | 168 | 'ID' => self::$request_id, |
| | 169 | 'post_status' => 'request-completed', |
| | 170 | ) |
| | 171 | ); |
| | 172 | |
| | 173 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 174 | |
| | 175 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 176 | |
| | 177 | $this->assertContains( self::$requester_email, $mailer->get_sent()->to[0] ); |
| | 178 | $this->assertContains( 'you can also read our privacy policy', $mailer->get_sent()->body ); |
| | 179 | $this->assertContains( get_privacy_policy_url(), $mailer->get_sent()->body ); |
| | 180 | $this->assertTrue( (bool) get_post_meta( self::$request_id, '_wp_user_notified', true ) ); |
| | 181 | } |
| | 182 | |
| | 183 | /** |
| | 184 | * The function should send a fulfillment email only once. |
| | 185 | * |
| | 186 | * @ticket 44234 |
| | 187 | */ |
| | 188 | public function test_should_send_email_only_once() { |
| | 189 | wp_update_post( |
| | 190 | array( |
| | 191 | 'ID' => self::$request_id, |
| | 192 | 'post_status' => 'request-completed', |
| | 193 | ) |
| | 194 | ); |
| | 195 | |
| | 196 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 197 | |
| | 198 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 199 | |
| | 200 | $this->assertContains( self::$requester_email, $mailer->get_sent()->to[0] ); |
| | 201 | $this->assertContains( 'Erasure Request Fulfilled', $mailer->get_sent()->subject ); |
| | 202 | $this->assertTrue( (bool) get_post_meta( self::$request_id, '_wp_user_notified', true ) ); |
| | 203 | |
| | 204 | reset_phpmailer_instance(); |
| | 205 | _wp_privacy_send_erasure_fulfillment_notification( self::$request_id ); |
| | 206 | $mailer = tests_retrieve_phpmailer_instance(); |
| | 207 | |
| | 208 | $this->assertEmpty( $mailer->mock_sent ); |
| | 209 | $this->assertTrue( metadata_exists( 'post', self::$request_id, '_wp_user_notified' ) ); |
| | 210 | } |
| | 211 | } |