You are here

public function CommerceAutoSkuTest::testCommerceAutoSKUGeneration in Commerce AutoSKU 7

Test the basic generation and token replacement.

File

./commerce_autosku.test, line 45
Contains simpletests for commerce_autosku

Class

CommerceAutoSkuTest
@file Contains simpletests for commerce_autosku

Code

public function testCommerceAutoSKUGeneration() {

  //Create a product pattern.
  $edit = array(
    'product_type[autosku_status]' => TRUE,
    'product_type[autosku][pattern]' => '[commerce-product:title]',
  );
  $this
    ->drupalPost('admin/commerce/products/types/product/edit', $edit, 'Save product type');
  $this
    ->productTypeSubmit(array(
    'product_type[autosku][pattern]' => '[commerce-product:title]',
  ));

  //Test Title replacement.
  $title = $this
    ->randomName();
  $edit = array(
    'title' => $title,
    'commerce_price[' . LANGUAGE_NONE . '][0][amount]' => 500,
  );
  $this
    ->drupalPost('admin/commerce/products/add/product', $edit, 'Save product');
  $product = commerce_product_load(1);
  $this
    ->assertEqual($product->sku, $title, t('Product title token replaced.'));

  //Test Duplicate SKU Handling.
  $edit = array(
    'title' => $title,
    'commerce_price[' . LANGUAGE_NONE . '][0][amount]' => 500,
  );
  $this
    ->drupalPost('admin/commerce/products/add/product', $edit, 'Save product');
  $product = commerce_product_load(2);
  $this
    ->assertEqual($product->sku, $title . '0', t('Product title token replaced.'));

  //Test the ID replacement.
  $this
    ->productTypeSubmit(array(
    'product_type[autosku][pattern]' => '[commerce-product:product-id]',
  ));
  $title = $this
    ->randomName();
  $edit = array(
    'title' => $title,
    'commerce_price[' . LANGUAGE_NONE . '][0][amount]' => 500,
  );
  $this
    ->drupalPost('admin/commerce/products/add/product', $edit, 'Save product');
  $product = commerce_product_load(3);
  $this
    ->assertEqual($product->sku, '3', t('Product ID token replaced.'));

  //Test that empty/nonexistent tokens are not printed in the generated pattern.
  $title = $this
    ->randomName();
  $this
    ->productTypeSubmit(array(
    'product_type[autosku][pattern]' => '[commerce-product:title][commerce-product:nonexistent-token]',
  ));
  $edit = array(
    'title' => $title,
    'commerce_price[' . LANGUAGE_NONE . '][0][amount]' => 500,
  );
  $this
    ->drupalPost('admin/commerce/products/add/product', $edit, 'Save product');
  $product = commerce_product_load(4);
  $this
    ->assertEqual($product->sku, $title);

  // Test that a pattern that resolves to more than 255 characters is trimmed.

  //Test that empty/nonexistent tokens are not printed in the generated pattern.
  $title = $this
    ->randomName(100);
  $this
    ->productTypeSubmit(array(
    'product_type[autosku][pattern]' => '[commerce-product:title][commerce-product:title][commerce-product:title]',
  ));
  $edit = array(
    'title' => $title,
    'commerce_price[' . LANGUAGE_NONE . '][0][amount]' => 500,
  );
  $this
    ->drupalPost('admin/commerce/products/add/product', $edit, 'Save product');
  $product = commerce_product_load(5);
  $this
    ->assertEqual(drupal_strlen($product->sku), 255);
  $this
    ->assertEqual($product->sku, drupal_substr($title . $title . $title, 0, 255));

  // Test that when trimming SKU's, we trim from the original sku rather
  // than the number-incremented version.
  $edit = array(
    'title' => $title,
    'commerce_price[' . LANGUAGE_NONE . '][0][amount]' => 500,
  );
  $this
    ->drupalPost('admin/commerce/products/add/product', $edit, 'Save product');
  $product = commerce_product_load(6);
  $this
    ->assertEqual(drupal_strlen($product->sku), 255);
  $this
    ->assertEqual($product->sku, drupal_substr($title . $title . $title, 0, 254) . 0);
}