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