You are here

function CommerceProductCRUDTestCase::testCommerceProductCrud in Commerce Core 7

Test the product CRUD functions.

File

modules/product/tests/commerce_product.test, line 107
Unit tests for the commerce product module.

Class

CommerceProductCRUDTestCase
Test the product and product type CRUD.

Code

function testCommerceProductCrud() {
  global $commerce_product_crud_tests;

  // Ensure commerce_product_new() returns a new product.
  $new_product = commerce_product_new('product');
  $fields = array(
    'sku',
    'type',
    'title',
    'uid',
  );
  foreach ($fields as $field) {
    $this
      ->assertNotNull($new_product->{$field}, 'commerce_product_new() instantiated the ' . check_plain($field) . ' property.');
  }
  $new_product->sku = $sku = $this
    ->randomName(10);
  $new_product->type = $type = 'product';
  $new_product->title = $title = $this
    ->randomName(10);
  $new_product->uid = 1;

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

  // Ensure commerce_product_load() loaded the saved product.
  $loaded_product = commerce_product_load($new_product->product_id);
  foreach ($fields as $field) {
    $this
      ->assertEqual($loaded_product->{$field}, $new_product->{$field}, 'The ' . check_plain($field) . ' value loaded by commerce_product_load() matches the value saved by commerce_product_save()');
  }
  $this
    ->assertTrue($loaded_product->created > 0, 'commerce_product_save() added a created date to the product');
  $this
    ->assertTrue($loaded_product->changed > 0, 'commerce_product_save() added a changed date to the product');

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

  // Ensure hooks invoked during the save operation can load identical objects
  // be checking a global variable set in commerce_product_crud_test_commerce_product_update()
  // if the uid is in fact being loaded as 0 in the update hook.
  $this
    ->assertTrue($commerce_product_crud_tests['hook_update_identical_uid'], t('Invoked hooks during save operation can successfully load identical object.'));

  // Ensure commerce_product_load_by_sku() can load a product by SKU.
  $loaded_product_by_sku = commerce_product_load_by_sku($sku);
  $this
    ->assertEqual($loaded_product_by_sku->product_id, $new_product->product_id, 'The ID of the product loaded via commerce_product_load_by_sku() matches the saved product ID.');

  // Ensure commerce_product_load_multiple() can load multiple multiple products.
  $saved_product_ids = array();

  // First create and save multiple new products.
  for ($i = 0; $i < 3; $i++) {
    $product = commerce_product_new('product');
    $product->type = 'product';
    $product->sku = $this
      ->randomName(10);
    $product->title = $this
      ->randomName(10);
    $product->uid = 1;
    commerce_product_save($product);

    // Save the ID and title of the newly created product.
    $saved_products[$product->product_id] = $product->title;
  }
  $loaded_products = commerce_product_load_multiple(array_keys($saved_products));
  $this
    ->assertEqual(count($saved_products), count($loaded_products), 'commerce_product_load_multiple() loaded the proper number of the products.');
  foreach ($loaded_products as $loaded_product) {
    $this
      ->assertEqual($loaded_product->title, $saved_products[$loaded_product->product_id], 'commerce_product_load_multiple() successfully loaded a product.');
  }

  // Ensure commerce_product_delete() can remove a product.
  $return = commerce_product_delete($new_product->product_id);
  $this
    ->assertTrue($return, 'commerce_product_delete() returned TRUE when deleting a product.');
  $deleted_product_load = commerce_product_load_multiple(array(
    $new_product->product_id,
  ), array(), TRUE);
  $this
    ->assertFalse($deleted_product_load, 'commerce_product_load_multiple() could not load the deleted product.');

  // Ensure commerce_product_delete_multiple() can delete multiple products.
  $return = commerce_product_delete_multiple(array_keys($saved_products));
  $this
    ->assertTrue($return, 'commerce_product_delete_multiple() returned TRUE when deleting a product.');
  $deleted_products_load = commerce_product_load_multiple(array_keys($saved_products), array(), TRUE);
  $this
    ->assertFalse($deleted_product_load, 'commerce_product_load_multiple() could not load the deleted products.');
}