You are here

public function RelationAddTestCase::testRelationAddCRU in Relation add 7

Tests relation add field.

@codingStandardsIgnoreStart

File

tests/relation_add.test, line 87
Tests for Relation Add module.

Class

RelationAddTestCase
Tests Relation Add.

Code

public function testRelationAddCRU() {

  // @codingStandardsIgnoreEnd
  $node = array(
    'type' => 'article',
    'promote' => 1,
    'title' => 'Boy',
  );
  $node['reladd']['und'][0] = array(
    'relation_type' => 'symmetric',
    'endpoints' => array(
      LANGUAGE_NONE => array(
        0 => array(
          'entity_type' => 'node',
          'entity_id' => $this->node1->nid,
        ),
      ),
    ),
  );

  // Create new node and relation.
  $node = $this
    ->drupalCreateNode($node);
  $reladd_field_items = field_get_items('node', $node, 'reladd');
  $found_this_endpoint = FALSE;
  $found_target_endpoint = FALSE;
  foreach ($reladd_field_items[0]['endpoints'][LANGUAGE_NONE] as $endpoint) {
    if ($endpoint['entity_type'] == 'node' && $endpoint['entity_id'] == $node->nid) {
      $found_this_endpoint = TRUE;
    }
    if ($endpoint['entity_type'] == 'node' && $endpoint['entity_id'] == $this->node1->nid) {
      $found_target_endpoint = TRUE;
    }
  }
  $this
    ->assertTrue($found_this_endpoint && $found_target_endpoint, 'Relation created.');

  // Update node relation.
  $node->reladd[LANGUAGE_NONE][0]['endpoints'][LANGUAGE_NONE][0]['entity_id'] = $this->node4->nid;
  node_save($node);
  $relation = relation_load($node->reladd[LANGUAGE_NONE][0]['rid'], NULL, TRUE);
  $this
    ->assertTrue(!empty($relation), 'Relation load successfull.');
  $found_this_endpoint = FALSE;
  $found_target_endpoint = FALSE;
  if (!empty($relation)) {
    foreach ($relation->endpoints[LANGUAGE_NONE] as $endpoint) {
      if ($endpoint['entity_type'] == 'node' && $endpoint['entity_id'] == $node->nid) {
        $found_this_endpoint = TRUE;
      }
      if ($endpoint['entity_type'] == 'node' && $endpoint['entity_id'] == $this->node4->nid) {
        $found_target_endpoint = TRUE;
      }
    }
    $this
      ->assertTrue($found_this_endpoint && $found_target_endpoint, 'Relation update successfull.');
  }
}