2021-06-07 09:16:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Cart extensions route tests.
|
|
|
|
*/
|
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes;
|
2021-06-07 09:16:47 +00:00
|
|
|
|
2021-08-20 13:58:32 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes\ControllerTestCase;
|
2022-10-27 14:29:27 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\Tests\Helpers\FixtureData;
|
2021-06-07 09:16:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cart Controller Tests.
|
|
|
|
*/
|
2021-08-20 13:58:32 +00:00
|
|
|
class CartExtensions extends ControllerTestCase {
|
2021-06-07 09:16:47 +00:00
|
|
|
|
2022-10-27 14:29:27 +00:00
|
|
|
/**
|
2022-11-09 15:28:08 +00:00
|
|
|
* Setup test product data. Called before every test.
|
2022-10-27 14:29:27 +00:00
|
|
|
*/
|
2022-11-09 15:28:08 +00:00
|
|
|
protected function setUp(): void {
|
2022-10-27 14:29:27 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$fixtures = new FixtureData();
|
|
|
|
|
|
|
|
$this->product = $fixtures->get_simple_product(
|
|
|
|
array(
|
|
|
|
'name' => 'Test Product 1',
|
|
|
|
'regular_price' => 10,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
wc_empty_cart();
|
|
|
|
|
|
|
|
wc()->cart->add_to_cart( $this->product->get_id(), 1 );
|
|
|
|
|
|
|
|
woocommerce_store_api_register_update_callback(
|
|
|
|
array(
|
|
|
|
'namespace' => 'valid-test-plugin',
|
|
|
|
'callback' => function() {
|
|
|
|
add_action(
|
|
|
|
'woocommerce_cart_calculate_fees',
|
|
|
|
function() {
|
|
|
|
wc()->cart->add_fee( 'Surcharge', 10, true, 'standard' );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2021-06-07 09:16:47 +00:00
|
|
|
/**
|
|
|
|
* Test getting cart with invalid namespace.
|
|
|
|
*/
|
2022-10-27 14:29:27 +00:00
|
|
|
public function test_invalid_namespace() {
|
2022-02-23 12:00:45 +00:00
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/extensions' );
|
2022-03-11 12:07:08 +00:00
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
2021-06-07 09:16:47 +00:00
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'namespace' => 'test-plugin',
|
|
|
|
)
|
|
|
|
);
|
2022-02-23 12:00:45 +00:00
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
400
|
|
|
|
);
|
2021-06-07 09:16:47 +00:00
|
|
|
}
|
2022-10-27 14:29:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting cart with invalid namespace.
|
|
|
|
*/
|
|
|
|
public function test_cart_being_updated() {
|
|
|
|
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/extensions' );
|
|
|
|
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
|
|
|
|
$request->set_body_params(
|
|
|
|
array(
|
|
|
|
'namespace' => 'valid-test-plugin',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->assertAPIResponse(
|
|
|
|
$request,
|
|
|
|
200,
|
|
|
|
array(
|
|
|
|
'totals' => array(
|
|
|
|
'total_fees' => '1000',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2021-06-07 09:16:47 +00:00
|
|
|
}
|