You are here

public function CheckoutPaneTest::testNoRequiredShippingProfile in Commerce Shipping 8.2

Tests checkout when the shipping profile is not required for showing rates.

File

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

Class

CheckoutPaneTest
Tests the "Shipping information" checkout pane.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

public function testNoRequiredShippingProfile() {
  $checkout_flow = CheckoutFlow::load('shipping');
  $checkout_flow_configuration = $checkout_flow
    ->get('configuration');
  $checkout_flow_configuration['panes']['shipping_information']['require_shipping_profile'] = FALSE;

  // Confirm that enabling the auto recalculation doesn't have any effect
  // when a shipping profile is not required.
  $checkout_flow_configuration['panes']['shipping_information']['auto_recalculate'] = TRUE;
  $checkout_flow
    ->set('configuration', $checkout_flow_configuration);
  $checkout_flow
    ->save();
  $this
    ->drupalGet($this->firstProduct
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet($this->secondProduct
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet('checkout/1');

  // Confirm that the shipping methods are shown without a profile.
  $this
    ->assertSession()
    ->pageTextContains('Shipping method');
  $page = $this
    ->getSession()
    ->getPage();
  $first_radio_button = $page
    ->findField('Standard shipping: $9.99');

  // The $19.99 is displayed crossed out, but Mink strips HTML.
  $second_radio_button = $page
    ->findField('Overnight shipping: $19.99 $16.99');
  $this
    ->assertNotNull($first_radio_button);
  $this
    ->assertNotNull($second_radio_button);
  $this
    ->assertNotEmpty($first_radio_button
    ->getAttribute('checked'));

  // Confirm that the description for overnight shipping is shown.
  $this
    ->assertSession()
    ->pageTextContains('At your door tomorrow morning');
  $selector = '//div[@data-drupal-selector="edit-order-summary"]';
  $this
    ->assertSession()
    ->elementTextContains('xpath', $selector, 'Shipping $9.99');

  // Complete the order information step.
  $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]';

  // Confirm that the country list has been restricted.
  $this
    ->assertOptions($address_prefix . '[country_code]', [
    'US',
    'FR',
    'DE',
  ]);
  $page
    ->fillField($address_prefix . '[country_code]', 'US');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  foreach ($address as $property => $value) {
    $page
      ->fillField($address_prefix . '[' . $property . ']', $value);
  }
  $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]' => '2024',
    'payment_information[add_payment_method][payment_details][security_code]' => '123',
  ], 'Continue to review');

  // Confirm that the review is rendered correctly.
  $this
    ->assertSession()
    ->pageTextContains('Shipping information');
  foreach ($address as $property => $value) {
    $this
      ->assertSession()
      ->pageTextContains($value);
  }
  $this
    ->assertSession()
    ->pageTextContains('Standard shipping');
  $this
    ->submitForm([], 'Pay and complete purchase');
  $order = Order::load(1);

  // Confirm the integrity of the shipment.
  $shipments = $order
    ->get('shipments')
    ->referencedEntities();
  $this
    ->assertCount(1, $shipments);

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
  $shipment = reset($shipments);
  $this
    ->assertEquals('custom_box', $shipment
    ->getPackageType()
    ->getId());
  $this
    ->assertEquals('Mountain View', $shipment
    ->getShippingProfile()
    ->get('address')->locality);
  $this
    ->assertEquals('Standard shipping', $shipment
    ->getShippingMethod()
    ->label());
  $this
    ->assertEquals('default', $shipment
    ->getShippingService());
  $this
    ->assertEquals('9.99', $shipment
    ->getAmount()
    ->getNumber());
  $this
    ->assertCount(2, $shipment
    ->getItems());
  $this
    ->assertEquals('draft', $shipment
    ->getState()->value);

  // Confirm that the order total contains the shipment amounts.
  $this
    ->assertEquals(new Price('26.97', 'USD'), $order
    ->getTotalPrice());
}