You are here

function RelationRulesTestCase::testRelationCreateRelation in Relation 7

Test to create a relation in different ways by executing a rule.

File

tests/relation.rules.test, line 26
Tests for Rules support.

Class

RelationRulesTestCase
Functional test of Relation's integration with Rules.

Code

function testRelationCreateRelation() {
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $user = $this
    ->drupalCreateUser();
  $endpoints = array(
    entity_metadata_wrapper('node', $node),
    entity_metadata_wrapper('user', $user),
  );

  // Test to create a relation by setting two endpoints and saving it. This
  // primarily tests the endpoint property set callback.
  $rule = rule();
  $rule
    ->action('entity_create', array(
    'type' => 'relation',
    'param_relation_type' => $this->relation_types['symmetric']['relation_type'],
    'param_author:select' => 'site:current-user',
    'param_endpoints' => $endpoints,
    'entity_created:var' => 'relation',
  ));
  $rule
    ->execute();

  // There is no way for the rule to return the created relation. So get the
  // last inserted id, which should be the relation we are looking for.
  $rid = db_query('SELECT MAX(rid) FROM {relation}')
    ->fetchField();

  // If all went well, we should now have a relation with correct endpoints.
  $relation = relation_load($rid);
  $correct = $relation->endpoints[LANGUAGE_NONE][0]['entity_id'] == $node->nid && $relation->endpoints[LANGUAGE_NONE][1]['entity_id'] == $user->uid;
  $this
    ->assertTrue($correct, 'Relation was created by setting two endpoints from rule context and saving it.');

  // Test to create a relation by first fetching the endpoints from the
  // last relation and then settings them in a new one. This primarily tests
  // the endpoint property get callback.
  $rule = rule();

  // This will load a relation into the context of the rule.
  $rule
    ->action('entity_fetch', array(
    'type' => 'relation',
    'id' => $rid,
  ));
  $rule
    ->action('entity_create', array(
    'type' => 'relation',
    'param_relation_type' => $this->relation_types['symmetric']['relation_type'],
    'param_author:select' => 'site:current-user',
    // Now, load the endpoints from the fetched relation, into a new relation.
    'param_endpoints:select' => 'entity-fetched:endpoints',
    'entity_created:var' => 'relation',
  ));
  $rule
    ->execute();

  // There is no way for the rule to return the created relation. So get the
  // last inserted id, which should be the relation we are looking for.
  $rid = db_query('SELECT MAX(rid) FROM {relation}')
    ->fetchField();
  $relation = relation_load($rid);

  // The $node and the $user should be the the same as in the last test, since
  // we fetched the endpoits from that relation.
  $correct = $relation->endpoints[LANGUAGE_NONE][0]['entity_id'] == $node->nid && $relation->endpoints[LANGUAGE_NONE][1]['entity_id'] == $user->uid;
  $this
    ->assertTrue($correct, 'Relation was created by fetching endpoints from another relation and saving it.');
}