You are here

protected function UCDropdownAttributesTestCase::testProductAttributeDependency in Dropdown Attributes 7

Test for dropdown attributes in products.

File

tests/uc_dropdown_attributes.test, line 34
Tests for Dropdown Attributes.

Class

UCDropdownAttributesTestCase
Provides tests for Dropdown Attributes.

Code

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