You are here

public function DynamicEntityReferenceTest::testDynamicEntityReference in Dynamic Entity Reference 7

Tests adding and editing values using dynamic entity reference.

File

./dynamic_entity_reference.test, line 128
Contains tests for the module.

Class

DynamicEntityReferenceTest
Ensures that Dynamic Entity References field works correctly.

Code

public function testDynamicEntityReference() {
  $this
    ->drupalLogin($this->adminUser);

  // Add a new dynamic entity reference field.
  $this
    ->drupalGet('admin/structure/types/manage/ponies/fields');
  $edit = array(
    'fields[_add_new_field][label]' => 'Foobar',
    'fields[_add_new_field][field_name]' => 'foobar',
    'fields[_add_new_field][type]' => 'dynamic_entity_reference',
    'fields[_add_new_field][widget_type]' => 'dynamic_entity_reference_default',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->drupalPost(NULL, array(), t('Save field settings'));
  $this
    ->drupalPost(NULL, array(
    'field[cardinality]' => '-1',
  ), t('Save settings'));
  $this
    ->assertRaw(t('Saved %name configuration', array(
    '%name' => 'Foobar',
  )));
  $this
    ->drupalPost('admin/structure/types/manage/ponies/display', array(
    'fields[field_foobar][type]' => 'dynamic_entity_reference_default',
  ), t('Save'));

  // Create some items to reference.
  $item1 = $this
    ->drupalCreateNode(array(
    'type' => 'ponies',
    'title' => 'item1',
    'status' => 1,
  ));
  $item2 = $this
    ->drupalCreateNode(array(
    'type' => 'ponies',
    'title' => 'item2',
    'status' => 1,
  ));

  // Test the new entity inherits default.
  $this
    ->drupalGet('node/add/ponies');
  $this
    ->assertField('field_foobar[und][0][target_id]', 'Found foobar field target id');
  $this
    ->assertField('field_foobar[und][0][target_type]', 'Found foobar field target type');

  // Add some extra dynamic entity reference fields.
  $this
    ->drupalPostAjax(NULL, array(), array(
    'field_foobar_add_more' => t('Add another item'),
  ), 'system/ajax', array(), array(), 'ponies-node-form');
  $this
    ->drupalPostAjax(NULL, array(), array(
    'field_foobar_add_more' => t('Add another item'),
  ), 'system/ajax', array(), array(), 'ponies-node-form');
  $edit = array(
    // Use a different user account (not the administrator) for this field
    // value so that its presence can be reliably asserted in the HTML output
    // (otherwise the assertion could pick up "Submitted by [admin]" text on
    // the node).
    'field_foobar[und][0][target_id]' => $this->anotherUser->name . ' (' . $this->anotherUser->uid . ')',
    'field_foobar[und][0][target_type]' => 'user',
    'field_foobar[und][1][target_id]' => 'item1 (' . $item1->nid . ')',
    'field_foobar[und][1][target_type]' => 'node',
    'field_foobar[und][2][target_id]' => 'item2 (' . $item2->nid . ')',
    'field_foobar[und][2][target_type]' => 'node',
    'title' => 'Barfoo',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $entities = entity_load('node', FALSE, array(
    'title' => 'Barfoo',
  ));
  $this
    ->assertEqual(1, count($entities), 'Entity was saved');
  $node = reset($entities);
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText('Barfoo');
  $this
    ->assertText($this->anotherUser->name);
  $this
    ->assertText('item1');
  $this
    ->assertText('item2');
  $this
    ->assertEqual(count($node->field_foobar[LANGUAGE_NONE]), 3, 'Three items in field');
  $this
    ->assertEqual($node->field_foobar[LANGUAGE_NONE][0]['target_type'], 'user');
  $this
    ->assertEqual($node->field_foobar[LANGUAGE_NONE][0]['target_id'], $this->anotherUser->uid);
  $this
    ->assertEqual($node->field_foobar[LANGUAGE_NONE][1]['target_type'], 'node');
  $this
    ->assertEqual($node->field_foobar[LANGUAGE_NONE][1]['target_id'], $item1->nid);
  $this
    ->assertEqual($node->field_foobar[LANGUAGE_NONE][2]['target_type'], 'node');
  $this
    ->assertEqual($node->field_foobar[LANGUAGE_NONE][2]['target_id'], $item2->nid);
  $this
    ->drupalGet('node/' . $node->nid . '/edit');
  $edit = array(
    'title' => 'Bazbar',
    // Remove one child.
    'field_foobar[und][2][target_id]' => '',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText('Bazbar');

  // Reload entity.
  $node = node_load($node->nid, NULL, TRUE);
  $this
    ->assertEqual(count($node->field_foobar[LANGUAGE_NONE]), 2, 'Two values in field');
}