You are here

uc_dropdown_attributes.test in Dropdown Attributes 7

Same filename and directory in other branches
  1. 6 tests/uc_dropdown_attributes.test

Tests for Dropdown Attributes.

File

tests/uc_dropdown_attributes.test
View source
<?php

/**
 * @file
 * Tests for Dropdown Attributes.
 */

/**
 * Provides tests for Dropdown Attributes.
 */
class UCDropdownAttributesTestCase extends DrupalWebTestCase {

  /**
   * Test info.
   */
  public static function getInfo() {
    return array(
      'name' => 'Dropdown Attributes functionality',
      'description' => 'Create dependent attributes.',
      'group' => 'Dropdown Attributes',
    );
  }

  /**
   * Enable modules.
   */
  public function setUp() {
    parent::setUp('uc_dropdown_attributes');
  }

  /**
   * Test for dropdown attributes in products.
   */
  protected function testProductAttributeDependency() {

    // Create two attributes.
    $parent_attribute = self::createAttribute(array(
      'display' => 1,
    ));
    $child_attribute = self::createAttribute(array(
      'display' => 1,
    ));

    // Add a product.
    $product = $this
      ->createProduct();

    // Attach the attributes to a product.
    uc_attribute_subject_save($parent_attribute, 'product', $product->nid);
    uc_attribute_subject_save($child_attribute, 'product', $product->nid);

    // Add some options.
    $parent_options = array();
    $options = array();
    for ($i = 0; $i < 3; $i++) {
      $option = self::createAttributeOption(array(
        'aid' => $parent_attribute->aid,
      ));
      $parent_options[$option->oid] = $option;
      if ($i < 2) {
        $options[$option->oid] = $option->oid;
      }
      if ($i == 0) {
        $oid = $option->oid;
      }
    }
    $child_options = array();
    for ($i = 0; $i < 3; $i++) {
      $option = self::createAttributeOption(array(
        'aid' => $child_attribute->aid,
      ));
      $child_options[$option->oid] = $option;
    }

    // Check for child attribute.
    $this
      ->drupalGet('node/' . $product->nid);
    $this
      ->assertText($child_attribute->label, t('No dependency:  Child attribute found.'));

    // Create dependent attribute.
    uc_dropdown_attributes_product_create_dependency($product->nid, $child_attribute->aid, $parent_attribute->aid, $options, 1);

    // Confirm that database is correct.
    $type = uc_dropdown_attributes_dependency_type($product->nid);
    $this
      ->assertEqual($type, 'node');
    $query = db_select('uc_dropdown_attributes')
      ->fields('uc_dropdown_attributes')
      ->condition('nid', $product->nid)
      ->condition('aid', $child_attribute->aid)
      ->execute();
    foreach ($query as $item) {
      $this
        ->assertEqual($item->parent_aid, $parent_attribute->aid);
      $this
        ->assertEqual(unserialize($item->parent_values), $options);
      $this
        ->assertEqual($item->required, 1);
    }

    // Check for child attribute.
    $this
      ->drupalGet('node/' . $product->nid);
    $this
      ->assertRaw('form-item-attributes-2" style="display:none;">', t('Dependency:  Child attribute not found.'));
  }

  /**
   * Test for dropdown attributes in product classes.
   */
  protected function testClassAttributeDependency() {
    $admin_user = $this
      ->drupalCreateUser(array(
      'administer attributes',
      'administer products',
      'administer product classes',
      'bypass node access',
    ));
    $this
      ->drupalLogin($admin_user);
    $class = 'services';
    self::createProductClass($class);
    $this
      ->drupalGet('admin/store/products/classes');

    // Create two attributes.
    $parent_attribute = self::createAttribute(array(
      'display' => 1,
    ));
    $child_attribute = self::createAttribute(array(
      'display' => 1,
    ));

    // Attach the attributes to a product class.
    uc_attribute_subject_save($parent_attribute, 'class', $class);
    uc_attribute_subject_save($child_attribute, 'class', $class);

    // Add some options.
    $parent_options = array();
    $options = array();
    for ($i = 0; $i < 3; $i++) {
      $option = self::createAttributeOption(array(
        'aid' => $parent_attribute->aid,
      ));
      $parent_options[$option->oid] = $option;
      if ($i < 2) {
        $options[$option->oid] = $option->oid;
      }
      if ($i == 0) {
        $oid = $option->oid;
      }
    }
    $child_options = array();
    for ($i = 0; $i < 3; $i++) {
      $option = self::createAttributeOption(array(
        'aid' => $child_attribute->aid,
      ));
      $child_options[$option->oid] = $option;
    }

    // Add a product.
    $this
      ->createProductClassProduct($class);
    $nid = 1;

    // Check product class name.
    $pcid = uc_dropdown_attributes_get_type($nid);
    $this
      ->assertEqual($pcid, $class);

    // Check for child attribute.
    $this
      ->drupalGet('node');
    $this
      ->assertText($child_attribute->label, t('No dependency:  Child attribute found.'));

    // Create dependent attribute.
    uc_dropdown_attributes_class_create_dependency($class, $child_attribute->aid, $parent_attribute->aid, $options, 1);

    // Check type of dependency.
    $type = uc_dropdown_attributes_dependency_type($nid);
    $this
      ->assertEqual($type, 'class');

    // Confirm that database is correct.
    $query = db_select('uc_dropdown_classes')
      ->fields('uc_dropdown_classes')
      ->condition('pcid', $class)
      ->condition('aid', $child_attribute->aid)
      ->execute();
    foreach ($query as $item) {
      $this
        ->assertEqual($item->parent_aid, $parent_attribute->aid);
      $this
        ->assertEqual(unserialize($item->parent_values), $options);
      $this
        ->assertEqual($item->required, 1);
    }

    // Check for child attribute.
    $this
      ->drupalGet('node');
    $this
      ->assertNoText('form-item-attributes-2" style="display:none;">', t('Dependency:  Child attribute not found.'));
  }

  /**
   * Creates an attribute.
   *
   * @param array $data
   *   Array of overrides.
   * @param bool $save
   *   TRUE or FALSE.
   */
  protected function createAttribute($data = array(), $save = TRUE) {
    $attribute = $data + array(
      'name' => DrupalWebTestCase::randomName(8),
      'label' => DrupalWebTestCase::randomName(8),
      'description' => DrupalWebTestCase::randomName(8),
      'required' => mt_rand(0, 1) ? TRUE : FALSE,
      'display' => mt_rand(0, 3),
      'ordering' => mt_rand(-10, 10),
    );
    $attribute = (object) $attribute;
    if ($save) {
      uc_attribute_save($attribute);
    }
    return $attribute;
  }

  /**
   * Creates an attribute option.
   *
   * @param array $data
   *   Array of overrides.
   * @param bool $save
   *   TRUE or FALSE.
   */
  protected function createAttributeOption($data = array(), $save = TRUE) {
    $max_aid = db_select('uc_attributes', 'a')
      ->fields('a', array(
      'aid',
    ))
      ->orderBy('aid', 'DESC')
      ->range(0, 1)
      ->execute()
      ->fetchField();
    $option = $data + array(
      'aid' => $max_aid,
      'name' => DrupalWebTestCase::randomName(8),
      'cost' => mt_rand(-500, 500),
      'price' => mt_rand(-500, 500),
      'weight' => mt_rand(-500, 500),
      'ordering' => mt_rand(-10, 10),
    );
    $option = (object) $option;
    if ($save) {
      uc_attribute_option_save($option);
    }
    return $option;
  }

  /**
   * Creates a new product.
   */
  protected function createProduct($product = array()) {

    // Set the default required fields.
    $weight_units = array(
      'lb',
      'kg',
      'oz',
      'g',
    );
    $length_units = array(
      'in',
      'ft',
      'cm',
      'mm',
    );
    $product += array(
      'type' => 'product',
      'model' => $this
        ->randomName(8),
      'list_price' => mt_rand(1, 9999),
      'cost' => mt_rand(1, 9999),
      'sell_price' => mt_rand(1, 9999),
      'weight' => mt_rand(1, 9999),
      'weight_units' => array_rand(array_flip($weight_units)),
      'length' => mt_rand(1, 9999),
      'width' => mt_rand(1, 9999),
      'height' => mt_rand(1, 9999),
      'length_units' => array_rand(array_flip($length_units)),
      'pkg_qty' => mt_rand(1, 99),
      'default_qty' => 1,
      'ordering' => mt_rand(-25, 25),
      'shippable' => TRUE,
    );
    return $this
      ->drupalCreateNode($product);
  }

  /**
   * Creates a new product class.
   */
  protected function createProductClass($class) {
    $edit = array(
      'pcid' => $class,
      'name' => $class,
      'description' => $this
        ->randomName(32),
    );
    $this
      ->drupalPost('admin/store/products/classes', $edit, t('Save'));
  }

  /**
   * Creates a new product in a product class.
   */
  protected function createProductClassProduct($class) {
    $edit = array(
      'title' => $this
        ->randomName(8),
      'model' => $this
        ->randomName(8),
      'sell_price' => mt_rand(1, 9999),
      'pkg_qty' => 1,
    );
    $this
      ->drupalPost('node/add/' . $class, $edit, t('Save'));
  }

}

Classes

Namesort descending Description
UCDropdownAttributesTestCase Provides tests for Dropdown Attributes.