You are here

uc_dropdown_attributes.test in Dropdown Attributes 6

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

Dropdown Attribute Tests

File

tests/uc_dropdown_attributes.test
View source
<?php

/**
 * @file
 * Dropdown Attribute Tests
 */

// EnsureUbercartTestHelper is available.
module_load_include('test', 'uc_store', 'uc_store');

/**
 * Tests for Dropdown Attributes.
 */
class DropdownAttributeTestCase extends UbercartTestHelper {
  public static function getInfo() {
    return array(
      'name' => 'Dropdown Attributes functionality',
      'description' => 'Test Dropdown Attributes functionality.',
      'group' => 'Dropdown attributes',
    );
  }

  /**
   * Overrides DrupalWebTestCase::setUp().
   */
  function setUp() {
    parent::setUp(array(
      'uc_dropdown_attributes',
      'uc_attribute',
    ), array(
      'administer attributes',
      'administer nodes',
    ));
    $this
      ->drupalLogin($this->adminUser);
  }

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

    // Create an attribute.
    $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 the database is correct.
    $type = uc_dropdown_attributes_dependency_type($product->nid);
    $this
      ->assertEqual($type, 'node');
    $sql = 'SELECT parent_aid, parent_values, required
      FROM {uc_dropdown_attributes} WHERE nid=%d AND aid=%d';
    $attributes = db_query($sql, $product->nid, $child_attribute->aid);
    while ($attribute = db_fetch_object($attributes)) {
      $this
        ->assertEqual($attribute->parent_aid, $parent_attribute->aid);
      $this
        ->assertEqual(unserialize($attribute->parent_values), $options);
      $this
        ->assertEqual($attribute->required, 1);
    }

    // Check for child attribute (currently this is not much of a test).

    //$this->drupalGet('node/' . $product->nid);

    //$this->assertRaw('id="edit-attributes-2-wrapper">',

    //  t('Dependency:  Child attribute not found.'));
  }

  /**
   * Tests for dropdown attributes in product classes.
   */
  function testClassAttributeDependency() {
    $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 = 2;

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

    // Check for child attribute.
    $this
      ->drupalGet('node/' . $nid);
    $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 the database is correct.
    $sql = 'SELECT parent_aid, parent_values, required
      FROM {uc_dropdown_classes} WHERE pcid="%s" AND aid=%d';
    $result = db_query($sql, $class, $child_attribute->aid);
    if ($item = db_fetch_object($result)) {
      $this
        ->assertEqual($item->parent_aid, $parent_attribute->aid);
      $this
        ->assertEqual(unserialize($item->parent_values), $options);
      $this
        ->assertEqual($item->required, 1);
    }

    // Check for child attribute (currently this is not much of a test).

    //$this->drupalGet('node/' . $nid);

    //$this->assertRaw('id="edit-attributes-2-wrapper">',

    //  t('Dependency:  Child attribute not found.'));
  }

  /**
   * Tests the product class attribute option user interface.
   */
  public static 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 $data
   * @param $save
   */
  public static function createAttributeOption($data = array(), $save = TRUE) {
    $option = $data + array(
      '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 an attribute option.
   *
   * @param $data
   * @param $save
   */
  function showVar($var) {
    $this
      ->pass('<pre>' . print_r($var, TRUE) . '</pre>');
  }

  /**
   * Creates a new product.
   */
  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.
   */
  function createProductClass($class) {
    $edit = array(
      'pcid' => $class,
      'name' => $class,
      'description' => $this
        ->randomName(32),
    );
    $this
      ->drupalPost('admin/store/products/classes', $edit, t('Submit'));
  }

  /**
   * Creates a new product in a product class.
   */
  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
DropdownAttributeTestCase Tests for Dropdown Attributes.