You are here

public function CartIntegrationTest::testRecalculateRatesCart in Commerce Shipping 8.2

Test for recalculating shipping trough cart/checkout steps.

File

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

Class

CartIntegrationTest
Tests integration with the Cart module.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

public function testRecalculateRatesCart() {

  // Create a flat rate.
  $this
    ->createEntity('commerce_shipping_method', [
    'name' => 'Flat Rate',
    'stores' => [
      $this->store
        ->id(),
    ],
    'plugin' => [
      'target_plugin_id' => 'flat_rate',
      'target_plugin_configuration' => [
        'rate_label' => 'Free Shipping',
        'rate_amount' => [
          'number' => '0.00',
          'currency_code' => 'USD',
        ],
      ],
    ],
    'conditions' => [
      [
        'target_plugin_id' => 'shipment_weight',
        'target_plugin_configuration' => [
          'operator' => '>',
          'weight' => [
            'number' => '120',
            'unit' => 'g',
          ],
        ],
      ],
    ],
  ]);

  // 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
    ->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',
  ], '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]', 10);
  $this
    ->getSession()
    ->getPage()
    ->findButton('Update cart')
    ->click();
  $this
    ->assertSession()
    ->pageTextContains('Shipping $0.00');
  $this
    ->drupalGet('checkout/1');
  $this
    ->assertSession()
    ->pageTextContains('Shipping $0.00');
  $this
    ->getSession()
    ->getPage()
    ->findButton('Recalculate shipping')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('Shipping method');
  $this
    ->assertSession()
    ->pageTextContains('Shipping $0.00');
  $this
    ->getSession()
    ->getPage()
    ->findButton('Continue to review')
    ->click();
  $this
    ->assertSession()
    ->pageTextContains('Shipping $0.00');

  // Test whether the shipping amount is cleared if there is no valid methods.
  $this
    ->drupalGet('/cart');
  $this
    ->getSession()
    ->getPage()
    ->fillField('edit_quantity[0]', 6);
  $this
    ->getSession()
    ->getPage()
    ->findButton('Update cart')
    ->click();
  $this
    ->assertSession()
    ->pageTextNotContains('Shipping $0.00');
  $this
    ->drupalGet('checkout/1');
  $this
    ->assertSession()
    ->pageTextNotContains('Shipping $0.00');

  // There is no valid methods.
  $this
    ->getSession()
    ->getPage()
    ->findButton('Recalculate shipping')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextNotContains('Shipping method');

  // Test adding a new product to the cart to see if the rates are
  // recalculated.
  $variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => 'default',
    'sku' => strtolower($this
      ->randomMachineName()),
    'price' => [
      'number' => '7.99',
      'currency_code' => 'USD',
    ],
    'weight' => [
      'number' => '20',
      'unit' => 'g',
    ],
  ]);

  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $another_product = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => $this
      ->randomString(),
    'variations' => [
      $variation,
    ],
    'stores' => [
      $this->store,
    ],
  ]);
  $this
    ->drupalGet($another_product
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet('/cart');
  $this
    ->assertSession()
    ->pageTextContains('Shipping $0.00');
}