160 lines
4.1 KiB
PHP
160 lines
4.1 KiB
PHP
<?php
|
|
/**
|
|
* Tests for the Shipping Methods REST API.
|
|
*
|
|
* @package WooCommerce\Tests\API
|
|
* @since 3.5.0
|
|
*/
|
|
|
|
namespace WooCommerce\RestApi\UnitTests\Tests\Version4;
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
use \WP_REST_Request;
|
|
use \WC_REST_Unit_Test_Case;
|
|
|
|
class ShippingMethods extends WC_REST_Unit_Test_Case {
|
|
|
|
/**
|
|
* User variable.
|
|
*
|
|
* @var WP_User
|
|
*/
|
|
protected static $user;
|
|
|
|
/**
|
|
* Setup once before running tests.
|
|
*
|
|
* @param object $factory Factory object.
|
|
*/
|
|
public static function wpSetUpBeforeClass( $factory ) {
|
|
self::$user = $factory->user->create(
|
|
array(
|
|
'role' => 'administrator',
|
|
)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Setup our test server, endpoints, and user info.
|
|
*/
|
|
public function setUp() {
|
|
parent::setUp();
|
|
wp_set_current_user( self::$user );
|
|
$this->zones = array();
|
|
}
|
|
|
|
/**
|
|
* Test route registration.
|
|
*
|
|
* @since 3.5.0
|
|
*/
|
|
public function test_register_routes() {
|
|
$routes = $this->server->get_routes();
|
|
$this->assertArrayHasKey( '/wc/v4/shipping_methods', $routes );
|
|
$this->assertArrayHasKey( '/wc/v4/shipping_methods/(?P<id>[\w-]+)', $routes );
|
|
}
|
|
|
|
/**
|
|
* Test getting all shipping methods.
|
|
*
|
|
* @since 3.5.0
|
|
*/
|
|
public function test_get_shipping_methods() {
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping_methods' ) );
|
|
$methods = $response->get_data();
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
$this->assertContains(
|
|
array(
|
|
'id' => 'free_shipping',
|
|
'title' => 'Free shipping',
|
|
'description' => 'Free shipping is a special method which can be triggered with coupons and minimum spends.',
|
|
'_links' => array(
|
|
'self' => array(
|
|
array(
|
|
'href' => rest_url( '/wc/v4/shipping_methods/free_shipping' ),
|
|
),
|
|
),
|
|
'collection' => array(
|
|
array(
|
|
'href' => rest_url( '/wc/v4/shipping_methods' ),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
$methods
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Tests to make sure shipping methods cannot viewed without valid permissions.
|
|
*
|
|
* @since 3.5.0
|
|
*/
|
|
public function test_get_shipping_methods_without_permission() {
|
|
wp_set_current_user( 0 );
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping_methods' ) );
|
|
$this->assertEquals( 401, $response->get_status() );
|
|
}
|
|
|
|
/**
|
|
* Tests getting a single shipping method.
|
|
*
|
|
* @since 3.5.0
|
|
*/
|
|
public function test_get_shipping_method() {
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping_methods/local_pickup' ) );
|
|
$method = $response->get_data();
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
$this->assertEquals(
|
|
array(
|
|
'id' => 'local_pickup',
|
|
'title' => 'Local pickup',
|
|
'description' => 'Allow customers to pick up orders themselves. By default, when using local pickup store base taxes will apply regardless of customer address.',
|
|
),
|
|
$method
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Tests getting a single shipping method without the correct permissions.
|
|
*
|
|
* @since 3.5.0
|
|
*/
|
|
public function test_get_shipping_method_without_permission() {
|
|
wp_set_current_user( 0 );
|
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping_methods/local_pickup' ) );
|
|
$this->assertEquals( 401, $response->get_status() );
|
|
}
|
|
|
|
/**
|
|
* Tests getting a shipping method with an invalid ID.
|
|
*
|
|
* @since 3.5.0
|
|
*/
|
|
public function test_get_shipping_method_invalid_id() {
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v4/shipping_methods/fake_method' ) );
|
|
$this->assertEquals( 404, $response->get_status() );
|
|
}
|
|
|
|
/**
|
|
* Test the shipping method schema.
|
|
*
|
|
* @since 3.5.0
|
|
*/
|
|
public function test_shipping_method_schema() {
|
|
$request = new WP_REST_Request( 'OPTIONS', '/wc/v4/shipping_methods' );
|
|
$response = $this->server->dispatch( $request );
|
|
$data = $response->get_data();
|
|
$properties = $data['schema']['properties'];
|
|
|
|
$this->assertEquals( 3, count( $properties ) );
|
|
$this->assertArrayHasKey( 'id', $properties );
|
|
$this->assertArrayHasKey( 'title', $properties );
|
|
$this->assertArrayHasKey( 'description', $properties );
|
|
}
|
|
}
|