| 1 | <?php |
| 2 | /** |
| 3 | * Test cases for the `wp_ajax_wp_privacy_export_personal_data()` function. |
| 4 | * |
| 5 | * @package WordPress\UnitTests |
| 6 | * |
| 7 | * @since 4.9.6 |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Tests_Ajax_WpAjaxWpPrivacyExportPersonalData class. |
| 12 | * |
| 13 | * @since 4.9.6 |
| 14 | * |
| 15 | * @group ajax |
| 16 | * @group privacy |
| 17 | * @covers wp_ajax_wp_privacy_export_personal_data |
| 18 | */ |
| 19 | class Tests_Ajax_WpAjaxWpPrivacyExportPersonalData extends WP_Ajax_UnitTestCase { |
| 20 | /** |
| 21 | * User Request ID. |
| 22 | * |
| 23 | * @since 4.9.6 |
| 24 | * |
| 25 | * @var int |
| 26 | */ |
| 27 | protected static $request_id; |
| 28 | |
| 29 | /** |
| 30 | * User Request Email. |
| 31 | * |
| 32 | * @since 4.9.6 |
| 33 | * |
| 34 | * @var string |
| 35 | */ |
| 36 | protected static $request_email; |
| 37 | |
| 38 | /** |
| 39 | * Set up before class. Create fixtures. |
| 40 | * |
| 41 | * @param WP_UnitTest_Factory $factory Factory. |
| 42 | */ |
| 43 | public static function wpSetUpBeforeClass( $factory ) { |
| 44 | self::$request_email = 'user.request@example.com'; |
| 45 | self::$request_id = wp_create_user_request( self::$request_email, 'export_personal_data' ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Tear down after class. |
| 50 | */ |
| 51 | public static function tearDownAfterClass() { |
| 52 | wp_delete_post( self::$request_id, true ); |
| 53 | parent::tearDownAfterClass(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Set up. |
| 58 | */ |
| 59 | public function setUp() { |
| 60 | // Only use our custom privacy personal data exporter. |
| 61 | remove_all_filters( 'wp_privacy_personal_data_exporters' ); |
| 62 | add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_custom_personal_data_exporter' ) ); |
| 63 | |
| 64 | // Make sure the exporter response is not modified and avoid e.g. writing export file to disk. |
| 65 | remove_all_filters( 'wp_privacy_personal_data_export_page' ); |
| 66 | |
| 67 | parent::setUp(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tear down. |
| 72 | */ |
| 73 | public function tearDown() { |
| 74 | remove_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_custom_personal_data_exporter' ) ); |
| 75 | parent::tearDown(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * The function should successfully send exporters response data when the user is administrator. |
| 80 | * |
| 81 | * @ticket 43438 |
| 82 | */ |
| 83 | public function test_wp_ajax_wp_privacy_export_personal_data_should_success_when_admin_user() { |
| 84 | $this->_setRole( 'administrator' ); |
| 85 | $this->assertNotWPError( self::$request_id ); |
| 86 | |
| 87 | // Set up a request. |
| 88 | $_POST['security'] = wp_create_nonce( 'wp-privacy-export-personal-data-' . self::$request_id ); |
| 89 | $_POST['exporter'] = 1; |
| 90 | $_POST['page'] = 1; |
| 91 | $_POST['id'] = self::$request_id; |
| 92 | |
| 93 | // Make the request. |
| 94 | try { |
| 95 | $this->_handleAjax( 'wp-privacy-export-personal-data' ); |
| 96 | } catch ( WPAjaxDieContinueException $e ) { |
| 97 | unset( $e ); |
| 98 | } |
| 99 | |
| 100 | // Get the response. |
| 101 | $response = json_decode( $this->_last_response, true ); |
| 102 | |
| 103 | $this->assertTrue( $response['success'] ); |
| 104 | $this->assertSame( 'custom-exporter-item-id', $response['data']['data']['item_id'] ); |
| 105 | $this->assertSame( 'Email', $response['data']['data']['data'][0]['name'] ); |
| 106 | $this->assertSame( self::$request_email, $response['data']['data']['data'][0]['value'] ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * The function should send error when the request ID is missing. |
| 111 | * |
| 112 | * @ticket 43438 |
| 113 | */ |
| 114 | public function test_wp_ajax_wp_privacy_export_personal_data_should_error_when_missing_request_id() { |
| 115 | $this->_setRole( 'administrator' ); |
| 116 | $this->assertNotWPError( self::$request_id ); |
| 117 | |
| 118 | // Set up a request. |
| 119 | $_POST['security'] = wp_create_nonce( 'wp-privacy-export-personal-data-' . self::$request_id ); |
| 120 | $_POST['exporter'] = 1; |
| 121 | $_POST['page'] = 1; |
| 122 | // Missing request ID. |
| 123 | // $_POST['id'] = self::$request_id; |
| 124 | |
| 125 | // Make the request. |
| 126 | try { |
| 127 | $this->_handleAjax( 'wp-privacy-export-personal-data' ); |
| 128 | } catch ( WPAjaxDieContinueException $e ) { |
| 129 | unset( $e ); |
| 130 | } |
| 131 | |
| 132 | // Get the response. |
| 133 | $response = json_decode( $this->_last_response, true ); |
| 134 | |
| 135 | $this->assertFalse( $response['success'] ); |
| 136 | $this->assertSame( 'Missing request ID.', $response['data'] ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * The function should send error when the request ID is invalid. |
| 141 | * |
| 142 | * @ticket 43438 |
| 143 | */ |
| 144 | public function test_wp_ajax_wp_privacy_export_personal_data_should_error_when_request_ID_invalid() { |
| 145 | $this->_setRole( 'administrator' ); |
| 146 | // Invalid request ID. |
| 147 | $request_id = PHP_INT_MAX; |
| 148 | |
| 149 | // Set up a request. |
| 150 | $_POST['security'] = wp_create_nonce( 'wp-privacy-export-personal-data-' . $request_id ); |
| 151 | $_POST['exporter'] = 1; |
| 152 | $_POST['page'] = 1; |
| 153 | $_POST['id'] = $request_id; |
| 154 | |
| 155 | // Make the request. |
| 156 | try { |
| 157 | $this->_handleAjax( 'wp-privacy-export-personal-data' ); |
| 158 | } catch ( WPAjaxDieContinueException $e ) { |
| 159 | unset( $e ); |
| 160 | } |
| 161 | |
| 162 | // Get the response. |
| 163 | $response = json_decode( $this->_last_response, true ); |
| 164 | |
| 165 | $this->assertFalse( $response['success'] ); |
| 166 | $this->assertSame( 'Invalid request type.', $response['data'] ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Registers a custom personal data exporter. |
| 171 | * |
| 172 | * @since 4.9.6 |
| 173 | * |
| 174 | * @param array $exporters An array of personal data exporters. |
| 175 | * @return array $exporters An array of personal data exporters. |
| 176 | */ |
| 177 | public function register_custom_personal_data_exporter( $exporters ) { |
| 178 | $exporters[] = array( |
| 179 | 'exporter_friendly_name' => __( 'Custom Exporter' ), |
| 180 | 'callback' => array( $this, 'custom_personal_data_exporter' ), |
| 181 | ); |
| 182 | |
| 183 | return $exporters; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Custom Personal Data Exporter. |
| 188 | * |
| 189 | * @since 4.9.6 |
| 190 | * |
| 191 | * @param string $email_address The comment author email address. |
| 192 | * @param int $page Comment page. |
| 193 | * @return array $return An array of personal data. |
| 194 | */ |
| 195 | public function custom_personal_data_exporter( $email_address, $page = 1 ) { |
| 196 | return array( |
| 197 | 'data' => array( |
| 198 | 'group_id' => 'custom-exporter-group-id', |
| 199 | 'group_label' => 'custom-exporter-group-label', |
| 200 | 'item_id' => 'custom-exporter-item-id', |
| 201 | 'data' => array( |
| 202 | array( |
| 203 | 'name' => 'Email', |
| 204 | 'value' => $email_address, |
| 205 | ), |
| 206 | ), |
| 207 | ), |
| 208 | 'done' => true, |
| 209 | ); |
| 210 | } |
| 211 | |
| 212 | } |