View source
<?php
namespace Drupal\Tests\commerce_xquantity\Functional;
use Drupal\Tests\commerce_cart\Functional\CartBrowserTestBase;
class XquantityTest extends CartBrowserTestBase {
public static $modules = [
'commerce_xquantity',
];
protected function getAdministratorPermissions() {
return array_merge([
'administer commerce_order_item form display',
], parent::getAdministratorPermissions());
}
public function testAddToCartUpdateCartQuantity() {
$product_url = $this->variation
->getProduct()
->toUrl();
$this
->drupalGet('admin/commerce/config/order-item-types/default/edit/form-display/add_to_cart');
$assert = $this
->assertSession();
$assert
->pageTextNotContains('step: 0.01');
$assert
->pageTextNotContains('default_value: 1');
$assert
->pageTextNotContains('min: 0');
$assert
->pageTextNotContains('max: 9999999999.9999');
$assert
->pageTextNotContains('base_default_value: 1');
$assert
->pageTextNotContains('base_step: 0.0001');
$assert
->pageTextNotContains('floor: 0');
$assert
->pageTextNotContains('ceil: 9999999999.9999');
$this
->drupalGet('admin/commerce/config/order-item-types/default/edit/form-display');
$assert = $this
->assertSession();
$assert
->pageTextContains('step: 1');
$assert
->pageTextContains('default_value: 1');
$assert
->pageTextContains('min: 1');
$assert
->pageTextContains('max: 9999999999.9999');
$assert
->pageTextContains('base_default_value: 1');
$assert
->pageTextContains('base_step: 0.0001');
$assert
->pageTextContains('floor: 0');
$assert
->pageTextContains('ceil: 9999999999.9999');
$this
->drupalGet($product_url);
$this
->submitForm([], t('Add to cart'));
$this
->drupalGet('cart');
$this
->assertSession()
->fieldValueEquals('edit-edit-quantity-0', '1');
$widget = \Drupal::service('entity_display.repository')
->getFormDisplay('commerce_order_item', 'default', 'add_to_cart');
$widget
->setComponent('quantity', [
'type' => 'xnumber',
])
->save();
$this
->drupalGet('cart');
$this
->assertSession()
->fieldValueEquals('edit-edit-quantity-0', '1');
$this
->submitForm([
'edit_quantity[0]' => '1.02',
], t('Update cart'));
$this
->assertSession()
->pageTextContains(t('Your shopping cart has been updated.'));
$this
->assertSession()
->fieldValueEquals('edit-edit-quantity-0', '1.02');
$this
->submitForm([
'edit_quantity[0]' => '0',
], t('Update cart'));
$this
->assertSession()
->pageTextContains(t('Your shopping cart is empty.'));
$widget
->setComponent('quantity', [
'settings' => [
'default_value' => '1.0015',
'step' => '0.0005',
],
])
->save();
$this
->drupalGet($product_url);
$this
->submitForm([
'quantity[0][value]' => '',
], t('Add to cart'));
$this
->assertSession()
->pageTextContains(t('The quantity should be no less than 0.0005'));
$this
->submitForm([
'quantity[0][value]' => '0',
], t('Add to cart'));
$this
->assertSession()
->pageTextContains(t('The quantity should be no less than 0.0005'));
$widget
->setComponent('quantity', [
'settings' => [
'default_value' => '1.0015',
'step' => '0.0005',
'min' => '1.001',
],
])
->save();
$this
->drupalGet($product_url);
$this
->submitForm([], t('Add to cart'));
$this
->assertSession()
->pageTextContains(t('@label added to your cart.', [
'@label' => $this->variation
->label(),
]));
$this
->submitForm([
'quantity[0][value]' => '',
], t('Add to cart'));
$this
->assertSession()
->pageTextContains(t('The quantity should be no less than 1.001'));
$this
->submitForm([
'quantity[0][value]' => '0',
], t('Add to cart'));
$this
->assertSession()
->pageTextContains(t('The quantity should be no less than 1.001'));
$this
->drupalGet('cart');
$values = [
'edit_quantity[0]' => '0',
];
$this
->submitForm($values, t('Update cart'));
$this
->assertSession()
->pageTextContains(t('Quantity must be higher than or equal to 1.001.'));
$values = [
'edit_quantity[0]' => '',
];
$this
->submitForm($values, t('Update cart'));
$this
->assertSession()
->fieldValueEquals('edit-edit-quantity-0', '1.0015');
}
}