Commit 7c2decca authored by Matt Glaman's avatar Matt Glaman Committed by Bojan Živanović
Browse files

Issue #2903935 by mglaman: Checkout flows should have the order and...

Issue #2903935 by mglaman: Checkout flows should have the order and configuration cache metadata attached
parent 0fde2eeb
Loading
Loading
Loading
Loading
+3 −1
Changes for modules/checkout/src/CheckoutFlowPluginCollection.php: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -54,7 +54,9 @@ class CheckoutFlowPluginCollection extends DefaultSingleLazyPluginCollection {
      throw new PluginException("The checkout flow '{$this->entityId}' did not specify a plugin.");
    }

    parent::initializePlugin($instance_id);
    $configuration = ['_entity_id' => $this->entityId] + $this->configuration;
    $plugin = $this->manager->createInstance($instance_id, $configuration);
    $this->set($instance_id, $plugin);
  }

}
+22 −1
Changes for modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php: 22 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow;

use Drupal\commerce\Response\NeedsRedirectException;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
@@ -43,6 +44,15 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf
   */
  protected $order;

  /**
   * The ID of the parent config entity.
   *
   * Not available while the plugin is being configured.
   *
   * @var string
   */
  protected $entityId;

  /**
   * Constructs a new CheckoutFlowBase object.
   *
@@ -62,10 +72,14 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, RouteMatchInterface $route_match) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->setConfiguration($configuration);
    $this->entityTypeManager = $entity_type_manager;
    $this->eventDispatcher = $event_dispatcher;
    $this->order = $route_match->getParameter('commerce_order');
    if (array_key_exists('_entity_id', $configuration)) {
      $this->entityId = $configuration['_entity_id'];
      unset($configuration['_entity_id']);
    }
    $this->setConfiguration($configuration);
  }

  /**
@@ -258,6 +272,13 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf
    }
    $form['actions'] = $this->actions($form, $form_state);

    // Make sure the cache is removed if the parent entity or the order change.
    $parent_entity = $this->entityTypeManager->getStorage('commerce_checkout_flow')->load($this->entityId);
    CacheableMetadata::createFromRenderArray($form)
      ->addCacheableDependency($parent_entity)
      ->addCacheableDependency($this->order)
      ->applyTo($form);

    return $form;
  }

+55 −2
Changes for modules/checkout/tests/src/Functional/CheckoutOrderTest.php: 55 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -75,10 +75,63 @@ class CheckoutOrderTest extends CommerceBrowserTestBase {
    ]);
  }

  /**
   * Tests checkout flow cache metadata.
   */
  public function testCacheMetadata() {
    $this->drupalLogout();
    $this->drupalGet($this->product->toUrl()->toString());
    $this->submitForm([], 'Add to cart');
    $this->assertSession()->pageTextContains('1 item');
    $cart_link = $this->getSession()->getPage()->findLink('your cart');
    $cart_link->click();
    $this->submitForm([], 'Checkout');
    $this->assertSession()->pageTextNotContains('Order Summary');
    $this->assertCheckoutProgressStep('Login');

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $this->container->get('entity_type.manager')->getStorage('commerce_order')->load(1);
    /** @var \Drupal\commerce_checkout\Entity\CheckoutFlowInterface $checkout_flow */
    $checkout_flow = $this->container->get('entity_type.manager')->getStorage('commerce_checkout_flow')->load('default');

    // We're on a form, so no Page Cache.
    $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', NULL);
    // Dynamic page cache should be present, and a MISS.
    $this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');

    // Assert cache tags bubbled.
    $cache_tags_header = $this->getSession()->getResponseHeader('X-Drupal-Cache-Tags');
    $this->assertTrue(strpos($cache_tags_header, 'commerce_order:' . $order->id()) !== FALSE);
    foreach ($order->getItems() as $item) {
      $this->assertTrue(strpos($cache_tags_header, 'commerce_order_item:' . $item->id()) !== FALSE);
    }
    foreach ($checkout_flow->getCacheTags() as $cache_tag) {
      $this->assertTrue(strpos($cache_tags_header, $cache_tag) !== FALSE);
    }

    $this->getSession()->reload();
    $this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');

    // Saving the order should bust the cache.
    $this->container->get('commerce_order.order_refresh')->refresh($order);
    $order->save();

    $this->getSession()->reload();
    $this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
    $this->getSession()->reload();
    $this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');

    // Saving the checkout flow configuration entity should bust the cache.
    $checkout_flow->save();

    $this->getSession()->reload();
    $this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
    $this->getSession()->reload();
    $this->assertSession()->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'HIT');
  }

  /**
   * Tests than an order can go through checkout steps.
   *
   * @group debug
   */
  public function testGuestOrderCheckout() {
    $this->drupalLogout();