diff --git a/modules/checkout/src/CheckoutOrderManager.php b/modules/checkout/src/CheckoutOrderManager.php index 766d1f942a2119b76409f4f1775d9c65fe91e68e..5217b40c75643faa014a78a6301545246ad623fe 100644 --- a/modules/checkout/src/CheckoutOrderManager.php +++ b/modules/checkout/src/CheckoutOrderManager.php @@ -28,6 +28,9 @@ class CheckoutOrderManager implements CheckoutOrderManagerInterface { $order->save(); } + // Set the order on the checkout flow's checkout flow plugin. + $order->get('checkout_flow')->entity->getPlugin()->setOrder($order); + return $order->get('checkout_flow')->entity; } diff --git a/modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php b/modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php index 96bca7e7d008cf5a6308e74ce20d1c7126c81f6a..059d1febe5dd016859ca615e34ac741a9fb3cda4 100644 --- a/modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php +++ b/modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowBase.php @@ -15,6 +15,7 @@ use Drupal\Core\Url; use Drupal\commerce\AjaxFormTrait; use Drupal\commerce\Response\NeedsRedirectException; use Drupal\commerce_checkout\Event\CheckoutEvents; +use Drupal\commerce_order\Entity\OrderInterface; use Drupal\commerce_order\Event\OrderEvent; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -160,6 +161,14 @@ abstract class CheckoutFlowBase extends PluginBase implements CheckoutFlowInterf } } + /** + * {@inheritdoc} + */ + public function setOrder(OrderInterface $order): static { + $this->order = $order; + return $this; + } + /** * {@inheritdoc} */ diff --git a/modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowInterface.php b/modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowInterface.php index 3c1b0416e65c82817014ce3f54609bbcae32332b..33b85870a149f283e2d432c760c16517e043e9da 100644 --- a/modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowInterface.php +++ b/modules/checkout/src/Plugin/Commerce/CheckoutFlow/CheckoutFlowInterface.php @@ -9,6 +9,7 @@ use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Form\BaseFormIdInterface; use Drupal\Core\Form\FormInterface; use Drupal\Core\Plugin\PluginFormInterface; +use Drupal\commerce_order\Entity\OrderInterface; /** * Places an order through a series of steps. @@ -19,6 +20,16 @@ use Drupal\Core\Plugin\PluginFormInterface; */ interface CheckoutFlowInterface extends FormInterface, BaseFormIdInterface, ConfigurableInterface, DependentPluginInterface, PluginFormInterface, PluginInspectionInterface, DerivativeInspectionInterface { + /** + * Sets the current order. + * + * @param \Drupal\commerce_order\Entity\OrderInterface $order + * The order. + * + * @return $this + */ + public function setOrder(OrderInterface $order): static; + /** * Gets the current order. * diff --git a/modules/checkout/tests/src/Kernel/CheckoutOrderManagerTest.php b/modules/checkout/tests/src/Kernel/CheckoutOrderManagerTest.php index 2dd2ed11cfaa1c6170509179a9103e614f795eba..44167a77ee949c8c9fd8c4c39e8d75baca3746b4 100644 --- a/modules/checkout/tests/src/Kernel/CheckoutOrderManagerTest.php +++ b/modules/checkout/tests/src/Kernel/CheckoutOrderManagerTest.php @@ -51,21 +51,25 @@ class CheckoutOrderManagerTest extends OrderKernelTestBase { $this->installConfig('commerce_checkout'); $user = $this->createUser(); - $order = Order::create([ + $this->order = Order::create([ 'type' => 'default', 'mail' => $user->getEmail(), 'uid' => $user->id(), 'store_id' => $this->store->id(), ]); - $order->save(); - $this->order = $order; + $this->order->save(); $this->checkoutOrderManager = $this->container->get('commerce_checkout.checkout_order_manager'); + } - // Fake a request so that the current_route_match works. - // @todo Remove this when CheckoutFlowBase stops using the route match. + /** + * Fakes a request so that the current_route_match works. + * + * @todo Remove this when CheckoutFlowBase stops using the route match. + */ + protected function setupRequestWithOrderParameter() { $url = Url::fromRoute('commerce_checkout.form', [ - 'commerce_order' => $order->id(), + 'commerce_order' => $this->order->id(), ]); $route_provider = $this->container->get('router.route_provider'); $route = $route_provider->getRouteByName($url->getRouteName()); @@ -73,7 +77,7 @@ class CheckoutOrderManagerTest extends OrderKernelTestBase { $request->setSession(new Session(new MockArraySessionStorage())); $request->attributes->add([ RouteObjectInterface::ROUTE_OBJECT => $route, - 'commerce_order' => $order, + 'commerce_order' => $this->order, ]); $this->container->get('request_stack')->push($request); } @@ -82,6 +86,8 @@ class CheckoutOrderManagerTest extends OrderKernelTestBase { * Tests getting the order's checkout flow. */ public function testGetCheckoutFlow() { + $this->setupRequestWithOrderParameter(); + $checkout_flow = $this->checkoutOrderManager->getCheckoutFlow($this->order); $this->assertInstanceOf(CheckoutFlow::class, $checkout_flow); $this->assertEquals('default', $checkout_flow->id()); @@ -96,6 +102,8 @@ class CheckoutOrderManagerTest extends OrderKernelTestBase { * Tests getting the order's checkout step ID. */ public function testGetCheckoutStepId() { + $this->setupRequestWithOrderParameter(); + // Empty requested step ID when no checkout step was set. $step_id = $this->checkoutOrderManager->getCheckoutStepId($this->order); $this->assertEquals('login', $step_id); @@ -137,4 +145,40 @@ class CheckoutOrderManagerTest extends OrderKernelTestBase { } + /** + * Tests getting checkout's visible steps. + */ + public function testGetVisibleSteps() { + /** @var \Drupal\commerce_checkout\Entity\CheckoutFlowInterface $checkout_flow */ + $checkout_flow = $this->checkoutOrderManager->getCheckoutFlow($this->order); + + /** @var \Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface $checkout_flow_plugin */ + $checkout_flow_plugin = $checkout_flow->getPlugin(); + + /** @var array $steps */ + $steps = $checkout_flow_plugin->getVisibleSteps(); + + $expected_steps = [ + 'login', + 'order_information', + 'review', + 'complete', + ]; + $this->assertEquals($expected_steps, array_keys($steps)); + } + + /** + * Tests getting the order from the checkout flow plugin. + */ + public function testGetOrderFromCheckoutPane() { + /** @var \Drupal\commerce_checkout\Entity\CheckoutFlowInterface $checkout_flow */ + $checkout_flow = $this->checkoutOrderManager->getCheckoutFlow($this->order); + + /** @var \Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface $checkout_flow_plugin */ + $checkout_flow_plugin = $checkout_flow->getPlugin(); + + // Assert that the checkout flow plugin contains the order. + $this->assertSame($this->order, $checkout_flow_plugin->getOrder()); + } + }