mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 12:20:22 +00:00
REST API: Ensure that the export template endpoint returns a valid WP_Error object.
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
This commit is contained in:
@@ -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' );
|
||||
|
||||
@@ -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() )
|
||||
);
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
/**
|
||||
* WP_REST_Edit_Site_Export_Controller tests.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage REST_API
|
||||
* @since 5.9.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for WP_REST_Edit_Site_Export_Controller.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @covers WP_REST_Edit_Site_Export_Controller
|
||||
*
|
||||
* @group restapi
|
||||
*/
|
||||
class Tests_REST_WpRestEditSiteExportController extends WP_Test_REST_Controller_Testcase {
|
||||
|
||||
/**
|
||||
* The REST API route for the edit site export.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const REQUEST_ROUTE = '/wp-block-editor/v1/export';
|
||||
|
||||
/**
|
||||
* Subscriber user ID.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected static $subscriber_id;
|
||||
|
||||
/**
|
||||
* Set up class test fixtures.
|
||||
*
|
||||
* @since 5.9.0
|
||||
*
|
||||
* @param WP_UnitTest_Factory $factory WordPress unit test factory.
|
||||
*/
|
||||
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
|
||||
self::$subscriber_id = $factory->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().' );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user