| 1 | Index: tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php
|
|---|
| 2 | ===================================================================
|
|---|
| 3 | --- tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php (nonexistent)
|
|---|
| 4 | +++ tests/phpunit/tests/privacy/wpPrivacyGeneratePersonalDataExportFile.php (working copy)
|
|---|
| 5 | @@ -0,0 +1,118 @@
|
|---|
| 6 | +<?php
|
|---|
| 7 | +/**
|
|---|
| 8 | + * Define a class to test `wp_privacy_generate_personal_data_export_file()`.
|
|---|
| 9 | + *
|
|---|
| 10 | + * @package WordPress
|
|---|
| 11 | + * @subpackage UnitTests
|
|---|
| 12 | + * @since 4.9.6
|
|---|
| 13 | + */
|
|---|
| 14 | +
|
|---|
| 15 | +/**
|
|---|
| 16 | + * Test cases for `wp_privacy_generate_personal_data_export_file()`.
|
|---|
| 17 | + *
|
|---|
| 18 | + * @group privacy
|
|---|
| 19 | + * @covers wp_privacy_generate_personal_data_export_file
|
|---|
| 20 | + *
|
|---|
| 21 | + * @since 4.9.6
|
|---|
| 22 | + */
|
|---|
| 23 | +class Tests_Privacy_wpPrivacyGeneratePersonalDataExportFile extends WP_UnitTestCase {
|
|---|
| 24 | + /**
|
|---|
| 25 | + * An Export Request ID
|
|---|
| 26 | + *
|
|---|
| 27 | + * @since 4.9.6
|
|---|
| 28 | + *
|
|---|
| 29 | + * @var int $export_request_id
|
|---|
| 30 | + */
|
|---|
| 31 | + protected static $export_request_id;
|
|---|
| 32 | +
|
|---|
| 33 | + /**
|
|---|
| 34 | + * Create fixtures that are shared by multiple test cases.
|
|---|
| 35 | + *
|
|---|
| 36 | + * @param WP_UnitTest_Factory $factory The base factory object.
|
|---|
| 37 | + */
|
|---|
| 38 | + public static function wpSetUpBeforeClass( $factory ) {
|
|---|
| 39 | + self::$export_request_id = wp_create_user_request( 'export-requester@example.com', 'export_personal_data' );
|
|---|
| 40 | + }
|
|---|
| 41 | +
|
|---|
| 42 | + /**
|
|---|
| 43 | + * Set up the test fixture.
|
|---|
| 44 | + * Override wp_die(), pretend to be ajax, and suppres E_WARNINGs
|
|---|
| 45 | + */
|
|---|
| 46 | + public function setUp() {
|
|---|
| 47 | + parent::setUp();
|
|---|
| 48 | +
|
|---|
| 49 | + add_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ), 1, 1 );
|
|---|
| 50 | + add_filter( 'wp_doing_ajax', '__return_true' );
|
|---|
| 51 | +
|
|---|
| 52 | + // Suppress warnings from "Cannot modify header information - headers already sent by"
|
|---|
| 53 | + $this->_error_level = error_reporting();
|
|---|
| 54 | + error_reporting( $this->_error_level & ~E_WARNING );
|
|---|
| 55 | + }
|
|---|
| 56 | +
|
|---|
| 57 | + /**
|
|---|
| 58 | + * Tear down the test fixture.
|
|---|
| 59 | + * Remove the wp_die() override, restore error reporting
|
|---|
| 60 | + */
|
|---|
| 61 | + public function tearDown() {
|
|---|
| 62 | + remove_filter( 'wp_die_ajax_handler', array( $this, 'getDieHandler' ), 1, 1 );
|
|---|
| 63 | + error_reporting( $this->_error_level );
|
|---|
| 64 | + parent::tearDown();
|
|---|
| 65 | + }
|
|---|
| 66 | +
|
|---|
| 67 | + /**
|
|---|
| 68 | + * Return our callback handler
|
|---|
| 69 | + *
|
|---|
| 70 | + * @return callback
|
|---|
| 71 | + */
|
|---|
| 72 | + public function getDieHandler() {
|
|---|
| 73 | + return array( $this, 'dieHandler' );
|
|---|
| 74 | + }
|
|---|
| 75 | +
|
|---|
| 76 | + /**
|
|---|
| 77 | + * Handler for wp_die()
|
|---|
| 78 | + * Don't die, just continue on.
|
|---|
| 79 | + *
|
|---|
| 80 | + * @param string $message
|
|---|
| 81 | + */
|
|---|
| 82 | + public function dieHandler( $message ) {
|
|---|
| 83 | + }
|
|---|
| 84 | +
|
|---|
| 85 | + // TODO test ziparchive not available
|
|---|
| 86 | +
|
|---|
| 87 | + public function test_wp_privacy_generate_personal_data_export_file_rejects_remove_requests() {
|
|---|
| 88 | + $request_id = wp_create_user_request( 'removal-requester@example.com', 'remove_personal_data' );
|
|---|
| 89 | +
|
|---|
| 90 | + ob_start();
|
|---|
| 91 | + wp_privacy_generate_personal_data_export_file( $request_id );
|
|---|
| 92 | +
|
|---|
| 93 | + $response = json_decode( ob_get_clean() );
|
|---|
| 94 | + $this->assertFalse( $response->success );
|
|---|
| 95 | + $this->assertEquals( 'Invalid request ID when generating export file.', $response->data );
|
|---|
| 96 | + }
|
|---|
| 97 | +
|
|---|
| 98 | + public function test_wp_privacy_generate_personal_data_export_file_rejects_requests_with_bad_email_addresses() {
|
|---|
| 99 | + $request_id = wp_create_user_request( 'bad-email-requester@example.com', 'export_personal_data' );
|
|---|
| 100 | + wp_update_post(
|
|---|
| 101 | + array(
|
|---|
| 102 | + 'ID' => $request_id,
|
|---|
| 103 | + 'post_title' => 'not-a-valid-email-address'
|
|---|
| 104 | + )
|
|---|
| 105 | + );
|
|---|
| 106 | +
|
|---|
| 107 | + ob_start();
|
|---|
| 108 | + wp_privacy_generate_personal_data_export_file( $request_id );
|
|---|
| 109 | +
|
|---|
| 110 | + $response = json_decode( ob_get_clean() );
|
|---|
| 111 | + $this->assertFalse( $response->success );
|
|---|
| 112 | + $this->assertEquals( 'Invalid email address when generating export file.', $response->data );
|
|---|
| 113 | + }
|
|---|
| 114 | +
|
|---|
| 115 | + // TODO mkdir_p fails
|
|---|
| 116 | + // TODO index.html created in export folder - silence is golden
|
|---|
| 117 | + // TODO unable to protect error if can't create index.html file
|
|---|
| 118 | + // TODO unable to open export file error if can't write
|
|---|
| 119 | + // TODO report file exists, has starts with <html> and ends with </html>
|
|---|
| 120 | + // TODO unable to add data error if can't add file to export
|
|---|
| 121 | + // TODO error if can't open archive file
|
|---|
| 122 | + // TODO report file removed, even if errors
|
|---|
| 123 | +}
|
|---|