2021-11-05 15:39:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Controller Tests.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes;
|
|
|
|
|
2022-03-04 13:28:37 +00:00
|
|
|
use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;
|
|
|
|
use Automattic\WooCommerce\StoreApi\Formatters;
|
|
|
|
use Automattic\WooCommerce\StoreApi\Formatters\MoneyFormatter;
|
|
|
|
use Automattic\WooCommerce\StoreApi\Formatters\HtmlFormatter;
|
|
|
|
use Automattic\WooCommerce\StoreApi\Formatters\CurrencyFormatter;
|
|
|
|
use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema;
|
2021-11-05 15:39:08 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\Tests\Helpers\FixtureData;
|
2022-03-04 13:28:37 +00:00
|
|
|
use Automattic\WooCommerce\StoreApi\Routes\V1\Checkout as CheckoutRoute;
|
|
|
|
use Automattic\WooCommerce\StoreApi\SchemaController;
|
2021-11-05 15:39:08 +00:00
|
|
|
|
|
|
|
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 );
|
|
|
|
|
2022-03-04 13:28:37 +00:00
|
|
|
wp_set_current_user( 0 );
|
|
|
|
$customer = get_user_by( 'email', 'testaccount@test.com' );
|
|
|
|
wp_delete_user( $customer->id );
|
|
|
|
|
2021-11-05 15:39:08 +00:00
|
|
|
$formatters = new Formatters();
|
|
|
|
$formatters->register( 'money', MoneyFormatter::class );
|
|
|
|
$formatters->register( 'html', HtmlFormatter::class );
|
|
|
|
$formatters->register( 'currency', CurrencyFormatter::class );
|
|
|
|
|
2022-03-01 10:34:05 +00:00
|
|
|
$this->mock_extend = new ExtendSchema( $formatters );
|
2021-11-05 15:39:08 +00:00
|
|
|
$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'],
|
2022-03-01 10:34:05 +00:00
|
|
|
print_r( $data['schema']['properties']['extensions']['properties']['extension_namespace'], 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' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2021-11-05 15:39:08 +00:00
|
|
|
$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' => '',
|
2022-03-04 13:28:37 +00:00
|
|
|
'email' => 'testaccount@test.com',
|
2021-11-05 15:39:08 +00:00
|
|
|
),
|
|
|
|
'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();
|
2022-03-07 13:51:07 +00:00
|
|
|
add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( $action_callback, 'do_callback' ), 10, 2 );
|
2021-11-05 15:39:08 +00:00
|
|
|
$response = rest_get_server()->dispatch( $request );
|
|
|
|
$this->assertEquals( 200, $response->get_status() );
|
2022-03-07 13:51:07 +00:00
|
|
|
remove_action( 'woocommerce_store_api_checkout_update_order_from_request', array( $action_callback, 'do_callback' ), 10, 2 );
|
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_invalid_extension_data() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/checkout' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2021-11-05 15:39:08 +00:00
|
|
|
$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' => '',
|
2022-03-04 13:28:37 +00:00
|
|
|
'email' => 'testaccount@test.com',
|
2021-11-05 15:39:08 +00:00
|
|
|
),
|
|
|
|
'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'] );
|
|
|
|
}
|
2022-03-04 13:28:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that accounts are created on request.
|
|
|
|
*/
|
|
|
|
public function test_checkout_create_account() {
|
|
|
|
update_option( 'woocommerce_enable_guest_checkout', 'yes' );
|
|
|
|
update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'yes' );
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/checkout' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2022-03-04 13:28:37 +00:00
|
|
|
$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' => 'testaccount@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' => '',
|
|
|
|
),
|
|
|
|
'create_account' => true,
|
|
|
|
'payment_method' => 'bacs',
|
|
|
|
'extensions' => array(
|
|
|
|
'extension_namespace' => array(
|
|
|
|
'extension_key' => true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = rest_get_server()->dispatch( $request );
|
|
|
|
$status = $response->get_status();
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( $status, 200 );
|
|
|
|
$this->assertTrue( $data['customer_id'] > 0 );
|
|
|
|
|
|
|
|
$customer = get_user_by( 'id', $data['customer_id'] );
|
|
|
|
$this->assertEquals( $customer->user_email, 'testaccount@test.com' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test account creation options.
|
|
|
|
*/
|
|
|
|
public function test_checkout_do_not_create_account() {
|
|
|
|
update_option( 'woocommerce_enable_guest_checkout', 'yes' );
|
|
|
|
update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'yes' );
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/checkout' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2022-03-04 13:28:37 +00:00
|
|
|
$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' => 'testaccount@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' => '',
|
|
|
|
),
|
|
|
|
'create_account' => false,
|
|
|
|
'payment_method' => 'bacs',
|
|
|
|
'extensions' => array(
|
|
|
|
'extension_namespace' => array(
|
|
|
|
'extension_key' => true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = rest_get_server()->dispatch( $request );
|
|
|
|
$status = $response->get_status();
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( $status, 200 );
|
|
|
|
$this->assertEquals( $data['customer_id'], 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test account creation options.
|
|
|
|
*/
|
|
|
|
public function test_checkout_force_create_account() {
|
|
|
|
update_option( 'woocommerce_enable_guest_checkout', 'no' );
|
|
|
|
update_option( 'woocommerce_enable_signup_and_login_from_checkout', 'yes' );
|
|
|
|
|
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/checkout' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2022-03-04 13:28:37 +00:00
|
|
|
$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' => 'testaccount@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,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$response = rest_get_server()->dispatch( $request );
|
|
|
|
$status = $response->get_status();
|
|
|
|
$data = $response->get_data();
|
|
|
|
|
|
|
|
$this->assertEquals( 200, $status, print_r( $data, true ) );
|
|
|
|
$this->assertTrue( $data['customer_id'] > 0 );
|
|
|
|
|
|
|
|
$customer = get_user_by( 'id', $data['customer_id'] );
|
|
|
|
$this->assertEquals( $customer->user_email, 'testaccount@test.com' );
|
|
|
|
}
|
2021-11-05 15:39:08 +00:00
|
|
|
}
|