You are here

public function CheckoutPaneTest::testSingleShipment in Commerce Shipping 8.2

Tests checkout with a single shipment.

File

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

Class

CheckoutPaneTest
Tests the "Shipping information" checkout pane.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

public function testSingleShipment() {

  // Create a default profile to confirm that it is used at checkout.
  $default_profile = $this
    ->createEntity('profile', [
    'type' => 'customer',
    'uid' => $this->adminUser
      ->id(),
    'address' => $this->defaultAddress,
  ]);
  $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');
  $this
    ->assertSession()
    ->pageTextContains('Shipping information');
  $this
    ->assertRenderedAddress($this->defaultAddress, 'shipping_information[shipping_profile]');
  $this
    ->assertRenderedAddress($this->defaultAddress, 'payment_information[add_payment_method][billing_information]');

  // Confirm that shipping method selection is available, because the
  // selected profile has a complete address.
  $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');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $selector = '//div[@data-drupal-selector="edit-order-summary"]';
  $this
    ->assertSession()
    ->elementTextContains('xpath', $selector, 'Shipping $9.99');
  $second_radio_button
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->elementTextContains('xpath', $selector, 'Shipping $16.99');
  $first_radio_button
    ->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]' => '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 ($this->defaultAddress as $property => $value) {
    if ($property != 'country_code') {
      $this
        ->assertSession()
        ->pageTextContains($value);
    }
  }
  $this
    ->assertSession()
    ->pageTextContains('Standard shipping');
  $this
    ->assertSession()
    ->pageTextNotContains('Secret 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('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 amount.
  $this
    ->assertEquals(new Price('26.97', 'USD'), $order
    ->getTotalPrice());

  // Confirm the integrity of the profiles.
  $billing_profile = $order
    ->getBillingProfile();
  $shipping_profile = $shipment
    ->getShippingProfile();
  $this
    ->assertNotEquals($billing_profile
    ->id(), $shipping_profile
    ->id());
  $this
    ->assertEquals($this->defaultAddress['address_line1'], $billing_profile
    ->get('address')->address_line1);
  $this
    ->assertEquals($this->defaultAddress['address_line1'], $shipping_profile
    ->get('address')->address_line1);
  $this
    ->assertEquals($default_profile
    ->id(), $billing_profile
    ->getData('address_book_profile_id'));
  $this
    ->assertEquals($default_profile
    ->id(), $shipping_profile
    ->getData('address_book_profile_id'));
  $this
    ->assertEmpty($billing_profile
    ->getData('copy_to_address_book'));
  $this
    ->assertEmpty($shipping_profile
    ->getData('copy_to_address_book'));
}