2021-11-05 15:39:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Controller Tests.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes;
|
|
|
|
|
|
|
|
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;
|
|
|
|
use Automattic\WooCommerce\Blocks\Domain\Package as DomainPackage;
|
|
|
|
use Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
|
|
|
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\MoneyFormatter;
|
|
|
|
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\HtmlFormatter;
|
|
|
|
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\CurrencyFormatter;
|
|
|
|
use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;
|
2022-02-23 12:00:45 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\StoreApi\Schemas\V1\CheckoutSchema;
|
2021-11-05 15:39:08 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\Tests\Helpers\FixtureData;
|
2022-02-23 12:00:45 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\StoreApi\Routes\V1\Checkout as CheckoutRoute;
|
2021-11-05 15:39:08 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\StoreApi\SchemaController;
|
|
|
|
|
|
|
|
use Mockery\Adapter\Phpunit\MockeryTestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checkout Controller Tests.
|
|
|
|
*/
|
|
|
|
class Checkout extends MockeryTestCase {
|
|
|
|
/**
|
|
|
|
* Setup test products data. Called before every test.
|
|
|
|
*/
|
|
|
|
protected function setUp(): void {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
global $wp_rest_server;
|
|
|
|
$wp_rest_server = new \Spy_REST_Server();
|
|
|
|
do_action( 'rest_api_init', $wp_rest_server );
|
|
|
|
|
|
|
|
$formatters = new Formatters();
|
|
|
|
$formatters->register( 'money', MoneyFormatter::class );
|
|
|
|
$formatters->register( 'html', HtmlFormatter::class );
|
|
|
|
$formatters->register( 'currency', CurrencyFormatter::class );
|
|
|
|
|
|
|
|
$this->mock_extend = new ExtendRestApi( new DomainPackage( '', '', new FeatureGating( 2 ) ), $formatters );
|
|
|
|
$this->mock_extend->register_endpoint_data(
|
|
|
|
array(
|
|
|
|
'endpoint' => CheckoutSchema::IDENTIFIER,
|
|
|
|
'namespace' => 'extension_namespace',
|
|
|
|
'schema_callback' => function() {
|
|
|
|
return array(
|
|
|
|
'extension_key' => array(
|
|
|
|
'description' => 'Test key',
|
|
|
|
'type' => 'boolean',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$schema_controller = new SchemaController( $this->mock_extend );
|
2022-02-23 12:00:45 +00:00
|
|
|
$route = new CheckoutRoute( $schema_controller, $schema_controller->get( 'checkout' ) );
|
2021-11-05 15:39:08 +00:00
|
|
|
register_rest_route( $route->get_namespace(), $route->get_path(), $route->get_args(), true );
|
|
|
|
|
|
|
|
$fixtures = new FixtureData();
|
|
|
|
$fixtures->shipping_add_flat_rate();
|
2022-01-04 18:04:08 +00:00
|
|
|
$fixtures->payments_enable_bacs();
|
2021-11-05 15:39:08 +00:00
|
|
|
$this->products = array(
|
|
|
|
$fixtures->get_simple_product(
|
|
|
|
array(
|
|
|
|
'name' => 'Test Product 1',
|
|
|
|
'stock_status' => 'instock',
|
|
|
|
'regular_price' => 10,
|
|
|
|
'weight' => 10,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$fixtures->get_simple_product(
|
|
|
|
array(
|
|
|
|
'name' => 'Test Product 2',
|
|
|
|
'stock_status' => 'instock',
|
|
|
|
'regular_price' => 10,
|
|
|
|
'weight' => 10,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
wc_empty_cart();
|
|
|
|
$this->keys = array();
|
|
|
|
$this->keys[] = wc()->cart->add_to_cart( $this->products[0]->get_id(), 2 );
|
|
|
|
$this->keys[] = wc()->cart->add_to_cart( $this->products[1]->get_id(), 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tear down Rest API server.
|
|
|
|
*/
|
|
|
|
protected function tearDown(): void {
|
|
|
|
parent::tearDown();
|
|
|
|
global $wp_rest_server;
|
|
|
|
$wp_rest_server = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that registered extension data is correctly shown on options requests.
|
|
|
|
*/
|
|
|
|
public function test_options_extension_data() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'OPTIONS', '/wc/store/v1/checkout' );
|
2021-11-05 15:39:08 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
|
|
|
$data = $response->get_data();
|
|
|
|
$this->assertEquals(
|
|
|
|
array(
|
|
|
|
'description' => 'Test key',
|
|
|
|
'type' => 'boolean',
|
|
|
|
),
|
2022-02-23 12:00:45 +00:00
|
|
|
$data['schema']['properties']['extensions']['properties']['extension_namespace']['properties']['extension_key'],
|
|
|
|
print_r( $data, true )
|
2021-11-05 15:39:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that registered extension data is correctly posted and visible on the server after sanitization.
|
|
|
|
*/
|
|
|
|
public function test_post_extension_data() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/checkout' );
|
2021-11-05 15:39:08 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'billing_address' => (object) array(
|
|
|
|
'first_name' => 'test',
|
|
|
|
'last_name' => 'test',
|
|
|
|
'company' => '',
|
|
|
|
'address_1' => 'test',
|
|
|
|
'address_2' => '',
|
|
|
|
'city' => 'test',
|
|
|
|
'state' => '',
|
|
|
|
'postcode' => 'cb241ab',
|
|
|
|
'country' => 'GB',
|
|
|
|
'phone' => '',
|
|
|
|
'email' => 'test@test.com',
|
|
|
|
),
|
|
|
|
'shipping_address' => (object) array(
|
|
|
|
'first_name' => 'test',
|
|
|
|
'last_name' => 'test',
|
|
|
|
'company' => '',
|
|
|
|
'address_1' => 'test',
|
|
|
|
'address_2' => '',
|
|
|
|
'city' => 'test',
|
|
|
|
'state' => '',
|
|
|
|
'postcode' => 'cb241ab',
|
|
|
|
'country' => 'GB',
|
|
|
|
'phone' => '',
|
|
|
|
),
|
|
|
|
'payment_method' => 'bacs',
|
|
|
|
'extensions' => array(
|
|
|
|
'extension_namespace' => array(
|
|
|
|
'extension_key' => true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$action_callback = \Mockery::mock( 'ActionCallback' );
|
|
|
|
$action_callback->shouldReceive( 'do_callback' )->withArgs(
|
|
|
|
array(
|
|
|
|
\Mockery::any(),
|
|
|
|
\Mockery::on(
|
|
|
|
function ( $argument ) {
|
|
|
|
return true === $argument['extensions']['extension_namespace']['extension_key'];
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
)->once();
|
|
|
|
add_action( 'woocommerce_blocks_checkout_update_order_from_request', array( $action_callback, 'do_callback' ), 10, 2 );
|
|
|
|
$response = rest_get_server()->dispatch( $request );
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
|
|
|
remove_action( 'woocommerce_blocks_checkout_update_order_from_request', array( $action_callback, 'do_callback' ), 10, 2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that registered extension data is correctly posted and visible on the server after sanitization.
|
|
|
|
*/
|
|
|
|
public function test_post_invalid_extension_data() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/checkout' );
|
2021-11-05 15:39:08 +00:00
|
|
|
$request->set_header( 'X-WC-Store-API-Nonce', wp_create_nonce( 'wc_store_api' ) );
|
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'billing_address' => (object) array(
|
|
|
|
'first_name' => 'test',
|
|
|
|
'last_name' => 'test',
|
|
|
|
'company' => '',
|
|
|
|
'address_1' => 'test',
|
|
|
|
'address_2' => '',
|
|
|
|
'city' => 'test',
|
|
|
|
'state' => '',
|
|
|
|
'postcode' => 'cb241ab',
|
|
|
|
'country' => 'GB',
|
|
|
|
'phone' => '',
|
|
|
|
'email' => 'test@test.com',
|
|
|
|
),
|
|
|
|
'shipping_address' => (object) array(
|
|
|
|
'first_name' => 'test',
|
|
|
|
'last_name' => 'test',
|
|
|
|
'company' => '',
|
|
|
|
'address_1' => 'test',
|
|
|
|
'address_2' => '',
|
|
|
|
'city' => 'test',
|
|
|
|
'state' => '',
|
|
|
|
'postcode' => 'cb241ab',
|
|
|
|
'country' => 'GB',
|
|
|
|
'phone' => '',
|
|
|
|
),
|
|
|
|
'payment_method' => 'bacs',
|
|
|
|
'extensions' => array(
|
|
|
|
'extension_namespace' => array(
|
|
|
|
'extension_key' => 'invalid-string',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$response = rest_get_server()->dispatch( $request );
|
|
|
|
$this->assertEquals( 400, $response->get_status() );
|
|
|
|
$this->assertEquals( 'rest_invalid_param', $response->get_data()['code'] );
|
|
|
|
}
|
|
|
|
}
|