function RulesIntegrationTestCase::testEntityIntegration

Same name in other branches
  1. 8.x-3.x d7-tests/rules_integration_test_case.test \RulesIntegrationTestCase::testEntityIntegration()

Tests entity related integration.

File

tests/rules.test, line 1573

Class

RulesIntegrationTestCase
Tests provided module integration.

Code

public function testEntityIntegration() {
    global $user;
    $page = $this->drupalCreateNode(array(
        'type' => 'page',
    ));
    $article = $this->drupalCreateNode(array(
        'type' => 'article',
    ));
    $result = rules_condition('entity_field_access')->execute(entity_metadata_wrapper('node', $article), 'field_tags');
    $this->assertTrue($result);
    // Test entity_is_of_bundle condition.
    $result = rules_condition('entity_is_of_bundle', array(
        'type' => 'node',
        'bundle' => array(
            'article',
        ),
    ))->execute(entity_metadata_wrapper('node', $page));
    $this->assertFalse($result, 'Entity is of bundle condition has not been met.');
    $result = rules_condition('entity_is_of_bundle', array(
        'type' => 'node',
        'bundle' => array(
            'article',
        ),
    ))->execute(entity_metadata_wrapper('node', $article));
    $this->assertTrue($result, 'Entity is of bundle condition has been met.');
    // Also test a full rule so the integrity check must work.
    $term_wrapped = entity_property_values_create_entity('taxonomy_term', array(
        'name' => $this->randomName(),
        'vocabulary' => 1,
    ))
        ->save();
    $rule = rule(array(
        'node' => array(
            'type' => 'node',
        ),
    ));
    $rule->condition('entity_is_of_bundle', array(
        'entity:select' => 'node',
        'bundle' => array(
            'article',
        ),
    ));
    $rule->action('data_set', array(
        'data:select' => 'node:field_tags',
        'value' => array(
            $term_wrapped->getIdentifier(),
        ),
    ));
    $rule->integrityCheck();
    $rule->execute($article);
    $this->assertEqual($term_wrapped->getIdentifier(), $article->field_tags[LANGUAGE_NONE][0]['tid'], 'Entity is of bundle condition has been met.');
    // Test again using an entity variable.
    $article = $this->drupalCreateNode(array(
        'type' => 'article',
    ));
    $rule = rule(array(
        'entity' => array(
            'type' => 'entity',
        ),
    ));
    $rule->condition('entity_is_of_bundle', array(
        'entity:select' => 'entity',
        'type' => 'node',
        'bundle' => array(
            'article',
        ),
    ));
    $rule->action('data_set', array(
        'data:select' => 'entity:field_tags',
        'value' => array(
            $term_wrapped->getIdentifier(),
        ),
    ));
    $rule->integrityCheck();
    $rule->execute(entity_metadata_wrapper('node', $article));
    $this->assertEqual($term_wrapped->getIdentifier(), $article->field_tags[LANGUAGE_NONE][0]['tid'], 'Entity is of bundle condition has been met.');
    // Test CRUD actions.
    $action = rules_action('entity_create', array(
        'type' => 'node',
        'param_type' => 'page',
        'param_title' => 'foo',
        'param_author' => $GLOBALS['user'],
    ));
    $action->access();
    $action->execute();
    $text = RulesLog::logger()->render();
    $pos = strpos($text, RulesTestCase::t('Added the provided variable %entity_created of type %node', array(
        'entity_created',
        'node',
    )));
    $pos = $pos !== FALSE ? strpos($text, RulesTestCase::t('Saved %entity_created of type %node.', array(
        'entity_created',
        'node',
    )), $pos) : FALSE;
    $this->assertTrue($pos !== FALSE, 'Data has been created and saved.');
    $node = $this->drupalCreateNode(array(
        'type' => 'page',
        'sticky' => 0,
        'status' => 0,
    ));
    $rule = rule();
    $rule->action('entity_fetch', array(
        'type' => 'node',
        'id' => $node->nid,
        'entity_fetched:var' => 'node',
    ));
    $rule->action('entity_save', array(
        'data:select' => 'node',
        'immediate' => TRUE,
    ));
    $rule->action('entity_delete', array(
        'data:select' => 'node',
    ));
    $rule->access();
    $rule->integrityCheck()
        ->execute();
    $text = RulesLog::logger()->render();
    $pos = strpos($text, RulesTestCase::t('Evaluating the action %entity_fetch.', array(
        'entity_fetch' => 'Fetch entity by id',
    )));
    $pos = $pos !== FALSE ? strpos($text, RulesTestCase::t('Added the provided variable %node of type %node', array(
        'node',
    )), $pos) : FALSE;
    $pos = $pos !== FALSE ? strpos($text, RulesTestCase::t('Saved %node of type %node.', array(
        'node',
    )), $pos) : FALSE;
    $pos = $pos !== FALSE ? strpos($text, RulesTestCase::t('Evaluating the action %entity_delete.', array(
        'entity_delete' => 'Delete entity',
    )), $pos) : FALSE;
    $this->assertTrue($pos !== FALSE, 'Data has been fetched, saved and deleted.');
    // debug(RulesLog::logger()->render());
    $node = entity_property_values_create_entity('node', array(
        'type' => 'article',
        'author' => $user,
        'title' => 'foo',
    ))->value();
    $term_wrapped = entity_property_values_create_entity('taxonomy_term', array(
        'name' => $this->randomName(),
        'vocabulary' => 1,
    ))
        ->save();
    // Test asserting the field and using it afterwards.
    $rule = rule(array(
        'node' => array(
            'type' => 'node',
        ),
    ));
    $rule->condition('entity_has_field', array(
        'entity:select' => 'node',
        'field' => 'field_tags',
    ));
    $rule->condition('entity_is_new', array(
        'entity:select' => 'node',
    ));
    $rule->action('list_add', array(
        'list:select' => 'node:field-tags',
        'item' => $term_wrapped,
    ));
    $rule->integrityCheck();
    $rule->execute($node);
    $tid = $term_wrapped->getIdentifier();
    $this->assertEqual(array_values($node->field_tags[LANGUAGE_NONE]), array(
        0 => array(
            'tid' => $tid,
        ),
    ), 'Entity has field conditions evaluted.');
    // Test loading a non-node entity.
    $action = rules_action('entity_fetch', array(
        'type' => 'taxonomy_term',
        'id' => $tid,
    ));
    list($term) = $action->execute();
    $this->assertEqual($term->tid, $tid, 'Fetched a taxonomy term using "entity_fetch".');
    // Test the entity is of type condition.
    $rule = rule(array(
        'entity' => array(
            'type' => 'entity',
            'label' => 'entity',
        ),
    ));
    $rule->condition('entity_is_of_type', array(
        'type' => 'node',
    ));
    $rule->action('data_set', array(
        'data:select' => 'entity:title',
        'value' => 'bar',
    ));
    $rule->integrityCheck();
    $rule->execute(entity_metadata_wrapper('node', $node));
    $this->assertEqual(entity_metadata_wrapper('node', $node->nid)->title
        ->value(), 'bar', 'Entity is of type condition correctly asserts the entity type.');
    // Test the entity_query action.
    $node = $this->drupalCreateNode(array(
        'type' => 'page',
        'title' => 'foo2',
    ));
    $rule = rule();
    $rule->action('entity_query', array(
        'type' => 'node',
        'property' => 'title',
        'value' => 'foo2',
    ))
        ->action('data_set', array(
        'data:select' => 'entity_fetched:0:title',
        'value' => 'bar',
    ));
    $rule->access();
    $rule->integrityCheck();
    $rule->execute();
    $node = node_load($node->nid);
    $this->assertEqual('bar', $node->title, 'Fetched a node by title and modified it.');
    RulesLog::logger()->checkLog();
}