function RulesIntegrationTestCase::testTaxonomyIntegration

Same name and namespace in other branches
  1. 7.x-2.x tests/rules.test \RulesIntegrationTestCase::testTaxonomyIntegration()

Test integration for the taxonomy module.

File

d7-tests/rules_integration_test_case.test, line 352

Class

RulesIntegrationTestCase
Tests provided module integration.

Code

function testTaxonomyIntegration() {
  $term = entity_property_values_create_entity('taxonomy_term', array(
    'name' => $this->randomName(),
    'vocabulary' => 1,
  ))
    ->value();
  $term2 = clone $term;
  taxonomy_term_save($term);
  taxonomy_term_save($term2);
  $tags[LANGUAGE_NONE][0]['tid'] = $term->tid;
  $node = $this->drupalCreateNode(array(
    'title' => 'foo',
    'type' => 'article',
    'field_tags' => $tags,
  ));
  // Test assigning and remove a term from an article.
  $rule = rule(array(
    'node' => array(
      'type' => 'node',
      'bundle' => 'article',
    ),
  ));
  $term_wrapped = rules_wrap_data($term->tid, array(
    'type' => 'taxonomy_term',
  ));
  $term_wrapped2 = rules_wrap_data($term2->tid, array(
    'type' => 'taxonomy_term',
  ));
  $rule->action('list_add', array(
    'list:select' => 'node:field-tags',
    'item' => $term_wrapped2,
  ));
  $rule->action('list_remove', array(
    'list:select' => 'node:field-tags',
    'item' => $term_wrapped,
  ));
  $rule->execute($node);
  RulesLog::logger()->checkLog();
  $this->assertEqual(array_values($node->field_tags[LANGUAGE_NONE]), array(
    0 => array(
      'tid' => $term2->tid,
    ),
  ), 'Term removed and added from a node.');
  // Test using the taxonomy term reference field on a term object.
  $field_name = drupal_strtolower($this->randomName() . '_field_name');
  $field = field_create_field(array(
    'field_name' => $field_name,
    'type' => 'taxonomy_term_reference',
    // Set cardinality to unlimited for tagging.
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => 'tags',
          'parent' => 0,
        ),
      ),
    ),
  ));
  $instance = array(
    'field_name' => $field_name,
    'entity_type' => 'taxonomy_term',
    'bundle' => 'tags',
    // Machine name of vocabulary.
'label' => $this->randomName() . '_label',
    'description' => $this->randomName() . '_description',
    'weight' => mt_rand(0, 127),
    'widget' => array(
      'type' => 'taxonomy_autocomplete',
      'weight' => -4,
    ),
    'display' => array(
      'default' => array(
        'type' => 'taxonomy_term_reference_link',
        'weight' => 10,
      ),
    ),
  );
  field_create_instance($instance);
  $term1 = entity_property_values_create_entity('taxonomy_term', array(
    'name' => $this->randomName(),
    'vocabulary' => 1,
  ))
    ->save();
  $term2 = entity_property_values_create_entity('taxonomy_term', array(
    'name' => $this->randomName(),
    'vocabulary' => 1,
  ))
    ->save();
  // Test asserting the term reference field and using it afterwards.
  $rule = rule(array(
    'taxonomy_term' => array(
      'type' => 'taxonomy_term',
    ),
  ));
  $rule->condition('entity_has_field', array(
    'entity:select' => 'taxonomy-term',
    'field' => $field_name,
  ));
  // Add $term2 to $term1 using the term reference field.
  $selector = str_replace('_', '-', 'taxonomy_term:' . $field_name);
  $rule->action('list_add', array(
    'list:select' => $selector,
    'item' => $term2,
  ));
  $rule->integrityCheck();
  $rule->execute($term1);
  RulesLog::logger()->checkLog();
  $this->assertEqual($term1->{$field_name}[0]
    ->getIdentifier(), $term2->getIdentifier(), 'Rule appended a term to the term reference field on a term.');
  // Test an action set for merging term parents, which is provided as default
  // config.
  $term = entity_property_values_create_entity('taxonomy_term', array(
    'name' => $this->randomName(),
    'vocabulary' => 1,
    'parent' => array(
      $term1->value(),
    ),
  ))
    ->save();
  $action = rules_action('component_rules_retrieve_term_parents');
  list($parents) = $action->execute(array(
    $term->getIdentifier(),
  ));
  $this->assertTrue($parents[0]->tid == $term1->getIdentifier(), 'Invoked component to retrieve term parents.');
  RulesLog::logger()->checkLog();
}