From 8697aee5f9d834c1d7135524c7f13dc3c96f4d85 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 7 Dec 2021 18:36:29 +0000 Subject: [PATCH] REST API: Ensure that the export template endpoint returns a valid WP_Error object. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ensure that the export template endpoint returns a WP_Error object, including code and message, so that the site editor can display an error message. Add some basic unit tests to ensure that permission checks are working as expected. Follow-up to [52286]. Props Spacedmonkey, dlh, hellofromTonya , Mamaduka, TimothyBlynJacobs. Fixes #54448. git-svn-id: https://develop.svn.wordpress.org/trunk@52340 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/block-template-utils.php | 4 +- ...ss-wp-rest-edit-site-export-controller.php | 4 +- .../wpRestEditSiteExportController.php | 156 ++++++++++++++++++ 3 files changed, 160 insertions(+), 4 deletions(-) create mode 100644 tests/phpunit/tests/rest-api/wpRestEditSiteExportController.php diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 6a53fd6730..28efb5cbad 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -913,7 +913,7 @@ function block_footer_area() { */ function wp_generate_block_templates_export_file() { if ( ! class_exists( 'ZipArchive' ) ) { - return new WP_Error( __( 'Zip Export not supported.' ) ); + return new WP_Error( 'missing_zip_package', __( 'Zip Export not supported.' ) ); } $obscura = wp_generate_password( 12, false, false ); @@ -921,7 +921,7 @@ function wp_generate_block_templates_export_file() { $zip = new ZipArchive(); if ( true !== $zip->open( $filename, ZipArchive::CREATE ) ) { - return new WP_Error( __( 'Unable to open export file (archive) for writing.' ) ); + return new WP_Error( 'unable_to_create_zip', __( 'Unable to open export file (archive) for writing.' ) ); } $zip->addEmptyDir( 'theme' ); diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php index 1bd40cd637..d5abfa5682 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-edit-site-export-controller.php @@ -54,8 +54,8 @@ class WP_REST_Edit_Site_Export_Controller extends WP_REST_Controller { */ public function permissions_check() { if ( ! current_user_can( 'edit_theme_options' ) ) { - new WP_Error( - 'rest_cannot_view_url_details', + return new WP_Error( + 'rest_cannot_export_templates', __( 'Sorry, you are not allowed to export templates and template parts.' ), array( 'status' => rest_authorization_required_code() ) ); diff --git a/tests/phpunit/tests/rest-api/wpRestEditSiteExportController.php b/tests/phpunit/tests/rest-api/wpRestEditSiteExportController.php new file mode 100644 index 0000000000..4683d68fed --- /dev/null +++ b/tests/phpunit/tests/rest-api/wpRestEditSiteExportController.php @@ -0,0 +1,156 @@ +user->create( + array( + 'role' => 'subscriber', + ) + ); + } + + /** + * Delete test data after our tests run. + * + * @since 5.9.0 + */ + public static function wpTearDownAfterClass() { + self::delete_user( self::$subscriber_id ); + } + + /** + * @covers WP_REST_Edit_Site_Export_Controller::register_routes + * @ticket 54448 + */ + public function test_register_routes() { + $routes = rest_get_server()->get_routes(); + $this->assertArrayHasKey( static::REQUEST_ROUTE, $routes ); + $this->assertCount( 1, $routes[ static::REQUEST_ROUTE ] ); + } + + /** + * @covers WP_REST_Edit_Site_Export_Controller::permissions_check + * + * @ticket 54448 + */ + public function test_export_for_no_user_permissions() { + wp_set_current_user( 0 ); + + $request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_cannot_export_templates', $response, 401 ); + } + + /** + * @covers WP_REST_Edit_Site_Export_Controller::permissions_check + * + * @ticket 54448 + */ + public function test_export_for_user_with_insufficient_permissions() { + wp_set_current_user( self::$subscriber_id ); + + $request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertErrorResponse( 'rest_cannot_export_templates', $response, 403 ); + } + + /** + * @ticket 54448 + */ + public function test_context_param() { + $this->markTestSkipped( 'Controller does not implement context_param().' ); + } + + /** + * @ticket 54448 + */ + public function test_get_item() { + $this->markTestSkipped( 'Controller does not implement get_item().' ); + } + + /** + * @ticket 54448 + */ + public function test_get_items() { + $this->markTestSkipped( 'Controller does not implement get_items().' ); + } + + /** + * @ticket 54448 + */ + public function test_create_item() { + $this->markTestSkipped( 'Controller does not implement create_item().' ); + } + + /** + * @ticket 54448 + */ + public function test_update_item() { + $this->markTestSkipped( 'Controller does not implement update_item().' ); + } + + /** + * @ticket 54448 + */ + public function test_delete_item() { + $this->markTestSkipped( 'Controller does not implement delete_item().' ); + } + + /** + * @ticket 54448 + */ + public function test_prepare_item() { + $this->markTestSkipped( 'Controller does not implement prepare_item().' ); + } + + /** + * @ticket 54448 + */ + public function test_get_item_schema() { + $this->markTestSkipped( 'Controller does not implement get_item_schema().' ); + } +}