You are here

function CommerceOrderCRUDTestCase::testCommerceOrderCrud in Commerce Core 7

Test the order CRUD functions.

File

modules/order/tests/commerce_order.test, line 31
Unit tests for the commerce order module.

Class

CommerceOrderCRUDTestCase
Test the order and order type CRUD.

Code

function testCommerceOrderCrud() {

  // Ensure commerce_order_new() returns a new order.
  $new_order = commerce_order_new(1);
  $fields = array(
    'type',
    'status',
    'uid',
  );
  foreach ($fields as $field) {
    $this
      ->assertNotNull($new_order->{$field}, 'commerce_order_new() instantiated the ' . check_plain($field) . ' property.');
  }
  $new_order->order_number = $order_number = $this
    ->randomName(10);
  $new_order->mail = $this
    ->generateEmail();
  $new_order->hostname = $this
    ->randomName(10);

  // Ensure commerce_order_save() returns SAVED_NEW when saving a new order
  $return = commerce_order_save($new_order);
  $this
    ->assertIdentical($return, SAVED_NEW, 'commerce_order_save() successfully saved the new order.');

  // Ensure commerce_order_load() loaded the saved order.
  $loaded_order = commerce_order_load($new_order->order_id);
  foreach ($fields as $field) {
    $this
      ->assertEqual($loaded_order->{$field}, $new_order->{$field}, 'The ' . check_plain($field) . ' value loaded by commerce_order_load() matches the value saved by commerce_order_save()');
  }
  $this
    ->assertTrue($loaded_order->created > 0, 'commerce_order_save() added a created date to the order');
  $this
    ->assertTrue($loaded_order->changed > 0, 'commerce_order_save() added a changed date to the order');

  // Ensure commerce_order_save() returns SAVED_UPDATED when saving an existing order.
  $loaded_order->uid = 0;
  $return = commerce_order_save($loaded_order);
  $this
    ->assertIdentical($return, SAVED_UPDATED, 'commerce_order_save() successfully updated the order.');

  // Ensure commerce_order_load_by_sku() can load an order by order number.
  $loaded_order_by_number = commerce_order_load_by_number($order_number);
  $this
    ->assertEqual($loaded_order_by_number->order_id, $new_order->order_id, 'The ID of the order loaded via commerce_order_load_by_sku() matches the saved order ID.');

  // Ensure commerce_order_load_multiple() can load multiple multiple orders.
  $saved_order_ids = array();

  // First create and save multiple new orders.
  for ($i = 0; $i < 3; $i++) {
    $order = commerce_order_new(1);
    $order->order_number = $this
      ->randomName(10);
    $order->mail = $this
      ->generateEmail();
    $order->hostname = $this
      ->randomName(10);
    commerce_order_save($order);

    // Save the ID and order number of the newly created order.
    $saved_orders[$order->order_id] = $order->order_number;
  }
  $loaded_orders = commerce_order_load_multiple(array_keys($saved_orders));
  $this
    ->assertEqual(count($saved_orders), count($loaded_orders), 'commerce_order_load_multiple() loaded the proper number of the orders.');
  foreach ($loaded_orders as $loaded_order) {
    $this
      ->assertEqual($loaded_order->order_number, $saved_orders[$loaded_order->order_id], 'commerce_order_load_multiple() successfully loaded a order.');
  }

  // Ensure commerce_order_delete() can remove a order.
  $return = commerce_order_delete($new_order->order_id);
  $this
    ->assertTrue($return, 'commerce_order_delete() returned TRUE when deleting a order.');
  $deleted_order_load = commerce_order_load_multiple(array(
    $new_order->order_id,
  ), array(), TRUE);
  $this
    ->assertFalse($deleted_order_load, 'commerce_order_load_multiple() could not load the deleted order.');

  // Ensure commerce_order_delete_multiple() can delete multiple orders.
  $return = commerce_order_delete_multiple(array_keys($saved_orders));
  $this
    ->assertTrue($return, 'commerce_order_delete_multiple() returned TRUE when deleting a order.');
  $deleted_orders_load = commerce_order_load_multiple(array_keys($saved_orders), array(), TRUE);
  $this
    ->assertFalse($deleted_order_load, 'commerce_order_load_multiple() could not load the deleted orders.');
}