wordpress-develop/tests/phpunit/tests/rest-api/wpRestBlockNavigationAreasController.php
Sergey Biryukov 53d604365e External Libraries: Update the Requests library to version 2.0.0.
This is a major release and contains breaking changes.

Most important changes to be aware of for this release:
* All code is now namespaced. Though there is a full backward compatibility layer available and the old class names are still supported, using them will generate a deprecation notice (which can be silenced by plugins if they'd need to support multiple WP versions). See the [https://requests.ryanmccue.info/docs/upgrading.html upgrade guide] for more details.
* A lot of classes have been marked `final`. This should generally not affect userland code as care has been taken to not apply the `final` keyword to classes which are known to be extended in userland code.
* Extensive input validation has been added to Requests. When Requests is used as documented though, this will be unnoticable.
* A new `WpOrg\Requests\Requests::has_capabilities()` method has been introduced which can be used to address #37708.
* A new `WpOrg\Requests\Response::decode_body()` method has been introduced which may be usable to simplify some of the WP native wrapper code.
* Remaining PHP 8.0 compatibility fixed (support for named parameters).
* PHP 8.1 compatibility.

Release notes: https://github.com/WordPress/Requests/releases/tag/v2.0.0

For a full list of changes in this update, see the Requests GitHub:
https://github.com/WordPress/Requests/compare/v1.8.1...v2.0.0

Follow-up to [50842], [51078].

Props jrf, schlessera, datagutten, wojsmol, dd32, dustinrue, soulseekah, costdev, szepeviktor.
Fixes #54504.

git-svn-id: https://develop.svn.wordpress.org/trunk@52244 602fd350-edb4-49c9-b593-d223f7449a82
2021-11-25 01:10:30 +00:00

137 lines
4.4 KiB
PHP

<?php
/**
* Unit tests covering WP_REST_Block_Navigation_Areas_Controller functionality.
*
* @package WordPress
* @subpackage REST_API
* @group restapi
* @since 5.9.0
*/
class Tests_REST_WpRestBlockNavigationAreasController extends WP_Test_REST_Controller_Testcase {
const OPTION = 'wp_navigation_areas';
/**
* @var int Administrator user ID
*/
protected static $admin_id;
/**
* @var array Contains mapping
*/
protected static $old_mapping;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
)
);
static::$old_mapping = get_option( static::OPTION, array() );
}
public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_id );
update_option( static::OPTION, static::$old_mapping );
}
public function test_get_items() {
wp_set_current_user( static::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-navigation-areas' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$data = $response->get_data();
$this->assertIsArray( $data );
$navigation_areas = get_navigation_areas();
$expected_data = array();
foreach ( $navigation_areas as $name => $navigation_area ) {
$expected_data[] = array(
'name' => $name,
'description' => $navigation_area,
'navigation' => 0,
);
}
$this->assertSameSets( $expected_data, $data );
}
public function test_register_routes() {
wp_set_current_user( static::$admin_id );
$routes = rest_get_server()->get_routes();
$this->assertArrayHasKey( '/wp/v2/block-navigation-areas', $routes );
$this->assertArrayHasKey( '/wp/v2/block-navigation-areas/(?P<area>[\\w-]+)', $routes );
}
public function test_context_param() {
wp_set_current_user( static::$admin_id );
$request = new WP_REST_Request( WpOrg\Requests\Requests::OPTIONS, '/wp/v2/block-navigation-areas' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$data = $response->get_data();
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}
public function test_get_item() {
wp_set_current_user( static::$admin_id );
$navigation_area = array_rand( get_navigation_areas(), 1 );
$this->assertIsString( $navigation_area );
$this->assertNotEmpty( $navigation_area );
$route = sprintf( '/wp/v2/block-navigation-areas/%s', urlencode( $navigation_area ) );
$request = new WP_REST_Request( 'GET', $route );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$data = $response->get_data();
$this->assertIsArray( $data );
$this->assertArrayHasKey( 'name', $data );
$this->assertSame( $navigation_area, $data['name'] );
}
public function test_create_item() {
// We cannot create new navigation areas using the current block navigation areas API,
// so the test should be marked as passed.
$this->markTestSkipped();
}
public function test_update_item() {
wp_set_current_user( static::$admin_id );
$navigation_area = array_rand( get_navigation_areas(), 1 );
$route = sprintf( '/wp/v2/block-navigation-areas/%s', urlencode( $navigation_area ) );
$request = new WP_REST_Request( WpOrg\Requests\Requests::POST, $route );
$updated_navigation_area = array(
'name' => $navigation_area,
'description' => 'Test Description',
);
$request->set_param( 'navigation', $updated_navigation_area );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );
$new_mapping = get_option( static::OPTION, array() );
$this->assertSame( $new_mapping[ $navigation_area ], $updated_navigation_area );
}
public function test_delete_item() {
// We cannot delete navigation areas using the current block navigation areas API,
// so the test should be marked as passed.
$this->markTestSkipped();
}
public function test_prepare_item() {
// The current block navigation areas API doesn't implement any custom prepare_item logic
// so there is nothing to test.
$this->markTestSkipped();
}
public function test_get_item_schema() {
// The current block navigation areas API doesn't implement any custom item schema
// so there is nothing to test.
$this->markTestSkipped();
}
}