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