You are here

public function OrderAdminTest::testCreateOrder in Commerce Core 8.2

Same name in this branch
  1. 8.2 modules/log/tests/src/Functional/OrderAdminTest.php \Drupal\Tests\commerce_log\Functional\OrderAdminTest::testCreateOrder()
  2. 8.2 modules/order/tests/src/FunctionalJavascript/OrderAdminTest.php \Drupal\Tests\commerce_order\FunctionalJavascript\OrderAdminTest::testCreateOrder()

Tests creating an order.

File

modules/order/tests/src/FunctionalJavascript/OrderAdminTest.php, line 83

Class

OrderAdminTest
Tests the order admin UI.

Namespace

Drupal\Tests\commerce_order\FunctionalJavascript

Code

public function testCreateOrder() {

  // Create an order through the add form.
  $this
    ->drupalGet('/admin/commerce/orders');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Create a new order');
  $user = $this->loggedInUser
    ->getAccountName() . ' (' . $this->loggedInUser
    ->id() . ')';
  $edit = [
    'customer_type' => 'existing',
    'uid' => $user,
  ];
  $this
    ->submitForm($edit, t('Create'));
  $this
    ->getSession()
    ->getPage()
    ->pressButton('add_billing_information');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->buttonExists('hide_profile_form');
  $this
    ->assertRenderedAddress($this->defaultAddress, 'billing_profile[0][profile]');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('hide_profile_form');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Test that commerce_order_test_field_widget_form_alter() has the expected
  // outcome.
  $this
    ->assertSame([], \Drupal::state()
    ->get("commerce_order_test_field_widget_form_alter"));

  // Test creating order items.
  $page = $this
    ->getSession()
    ->getPage();

  // First item with overriding the price.
  $this
    ->getSession()
    ->getPage()
    ->checkField('Override the unit price');
  $purchased_entity_field = $this
    ->assertSession()
    ->waitForElement('css', '[name="order_items[form][0][purchased_entity][0][target_id]"].ui-autocomplete-input');
  $purchased_entity_field
    ->setValue(substr($this->variation
    ->getSku(), 0, 4));
  $this
    ->getSession()
    ->getDriver()
    ->keyDown($purchased_entity_field
    ->getXpath(), ' ');
  $this
    ->assertSession()
    ->waitOnAutocomplete();
  $this
    ->assertSession()
    ->pageTextContains($this->variation
    ->getSku());
  $this
    ->assertCount(1, $this
    ->getSession()
    ->getPage()
    ->findAll('css', '.ui-autocomplete li'));
  $this
    ->getSession()
    ->getPage()
    ->find('css', '.ui-autocomplete li:first-child a')
    ->click();
  $this
    ->assertSession()
    ->fieldValueEquals('order_items[form][0][purchased_entity][0][target_id]', $this->variation
    ->getSku() . ': ' . $this->variation
    ->label() . ' (' . $this->variation
    ->id() . ')');
  $page
    ->fillField('order_items[form][0][quantity][0][value]', '1');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Create order item');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContainsOnce('Unit price must be a number.');
  $page
    ->fillField('order_items[form][0][unit_price][0][amount][number]', '9.99');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Create order item');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('9.99');

  // Second item without overriding the price.
  $entity2 = $this->secondVariation
    ->getSku() . ' (' . $this->secondVariation
    ->id() . ')';
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Add new order item');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $page
    ->fillField('order_items[form][1][purchased_entity][0][target_id]', $entity2);
  $page
    ->fillField('order_items[form][1][quantity][0][value]', '1');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Create order item');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('5.55');

  // Test editing an order item.
  $edit_buttons = $this
    ->xpath('//div[@data-drupal-selector="edit-order-items-wrapper"]//input[@value="Edit"]');
  $edit_button = reset($edit_buttons);
  $edit_button
    ->click();
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $page
    ->fillField('order_items[form][inline_entity_form][entities][0][form][quantity][0][value]', '3');
  $page
    ->fillField('order_items[form][inline_entity_form][entities][0][form][unit_price][0][amount][number]', '1.11');
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Update order item');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertSession()
    ->pageTextContains('1.11');

  // There is no adjustment - the order should save successfully.
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The order has been successfully saved.');
  $order = Order::load(1);
  $this
    ->assertNull($order
    ->getBillingProfile());

  // Use an adjustment that is not locked by default.
  $this
    ->drupalGet($order
    ->toUrl('edit-form'));
  $this
    ->getSession()
    ->getPage()
    ->pressButton('add_billing_information');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $edit = [
    'adjustments[0][type]' => 'fee',
    'adjustments[0][definition][label]' => '',
    'adjustments[0][definition][amount][number]' => '2.00',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The adjustment label field is required.');
  $edit['adjustments[0][definition][label]'] = 'Test fee';
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('The order has been successfully saved.');
  $this
    ->drupalGet('/admin/commerce/orders');
  $order_number = $this
    ->getSession()
    ->getPage()
    ->findAll('css', 'tr td.views-field-order-number');
  $this
    ->assertEquals(1, count($order_number));
  $order = $this
    ->reloadEntity($order);
  $this
    ->assertEquals(2, count($order
    ->getItems()));
  $this
    ->assertEquals(new Price('10.88', 'USD'), $order
    ->getTotalPrice());
  $this
    ->assertCount(1, $order
    ->getAdjustments());
  $billing_profile = $order
    ->getBillingProfile();
  $this
    ->assertEquals($this->defaultAddress, array_filter($billing_profile
    ->get('address')
    ->first()
    ->toArray()));
  $this
    ->assertEquals($this->defaultProfile
    ->id(), $billing_profile
    ->getData('address_book_profile_id'));
}