Skip to content

Commit

Permalink
Issue #2910738 by subhojit777: Ajaxify the confirmation message
Browse files Browse the repository at this point in the history
  • Loading branch information
subhojit777 committed Oct 10, 2017
1 parent f6bb177 commit e77d260
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 42 deletions.
47 changes: 46 additions & 1 deletion src/Form/AjaxAddToCartForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_cart\Form\AddToCartForm;
use Drupal\Component\Utility\Html;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\RemoveCommand;
use Drupal\Core\Ajax\AppendCommand;
use Drupal\Core\Ajax\UpdateBuildIdCommand;
use Drupal\block\Entity\Block;

/**
* Provides the order item ajax add to cart form.
Expand Down Expand Up @@ -55,7 +60,47 @@ protected function actions(array $form, FormStateInterface $form_state) {
* Fixes https://www.drupal.org/node/2905814
*/
public static function refreshAddToCartForm(array $form, FormStateInterface $form_state) {
return $form['actions']['submit'];
$response = new AjaxResponse();

// If the form build ID has changed, issue an Ajax command to update it.
if (isset($form['#build_id_old']) && $form['#build_id_old'] !== $form['#build_id']) {
$response->addCommand(new UpdateBuildIdCommand($form['#build_id_old'], $form['#build_id']));
}

/*
* Update status messages.
*
* @TODO Get the following approach reviewed by someone.
*/
/** @var \Drupal\block\BlockInterface $block */
$block = self::getStatusMessagesBlock();
if ($block) {
$elements = [
'#type' => 'status_messages',
];

$response->addCommand(new RemoveCommand('.messages__wrapper'));
$response->addCommand(new AppendCommand(".region-{$block->getRegion()}", \Drupal::service('renderer')->renderRoot($elements)));
}

return $response;
}

/**
* Returns the region where messages block is placed in the current theme.
*
* @return \Drupal\block\BlockInterface|null
* Returns status_messages block entity, or NULL if not present.
*/
protected static function getStatusMessagesBlock() {
/** @var \Drupal\Core\Theme\ThemeManagerInterface $theme_manager */
$theme_manager = \Drupal::service('theme.manager');
$active_theme = $theme_manager->getActiveTheme()->getName();

/** @var \Drupal\block\BlockInterface $block */
$block = Block::load("{$active_theme}_messages");

return $block;
}

}
51 changes: 51 additions & 0 deletions tests/src/Functional/AjaxAddCartTestBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Drupal\Tests\dc_ajax_add_cart\Functional;

use Drupal\Tests\commerce\FunctionalJavascript\JavascriptTestTrait;
use Drupal\Tests\commerce_cart\Functional\CartBrowserTestBase;

/**
* Base class for ajax add cart tests.
*/
abstract class AjaxAddCartTestBase extends CartBrowserTestBase {

use JavascriptTestTrait;

/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'commerce_product',
'commerce_cart',
'dc_ajax_add_cart',
'dc_ajax_add_cart_test',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

// Change commerce_product variation view display to dc_ajax_add_cart.
\Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load('commerce_product.default.default')
->setComponent('variations', [
'type' => 'dc_ajax_add_cart',
'settings' => [
'default_quantity' => '1',
'combine' => TRUE,
'show_quantity' => FALSE,
],
'weight' => 0,
'label' => 'hidden',
'region' => 'content',
])
->save();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Drupal\Tests\dc_ajax_add_cart\FunctionalJavascript;

use Drupal\commerce_order\Entity\Order;
use Drupal\Tests\dc_ajax_add_cart\Functional\AjaxAddCartTestBase;

/**
* Ajax add cart confirmation message tests.
*
* @TODO The default `testing` profile is unable to render the ajax confirmation
* message. Find out why this is happening, and move this test inside
* `AjaxAddCartTest`.
*
* @ingroup dc_ajax_add_cart
*
* @group dc_ajax_add_cart
*/
class AjaxAddCartConfirmationMessageTest extends AjaxAddCartTestBase {

/**
* Profile to be used for testing.
*
* @var string
*/
protected $profile = 'standard';

/**
* Tests whether the confirmation message appears after product added to cart.
*/
public function testConfirmationMessage() {
$this->drupalGet("product/{$this->variation->getProduct()->id()}");
$ajax_add_cart_button = $this->getSession()->getPage()->findButton('Add to cart');

$ajax_add_cart_button->click();
$this->waitForAjaxToFinish();

/*
* Confirm that the initial add to cart submit works.
*/
$this->cart = Order::load($this->cart->id());
$order_items = $this->cart->getItems();
$this->assertOrderItemInOrder($this->variation, $order_items[0]);

// Confirm that the confirmation message has appeared.
$this->assertSession()->pageTextContains("{$this->variation->getProduct()->label()} added to your cart.");
}

}
43 changes: 2 additions & 41 deletions tests/src/FunctionalJavascript/AjaxAddCartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Drupal\Tests\dc_ajax_add_cart\FunctionalJavascript;

use Drupal\Tests\commerce\FunctionalJavascript\JavascriptTestTrait;
use Drupal\Tests\commerce_cart\Functional\CartBrowserTestBase;
use Drupal\commerce_order\Entity\Order;
use Drupal\Tests\dc_ajax_add_cart\Functional\AjaxAddCartTestBase;

/**
* Ajax add cart tests.
Expand All @@ -13,45 +12,7 @@
*
* @group dc_ajax_add_cart
*/
class AjaxAddCartTest extends CartBrowserTestBase {

use JavascriptTestTrait;

/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'commerce_product',
'commerce_cart',
'dc_ajax_add_cart',
'dc_ajax_add_cart_test',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

// Change commerce_product variation view display to dc_ajax_add_cart.
\Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load('commerce_product.default.default')
->setComponent('variations', [
'type' => 'dc_ajax_add_cart',
'settings' => [
'default_quantity' => '1',
'combine' => TRUE,
'show_quantity' => FALSE,
],
'weight' => 0,
'label' => 'hidden',
'region' => 'content',
])
->save();
}
class AjaxAddCartTest extends AjaxAddCartTestBase {

/**
* Tests add to cart form.
Expand Down

0 comments on commit e77d260

Please sign in to comment.