You are here

public function FieldCollectionRulesIntegrationTestCase::testUsageDuringCreation in Field collection 7

Test using field collection items via the host while they are being created.

File

./field_collection.test, line 541
Tests for field_collections.

Class

FieldCollectionRulesIntegrationTestCase
Test using field collection with Rules.

Code

public function testUsageDuringCreation() {

  // Test using a single-cardinality field collection.
  $this
    ->createFields(1);
  $node = $this
    ->drupalCreateNode(array(
    'type' => 'article',
  ));
  $entity = entity_create('field_collection_item', array(
    'field_name' => $this->field_name,
  ));
  $entity
    ->setHostEntity('node', $node);

  // Now the field collection is linked to the host, but not yet saved.
  // Test using the wrapper on it.
  $wrapper = entity_metadata_wrapper('node', $node);
  $wrapper
    ->get($this->field_name)->field_text
    ->set('foo');
  $this
    ->assertEqual($entity->field_text[LANGUAGE_NONE][0]['value'], 'foo', 'Field collection item used during creation via the wrapper.');

  // Now test it via Rules, which should save our changes.
  $set = rules_action_set(array(
    'node' => array(
      'type' => 'node',
      'bundle' => 'article',
    ),
  ));
  $set
    ->action('data_set', array(
    'data:select' => 'node:' . $this->field_name . ':field-text',
    'value' => 'bar',
  ));
  $set
    ->execute($node);
  $this
    ->assertEqual($entity->field_text[LANGUAGE_NONE][0]['value'], 'bar', 'Field collection item used during creation via Rules.');
  $this
    ->assertTrue(!empty($entity->item_id) && !empty($entity->revision_id), 'Field collection item has been saved by Rules and the host entity.');
  RulesLog::logger()
    ->checkLog();
}