You are here

public function ShipmentAdminTest::testShipmentCreate in Commerce Shipping 8.2

Tests creating a shipment for an order.

File

tests/src/FunctionalJavascript/ShipmentAdminTest.php, line 306

Class

ShipmentAdminTest
Tests the shipment admin UI.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

public function testShipmentCreate() {
  $this
    ->drupalGet($this->shipmentUri);
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->clickLink('Add shipment');
  $this
    ->assertSession()
    ->addressEquals($this->shipmentUri . '/add/default');
  $this
    ->assertTrue($page
    ->hasSelect('package_type'));
  $this
    ->assertSession()
    ->optionExists('package_type', 'Custom box');
  $this
    ->assertSession()
    ->optionExists('package_type', 'Package Type A');
  $this
    ->assertTrue($page
    ->hasButton('Recalculate shipping'));
  $this
    ->assertSession()
    ->pageTextContains('Shipment items');
  list($order_item) = $this->order
    ->getItems();
  $this
    ->assertSession()
    ->pageTextContains($order_item
    ->label());
  $this
    ->assertRenderedAddress($this->defaultAddress, 'shipping_profile[0][profile]');
  $this
    ->assertSession()
    ->pageTextContains('202-555-0108');
  $this
    ->assertSession()
    ->pageTextContains('Shipping method');
  $first_radio_button = $page
    ->findField('Standard shipping: $9.99');
  $second_radio_button = $page
    ->findField('Overnight shipping: $19.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');
  $page
    ->findButton('Recalculate shipping')
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([
    'shipment_items[1]' => TRUE,
    'title[0][value]' => 'Test shipment',
  ], 'Save');
  $this
    ->assertSession()
    ->addressEquals($this->shipmentUri);
  $this
    ->assertSession()
    ->pageTextContains(t('Shipment for order @order created.', [
    '@order' => $this->order
      ->getOrderNumber(),
  ]));
  \Drupal::entityTypeManager()
    ->getStorage('commerce_order')
    ->resetCache([
    $this->order
      ->id(),
  ]);
  $this->order = Order::load($this->order
    ->id());

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
  $shipment = Shipment::load(1);
  $this
    ->assertEquals($this->order
    ->id(), $shipment
    ->getOrderId());
  $this
    ->assertEquals('9.99', $shipment
    ->getAmount()
    ->getNumber());
  $this
    ->assertSession()
    ->pageTextContains($shipment
    ->label());
  $shipping_profile = $shipment
    ->getShippingProfile();
  $this
    ->assertEquals('customer_shipping', $shipping_profile
    ->bundle());
  $this
    ->assertEquals('202-555-0108', $shipping_profile
    ->get('field_phone')->value);
  $this
    ->assertEquals($this->defaultAddress, array_filter($shipping_profile
    ->get('address')
    ->first()
    ->toArray()));
  $this
    ->assertEquals($this->defaultProfile
    ->id(), $shipping_profile
    ->getData('address_book_profile_id'));
  $this
    ->assertSession()
    ->pageTextContains('$9.99');
  $this
    ->assertTrue($page
    ->hasLink('Finalize shipment'));
  $this
    ->assertTrue($page
    ->hasLink('Cancel shipment'));
  $adjustments = $this->order
    ->getAdjustments();
  $this
    ->assertCount(1, $adjustments);
  $this
    ->assertEquals($adjustments[0]
    ->getAmount(), $shipment
    ->getAmount());
}