You are here

public function CartIntegrationTest::testRecalculatePerItem in Commerce Shipping 8.2

Test for Flat Rate Per Item shipping cost updates.

File

tests/src/FunctionalJavascript/CartIntegrationTest.php, line 162

Class

CartIntegrationTest
Tests integration with the Cart module.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

public function testRecalculatePerItem() {

  // Add product to order and calculate shipping.
  $this
    ->drupalGet($this->firstProduct
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet('checkout/1');
  $address = [
    'given_name' => 'John',
    'family_name' => 'Smith',
    'address_line1' => '1098 Alta Ave',
    'locality' => 'Mountain View',
    'administrative_area' => 'CA',
    'postal_code' => '94043',
  ];
  $address_prefix = 'shipping_information[shipping_profile][address][0][address]';
  $this
    ->getSession()
    ->getPage()
    ->fillField($address_prefix . '[country_code]', 'US');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  foreach ($address as $property => $value) {
    $this
      ->getSession()
      ->getPage()
      ->fillField($address_prefix . '[' . $property . ']', $value);
  }
  $this
    ->getSession()
    ->getPage()
    ->findButton('Recalculate shipping')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->uncheckField('payment_information[add_payment_method][billing_information][copy_fields][enable]');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([
    'payment_information[add_payment_method][payment_details][number]' => '4111111111111111',
    'payment_information[add_payment_method][payment_details][expiration][month]' => '02',
    'payment_information[add_payment_method][payment_details][expiration][year]' => '2023',
    'payment_information[add_payment_method][payment_details][security_code]' => '123',
    'payment_information[add_payment_method][billing_information][address][0][address][given_name]' => 'Johnny',
    'payment_information[add_payment_method][billing_information][address][0][address][family_name]' => 'Appleseed',
    'payment_information[add_payment_method][billing_information][address][0][address][address_line1]' => '123 New York Drive',
    'payment_information[add_payment_method][billing_information][address][0][address][locality]' => 'New York City',
    'payment_information[add_payment_method][billing_information][address][0][address][administrative_area]' => 'NY',
    'payment_information[add_payment_method][billing_information][address][0][address][postal_code]' => '10001',
  ], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextContains('Shipping $10.00');

  // Test whether the shipping amount gets updated.
  $this
    ->drupalGet('/cart');
  $this
    ->getSession()
    ->getPage()
    ->fillField('edit_quantity[0]', 5);
  $this
    ->getSession()
    ->getPage()
    ->findButton('Update cart')
    ->click();
  $this
    ->assertSession()
    ->pageTextContains('Shipping $50.00');
  $this
    ->drupalGet('checkout/1');
  $this
    ->assertSession()
    ->pageTextContains('Shipping $50.00');
  $this
    ->getSession()
    ->getPage()
    ->findButton('Recalculate shipping')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('Shipping method');
  $this
    ->assertSession()
    ->pageTextContains('Shipping $50.00');
  $this
    ->getSession()
    ->getPage()
    ->findButton('Continue to review')
    ->click();
  $this
    ->assertSession()
    ->pageTextContains('Shipping $50.00');
}