| | 1 | <?php |
| | 2 | /** |
| | 3 | * Test cases for the `wp_privacy_process_personal_data_export_page()` function. |
| | 4 | * |
| | 5 | * @package WordPress\UnitTests |
| | 6 | * @since 4.9.7 |
| | 7 | */ |
| | 8 | |
| | 9 | /** |
| | 10 | * Tests_Privacy_WpPrivacyProcessPersonalDataExportPage class. |
| | 11 | * |
| | 12 | * @group privacy |
| | 13 | * @covers wp_privacy_process_personal_data_export_page |
| | 14 | * |
| | 15 | * @since 4.9.7 |
| | 16 | */ |
| | 17 | class Tests_Privacy_WpPrivacyProcessPersonalDataExportPage extends WP_UnitTestCase { |
| | 18 | /** |
| | 19 | * Request ID. |
| | 20 | * |
| | 21 | * @since 4.9.7 |
| | 22 | * |
| | 23 | * @var int $request_id |
| | 24 | */ |
| | 25 | protected static $request_id; |
| | 26 | |
| | 27 | /** |
| | 28 | * Requester Email. |
| | 29 | * |
| | 30 | * @since 4.9.7 |
| | 31 | * |
| | 32 | * @var int $request_email |
| | 33 | */ |
| | 34 | protected static $request_email; |
| | 35 | |
| | 36 | /** |
| | 37 | * Response for the First Page. |
| | 38 | * |
| | 39 | * @since 4.9.7 |
| | 40 | * |
| | 41 | * @var array $response |
| | 42 | */ |
| | 43 | protected static $response_first_page; |
| | 44 | |
| | 45 | /** |
| | 46 | * Response for the Last Page. |
| | 47 | * |
| | 48 | * @since 4.9.7 |
| | 49 | * |
| | 50 | * @var array $response_last_page |
| | 51 | */ |
| | 52 | protected static $response_last_page; |
| | 53 | |
| | 54 | /** |
| | 55 | * Export File Url. |
| | 56 | * |
| | 57 | * @since 4.9.7 |
| | 58 | * |
| | 59 | * @var string $export_file_url |
| | 60 | */ |
| | 61 | protected static $export_file_url; |
| | 62 | |
| | 63 | /** |
| | 64 | * Requester Email. |
| | 65 | * |
| | 66 | * @since 4.9.7 |
| | 67 | * |
| | 68 | * @var string $requester_email |
| | 69 | */ |
| | 70 | protected static $requester_email; |
| | 71 | |
| | 72 | /** |
| | 73 | * Send As Email. |
| | 74 | * |
| | 75 | * @since 4.9.7 |
| | 76 | * |
| | 77 | * @var bool $send_as_email |
| | 78 | */ |
| | 79 | protected static $send_as_email; |
| | 80 | |
| | 81 | /** |
| | 82 | * Index Of The First Page. |
| | 83 | * |
| | 84 | * @since 4.9.7 |
| | 85 | * |
| | 86 | * @var int $page |
| | 87 | */ |
| | 88 | protected static $page_index_first; |
| | 89 | |
| | 90 | /** |
| | 91 | * Index Of The Last Page. |
| | 92 | * |
| | 93 | * @since 4.9.7 |
| | 94 | * |
| | 95 | * @var int $page_index_last |
| | 96 | */ |
| | 97 | protected static $page_index_last; |
| | 98 | |
| | 99 | /** |
| | 100 | * Index of the First Exporter. |
| | 101 | * |
| | 102 | * @since 4.9.7 |
| | 103 | * |
| | 104 | * @var int $exporter_index_first |
| | 105 | */ |
| | 106 | protected static $exporter_index_first; |
| | 107 | |
| | 108 | /** |
| | 109 | * Index of the Last Exporter. |
| | 110 | * |
| | 111 | * @since 4.9.7 |
| | 112 | * |
| | 113 | * @var int $exporter_index_last |
| | 114 | */ |
| | 115 | protected static $exporter_index_last; |
| | 116 | |
| | 117 | /** |
| | 118 | * Key of the First Exporter. |
| | 119 | * |
| | 120 | * @since 4.9.7 |
| | 121 | * |
| | 122 | * @var int $exporter_key_first |
| | 123 | */ |
| | 124 | protected static $exporter_key_first; |
| | 125 | |
| | 126 | /** |
| | 127 | * Key of the Last Exporter. |
| | 128 | * |
| | 129 | * @since 4.9.7 |
| | 130 | * |
| | 131 | * @var int $exporter_key_last |
| | 132 | */ |
| | 133 | protected static $exporter_key_last; |
| | 134 | |
| | 135 | /** |
| | 136 | * Export data stored on the `wp_privacy_personal_data_export_file` action hook. |
| | 137 | * |
| | 138 | * @var string $_export_data_grouped_fetched_within_callback |
| | 139 | */ |
| | 140 | public $_export_data_grouped_fetched_within_callback; |
| | 141 | |
| | 142 | /** |
| | 143 | * Setup before each test method. |
| | 144 | * |
| | 145 | * @since 4.9.7 |
| | 146 | */ |
| | 147 | public function setUp() { |
| | 148 | parent::setUp(); |
| | 149 | |
| | 150 | // Avoid writing export files to disk. |
| | 151 | remove_action( 'wp_privacy_personal_data_export_file', 'wp_privacy_generate_personal_data_export_file', 10 ); |
| | 152 | |
| | 153 | // Register our custom data exporters, very late, so we can override other unrelated exporters. |
| | 154 | add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'filter_register_custom_personal_data_exporters' ), 9999 ); |
| | 155 | |
| | 156 | // Set ajax context for `wp_send_json()` and `wp_die()`. |
| | 157 | add_filter( 'wp_doing_ajax', '__return_true' ); |
| | 158 | |
| | 159 | // Set up a `wp_die()` ajax handler that throws an exception, to be able to get |
| | 160 | // the error message from `wp_send_json_error( 'some message here' )`, |
| | 161 | // called by `wp_privacy_process_personal_data_export_page()`. |
| | 162 | add_filter( 'wp_die_ajax_handler', array( $this, 'get_die_handler' ), 1, 1 ); |
| | 163 | |
| | 164 | // Suppress warnings from "Cannot modify header information - headers already sent by". |
| | 165 | $this->_error_level = error_reporting(); |
| | 166 | error_reporting( $this->_error_level & ~E_WARNING ); |
| | 167 | } |
| | 168 | |
| | 169 | /** |
| | 170 | * Clean up after each test method. |
| | 171 | * |
| | 172 | * @since 4.9.7 |
| | 173 | */ |
| | 174 | public function tearDown() { |
| | 175 | add_action( 'wp_privacy_personal_data_export_file', 'wp_privacy_generate_personal_data_export_file', 10 ); |
| | 176 | |
| | 177 | remove_filter( 'wp_privacy_personal_data_exporters', array( $this, 'filter_register_custom_personal_data_exporters' ), 9999 ); |
| | 178 | remove_filter( 'wp_doing_ajax', '__return_true' ); |
| | 179 | remove_filter( 'wp_die_ajax_handler', array( $this, 'get_die_handler' ), 1, 1 ); |
| | 180 | remove_filter( 'wp_mail_from', '__return_empty_string' ); |
| | 181 | |
| | 182 | error_reporting( $this->_error_level ); |
| | 183 | |
| | 184 | parent::tearDown(); |
| | 185 | } |
| | 186 | |
| | 187 | /** |
| | 188 | * Return our die ajax callback handler. |
| | 189 | * |
| | 190 | * @since 4.9.7 |
| | 191 | * |
| | 192 | * @return callback |
| | 193 | */ |
| | 194 | public function get_die_handler() { |
| | 195 | return array( $this, 'die_handler' ); |
| | 196 | } |
| | 197 | |
| | 198 | /** |
| | 199 | * Ajax handler for `wp_die()`. |
| | 200 | * |
| | 201 | * @since 4.9.7 |
| | 202 | * |
| | 203 | * @param string $message Message is empty in the ajax context. |
| | 204 | * @throws Exception Throws an exception, containing a message from the current output buffer. |
| | 205 | */ |
| | 206 | public function die_handler( $message ) { |
| | 207 | throw new Exception( ob_get_clean() ); |
| | 208 | } |
| | 209 | |
| | 210 | /** |
| | 211 | * Create user request fixtures shared by test methods. |
| | 212 | * |
| | 213 | * @since 4.9.7 |
| | 214 | * |
| | 215 | * @param WP_UnitTest_Factory $factory Factory. |
| | 216 | */ |
| | 217 | public static function wpSetUpBeforeClass( $factory ) { |
| | 218 | self::$requester_email = 'requester@example.com'; |
| | 219 | self::$export_file_url = wp_privacy_exports_url() . 'wp-personal-data-file-requester-at-example-com-Wv0RfMnGIkl4CFEDEEkSeIdfLmaUrLsl.zip'; |
| | 220 | $data = array( |
| | 221 | array( |
| | 222 | 'group_id' => 'custom-exporter-group-id', |
| | 223 | 'group_label' => 'custom-exporter-group-label', |
| | 224 | 'item_id' => 'custom-exporter-item-id', |
| | 225 | 'data' => array( |
| | 226 | array( |
| | 227 | 'name' => 'Email', |
| | 228 | 'value' => self::$requester_email, |
| | 229 | ), |
| | 230 | ), |
| | 231 | ), |
| | 232 | ); |
| | 233 | self::$response_first_page = array( |
| | 234 | 'done' => false, |
| | 235 | 'data' => $data, |
| | 236 | ); |
| | 237 | self::$response_last_page = array( |
| | 238 | 'done' => true, |
| | 239 | 'data' => $data, |
| | 240 | ); |
| | 241 | self::$request_id = wp_create_user_request( self::$requester_email, 'export_personal_data' ); |
| | 242 | self::$send_as_email = true; |
| | 243 | self::$page_index_first = 1; |
| | 244 | self::$page_index_last = 2; |
| | 245 | self::$exporter_index_first = 1; |
| | 246 | self::$exporter_index_last = 2; |
| | 247 | self::$exporter_key_first = 'custom-exporter-first'; |
| | 248 | self::$exporter_key_last = 'custom-exporter-last'; |
| | 249 | } |
| | 250 | |
| | 251 | /** |
| | 252 | * Filter to register custom personal data exporters. |
| | 253 | * |
| | 254 | * @since 4.9.7 |
| | 255 | * |
| | 256 | * @param array $exporters An array of personal data exporters. |
| | 257 | * @return array $exporters An array of personal data exporters. |
| | 258 | */ |
| | 259 | public function filter_register_custom_personal_data_exporters( $exporters ) { |
| | 260 | |
| | 261 | // Let's override other unrelated exporters. |
| | 262 | $exporters = array(); |
| | 263 | |
| | 264 | $exporters[ self::$exporter_key_first ] = array( |
| | 265 | 'exporter_friendly_name' => __( 'Custom Exporter #1' ), |
| | 266 | 'callback' => null, |
| | 267 | ); |
| | 268 | $exporters[ self::$exporter_key_last ] = array( |
| | 269 | 'exporter_friendly_name' => __( 'Custom Exporter #2' ), |
| | 270 | 'callback' => null, |
| | 271 | ); |
| | 272 | return $exporters; |
| | 273 | } |
| | 274 | |
| | 275 | /** |
| | 276 | * The function should return the response when it's not an array. |
| | 277 | * |
| | 278 | * @since 4.9.7 |
| | 279 | */ |
| | 280 | public function test_function_should_return_response_when_response_not_array() { |
| | 281 | $response = 'not-an-array'; |
| | 282 | // Process data, given the last exporter, on the last page and send as email. |
| | 283 | $actual_response = wp_privacy_process_personal_data_export_page( |
| | 284 | $response, |
| | 285 | self::$exporter_index_last, |
| | 286 | self::$requester_email, |
| | 287 | self::$page_index_last, |
| | 288 | self::$request_id, |
| | 289 | self::$send_as_email, |
| | 290 | self::$exporter_key_last |
| | 291 | ); |
| | 292 | $this->assertSame( $response, $actual_response ); |
| | 293 | } |
| | 294 | |
| | 295 | /** |
| | 296 | * The function should return the response when it's missing the 'done' array key. |
| | 297 | * |
| | 298 | * @since 4.9.7 |
| | 299 | */ |
| | 300 | public function test_function_should_return_response_when_missing_done_array_key() { |
| | 301 | $response = array( |
| | 302 | 'missing-done-array-key' => true, |
| | 303 | ); |
| | 304 | // Process data, given the last exporter, on the last page and send as email. |
| | 305 | $actual_response = wp_privacy_process_personal_data_export_page( |
| | 306 | $response, |
| | 307 | self::$exporter_index_last, |
| | 308 | self::$requester_email, |
| | 309 | self::$page_index_last, |
| | 310 | self::$request_id, |
| | 311 | self::$send_as_email, |
| | 312 | self::$exporter_key_last |
| | 313 | ); |
| | 314 | $this->assertSame( $response, $actual_response ); |
| | 315 | } |
| | 316 | |
| | 317 | /** |
| | 318 | * The function should return the response when it's missing the 'data' array key. |
| | 319 | * |
| | 320 | * @since 4.9.7 |
| | 321 | */ |
| | 322 | public function test_function_should_return_response_when_missing_data_array_key() { |
| | 323 | $response = array( |
| | 324 | 'done' => true, |
| | 325 | 'missing-data-array-key' => true, |
| | 326 | ); |
| | 327 | // Process data, given the last exporter, on the last page and send as email. |
| | 328 | $actual_response = wp_privacy_process_personal_data_export_page( |
| | 329 | $response, |
| | 330 | self::$exporter_index_last, |
| | 331 | self::$requester_email, |
| | 332 | self::$page_index_last, |
| | 333 | self::$request_id, |
| | 334 | self::$send_as_email, |
| | 335 | self::$exporter_key_last |
| | 336 | ); |
| | 337 | $this->assertSame( $response, $actual_response ); |
| | 338 | } |
| | 339 | |
| | 340 | /** |
| | 341 | * The function should return the response when data is not an array |
| | 342 | * |
| | 343 | * @since 4.9.7 |
| | 344 | */ |
| | 345 | public function test_function_should_return_response_when_data_not_array() { |
| | 346 | $response = array( |
| | 347 | 'done' => true, |
| | 348 | 'data' => 'not-an-array', |
| | 349 | ); |
| | 350 | // Process data, given the last exporter, on the last page and send as email. |
| | 351 | $actual_response = wp_privacy_process_personal_data_export_page( |
| | 352 | $response, |
| | 353 | self::$exporter_index_last, |
| | 354 | self::$requester_email, |
| | 355 | self::$page_index_last, |
| | 356 | self::$request_id, |
| | 357 | self::$send_as_email, |
| | 358 | self::$exporter_key_last |
| | 359 | ); |
| | 360 | $this->assertSame( $response, $actual_response ); |
| | 361 | } |
| | 362 | |
| | 363 | /** |
| | 364 | * The function should send json error when invalid request ID. |
| | 365 | * |
| | 366 | * @since 4.9.7 |
| | 367 | * @expectedException Exception |
| | 368 | * @expectedExceptionMessage {"success":false,"data":"Invalid request ID when merging exporter data."} |
| | 369 | */ |
| | 370 | public function test_function_should_send_error_when_invalid_request_id() { |
| | 371 | $response = array( |
| | 372 | 'done' => true, |
| | 373 | 'data' => array(), |
| | 374 | ); |
| | 375 | $request_id = 0; // Invalid request ID. |
| | 376 | |
| | 377 | // Process data, given the last exporter, on the last page and send as email. |
| | 378 | ob_start(); |
| | 379 | wp_privacy_process_personal_data_export_page( |
| | 380 | $response, |
| | 381 | self::$exporter_index_last, |
| | 382 | self::$requester_email, |
| | 383 | self::$page_index_last, |
| | 384 | $request_id, |
| | 385 | self::$send_as_email, |
| | 386 | self::$exporter_key_last |
| | 387 | ); |
| | 388 | } |
| | 389 | |
| | 390 | /** |
| | 391 | * The function should send json error when the request has an invalid action name. |
| | 392 | * |
| | 393 | * @since 4.9.7 |
| | 394 | * @expectedException Exception |
| | 395 | * @expectedExceptionMessage {"success":false,"data":"Invalid request ID when merging exporter data."} |
| | 396 | */ |
| | 397 | public function test_function_should_send_error_when_invalid_request_action_name() { |
| | 398 | $response = array( |
| | 399 | 'done' => true, |
| | 400 | 'data' => array(), |
| | 401 | ); |
| | 402 | // A request with an invalid action name, 'export_personal_data' is expected. |
| | 403 | $request_id = wp_create_user_request( self::$requester_email, 'remove_personal_data' ); |
| | 404 | |
| | 405 | // Process data, given the last exporter, on the last page and send as email. |
| | 406 | ob_start(); |
| | 407 | wp_privacy_process_personal_data_export_page( |
| | 408 | $response, |
| | 409 | self::$exporter_index_last, |
| | 410 | self::$requester_email, |
| | 411 | self::$page_index_last, |
| | 412 | $request_id, |
| | 413 | self::$send_as_email, |
| | 414 | self::$exporter_key_last |
| | 415 | ); |
| | 416 | } |
| | 417 | |
| | 418 | /** |
| | 419 | * The function should send json error on the last page of the last exporter when mail delivery fails. |
| | 420 | * |
| | 421 | * @since 4.9.7 |
| | 422 | * @expectedException Exception |
| | 423 | * @expectedExceptionMessage {"success":false,"data":"Unable to send personal data export email."} |
| | 424 | */ |
| | 425 | public function test_function_should_send_error_on_last_page_of_last_exporter_when_mail_delivery_fails() { |
| | 426 | // Cause `wp_mail()` to return false, to simulate mail delivery failure. Filter removed in tearDown. |
| | 427 | add_filter( 'wp_mail_from', '__return_empty_string' ); |
| | 428 | // Process data, given the last exporter, on the last page and send as email. |
| | 429 | ob_start(); |
| | 430 | wp_privacy_process_personal_data_export_page( |
| | 431 | self::$response_last_page, |
| | 432 | self::$exporter_index_last, |
| | 433 | self::$requester_email, |
| | 434 | self::$page_index_last, |
| | 435 | self::$request_id, |
| | 436 | self::$send_as_email, |
| | 437 | self::$exporter_key_last |
| | 438 | ); |
| | 439 | } |
| | 440 | |
| | 441 | /** |
| | 442 | * The function should return the response, containing the export file url, when not sent as email |
| | 443 | * for the last exporter on the last page. |
| | 444 | * |
| | 445 | * @since 4.9.7 |
| | 446 | */ |
| | 447 | public function test_function_should_return_response_with_export_file_url_when_not_sent_as_email_for_last_exporter_on_last_page() { |
| | 448 | update_post_meta( self::$request_id, '_export_file_url', self::$export_file_url ); |
| | 449 | |
| | 450 | // Process data, given the last exporter, on the last page and not send as email. |
| | 451 | $actual_response = wp_privacy_process_personal_data_export_page( |
| | 452 | self::$response_last_page, |
| | 453 | self::$exporter_index_last, |
| | 454 | self::$requester_email, |
| | 455 | self::$page_index_last, |
| | 456 | self::$request_id, |
| | 457 | ! self::$send_as_email, |
| | 458 | self::$exporter_key_last |
| | 459 | ); |
| | 460 | |
| | 461 | $this->assertArrayHasKey( 'url', $actual_response ); |
| | 462 | $this->assertSame( self::$export_file_url, $actual_response['url'] ); |
| | 463 | $this->assertSame( self::$response_last_page['done'], $actual_response['done'] ); |
| | 464 | $this->assertSame( self::$response_last_page['data'], $actual_response['data'] ); |
| | 465 | } |
| | 466 | |
| | 467 | /** |
| | 468 | * The function should return the response containing the export file url when not sent as email |
| | 469 | * for the last exporter on the last page. |
| | 470 | * |
| | 471 | * @since 4.9.7 |
| | 472 | */ |
| | 473 | public function test_function_should_return_response_without_export_file_url_when_sent_as_email_for_last_exporter_on_last_page() { |
| | 474 | update_post_meta( self::$request_id, '_export_file_url', self::$export_file_url ); |
| | 475 | |
| | 476 | // Process data, given the last exporter, on the last page and send as email. |
| | 477 | $actual_response = wp_privacy_process_personal_data_export_page( |
| | 478 | self::$response_last_page, |
| | 479 | self::$exporter_index_last, |
| | 480 | self::$requester_email, |
| | 481 | self::$page_index_last, |
| | 482 | self::$request_id, |
| | 483 | self::$send_as_email, |
| | 484 | self::$exporter_key_last |
| | 485 | ); |
| | 486 | |
| | 487 | $this->assertArrayNotHasKey( 'url', $actual_response ); |
| | 488 | $this->assertSame( self::$response_last_page['done'], $actual_response['done'] ); |
| | 489 | $this->assertSame( self::$response_last_page['data'], $actual_response['data'] ); |
| | 490 | } |
| | 491 | |
| | 492 | /** |
| | 493 | * The function should mark the request as completed for the last exporter on the last page. |
| | 494 | * |
| | 495 | * @since 4.9.7 |
| | 496 | */ |
| | 497 | public function test_function_should_mark_request_as_completed_when_last_exporter_on_last_page() { |
| | 498 | // Process data, given the last exporter on the last page and send as email. |
| | 499 | wp_privacy_process_personal_data_export_page( |
| | 500 | self::$response_last_page, |
| | 501 | self::$exporter_index_last, |
| | 502 | self::$requester_email, |
| | 503 | self::$page_index_last, |
| | 504 | self::$request_id, |
| | 505 | self::$send_as_email, |
| | 506 | self::$exporter_key_last |
| | 507 | ); |
| | 508 | $this->assertSame( 'request-completed', get_post_status( self::$request_id ) ); |
| | 509 | |
| | 510 | // Process data, given the last exporter on the last page and not send as email. |
| | 511 | wp_privacy_process_personal_data_export_page( |
| | 512 | self::$response_last_page, |
| | 513 | self::$exporter_index_last, |
| | 514 | self::$requester_email, |
| | 515 | self::$page_index_last, |
| | 516 | self::$request_id, |
| | 517 | ! self::$send_as_email, |
| | 518 | self::$exporter_key_last |
| | 519 | ); |
| | 520 | $this->assertSame( 'request-completed', get_post_status( self::$request_id ) ); |
| | 521 | } |
| | 522 | |
| | 523 | /** |
| | 524 | * The function should leave the request as pending when not the last exporter and not on the last page. |
| | 525 | * |
| | 526 | * @since 4.9.7 |
| | 527 | */ |
| | 528 | public function test_function_should_leave_request_as_pending_when_not_last_exporter_and_not_on_last_page() { |
| | 529 | // Process data, given the not last exporter, on the last page and send as email. |
| | 530 | wp_privacy_process_personal_data_export_page( |
| | 531 | self::$response_first_page, |
| | 532 | self::$exporter_index_first, |
| | 533 | self::$requester_email, |
| | 534 | self::$page_index_first, |
| | 535 | self::$request_id, |
| | 536 | self::$send_as_email, |
| | 537 | self::$exporter_key_first |
| | 538 | ); |
| | 539 | $this->assertSame( 'request-pending', get_post_status( self::$request_id ) ); |
| | 540 | |
| | 541 | // Process data, given the not last exporter, on the last page and not send as email. |
| | 542 | wp_privacy_process_personal_data_export_page( |
| | 543 | self::$response_first_page, |
| | 544 | self::$exporter_index_first, |
| | 545 | self::$requester_email, |
| | 546 | self::$page_index_first, |
| | 547 | self::$request_id, |
| | 548 | ! self::$send_as_email, |
| | 549 | self::$exporter_key_first |
| | 550 | ); |
| | 551 | $this->assertSame( 'request-pending', get_post_status( self::$request_id ) ); |
| | 552 | } |
| | 553 | |
| | 554 | /** |
| | 555 | * The function should leave the request as pending when last exporter and not on the last page. |
| | 556 | * |
| | 557 | * @since 4.9.7 |
| | 558 | */ |
| | 559 | public function test_function_should_leave_request_as_pending_when_last_exporter_and_not_on_last_page() { |
| | 560 | // Process data, given the last exporter, not on the last page and send as email. |
| | 561 | wp_privacy_process_personal_data_export_page( |
| | 562 | self::$response_first_page, |
| | 563 | self::$exporter_index_last, |
| | 564 | self::$requester_email, |
| | 565 | self::$page_index_first, |
| | 566 | self::$request_id, |
| | 567 | self::$send_as_email, |
| | 568 | self::$exporter_key_last |
| | 569 | ); |
| | 570 | $this->assertSame( 'request-pending', get_post_status( self::$request_id ) ); |
| | 571 | |
| | 572 | // Process data, given the last exporter, not on the last page and not send as email. |
| | 573 | wp_privacy_process_personal_data_export_page( |
| | 574 | self::$response_first_page, |
| | 575 | self::$exporter_index_last, |
| | 576 | self::$requester_email, |
| | 577 | self::$page_index_first, |
| | 578 | self::$request_id, |
| | 579 | ! self::$send_as_email, |
| | 580 | self::$exporter_key_last |
| | 581 | ); |
| | 582 | $this->assertSame( 'request-pending', get_post_status( self::$request_id ) ); |
| | 583 | } |
| | 584 | |
| | 585 | /** |
| | 586 | * The function should leave the request as pending when not last exporter on the last page. |
| | 587 | * |
| | 588 | * @since 4.9.7 |
| | 589 | */ |
| | 590 | public function test_function_should_leave_request_as_pending_when_not_last_exporter_on_last_page() { |
| | 591 | // Process data, given not the last exporter on the last page and sending as email. |
| | 592 | wp_privacy_process_personal_data_export_page( |
| | 593 | self::$response_last_page, |
| | 594 | self::$exporter_index_first, |
| | 595 | self::$requester_email, |
| | 596 | self::$page_index_last, |
| | 597 | self::$request_id, |
| | 598 | self::$send_as_email, |
| | 599 | self::$exporter_key_first |
| | 600 | ); |
| | 601 | $this->assertSame( 'request-pending', get_post_status( self::$request_id ) ); |
| | 602 | |
| | 603 | // Process data, given not the last exporter on the last page and not send as email. |
| | 604 | wp_privacy_process_personal_data_export_page( |
| | 605 | self::$response_last_page, |
| | 606 | self::$exporter_index_first, |
| | 607 | self::$requester_email, |
| | 608 | self::$page_index_last, |
| | 609 | self::$request_id, |
| | 610 | ! self::$send_as_email, |
| | 611 | self::$exporter_key_first |
| | 612 | ); |
| | 613 | $this->assertSame( 'request-pending', get_post_status( self::$request_id ) ); |
| | 614 | } |
| | 615 | |
| | 616 | /** |
| | 617 | * The function should add `_export_data_raw` post meta for the request, when sent as email |
| | 618 | * for the first exporter on the first page. |
| | 619 | * |
| | 620 | * @since 4.9.7 |
| | 621 | */ |
| | 622 | public function test_function_should_add_post_meta_with_raw_data_when_sent_as_email_for_first_exporter_on_first_page() { |
| | 623 | delete_post_meta( self::$request_id, '_export_data_raw' ); |
| | 624 | $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); |
| | 625 | |
| | 626 | // Process data, given the first exporter on the first page and send as email. |
| | 627 | wp_privacy_process_personal_data_export_page( |
| | 628 | self::$response_first_page, |
| | 629 | self::$exporter_index_first, |
| | 630 | self::$requester_email, |
| | 631 | self::$page_index_first, |
| | 632 | self::$request_id, |
| | 633 | self::$send_as_email, |
| | 634 | self::$exporter_key_first |
| | 635 | ); |
| | 636 | $this->assertNotEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); |
| | 637 | } |
| | 638 | |
| | 639 | /** |
| | 640 | * The function should delete `_export_data_raw` post meta for the request, when sent as email |
| | 641 | * for the last exporter on the last page. |
| | 642 | * |
| | 643 | * @since 4.9.7 |
| | 644 | */ |
| | 645 | public function test_function_should_delete_post_meta_with_raw_data_when_sent_as_email_for_last_exporter_and_last_page() { |
| | 646 | // Adds post meta when processing data, given the first exporter on the first page and send as email. |
| | 647 | wp_privacy_process_personal_data_export_page( |
| | 648 | self::$response_first_page, |
| | 649 | self::$exporter_index_first, |
| | 650 | self::$requester_email, |
| | 651 | self::$page_index_first, |
| | 652 | self::$request_id, |
| | 653 | self::$send_as_email, |
| | 654 | self::$exporter_key_first |
| | 655 | ); |
| | 656 | $this->assertNotEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); |
| | 657 | |
| | 658 | // Deletes post meta when processing data, given the last exporter on the last page and send as email. |
| | 659 | wp_privacy_process_personal_data_export_page( |
| | 660 | self::$response_last_page, |
| | 661 | self::$exporter_index_last, |
| | 662 | self::$requester_email, |
| | 663 | self::$page_index_last, |
| | 664 | self::$request_id, |
| | 665 | self::$send_as_email, |
| | 666 | self::$exporter_key_last |
| | 667 | ); |
| | 668 | $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); |
| | 669 | } |
| | 670 | |
| | 671 | /** |
| | 672 | * The function should add `_export_data_raw` post meta for the request, when first exporter and first page and |
| | 673 | * email is not sent. |
| | 674 | * |
| | 675 | * @since 4.9.7 |
| | 676 | */ |
| | 677 | public function test_function_should_add_post_meta_with_raw_data_when_not_sent_as_email_for_first_exporter_and_first_page() { |
| | 678 | $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); |
| | 679 | // Adds post meta when processing data, given the first exporter on the first page and not send as email. |
| | 680 | wp_privacy_process_personal_data_export_page( |
| | 681 | self::$response_first_page, |
| | 682 | self::$exporter_index_first, |
| | 683 | self::$requester_email, |
| | 684 | self::$page_index_first, |
| | 685 | self::$request_id, |
| | 686 | ! self::$send_as_email, |
| | 687 | self::$exporter_key_first |
| | 688 | ); |
| | 689 | $this->assertNotEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); |
| | 690 | |
| | 691 | } |
| | 692 | |
| | 693 | /** |
| | 694 | * The function should delete `_export_data_raw` post meta for the request, when sent as email |
| | 695 | * for the last exporter on the last page. |
| | 696 | * |
| | 697 | * @since 4.9.7 |
| | 698 | */ |
| | 699 | public function test_function_should_delete_post_meta_with_raw_data_when_not_sent_as_email_for_last_exporter_and_last_page() { |
| | 700 | // Adds post meta when processing data, given the first exporter on the first page and not send as email. |
| | 701 | wp_privacy_process_personal_data_export_page( |
| | 702 | self::$response_first_page, |
| | 703 | self::$exporter_index_first, |
| | 704 | self::$requester_email, |
| | 705 | self::$page_index_first, |
| | 706 | self::$request_id, |
| | 707 | ! self::$send_as_email, |
| | 708 | self::$exporter_key_first |
| | 709 | ); |
| | 710 | $this->assertNotEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); |
| | 711 | |
| | 712 | // Deletes post meta when processing data, given the last exporter on the last page and not send as email. |
| | 713 | wp_privacy_process_personal_data_export_page( |
| | 714 | self::$response_last_page, |
| | 715 | self::$exporter_index_last, |
| | 716 | self::$requester_email, |
| | 717 | self::$page_index_last, |
| | 718 | self::$request_id, |
| | 719 | ! self::$send_as_email, |
| | 720 | self::$exporter_key_last |
| | 721 | ); |
| | 722 | $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_raw', true ) ); |
| | 723 | } |
| | 724 | |
| | 725 | /** |
| | 726 | * The function should add `_export_data_grouped` post meta for the request, only available when personal data export file is generated. |
| | 727 | * |
| | 728 | * @since 4.9.7 |
| | 729 | */ |
| | 730 | public function test_function_should_add_post_meta_with_groups_data_only_available_when_export_file_generated() { |
| | 731 | // Adds post meta when processing data, given the first exporter on the first page and send as email. |
| | 732 | wp_privacy_process_personal_data_export_page( |
| | 733 | self::$response_first_page, |
| | 734 | self::$exporter_index_first, |
| | 735 | self::$requester_email, |
| | 736 | self::$page_index_first, |
| | 737 | self::$request_id, |
| | 738 | self::$send_as_email, |
| | 739 | self::$exporter_key_first |
| | 740 | ); |
| | 741 | $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_grouped', true ) ); |
| | 742 | |
| | 743 | add_action( 'wp_privacy_personal_data_export_file', array( $this, 'action_callback_to_get_export_groups_data' ) ); |
| | 744 | // Process data, given the last exporter on the last page and send as email. |
| | 745 | wp_privacy_process_personal_data_export_page( |
| | 746 | self::$response_last_page, |
| | 747 | self::$exporter_index_last, |
| | 748 | self::$requester_email, |
| | 749 | self::$page_index_last, |
| | 750 | self::$request_id, |
| | 751 | self::$send_as_email, |
| | 752 | self::$exporter_key_last |
| | 753 | ); |
| | 754 | remove_action( 'wp_privacy_personal_data_export_file', array( $this, 'action_callback_to_get_export_groups_data' ) ); |
| | 755 | |
| | 756 | $this->assertNotEmpty( $this->_export_data_grouped_fetched_within_callback ); |
| | 757 | $this->assertEmpty( get_post_meta( self::$request_id, '_export_data_grouped', true ) ); |
| | 758 | } |
| | 759 | |
| | 760 | /** |
| | 761 | * A callback for the `wp_privacy_personal_data_export_file` action that stores the `_export_data_grouped` meta |
| | 762 | * data locally for testing. |
| | 763 | * |
| | 764 | * @since 4.9.7 |
| | 765 | * |
| | 766 | * @param int $request_id Request ID. |
| | 767 | */ |
| | 768 | public function action_callback_to_get_export_groups_data( $request_id ) { |
| | 769 | $this->_export_data_grouped_fetched_within_callback = get_post_meta( $request_id, '_export_data_grouped', true ); |
| | 770 | } |
| | 771 | } |