|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\PageCache\Quote\Guest; |
| 9 | + |
| 10 | +use Magento\TestFramework\Helper\Bootstrap; |
| 11 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 12 | + |
| 13 | +/** |
| 14 | + * End to end test which creates an empty cart and add product to the cart and load the cart. |
| 15 | + * Validates that the cache-debug header is a MISS for any subsequent cart requests |
| 16 | + * |
| 17 | + * @magentoApiDataFixture Magento/Catalog/_files/products.php |
| 18 | + */ |
| 19 | +class CartCacheTest extends GraphQlAbstract |
| 20 | +{ |
| 21 | + /** @var string */ |
| 22 | + private $maskedQuoteId; |
| 23 | + |
| 24 | + protected function setUp() |
| 25 | + { |
| 26 | + $this->markTestSkipped( |
| 27 | + 'This test will stay skipped until DEVOPS-4924 is resolved' |
| 28 | + ); |
| 29 | + } |
| 30 | + /** |
| 31 | + * Tests that X-Magento-Tags are correct |
| 32 | + */ |
| 33 | + public function testCartIsNotCached() |
| 34 | + { |
| 35 | + $qty = 2; |
| 36 | + $sku = 'simple'; |
| 37 | + $cartId = $this->createEmptyCart(); |
| 38 | + $this->addSimpleProductToCart($cartId, $qty, $sku); |
| 39 | + $getCartQuery = $this->checkCart($cartId); |
| 40 | + $response = $this->graphQlQuery($getCartQuery); |
| 41 | + self::assertArrayHasKey('cart', $response); |
| 42 | + self::assertArrayHasKey('items', $response['cart']); |
| 43 | + |
| 44 | + $responseMissHeaders = $this->graphQlQueryForHttpHeaders($getCartQuery); |
| 45 | + $this->assertContains('X-Magento-Cache-Debug: MISS', $responseMissHeaders); |
| 46 | + |
| 47 | + /** Cache debug header value is still a MISS for any subsequent request */ |
| 48 | + $responseMissHeadersNext = $this->graphQlQueryForHttpHeaders($getCartQuery); |
| 49 | + $this->assertContains('X-Magento-Cache-Debug: MISS', $responseMissHeadersNext); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Create a guest cart which generates a maskedQuoteId |
| 54 | + * |
| 55 | + * @return mixed |
| 56 | + */ |
| 57 | + private function createEmptyCart() |
| 58 | + { |
| 59 | + $query = |
| 60 | + <<<QUERY |
| 61 | + mutation |
| 62 | + { |
| 63 | + createEmptyCart |
| 64 | + } |
| 65 | +QUERY; |
| 66 | + |
| 67 | + $response = $this->graphQlMutation($query); |
| 68 | + $this->maskedQuoteId = $response['createEmptyCart']; |
| 69 | + return $this->maskedQuoteId; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Add simple product to the cart using the maskedQuoteId |
| 74 | + * @param $maskedCartId |
| 75 | + * @param $qty |
| 76 | + * @param $sku |
| 77 | + */ |
| 78 | + private function addSimpleProductToCart($maskedCartId, $qty, $sku) |
| 79 | + { |
| 80 | + $addProductToCartQuery = |
| 81 | + <<<QUERY |
| 82 | + mutation { |
| 83 | + addSimpleProductsToCart( |
| 84 | + input: { |
| 85 | + cart_id: "{$maskedCartId}" |
| 86 | + cartItems: [ |
| 87 | + { |
| 88 | + data: { |
| 89 | + qty: $qty |
| 90 | + sku: "$sku" |
| 91 | + } |
| 92 | + } |
| 93 | + ] |
| 94 | + } |
| 95 | + ) { |
| 96 | + cart { |
| 97 | + items { |
| 98 | + qty |
| 99 | + product { |
| 100 | + sku |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +QUERY; |
| 107 | + $response = $this->graphQlMutation($addProductToCartQuery); |
| 108 | + self::assertArrayHasKey('cart', $response['addSimpleProductsToCart']); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * |
| 113 | + * @param string $maskedQuoteId |
| 114 | + * @return string |
| 115 | + */ |
| 116 | + private function checkCart(string $maskedQuoteId): string |
| 117 | + { |
| 118 | + return <<<QUERY |
| 119 | +{ |
| 120 | + cart(cart_id: "{$maskedQuoteId}") { |
| 121 | + items { |
| 122 | + id |
| 123 | + qty |
| 124 | + product { |
| 125 | + sku |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | +QUERY; |
| 131 | + } |
| 132 | +} |
0 commit comments