You are here

public function CheckoutPaneTest::testShippingMethodOptionChanges in Commerce Shipping 8.2

Tests the change of shipping rate options on checkout.

@dataProvider shippingMethodOptionChangeProvider

File

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

Class

CheckoutPaneTest
Tests the "Shipping information" checkout pane.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

public function testShippingMethodOptionChanges($auto_recalculate) {
  $checkout_flow = CheckoutFlow::load('shipping');
  $checkout_flow_configuration = $checkout_flow
    ->get('configuration');
  $checkout_flow_configuration['panes']['shipping_information']['auto_recalculate'] = $auto_recalculate;
  $checkout_flow
    ->set('configuration', $checkout_flow_configuration);
  $checkout_flow
    ->save();
  $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,
  ]);
  $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
    ->drupalGet($this->firstProduct
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet('checkout/1');
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->assertRenderedAddress($this->defaultAddress, 'shipping_information[shipping_profile]');
  $this
    ->assertSession()
    ->pageTextContains('Overnight shipping: $19.99 $16.99');
  $this
    ->assertSession()
    ->pageTextNotContains('Standard shipping');
  $this
    ->getSession()
    ->getPage()
    ->fillField('shipping_information[shipping_profile][select_address]', 2);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRenderedAddress($address_fr, 'shipping_information[shipping_profile]');
  if (!$auto_recalculate) {
    $this
      ->assertSession()
      ->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
    $page
      ->findButton('Recalculate shipping')
      ->click();
    $this
      ->assertSession()
      ->assertWaitOnAjaxRequest();
  }
  else {
    $this
      ->assertSession()
      ->waitForText('Standard shipping');
  }
  $this
    ->getSession()
    ->getPage()
    ->fillField('shipping_information[shipping_profile][select_address]', '_new');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $address_de = [
    'given_name' => 'John',
    'family_name' => 'Smith',
    'address_line1' => 'Hauptstrasse 38',
    'locality' => 'Berlin',
    'postal_code' => '75002',
  ];
  $address_prefix = 'shipping_information[shipping_profile][address][0][address]';
  $page
    ->fillField($address_prefix . '[country_code]', 'DE');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  foreach ($address_de as $property => $value) {
    $page
      ->fillField($address_prefix . '[' . $property . ']', $value);
  }
  if (!$auto_recalculate) {
    $page
      ->findButton('Recalculate shipping')
      ->click();
    $this
      ->assertSession()
      ->assertWaitOnAjaxRequest();
  }
  else {
    $this
      ->assertSession()
      ->waitForText('There are no shipping rates available for this address.');
  }
  $this
    ->assertSession()
    ->pageTextContains('There are no shipping rates available for this address.');
  $this
    ->assertSession()
    ->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
  $address_us = [
    'given_name' => 'John',
    'family_name' => 'Smith',
    'address_line1' => 'Main street 38',
    'locality' => 'Los Angeles',
    'administrative_area' => 'CA',
    'postal_code' => '90014',
  ];
  $address_prefix = 'shipping_information[shipping_profile][address][0][address]';
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->fillField($address_prefix . '[country_code]', 'US');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  foreach ($address_us as $property => $value) {
    $page
      ->fillField($address_prefix . '[' . $property . ']', $value);
  }
  if (!$auto_recalculate) {
    $page
      ->findButton('Recalculate shipping')
      ->click();
    $this
      ->assertSession()
      ->assertWaitOnAjaxRequest();
  }
  else {
    $this
      ->assertSession()
      ->waitForText('Overnight shipping');
  }
  $this
    ->assertSession()
    ->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
  $first_radio_button = $page
    ->findField('Standard shipping: $9.99');
  $second_radio_button = $page
    ->findField('Overnight shipping: $19.99 $16.99');
  $this
    ->assertNull($first_radio_button);
  $this
    ->assertNotNull($second_radio_button);
  $this
    ->assertNotEmpty($second_radio_button
    ->getAttribute('checked'));
  $selector = '//div[@data-drupal-selector="edit-order-summary"]';
  $this
    ->assertSession()
    ->elementTextContains('xpath', $selector, 'Shipping $16.99');

  // commerce_field_widget_form_alter() removes the description. Probably a
  // bug.
  $this
    ->assertSession()
    ->pageTextNotContains('At your door tomorrow morning');
  $address_prefix = 'shipping_information[shipping_profile][address][0][address]';
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->fillField($address_prefix . '[country_code]', 'FR');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  unset($address_fr['country_code']);
  foreach ($address_fr as $property => $value) {
    $page
      ->fillField($address_prefix . '[' . $property . ']', $value);
  }
  if (!$auto_recalculate) {
    $page
      ->findButton('Recalculate shipping')
      ->click();
    $this
      ->assertSession()
      ->assertWaitOnAjaxRequest();
  }
  else {
    $this
      ->assertSession()
      ->waitForText('Standard shipping');
  }
  $this
    ->assertSession()
    ->pageTextNotContains('An illegal choice has been detected. Please contact the site administrator.');
  $first_radio_button = $page
    ->findField('Standard shipping: $9.99');
  $second_radio_button = $page
    ->findField('Overnight shipping: $19.99 $16.99');
  $this
    ->assertNotNull($first_radio_button);
  $this
    ->assertNull($second_radio_button);
  $this
    ->assertNotEmpty($first_radio_button
    ->getAttribute('checked'));
  $this
    ->assertSession()
    ->pageTextNotContains('At your door tomorrow morning');
}