You are here

public function MultipleValuesWidgetTest::testReferencingExistingEntities in Inline Entity Form 7

Tests if referencing existing entities work.

File

tests/multiple_values_widget.test, line 350

Class

MultipleValuesWidgetTest
IEF multiple values field widget tests.

Code

public function testReferencingExistingEntities() {

  // Allow addition of existing nodes.
  $this
    ->setAllowExisting(TRUE);

  // Create three ief_reference_type entities.
  $referenceNodes = $this
    ->createReferenceContent(3);

  // Create a node for every bundle available.
  $bundle_nodes = $this
    ->createNodeForEveryBundle();

  // Create ief_test_multiple node with first ief_reference_type node and first
  // node from bundle nodes.
  $this
    ->drupalCreateNode(array(
    'type' => 'ief_test_multiple',
    'title' => 'Some title',
    'multi' => array(
      1,
    ),
    'all_bundles' => key($bundle_nodes),
  ));

  // Remove first node since we already added it.
  unset($bundle_nodes[key($bundle_nodes)]);
  $parent_node = $this
    ->drupalGetNodeByTitle('Some title', TRUE);

  // Add remaining existing reference nodes.
  $this
    ->drupalGet('node/' . $parent_node->nid . '/edit');
  for ($i = 2; $i <= 3; $i++) {
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @value="Add existing node" and @id="edit-field-multiple-nodes-und-actions-ief-add-existing"]'));
    $this
      ->assertResponse(200, 'Opening reference form was successful.');
    $title = 'Some reference ' . $i;
    $edit = array(
      'field_multiple_nodes[und][form][entity_id]' => $title . ' (' . $referenceNodes[$title] . ')',
    );
    $this
      ->drupalPostAjax(NULL, $edit, $this
      ->getButtonName('//input[@type="submit" and @id="edit-field-multiple-nodes-und-form-actions-ief-reference-save"]'));
    $this
      ->assertResponse(200, 'Adding new referenced entity was successful.');
  }

  // Add all remaining nodes from all bundles.
  foreach ($bundle_nodes as $id => $title) {
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @value="Add existing node" and @id="edit-field-all-bundles-und-actions-ief-add-existing"]'));
    $this
      ->assertResponse(200, 'Opening reference form was successful.');
    $edit = array(
      'field_all_bundles[und][form][entity_id]' => $title . ' (' . $id . ')',
    );
    $this
      ->drupalPostAjax(NULL, $edit, $this
      ->getButtonName('//input[@type="submit" and @id="edit-field-all-bundles-und-form-actions-ief-reference-save"]'));
    $this
      ->assertResponse(200, 'Adding new referenced entity was successful.');
  }

  // Save the node.
  $this
    ->drupalPost(NULL, array(), t('Save'));
  $this
    ->assertResponse(200, 'Saving parent for was successful.');

  // Check if entities are referenced.
  $this
    ->drupalGet('node/' . $parent_node->nid . '/edit');
  for ($i = 2; $i <= 3; $i++) {
    $cell = $this
      ->xpath('//table[@id="ief-entity-table-edit-field-multiple-nodes-und-entities"]/tbody/tr[' . ($i - 1) . ']/td[@class="inline-entity-form-node-title"]');
    $this
      ->assertTrue($cell[0] == 'Some reference ' . $i, 'Found reference node title "Some reference ' . $i . '" in the IEF table.');
  }

  // Check if all remaining nodes from all bundles are referenced.
  $count = 1;
  foreach ($bundle_nodes as $id => $title) {
    $cell = $this
      ->xpath('//table[@id="ief-entity-table-edit-field-all-bundles-und-entities"]/tbody/tr[' . $count . ']/td[@class="inline-entity-form-node-title"]');
    $this
      ->assertTrue($cell[0] == $title, 'Found reference node title "' . $title . '" in the IEF table.');
    $count++;
  }
}