| 1 | <?php |
| 2 | /** |
| 3 | * Test the `wp_create_user_request()` function. |
| 4 | * |
| 5 | * @package WordPress |
| 6 | * @subpackage UnitTests |
| 7 | * @since 4.9.9 |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Tests_WpCreateUserRequest class. |
| 12 | * |
| 13 | * @group privacy |
| 14 | * @covers wp_create_user_request |
| 15 | * |
| 16 | * @since 4.9.9 |
| 17 | */ |
| 18 | class Tests_WpCreateUserRequest 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 | * Request email for a registered user. |
| 30 | * |
| 31 | * @since 4.9.9 |
| 32 | * |
| 33 | * @var string $registered_user_email |
| 34 | */ |
| 35 | protected static $registered_user_email; |
| 36 | |
| 37 | /** |
| 38 | * Request email for a non-registered user. |
| 39 | * |
| 40 | * @since 4.9.9 |
| 41 | * |
| 42 | * @var string $non_registered_user_email |
| 43 | */ |
| 44 | protected static $non_registered_user_email; |
| 45 | |
| 46 | /** |
| 47 | * Test user ID. |
| 48 | * |
| 49 | * @since 4.9.9 |
| 50 | * |
| 51 | * @var string $user_id |
| 52 | */ |
| 53 | protected static $user_id; |
| 54 | |
| 55 | /** |
| 56 | * Create fixtures. |
| 57 | * |
| 58 | * @param WP_UnitTest_Factory $factory Factory. |
| 59 | */ |
| 60 | public static function wpSetUpBeforeClass( $factory ) { |
| 61 | self::$registered_user_email = 'export@local.test'; |
| 62 | self::$non_registered_user_email = 'non-registered-user@local.test'; |
| 63 | |
| 64 | self::$user_id = $factory->user->create( |
| 65 | array( |
| 66 | 'user_email' => self::$registered_user_email, |
| 67 | ) |
| 68 | ); |
| 69 | |
| 70 | self::$request_id = $factory->post->create( |
| 71 | array( |
| 72 | 'post_type' => 'user_request', |
| 73 | 'post_author' => self::$user_id, |
| 74 | 'post_name' => 'export_personal_data', |
| 75 | 'post_status' => 'request-pending', |
| 76 | 'post_title' => self::$registered_user_email, |
| 77 | ) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Ensure a WP_Error is returned when an invalid email is passed. |
| 83 | * |
| 84 | * @ticket 44707 |
| 85 | */ |
| 86 | public function test_invalid_email() { |
| 87 | $actual = wp_create_user_request( 'not-a-valid-email', 'export_personal_data' ); |
| 88 | |
| 89 | $this->assertWPError( $actual ); |
| 90 | $this->assertSame( 'invalid_email', $actual->get_error_code() ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Ensure a WP_Error is returned when an invalid action is passed. |
| 95 | * |
| 96 | * @ticket 44707 |
| 97 | */ |
| 98 | public function test_invalid_action() { |
| 99 | $actual = wp_create_user_request( self::$registered_user_email, false ); |
| 100 | |
| 101 | $this->assertWPError( $actual ); |
| 102 | $this->assertSame( 'invalid_action', $actual->get_error_code() ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * When there are duplicate requests for a registered user, a WP_Error should be returned. |
| 107 | * |
| 108 | * @ticket 44707 |
| 109 | */ |
| 110 | public function test_failure_due_to_duplicate_registered_user() { |
| 111 | // Second request (duplicated). |
| 112 | $actual = wp_create_user_request( self::$registered_user_email, 'export_personal_data' ); |
| 113 | |
| 114 | $this->assertWPError( $actual ); |
| 115 | $this->assertSame( 'duplicate_request', $actual->get_error_code() ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * When there are duplicate requests for an non-registered user, a WP_Error should be returned. |
| 120 | * |
| 121 | * @ticket 44707 |
| 122 | */ |
| 123 | public function test_failure_due_to_duplicate_unregistered_user() { |
| 124 | // Update first request. |
| 125 | wp_update_post( |
| 126 | array( |
| 127 | 'ID' => self::$request_id, |
| 128 | 'post_author' => 0, |
| 129 | 'post_title' => self::$non_registered_user_email, |
| 130 | ) |
| 131 | ); |
| 132 | |
| 133 | // Second request (duplicated). |
| 134 | $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data' ); |
| 135 | |
| 136 | $this->assertWPError( $actual ); |
| 137 | $this->assertSame( 'duplicate_request', $actual->get_error_code() ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Test a user request is created successfully for a registered user. |
| 142 | * |
| 143 | * @ticket 44707 |
| 144 | */ |
| 145 | public function test_create_request_registered_user() { |
| 146 | wp_delete_post( self::$request_id, true ); |
| 147 | |
| 148 | $test_data = array( |
| 149 | 'test-data' => 'test value here', |
| 150 | 'test index' => 'more privacy data', |
| 151 | ); |
| 152 | |
| 153 | $actual = wp_create_user_request( self::$registered_user_email, 'export_personal_data', $test_data ); |
| 154 | |
| 155 | $this->assertNotWPError( $actual ); |
| 156 | |
| 157 | $post = get_post( $actual ); |
| 158 | |
| 159 | $this->assertSame( self::$user_id, (int) $post->post_author ); |
| 160 | $this->assertSame( 'export_personal_data', $post->post_name ); |
| 161 | $this->assertSame( self::$registered_user_email, $post->post_title ); |
| 162 | $this->assertSame( 'request-pending', $post->post_status ); |
| 163 | $this->assertSame( 'user_request', $post->post_type ); |
| 164 | $this->assertSame( wp_json_encode( $test_data ), $post->post_content ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Test a user request is created successfully for an non-registered user. |
| 169 | * |
| 170 | * @ticket 44707 |
| 171 | */ |
| 172 | public function test_create_request_unregistered_user() { |
| 173 | wp_delete_post( self::$request_id, true ); |
| 174 | |
| 175 | $test_data = array( |
| 176 | 'test-data' => 'test value here', |
| 177 | 'test index' => 'more privacy data', |
| 178 | ); |
| 179 | |
| 180 | $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data', $test_data ); |
| 181 | |
| 182 | $this->assertNotWPError( $actual ); |
| 183 | |
| 184 | $post = get_post( $actual ); |
| 185 | |
| 186 | $this->assertSame( 0, (int) $post->post_author ); |
| 187 | $this->assertSame( 'export_personal_data', $post->post_name ); |
| 188 | $this->assertSame( self::$non_registered_user_email, $post->post_title ); |
| 189 | $this->assertSame( 'request-pending', $post->post_status ); |
| 190 | $this->assertSame( 'user_request', $post->post_type ); |
| 191 | $this->assertSame( wp_json_encode( $test_data ), $post->post_content ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Test that a pre-existing request for the same registered user that is not pending or confirmed status does not |
| 196 | * block a new request. |
| 197 | * |
| 198 | * @ticket 44707 |
| 199 | */ |
| 200 | public function test_completed_request_does_not_block_new_request() { |
| 201 | // Update first request. |
| 202 | wp_update_post( |
| 203 | array( |
| 204 | 'ID' => self::$request_id, |
| 205 | 'post_status' => 'request-completed', // Not 'request-pending' or 'request-confirmed'. |
| 206 | ) |
| 207 | ); |
| 208 | |
| 209 | // Second request. |
| 210 | $actual = wp_create_user_request( self::$registered_user_email, 'export_personal_data' ); |
| 211 | |
| 212 | $this->assertNotWPError( $actual ); |
| 213 | |
| 214 | $post = get_post( $actual ); |
| 215 | |
| 216 | $this->assertSame( self::$registered_user_email, $post->post_title ); |
| 217 | $this->assertSame( 'request-pending', $post->post_status ); |
| 218 | $this->assertSame( 'user_request', $post->post_type ); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Test that a pre-existing request for the same non-registered user that is not pending or confirmed status does not |
| 223 | * block a new request. |
| 224 | * |
| 225 | * @ticket 44707 |
| 226 | */ |
| 227 | public function test_completed_request_does_not_block_new_request_for_unregistered_user() { |
| 228 | wp_update_post( |
| 229 | array( |
| 230 | 'ID' => self::$request_id, |
| 231 | 'post_author' => 0, |
| 232 | 'post_title' => self::$non_registered_user_email, |
| 233 | 'post_status' => 'request-failed', // Not 'request-pending' or 'request-confirmed'. |
| 234 | ) |
| 235 | ); |
| 236 | |
| 237 | $actual = wp_create_user_request( self::$non_registered_user_email, 'export_personal_data' ); |
| 238 | |
| 239 | $this->assertNotWPError( $actual ); |
| 240 | |
| 241 | $post = get_post( $actual ); |
| 242 | |
| 243 | $this->assertSame( 0, (int) $post->post_author ); |
| 244 | $this->assertSame( 'export_personal_data', $post->post_name ); |
| 245 | $this->assertSame( self::$non_registered_user_email, $post->post_title ); |
| 246 | $this->assertSame( 'request-pending', $post->post_status ); |
| 247 | $this->assertSame( 'user_request', $post->post_type ); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Test that an error from `wp_insert_post()` is returned. |
| 252 | * |
| 253 | * @ticket 44707 |
| 254 | */ |
| 255 | public function test_wp_error_returned_from_wp_insert_post() { |
| 256 | wp_delete_post( self::$request_id, true ); |
| 257 | |
| 258 | add_filter( 'wp_insert_post_empty_content', '__return_true' ); |
| 259 | $actual = wp_create_user_request( self::$registered_user_email, 'export_personal_data' ); |
| 260 | |
| 261 | $this->assertWPError( $actual ); |
| 262 | $this->assertSame( 'empty_content', $actual->get_error_code() ); |
| 263 | } |
| 264 | } |