Skip to content

Commit

Permalink
Merge pull request #177 from kidunot89/feature/new-ql-session-handler…
Browse files Browse the repository at this point in the history
…-test

Codecoverage boost
  • Loading branch information
kidunot89 authored Nov 26, 2019
2 parents 0db0704 + 06a50ca commit 1610fda
Show file tree
Hide file tree
Showing 8 changed files with 394 additions and 29 deletions.
4 changes: 1 addition & 3 deletions includes/data/mutation/class-cart-mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ public static function item_is_valid( array $item ) {
public static function check_session_token() {
$token_invalid = apply_filters( 'woo_session_token_errors', null );
if ( $token_invalid ) {
if ( defined( 'GRAPHQL_DEBUG' ) && true === GRAPHQL_DEBUG ) {
throw new UserError( $token_invalid );
}
throw new UserError( $token_invalid );
}
}
}
12 changes: 9 additions & 3 deletions includes/utils/class-ql-session-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,13 @@ public function init_session_token() {
}
} else {
if ( is_wp_error( $token ) ) {
add_filter( 'woo_session_token_errors', $token->get_error_message() );
add_filter(
'woo_session_token_errors',
function( $errors ) use ( $token ) {
$errors = $token->get_error_message();
return $errors;
}
);
}
$this->set_session_expiration();
$this->_customer_id = $this->generate_customer_id();
Expand Down Expand Up @@ -168,12 +174,12 @@ public function get_session_token() {
}

// The Token is decoded now validate the iss.
if ( ! isset( $token->iss ) || get_bloginfo( 'url' ) !== $token->iss ) {
if ( empty( $token->iss ) || get_bloginfo( 'url' ) !== $token->iss ) {
throw new \Exception( __( 'The iss do not match with this server', 'wp-graphql-woocommerce' ) );
}

// Validate the customer id in the token.
if ( ! isset( $token->data->customer_id ) ) {
if ( empty( $token->data ) || empty( $token->data->customer_id ) ) {
throw new \Exception( __( 'Customer ID not found in the token', 'wp-graphql-woocommerce' ) );
}
} catch ( \Exception $error ) {
Expand Down
178 changes: 178 additions & 0 deletions tests/_support/Helper/GraphQLE2E.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,179 @@ public function restoreCartItems( $input, $request_headers = array() ) {
return $response;
}

/**
* Removes all items from the cart.
*
* @param array $input
* @param string $session_header
* @return array
*/
public function emptyCart( $input, $request_headers = array() ) {
$mutation = '
mutation emptyCart( $input: EmptyCartInput! ) {
emptyCart( input: $input ) {
clientMutationId
cart {
contents {
nodes {
key
product {
... on SimpleProduct {
id
}
... on VariableProduct {
id
}
}
variation {
id
}
quantity
subtotal
subtotalTax
total
tax
}
}
}
}
}
';

// Send GraphQL request and get response.
$response = $this->sendGraphQLRequest( $mutation, $input, $request_headers );

// Return response.
return $response;
}

/**
* Adds fee on cart.
*
* @param array $input
* @param string $session_header
* @return array
*/
public function addFee( $input, $request_headers = array() ) {
$mutation = '
mutation addFee( $input: AddFeeInput! ) {
addFee( input: $input ) {
clientMutationId
cartFee {
id
name
taxClass
taxable
amount
total
}
}
}
';

// Send GraphQL request and get response.
$response = $this->sendGraphQLRequest( $mutation, $input, $request_headers );

// Return response.
return $response;
}

/**
* Applies coupon to the cart.
*
* @param array $input
* @param string $session_header
* @return array
*/
public function applyCoupon( $input, $request_headers = array() ) {
$mutation = '
mutation applyCoupon( $input: ApplyCouponInput! ) {
applyCoupon( input: $input ) {
clientMutationId
cart {
appliedCoupons {
nodes {
code
}
}
contents {
nodes {
key
product {
... on SimpleProduct {
id
}
... on VariableProduct {
id
}
}
quantity
subtotal
subtotalTax
total
tax
}
}
}
}
}
';

// Send GraphQL request and get response.
$response = $this->sendGraphQLRequest( $mutation, $input, $request_headers );

// Return response.
return $response;
}

/**
* Removes coupons on the cart.
*
* @param array $input
* @param string $session_header
* @return array
*/
public function removeCoupons( $input, $request_headers = array() ) {
$mutation = '
mutation removeCoupons( $input: RemoveCouponsInput! ) {
removeCoupons( input: $input ) {
clientMutationId
cart {
appliedCoupons {
nodes {
code
}
}
contents {
nodes {
key
product {
... on SimpleProduct {
id
}
... on VariableProduct {
id
}
}
quantity
subtotal
subtotalTax
total
tax
}
}
}
}
}
';

// Send GraphQL request and get response.
$response = $this->sendGraphQLRequest( $mutation, $input, $request_headers );

// Return response.
return $response;
}

/**
* Place customer order.
*
Expand Down Expand Up @@ -406,6 +579,11 @@ public function sendGraphQLRequest( $query, $input, $request_headers = array() )
// use --debug flag to view
codecept_debug( $response );

// Delete request headers
foreach( $request_headers as $header => $value ) {
$rest->deleteHeader( $header );
}

return $response;
}

Expand Down
14 changes: 7 additions & 7 deletions tests/acceptance/NewCustomerCheckingOutCept.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$success = $I->addToCart( $add_to_cart_input );

$I->assertArrayNotHasKey( 'error', $success );
$I->assertArrayNotHasKey( 'errors', $success );
$I->assertArrayHasKey('data', $success );
$I->assertArrayHasKey('addToCart', $success['data'] );
$I->assertArrayHasKey('cartItem', $success['data']['addToCart'] );
Expand All @@ -47,7 +47,7 @@
$request_headers()
);

$I->assertArrayNotHasKey( 'error', $success );
$I->assertArrayNotHasKey( 'errors', $success );
$I->assertArrayHasKey('data', $success );
$I->assertArrayHasKey('addToCart', $success['data'] );
$I->assertArrayHasKey('cartItem', $success['data']['addToCart'] );
Expand All @@ -68,7 +68,7 @@
$request_headers()
);

$I->assertArrayNotHasKey( 'error', $success );
$I->assertArrayNotHasKey( 'errors', $success );
$I->assertArrayHasKey('data', $success );
$I->assertArrayHasKey('addToCart', $success['data'] );
$I->assertArrayHasKey('cartItem', $success['data']['addToCart'] );
Expand All @@ -89,7 +89,7 @@
$request_headers()
);

$I->assertArrayNotHasKey( 'error', $success );
$I->assertArrayNotHasKey( 'errors', $success );
$I->assertArrayHasKey('data', $success );
$I->assertArrayHasKey('addToCart', $success['data'] );
$I->assertArrayHasKey('cartItem', $success['data']['addToCart'] );
Expand All @@ -111,7 +111,7 @@
$request_headers()
);

$I->assertArrayNotHasKey( 'error', $success );
$I->assertArrayNotHasKey( 'errors', $success );
$I->assertArrayHasKey('data', $success );
$I->assertArrayHasKey('removeItemsFromCart', $success['data'] );
$I->assertArrayHasKey('cartItems', $success['data']['removeItemsFromCart'] );
Expand Down Expand Up @@ -141,7 +141,7 @@
$last_request_headers
);

$I->assertArrayNotHasKey( 'error', $success );
$I->assertArrayNotHasKey( 'errors', $success );
$I->assertArrayHasKey('data', $success );
$I->assertArrayHasKey('updateItemQuantities', $success['data'] );
$I->assertArrayHasKey('removed', $success['data']['updateItemQuantities'] );
Expand Down Expand Up @@ -194,7 +194,7 @@
// use --debug flag to view
codecept_debug( $success );

$I->assertArrayNotHasKey( 'error', $success );
$I->assertArrayNotHasKey( 'errors', $success );
$I->assertArrayHasKey('data', $success );
$I->assertArrayHasKey('checkout', $success['data'] );
$I->assertArrayHasKey('order', $success['data']['checkout'] );
Expand Down
Loading

0 comments on commit 1610fda

Please sign in to comment.