You are here

protected function OrderTest::ucCreateOrder in Ubercart 8.4

Helper function for creating an order programmatically.

5 calls to OrderTest::ucCreateOrder()
OrderTest::testCustomOrderStatus in uc_order/tests/src/Functional/OrderTest.php
Tests using custom order statuses.
OrderTest::testOrderCustomerView in uc_order/tests/src/Functional/OrderTest.php
Tests the customer View of the completed order.
OrderTest::testOrderEditing in uc_order/tests/src/Functional/OrderTest.php
Tests admin editing of orders.
OrderTest::testOrderState in uc_order/tests/src/Functional/OrderTest.php
Tests admin and automatic changing of order state and status.
OrderTest::testOrderView in uc_order/tests/src/Functional/OrderTest.php
Tests order admin View.

File

uc_order/tests/src/Functional/OrderTest.php, line 355

Class

OrderTest
Tests for Ubercart orders.

Namespace

Drupal\Tests\uc_order\Functional

Code

protected function ucCreateOrder($customer) {
  $order = Order::create([
    'uid' => $customer
      ->id(),
  ]);
  $order
    ->save();
  uc_order_comment_save($order
    ->id(), 0, 'Order created programmatically.', 'admin');
  $order_ids = \Drupal::entityQuery('uc_order')
    ->condition('order_id', $order
    ->id())
    ->execute();
  $this
    ->assertTrue(in_array($order
    ->id(), $order_ids), format_string('Found order ID @order_id', [
    '@order_id' => $order
      ->id(),
  ]));
  $country_manager = \Drupal::service('country_manager');
  $country = array_rand($country_manager
    ->getEnabledList());
  $zones = $country_manager
    ->getZoneList($country);
  $delivery_address = Address::create();
  $delivery_address
    ->setFirstName($this
    ->randomMachineName(12))
    ->setLastName($this
    ->randomMachineName(12))
    ->setStreet1($this
    ->randomMachineName(12))
    ->setStreet2($this
    ->randomMachineName(12))
    ->setCity($this
    ->randomMachineName(12))
    ->setPostalCode(mt_rand(10000, 99999))
    ->setCountry($country);
  if (!empty($zones)) {
    $delivery_address
      ->setZone(array_rand($zones));
  }
  $billing_address = Address::create();
  $billing_address
    ->setFirstName($this
    ->randomMachineName(12))
    ->setLastName($this
    ->randomMachineName(12))
    ->setStreet1($this
    ->randomMachineName(12))
    ->setStreet2($this
    ->randomMachineName(12))
    ->setCity($this
    ->randomMachineName(12))
    ->setPostalCode(mt_rand(10000, 99999))
    ->setCountry($country);
  if (!empty($zones)) {
    $billing_address
      ->setZone(array_rand($zones));
  }
  $order
    ->setAddress('delivery', $delivery_address)
    ->setAddress('billing', $billing_address)
    ->save();

  // Force the order to load from the DB instead of the entity cache.
  $db_order = \Drupal::entityTypeManager()
    ->getStorage('uc_order')
    ->loadUnchanged($order
    ->id());

  // Compare delivery and billing addresses to those loaded from the database.
  $db_delivery_address = $db_order
    ->getAddress('delivery');
  $db_billing_address = $db_order
    ->getAddress('billing');
  $this
    ->assertEquals($db_delivery_address, $delivery_address, 'Delivery address is equal to delivery address in database.');
  $this
    ->assertEquals($db_billing_address, $billing_address, 'Billing address is equal to billing address in database.');
  return $order;
}