@@ -341,6 +341,23 @@ public function testCartMutationsWithInvalidCartSessionToken( FunctionalTester $
341341
342342 $ I ->assertArrayHasKey ( 'errors ' , $ failed );
343343
344+ /**
345+ * Attempt to restore item to the cart with invalid session token.
346+ * GraphQL should throw an error and mutation will fail.
347+ *
348+ * @Note: No items have been removed from the cart in this session,
349+ * but mutation should failed before that becomes a factor.
350+ */
351+ $ failed = $ I ->updateShippingMethod (
352+ array (
353+ 'clientMutationId ' => 'someId ' ,
354+ 'shippingMethods ' => array ( 'legacy_flat_rate ' ),
355+ ),
356+ array ( 'woocommerce-session ' => "Session invalid-jwt-token-string " )
357+ );
358+
359+ $ I ->assertArrayHasKey ( 'errors ' , $ failed );
360+
344361 /**
345362 * Attempt to query cart with invalid session token.
346363 * GraphQL should throw an error and query will fail.
@@ -365,4 +382,153 @@ public function testCartMutationsWithInvalidCartSessionToken( FunctionalTester $
365382
366383 $ I ->assertArrayHasKey ( 'errors ' , $ failed );
367384 }
385+
386+ public function testCartSessionDataMutations ( FunctionalTester $ I ) {
387+ /**
388+ * Add item to the cart
389+ */
390+ $ success = $ I ->addToCart (
391+ array (
392+ 'clientMutationId ' => 'someId ' ,
393+ 'productId ' => $ this ->product_catalog ['socks ' ],
394+ 'quantity ' => 2 ,
395+ )
396+ );
397+
398+ $ I ->assertArrayNotHasKey ( 'errors ' , $ success );
399+ $ I ->assertArrayHasKey ('data ' , $ success );
400+ $ I ->assertArrayHasKey ('addToCart ' , $ success ['data ' ] );
401+ $ I ->assertArrayHasKey ('cartItem ' , $ success ['data ' ]['addToCart ' ] );
402+ $ I ->assertArrayHasKey ('key ' , $ success ['data ' ]['addToCart ' ]['cartItem ' ] );
403+ $ cart_item_key = $ success ['data ' ]['addToCart ' ]['cartItem ' ]['key ' ];
404+
405+ /**
406+ * Assert existence and validity of "woocommerce-session" HTTP header.
407+ */
408+ $ I ->seeHttpHeaderOnce ( 'woocommerce-session ' );
409+ $ session_token = $ I ->grabHttpHeader ( 'woocommerce-session ' );
410+
411+ // Decode token
412+ JWT ::$ leeway = 60 ;
413+ $ token_data = ! empty ( $ session_token )
414+ ? JWT ::decode ( $ session_token , 'graphql-woo-cart-session ' , array ( 'HS256 ' ) )
415+ : null ;
416+
417+ $ I ->assertNotEmpty ( $ token_data );
418+ $ I ->assertNotEmpty ( $ token_data ->iss );
419+ $ I ->assertNotEmpty ( $ token_data ->iat );
420+ $ I ->assertNotEmpty ( $ token_data ->nbf );
421+ $ I ->assertNotEmpty ( $ token_data ->exp );
422+ $ I ->assertNotEmpty ( $ token_data ->data );
423+ $ I ->assertNotEmpty ( $ token_data ->data ->customer_id );
424+
425+ $ wp_url = getenv ( 'WP_URL ' );
426+ $ I ->assertEquals ( $ token_data ->iss , $ wp_url );
427+
428+ /**
429+ * Make a cart query request with "woocommerce-session" HTTP Header and confirm
430+ * correct cart contents and chosen and available shipping methods.
431+ */
432+ $ query = '
433+ query {
434+ cart {
435+ contents {
436+ nodes {
437+ key
438+ }
439+ }
440+ availableShippingMethods {
441+ packageDetails
442+ supportsShippingCalculator
443+ rates {
444+ id
445+ cost
446+ label
447+ }
448+ }
449+ chosenShippingMethod
450+ }
451+ }
452+ ' ;
453+
454+ $ actual = $ I ->sendGraphQLRequest ( $ query , null , array ( 'woocommerce-session ' => "Session {$ session_token }" ) );
455+ $ expected = array (
456+ 'data ' => array (
457+ 'cart ' => array (
458+ 'contents ' => array (
459+ 'nodes ' => array (
460+ array (
461+ 'key ' => $ cart_item_key ,
462+ ),
463+ ),
464+ ),
465+ 'availableShippingMethods ' => array (
466+ array (
467+ 'packageDetails ' => 'socks ×2 ' ,
468+ 'supportsShippingCalculator ' => true ,
469+ 'rates ' => array (
470+ array (
471+ 'id ' => 'legacy_flat_rate ' ,
472+ 'cost ' => 10.00 ,
473+ 'label ' => 'Flat rate '
474+ ),
475+ array (
476+ 'id ' => 'legacy_free_shipping ' ,
477+ 'cost ' => 0 ,
478+ 'label ' => 'Free shipping '
479+ ),
480+ )
481+ )
482+ ),
483+ 'chosenShippingMethod ' => 'legacy_flat_rate '
484+ ),
485+ ),
486+ );
487+
488+ $ I ->assertEquals ( $ expected , $ actual );
489+
490+ /**
491+ * Update shipping method to 'legacy_flat_rate' shipping.
492+ */
493+ $ mutation = '
494+ mutation ($input: UpdateShippingMethodInput!){
495+ updateShippingMethod(input: $input) {
496+ cart {
497+ availableShippingMethods {
498+ packageDetails
499+ supportsShippingCalculator
500+ rates {
501+ id
502+ cost
503+ label
504+ }
505+ }
506+ chosenShippingMethod
507+ shippingTotal
508+ shippingTax
509+ subtotal
510+ subtotalTax
511+ total
512+ }
513+ }
514+ }
515+ ' ;
516+
517+ $ success = $ I ->sendGraphQLRequest (
518+ $ mutation ,
519+ array (
520+ 'clientMutationId ' => 'someId ' ,
521+ 'shippingMethods ' => array ( 'legacy_free_shipping ' ),
522+ ),
523+ array ( 'woocommerce-session ' => "Session {$ session_token }" )
524+ );
525+
526+ $ I ->assertArrayNotHasKey ( 'errors ' , $ success );
527+ $ I ->assertNotEmpty ( $ success ['data ' ] );
528+ $ I ->assertNotEmpty ( $ success ['data ' ]['updateShippingMethod ' ] );
529+ $ I ->assertNotEmpty ( $ success ['data ' ]['updateShippingMethod ' ]['cart ' ] );
530+ $ cart = $ success ['data ' ]['updateShippingMethod ' ]['cart ' ];
531+ $ I ->assertNotEmpty ( $ cart ['availableShippingMethods ' ] );
532+ $ I ->assertEquals ( 'legacy_free_shipping ' , $ cart ['chosenShippingMethod ' ] );
533+ }
368534}
0 commit comments