You are here

public function CheckoutPaneTest::testOrderSummaryRefresh in Commerce Shipping 8.2

Tests that the order summary is properly refreshed.

File

tests/src/FunctionalJavascript/CheckoutPaneTest.php, line 912

Class

CheckoutPaneTest
Tests the "Shipping information" checkout pane.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

public function testOrderSummaryRefresh() {

  // Create a tax type that applies to French addresses, to ensure the tax
  // adjustment is shown in the order summary when selecting the FR address.

  /** @var \Drupal\commerce_tax\Entity\TaxTypeInterface $tax_type */
  $this->store
    ->set('tax_registrations', [
    'FR',
  ])
    ->save();
  $this
    ->createEntity('commerce_tax_type', [
    'id' => 'fr_vat',
    'label' => 'French VAT',
    'plugin' => 'custom',
    'configuration' => [
      'display_inclusive' => TRUE,
      'display_label' => 'vat',
      'round' => TRUE,
      'rates' => [
        [
          'id' => 'standard',
          'label' => 'Standard',
          'percentage' => '0.2',
        ],
      ],
      'territories' => [
        [
          'country_code' => 'FR',
        ],
      ],
    ],
    'status' => TRUE,
  ]);
  $address_fr = [
    'given_name' => 'John',
    'family_name' => 'Smith',
    'address_line1' => '38 rue du sentier',
    'locality' => 'Paris',
    'postal_code' => '75002',
    'country_code' => 'FR',
  ];
  $default_profile = $this
    ->createEntity('profile', [
    'type' => 'customer',
    'uid' => $this->adminUser
      ->id(),
    'address' => $this->defaultAddress,
    'is_default' => TRUE,
  ]);
  $another_profile = $this
    ->createEntity('profile', [
    'type' => 'customer',
    'uid' => $this->adminUser
      ->id(),
    'address' => $address_fr,
  ]);
  $this
    ->drupalGet($this->firstProduct
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet('checkout/1');
  $this
    ->assertRenderedAddress($this->defaultAddress, 'shipping_information[shipping_profile]');
  $this
    ->assertSession()
    ->pageTextContains('Overnight shipping: $19.99 $16.99');
  $this
    ->assertSession()
    ->pageTextContains('Standard shipping');
  $selector = '//div[@data-drupal-selector="edit-order-summary"]';
  $this
    ->assertSession()
    ->elementTextContains('xpath', $selector, 'Shipping $9.99');
  $second_radio_button = $this
    ->getSession()
    ->getPage()
    ->findField('Overnight shipping: $19.99 $16.99');
  $second_radio_button
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->elementTextContains('xpath', $selector, 'Shipping $16.99');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('shipping_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->fillField('shipping_information[shipping_profile][address][0][address][address_line1]', '9 Drupal Avenue');
  $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]' => '2026',
    'payment_information[add_payment_method][payment_details][security_code]' => '123',
  ], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextNotContains('The content has either been modified by another user');
  foreach ($this->defaultAddress as $property => $value) {
    if ($property != 'country_code') {
      $this
        ->assertSession()
        ->pageTextContains($value);
    }
  }
  $this
    ->assertSession()
    ->pageTextContains('Overnight shipping');
  $this
    ->assertSession()
    ->pageTextNotContains('Standard shipping');
  $conditions = [
    'target_plugin_id' => 'shipment_address',
    'target_plugin_configuration' => [
      'zone' => [
        'territories' => [
          [
            'country_code' => 'US',
          ],
        ],
      ],
    ],
  ];
  $this->firstShippingMethod
    ->set('conditions', $conditions)
    ->save();
  $conditions['target_plugin_configuration']['zone']['territories'][0]['country_code'] = 'FR';
  $this->secondShippingMethod
    ->set('conditions', $conditions)
    ->save();
  $this
    ->assertSession()
    ->pageTextContains('Shipping information');

  // Go back to the order info page, attempt switching between different
  // addresses.
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Go back');
  $this
    ->getSession()
    ->getPage()
    ->fillField('shipping_information[shipping_profile][select_address]', 2);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRenderedAddress($address_fr, 'shipping_information[shipping_profile]');
  $this
    ->assertSession()
    ->elementTextContains('xpath', $selector, 'Shipping $9.99');

  // Ensure the tax is properly displayed in the summary when switching to the
  // FR address and assert that it's removed after switching back to the US
  // address.
  $this
    ->assertSession()
    ->elementTextContains('xpath', $selector, 'VAT $1.60');
  $this
    ->getSession()
    ->getPage()
    ->fillField('shipping_information[shipping_profile][select_address]', 1);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->elementTextNotContains('xpath', $selector, 'VAT $1.60');
  $this
    ->getSession()
    ->getPage()
    ->fillField('shipping_information[shipping_profile][select_address]', 2);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->elementTextContains('xpath', $selector, 'VAT $1.60');
  $this
    ->submitForm([], 'Continue to review');
  $this
    ->assertSession()
    ->pageTextContains('Standard shipping');
  $this
    ->assertSession()
    ->pageTextNotContains('Overnight shipping');
  $this
    ->assertSession()
    ->elementTextContains('xpath', $selector, 'VAT $1.60');
}