You are here

commerce_bpc.test in Commerce Bulk Product Creation 7

Same filename and directory in other branches
  1. 7.2 commerce_bpc.test

Tests for Commerce bulk product creation

File

commerce_bpc.test
View source
<?php

/**
 * @file
 * Tests for Commerce bulk product creation
 */
class CommerceBpcTestCase extends CommerceBaseTestCase {

  /**
   * Registers test case.
   */
  public static function getInfo() {
    return array(
      'name' => 'Commerce bulk product creation',
      'description' => 'Test the Commerce bulk product creation functionality.',
      'group' => 'Commerce (contrib)',
    );
  }
  protected $productType = NULL;
  protected $listFieldOne = NULL;
  protected $listFieldTwo = NULL;
  protected $storeAdmin = NULL;

  /**
   * Overrides CommerceBaseTestCase::setUp().
   */
  function setUp() {
    $modules = parent::setUpHelper('all', array(
      'commerce_bpc',
    ));
    parent::setUp($modules);
    $this->storeAdmin = $this
      ->createStoreAdmin();
    $this->productType = $this
      ->createDummyProductType();
    $this
      ->drupalLogin($this->storeAdmin);
  }

  /**
   * Test the correct creation of 2x3 combinations.
   */
  function testTwoFieldsTwoListValues() {
    $fields = array();
    $field = $this
      ->attachNewListField($this->productType, 2);
    $fields[$field['field_name']] = array_keys($field['settings']['allowed_values']);
    $field = $this
      ->attachNewListField($this->productType, 3);
    $fields[$field['field_name']] = array_keys($field['settings']['allowed_values']);
    $price = rand(2, 500);
    $sku_fragment = $this
      ->randomName(5);
    $params = array(
      'sku_fragment' => $sku_fragment,
      'title_fragment' => $this
        ->randomName(10),
      'commerce_price[' . LANGUAGE_NONE . '][0][amount]' => $price,
      'static_values[status]' => 1,
    );
    foreach ($fields as $name => $vals) {
      foreach ($vals as $val) {
        $params['combinations[' . $name . '][' . LANGUAGE_NONE . '][' . $val . ']'] = TRUE;
      }
    }
    $bulk_url = 'admin/commerce/products/add-bulk/';
    $bulk_url .= $this->productType['type'];
    $this
      ->drupalPost($bulk_url, $params, t('Create products'));
    $this
      ->assertCorrectPermutationsExist($fields, "All permutations of two fields have been created exactly once.");
    $n = $this
      ->getNumberOfProductsWithFieldValues();
    $this
      ->assertEqual($n, 6, 'No other product was created.');
    $attributes = array(
      'commerce_price' => array(
        'field' => 'commerce_price',
        // The price is stored in nondecimal form, hence we need to multiply.
        'value' => $price * 100,
        'column' => 'amount',
      ),
    );
    $n = $this
      ->getNumberOfProductsWithFieldValues($attributes);
    $this
      ->assertEqual($n, 6, "All created products have the correct price.");
    $active_products = commerce_product_load_multiple(FALSE, array(
      'status' => 1,
    ));
    $this
      ->assertEqual(count($active_products), 6, "All created products have are active.");
  }

  /**
   * Attach a new list field with the given number of options to a product
   * type.
   *
   * @param string $product_type
   *   An array representing the product type to which the field should
   *   be attached.
   *
   * @return array
   *   An array containing the field specification of the new field.
   */
  protected function attachNewListField($product_type, $no_options = 2) {
    $field_name = drupal_strtolower($this
      ->randomName() . '_field_name');
    $values = array();
    for ($i = 0; $i < $no_options; $i++) {
      $values[$this
        ->randomName(6) . 'val' . $i] = $this
        ->randomString(10);
    }
    $field = array(
      'field_name' => $field_name,
      'type' => 'list_text',
      'cardinality' => 1,
      'settings' => array(
        'allowed_values' => $values,
      ),
    );
    $field = field_create_field($field);
    $field_id = $field['id'];
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => 'commerce_product',
      'bundle' => $product_type['type'],
      'label' => $this
        ->randomName() . '_label',
      'description' => $this
        ->randomName() . '_description',
      'weight' => mt_rand(0, 127),
      'settings' => array(),
      'widget' => array(
        'type' => 'options_buttons',
        'label' => 'Test Field',
        'settings' => array(
          'test_widget_setting' => $this
            ->randomName(),
        ),
      ),
    );
    $instance = field_create_instance($instance);
    return $field;
  }

  /**
   * Asserts that a product with the correct permutations exists.
   *
   * @param array $parameters
   *   The expected parameters of the product.
   * @param string $text
   *   The message corresponding to the assertion.
   */
  protected function assertCorrectPermutationsExist($parameters, $text) {
    $combinations = $this
      ->generatePermutations($parameters);
    $count = 0;
    foreach ($combinations as $combination) {
      $attributes = array();
      foreach ($combination as $field => $value) {
        $attributes[] = array(
          'field' => $field,
          'value' => $value,
        );
      }
      if ($this
        ->getNumberOfProductsWithFieldValues($attributes) == 1) {
        $count++;
      }
    }
    $this
      ->assertEqual($count, count($combinations), $text);
  }

  /**
   * Determine how many products exist with the provided field values.
   *
   * @param array $attributes
   *   An array of associative arrays with the following keys, each specifying
   *   one field condition.
   *   - field: The name of the field.
   *   - column: The column of the field, as in
   *     EntityFieldQuery::entityCondition (optional, defaults to 'value').
   *   - value: The value to test for.
   */
  function getNumberOfProductsWithFieldValues($attributes = array()) {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'commerce_product', '=')
      ->entityCondition('bundle', $this->productType['type'], '=');
    foreach ($attributes as $attr) {
      $attr += array(
        'column' => 'value',
      );
      $query = $query
        ->fieldCondition($attr['field'], $attr['column'], $attr['value']);
    }
    $query
      ->count();
    return $query
      ->execute();
  }

}

/**
 * Unit tests for settings.
 */
class CommerceBpcSettingsTest extends DrupalWebTestCase {

  /**
   * Registers the test case.
   */
  public static function getInfo() {
    return array(
      'name' => 'Commerce Bulk Product Creation: Settings',
      'description' => 'Test settings-related functionality.',
      'group' => 'Commerce (contrib)',
    );
  }

  /**
   * Overrides DrupalWebTestCase::setUp().
   */
  function setUp() {
    parent::setUp('commerce_bpc');
  }

  /**
   * Tests saving and retrieval of settings.
   */
  function testSettingAndRetrievingSettings() {
    $group = $this
      ->randomName();
    $varname = $this
      ->randomName();
    $product_type = drupal_strtolower($this
      ->randomName());
    $global_value = $this
      ->randomName();
    commerce_bpc_setting_set($group, $varname, $global_value);
    $this
      ->assertEqual($global_value, commerce_bpc_setting($group, $varname), "Global setting retrieved as set.");
    $this
      ->assertEqual($global_value, commerce_bpc_setting($group, $varname, $product_type), "Global setting retrieved for product type if no override specified and no type-specific value is set.");
    $specific_value = $this
      ->randomName();
    commerce_bpc_setting_set($group, $varname, $specific_value, $product_type);
    $this
      ->assertEqual($global_value, commerce_bpc_setting($group, $varname), "Retrieval of global setting is not impaired by existence of type-specific value.");
    $this
      ->assertEqual($global_value, commerce_bpc_setting($group, $varname, $product_type), "Global setting retrieved for product type if no override is specified, even if specific value is set.");
    commerce_bpc_setting_set($group, 'override', TRUE, $product_type);
    $this
      ->assertEqual($global_value, commerce_bpc_setting($group, $varname), "Retrieval of global value not impaired by existence of override and specific value.");
    $this
      ->assertEqual($specific_value, commerce_bpc_setting($group, $varname, $product_type), "When override exists and a a specific value exists, the specific value is returned.");
    commerce_bpc_setting_set($group, $varname, NULL, $product_type);
    $this
      ->assertEqual($global_value, commerce_bpc_setting($group, $varname), "Retrieval of global value not impaired if override exists but no specific value.");
    $this
      ->assertEqual($global_value, commerce_bpc_setting($group, $varname, $product_type), "If override exists, but no specific value, retrieve global value.");
    commerce_bpc_setting_set($group, $varname, $specific_value, $product_type);
    $second_varname = $this
      ->randomName();
    $second_global_value = $this
      ->randomName();
    $second_local_value = $this
      ->randomName();
    commerce_bpc_setting_set($group, $second_varname, $second_global_value);
    commerce_bpc_setting_set($group, $second_varname, $second_local_value, $product_type);
    $this
      ->assertEqual($second_global_value, commerce_bpc_setting($group, $second_varname), "Second global value can be saved and retrieved works.");
    $this
      ->assertEqual($second_local_value, commerce_bpc_setting($group, $second_varname, $product_type), "Second type-specific value can be retrieved and saved.");
    $this
      ->assertEqual($global_value, commerce_bpc_setting($group, $varname), "Saving a second value does not impair retrieval of first value (global).");
    $this
      ->assertEqual($specific_value, commerce_bpc_setting($group, $varname, $product_type), "Saving a second value does not impair retrieval of first value (type-specific).");
    $group2 = $this
      ->randomName();
    commerce_bpc_setting_set($group2, $varname, $second_global_value);
    commerce_bpc_setting_set($group2, $varname, $second_local_value, $product_type);
    $this
      ->assertEqual($second_global_value, commerce_bpc_setting($group2, $varname), "Value with same name can be saved in different group");
    $this
      ->assertEqual($global_value, commerce_bpc_setting($group, $varname), "Saving value with same name in different group does not impact original value.");
    $this
      ->assertEqual($second_global_value, commerce_bpc_setting($group2, $varname, $product_type), "Existence of override in one group does not influence override behavior in another.");
  }

}

Classes

Namesort descending Description
CommerceBpcSettingsTest Unit tests for settings.
CommerceBpcTestCase @file Tests for Commerce bulk product creation