AjaxAddCartViewsRemoveButtonTest.php in Commerce Ajax Add to Cart 8
File
modules/dc_ajax_add_cart_views/tests/src/FunctionalJavascript/AjaxAddCartViewsRemoveButtonTest.php
View source
<?php
namespace Drupal\Tests\dc_ajax_add_cart_views\FunctionalJavascript;
use Drupal\Tests\dc_ajax_add_cart_views\Functional\AjaxAddCartViewsTestBase;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_order\Entity\Order;
class AjaxAddCartViewsRemoveButtonTest extends AjaxAddCartViewsTestBase {
public function testRemoveButton() {
$this
->drupalLogin($this->account);
foreach ($this->variations as $variation) {
$this->cartManager
->addEntity($this->cart, $variation);
}
$cart_variation = $this
->getRandomVariation();
$variation_ids = array_map(function ($variation) {
return $variation
->id();
}, $this->variations);
$other_variation_id = array_diff($variation_ids, [
$cart_variation
->id(),
]);
$other_variation = ProductVariation::load(array_pop($other_variation_id));
$this
->drupalGet("cart-ajax/{$this->cart->id()}");
$this
->assertCartAjaxPage();
$variation_row_element = $this
->getRowCartAjaxByVariation($cart_variation);
$this
->assertVariationRowCartAjax($variation_row_element);
$variation_row_element
->findButton('Remove')
->click();
$this
->waitForAjaxToFinish();
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$variation_row_element = $this
->getRowCartAjaxByVariation($cart_variation);
$this
->assertNotVariationRowCartAjax($variation_row_element);
$this
->assertNotVariationInOrder($cart_variation, $order_items);
$variation_row_element = $this
->getRowCartAjaxByVariation($other_variation);
$this
->assertVariationRowCartAjax($variation_row_element);
$this
->assertVariationInOrder($other_variation, $order_items);
$element = $variation_row_element
->find('css', "td.views-field-edit-quantity input[value='1']");
$this
->assertNotNull($element, t('@product with incorrect quantity found on ajax cart.', [
'@product' => $other_variation
->getProduct()
->getTitle(),
]));
}
public function testAjaxRemoveButton() {
$this
->drupalLogin($this->account);
foreach ($this->variations as $variation) {
$this->cartManager
->addEntity($this->cart, $variation);
}
$cart_variation = $this
->getRandomVariation();
$this
->drupalGet("cart-ajax/{$this->cart->id()}");
$this
->assertCartAjaxPage();
$variation_row_element = $this
->getRowCartAjaxByVariation($cart_variation);
$this
->assertVariationRowCartAjax($variation_row_element);
$variation_row_element
->findButton('Remove')
->click();
$this
->assertVariationRowCartAjax($variation_row_element);
$this->cart = Order::load($this->cart
->id());
$order_items = $this->cart
->getItems();
$this
->assertVariationInOrder($cart_variation, $order_items);
}
public function testOrderTotal() {
$this
->drupalLogin($this->account);
foreach ($this->variations as $variation) {
$this->cartManager
->addEntity($this->cart, $variation);
}
$cart_variation = $this
->getRandomVariation();
$this
->drupalGet("cart-ajax/{$this->cart->id()}");
$this
->assertCartAjaxPage();
$variation_row_element = $this
->getRowCartAjaxByVariation($cart_variation);
$this
->assertVariationRowCartAjax($variation_row_element);
$variation_row_element
->findButton('Remove')
->click();
$this
->waitForAjaxToFinish();
$this->cart = Order::load($this->cart
->id());
$price = $this
->getSession()
->getPage()
->find('css', '.order-total-line__total .order-total-line-value')
->getText();
$price = (double) preg_replace('/[^0-9\\.]/', '', $price);
$actual_price = (double) $this->cart
->getTotalPrice()
->getNumber();
$this
->assertEquals($price, $actual_price, 'Prices are not equal.');
}
}