You are here

public function EntityMetadataIntegrationTestCase::testTaxonomyProperties in Entity API 7

Tests properties provided by the taxonomy module.

File

./entity.test, line 1700
Entity CRUD API tests.

Class

EntityMetadataIntegrationTestCase
Tests provided entity property info of the core modules.

Code

public function testTaxonomyProperties() {
  $vocab = $this
    ->createVocabulary();
  $term_parent = entity_property_values_create_entity('taxonomy_term', array(
    'name' => $this
      ->randomName(),
    'vocabulary' => $vocab,
  ))
    ->save()
    ->value();
  $term_parent2 = entity_property_values_create_entity('taxonomy_term', array(
    'name' => $this
      ->randomName(),
    'vocabulary' => $vocab,
  ))
    ->save()
    ->value();
  $term = entity_property_values_create_entity('taxonomy_term', array(
    'name' => $this
      ->randomName(),
    'vocabulary' => $vocab,
    'description' => $this
      ->randomString(),
    'weight' => mt_rand(0, 10),
    'parent' => array(
      $term_parent->tid,
    ),
  ))
    ->save()
    ->value();
  $wrapper = entity_metadata_wrapper('taxonomy_term', $term);
  foreach ($wrapper as $key => $value) {
    $this
      ->assertValue($wrapper, $key);
  }

  // Test setting another parent using the full object.
  $wrapper->parent[] = $term_parent2;
  $this
    ->assertEqual($wrapper->parent[1]
    ->getIdentifier(), $term_parent2->tid, 'Term parent added.');
  $parents = $wrapper->parent
    ->value();
  $tids = $term_parent->tid . ':' . $term_parent2->tid;
  $this
    ->assertEqual($parents[0]->tid . ':' . $parents[1]->tid, $tids, 'Parents returned.');
  $this
    ->assertEqual(implode(':', $wrapper->parent
    ->value(array(
    'identifier' => TRUE,
  ))), $tids, 'Parent ids returned.');

  // Test vocabulary.
  foreach ($wrapper->vocabulary as $key => $value) {
    $this
      ->assertValue($wrapper->vocabulary, $key);
  }

  // Test field integration.
  $tags[LANGUAGE_NONE][0]['tid'] = $term->tid;
  $node = $this
    ->drupalCreateNode(array(
    'title' => 'foo',
    'type' => 'article',
    'field_tags' => $tags,
  ));
  $wrapper = entity_metadata_wrapper('node', $node);
  $this
    ->assertEqual($wrapper->field_tags[0]->name
    ->value(), $term->name, 'Get an associated tag of a node with the wrapper.');
  $wrapper->field_tags[1] = $term_parent;
  $tags = $wrapper->field_tags
    ->value();
  $this
    ->assertEqual($tags[1]->tid, $term_parent->tid, 'Associated a new tag with a node.');
  $this
    ->assertEqual($tags[0]->tid, $term->tid, 'Previsous set association kept.');

  // Test getting a list of identifiers.
  $tags = $wrapper->field_tags
    ->value(array(
    'identifier' => TRUE,
  ));
  $this
    ->assertEqual($tags, array(
    $term->tid,
    $term_parent->tid,
  ), 'List of referenced term identifiers returned.');

  // Test setting tags by using ids.
  $wrapper->field_tags
    ->set(array(
    2,
  ));
  $this
    ->assertEqual($wrapper->field_tags[0]->tid
    ->value(), 2, 'Specified tags by a list of term ids.');

  // Test unsetting all tags.
  $wrapper->field_tags = NULL;
  $this
    ->assertFalse($wrapper->field_tags
    ->value(), 'Unset all tags from a node.');

  // Test setting entity references to NULL.
  // Create a taxonomy term field for that purpose.
  $field_name = drupal_strtolower($this
    ->randomName() . '_field_name');
  $field = array(
    'field_name' => $field_name,
    'type' => 'taxonomy_term_reference',
    'cardinality' => 1,
  );
  $field = field_create_field($field);
  $field_id = $field['id'];
  $field_instance = array(
    'field_name' => $field_name,
    'entity_type' => 'node',
    'bundle' => 'article',
    'label' => $this
      ->randomName() . '_label',
    'description' => $this
      ->randomName() . '_description',
    'weight' => mt_rand(0, 127),
    'widget' => array(
      'type' => 'options_select',
      'label' => 'Test term field',
    ),
  );
  field_create_instance($field_instance);
  $term_field[LANGUAGE_NONE][0]['tid'] = $term->tid;
  $node = $this
    ->drupalCreateNode(array(
    'title' => 'foo',
    'type' => 'article',
    $field_name => $term_field,
  ));
  $wrapper = entity_metadata_wrapper('node', $node);
  $wrapper->{$field_name}
    ->set(NULL);
  $termref = $wrapper->{$field_name}
    ->value();
  $this
    ->assertNull($termref, 'Unset of a term reference successful.');
}