You are here

protected function UCDropdownAttributesTestCase::testClassAttributeDependency in Dropdown Attributes 7

Test for dropdown attributes in product classes.

File

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

Class

UCDropdownAttributesTestCase
Provides tests for Dropdown Attributes.

Code

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