You are here

function DropdownAttributeTestCase::testProductAttributeDependency in Dropdown Attributes 6

Tests for dropdown attributes in products.

File

tests/uc_dropdown_attributes.test, line 36
Dropdown Attribute Tests

Class

DropdownAttributeTestCase
Tests for Dropdown Attributes.

Code

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.'));
}