You are here

public function CheckoutPaneTest::testCustomShippingProfileType in Commerce Shipping 8.2

Test checkout with a custom shipping profile type.

File

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

Class

CheckoutPaneTest
Tests the "Shipping information" checkout pane.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

public function testCustomShippingProfileType() {
  $shipment_type = ShipmentType::load('default');
  $shipment_type
    ->setProfileTypeId('customer_shipping');
  $shipment_type
    ->save();
  $this
    ->drupalGet($this->firstProduct
    ->toUrl()
    ->toString());
  $this
    ->submitForm([], 'Add to cart');
  $this
    ->drupalGet('checkout/1');
  $this
    ->getSession()
    ->getPage()
    ->uncheckField('payment_information[add_payment_method][billing_information][copy_fields][enable]');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('Shipping information');

  // Confirm that the phone field is present, but only for shipping.
  $this
    ->assertSession()
    ->fieldExists('shipping_information[shipping_profile][field_phone][0][value]');
  $this
    ->assertSession()
    ->fieldNotExists('payment_information[add_payment_method][billing_information][field_phone][0][value]');
  $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 = $this
    ->getSession()
    ->getPage();
  $page
    ->fillField($address_prefix . '[country_code]', 'US');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  foreach ($address as $property => $value) {
    $page
      ->fillField($address_prefix . '[' . $property . ']', $value);
  }
  $page
    ->fillField('shipping_information[shipping_profile][field_phone][0][value]', '202-555-0108');

  // Confirm that the shipping method selection was not initially available
  // because there was no address known.
  $this
    ->assertSession()
    ->pageTextNotContains('Shipping method');
  $page
    ->findButton('Recalculate shipping')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('Shipping method');
  $first_radio_button = $page
    ->findField('Standard shipping: $9.99');
  $this
    ->assertNotNull($first_radio_button);
  $this
    ->assertNotEmpty($first_radio_button
    ->getAttribute('checked'));
  $selector = '//div[@data-drupal-selector="edit-order-summary"]';
  $this
    ->assertSession()
    ->elementTextContains('xpath', $selector, 'Shipping $9.99');
  $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',
    'payment_information[add_payment_method][billing_information][address][0][address][given_name]' => 'Johnny',
    'payment_information[add_payment_method][billing_information][address][0][address][family_name]' => 'Appleseed',
    'payment_information[add_payment_method][billing_information][address][0][address][address_line1]' => '123 New York Drive',
    'payment_information[add_payment_method][billing_information][address][0][address][locality]' => 'New York City',
    'payment_information[add_payment_method][billing_information][address][0][address][administrative_area]' => 'NY',
    'payment_information[add_payment_method][billing_information][address][0][address][postal_code]' => '10001',
  ], '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('202-555-0108');
  $this
    ->assertSession()
    ->pageTextContains('Standard shipping');
  $this
    ->submitForm([], 'Pay and complete purchase');
  $order = Order::load(1);
  $billing_profile = $order
    ->getBillingProfile();
  $this
    ->assertNotEmpty($billing_profile);
  $this
    ->assertEquals('customer', $billing_profile
    ->bundle());
  $this
    ->assertEquals('123 New York Drive', $billing_profile
    ->get('address')->address_line1);

  // Confirm that the billing profile was copied to the address book.
  $address_book_profile_id = $billing_profile
    ->getData('address_book_profile_id');
  $this
    ->assertNotEmpty($address_book_profile_id);
  $address_book_profile = Profile::load($address_book_profile_id);
  $this
    ->assertNotEmpty($address_book_profile);
  $this
    ->assertTrue($address_book_profile
    ->isDefault());
  $this
    ->assertEquals('123 New York Drive', $address_book_profile
    ->get('address')->address_line1);

  // 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);
  $shipping_profile = $shipment
    ->getShippingProfile();
  $this
    ->assertNotEmpty($shipping_profile);
  $this
    ->assertEquals('customer_shipping', $shipping_profile
    ->bundle());
  $this
    ->assertEquals('1098 Alta Ave', $shipping_profile
    ->get('address')->address_line1);
  $this
    ->assertEquals('202-555-0108', $shipping_profile
    ->get('field_phone')->value);

  // Confirm that the shipping profile was copied to the address book.
  $address_book_profile_id = $shipping_profile
    ->getData('address_book_profile_id');
  $this
    ->assertNotEmpty($address_book_profile_id);
  $address_book_profile = Profile::load($address_book_profile_id);
  $this
    ->assertNotEmpty($address_book_profile);
  $this
    ->assertTrue($address_book_profile
    ->isDefault());
  $this
    ->assertEquals('1098 Alta Ave', $address_book_profile
    ->get('address')->address_line1);
  $this
    ->assertEquals('202-555-0108', $address_book_profile
    ->get('field_phone')->value);
}