You are here

multiple_values_widget.test in Inline Entity Form 7

File

tests/multiple_values_widget.test
View source
<?php

/**
 * IEF multiple values field widget tests.
 *
 * @group inline_entity_form
 */
class MultipleValuesWidgetTest extends InlineEntityFormTestBase {

  /**
   * URL to add new content.
   *
   * @var string
   */
  protected $formContentAddUrl;
  public static function getInfo() {
    return array(
      'name' => 'Multiple values widget',
      'description' => 'Tests the "Inline Entity Form" multiple values widget.',
      'group' => 'Inline entity form',
    );
  }
  protected function setUp() {
    $modules = $modules = array(
      'inline_entity_form_test',
      'field',
      'field_ui',
    );
    parent::setUp($modules);
    $this->administrator_user = $this
      ->drupalCreateUser(array(
      'create ief_reference_type content',
      'edit any ief_reference_type content',
      'delete any ief_reference_type content',
      'create ief_test_multiple content',
      'edit any ief_test_multiple content',
      'delete any ief_test_multiple content',
      'edit any ief_test_nested1 content',
      'edit any ief_test_nested2 content',
      'edit any ief_test_nested3 content',
      'view own unpublished content',
      'administer content types',
      'administer nodes',
      'administer fields',
    ));
    $this
      ->drupalLogin($this->administrator_user);
    $this->formContentAddUrl = 'node/add/ief-test-multiple';
  }

  /**
   * Tests if form behaves correctly when field is empty.
   */
  public function testEmptyFieldIEF() {

    // Don't allow addition of existing nodes.
    $this
      ->setAllowExisting(FALSE);
    $this
      ->drupalGet($this->formContentAddUrl);
    $this
      ->assertFieldByName('field_multiple_nodes[und][form][title]', NULL, 'Title field on inline form exists.');
    $this
      ->assertFieldByName('field_multiple_nodes[und][form][field_first_name][und][0][value]', NULL, 'First name field on inline form exists.');
    $this
      ->assertFieldByName('field_multiple_nodes[und][form][field_last_name][und][0][value]', NULL, 'Last name field on inline form exists.');
    $this
      ->assertFieldByXpath('//input[@type="submit" and @value="Create node"]', NULL, 'Found "Create node" submit button');

    // Allow addition of existing nodes.
    $this
      ->setAllowExisting(TRUE);
    $this
      ->drupalGet($this->formContentAddUrl);
    $this
      ->assertNoFieldByName('field_multiple_nodes[und][form][title]]', NULL, 'Title field does not appear.');
    $this
      ->assertNoFieldByName('field_multiple_nodes[und][form][field_first_name][und][0][value]', NULL, 'First name field does not appear.');
    $this
      ->assertNoFieldByName('field_multiple_nodes[und][form][field_last_name][und][0][value]', NULL, 'Last name field does not appear.');
    $this
      ->assertFieldByXpath('//input[@type="submit" and @value="Add new node"]', NULL, 'Found "Add new node" submit button');
    $this
      ->assertFieldByXpath('//input[@type="submit" and @value="Add existing node"]', NULL, 'Found "Add existing node" submit button');

    // Now submit 'Add new node' button.
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @value="Add new node" and @id="edit-field-multiple-nodes-und-actions-ief-add"]'));
    $this
      ->assertFieldByName('field_multiple_nodes[und][form][title]', NULL, 'Title field on inline form exists.');
    $this
      ->assertFieldByName('field_multiple_nodes[und][form][field_first_name][und][0][value]', NULL, 'First name field on inline form exists.');
    $this
      ->assertFieldByName('field_multiple_nodes[und][form][field_last_name][und][0][value]', NULL, 'Second name field on inline form exists.');
    $this
      ->assertFieldByXpath('//input[@type="submit" and @value="Create node"]', NULL, 'Found "Create node" submit button');
    $this
      ->assertFieldByXpath('//input[@type="submit" and @value="Cancel"]', NULL, 'Found "Cancel" submit button');

    // Now submit 'Add Existing node' button.
    $this
      ->drupalGet($this->formContentAddUrl);
    $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
      ->assertFieldByName('field_multiple_nodes[und][form][entity_id]', NULL, 'Existing entity reference autocomplete field found.');
    $this
      ->assertFieldByXpath('//input[@type="submit" and @value="Add node"]', NULL, 'Found "Add node" submit button');
    $this
      ->assertFieldByXpath('//input[@type="submit" and @value="Cancel"]', NULL, 'Found "Cancel" submit button');
  }

  /**
   * Tests creation of entities.
   */
  public function testEntityCreation() {

    // Allow addition of existing nodes.
    $this
      ->setAllowExisting(TRUE);
    $this
      ->drupalGet($this->formContentAddUrl);
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @value="Add new node" and @id="edit-field-multiple-nodes-und-actions-ief-add"]'));
    $this
      ->assertResponse(200, 'Opening new inline form was successful.');
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @value="Create node" and @id="edit-field-multiple-nodes-und-form-actions-ief-add-save"]'));
    $this
      ->assertResponse(200, 'Submitting empty form was successful.');
    $this
      ->assertText('First name field is required.', 'Validation failed for empty "First name" field.');
    $this
      ->assertText('Last name field is required.', 'Validation failed for empty "Last name" field.');
    $this
      ->assertText('Title field is required.', 'Validation failed for empty "Title" field.');

    // Create ief_reference_type node in IEF.
    $this
      ->drupalGet($this->formContentAddUrl);
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @value="Add new node" and @id="edit-field-multiple-nodes-und-actions-ief-add"]'));
    $this
      ->assertResponse(200, 'Opening new inline form was successful.');
    $edit = array(
      'field_multiple_nodes[und][form][title]' => 'Some reference',
      'field_multiple_nodes[und][form][field_first_name][und][0][value]' => 'John',
      'field_multiple_nodes[und][form][field_last_name][und][0][value]' => 'Doe',
    );
    $this
      ->drupalPostAjax(NULL, $edit, $this
      ->getButtonName('//input[@type="submit" and @value="Create node" and @id="edit-field-multiple-nodes-und-form-actions-ief-add-save"]'));
    $this
      ->assertResponse(200, 'Creating node via inline form was successful.');

    // Tests if correct fields appear in the table.
    $this
      ->assertTrue((bool) $this
      ->xpath('//td[@class="inline-entity-form-node-title" and contains(.,"Some reference")]'), 'Node title field appears in the table');
    $this
      ->assertTrue((bool) $this
      ->xpath('//td[@class="inline-entity-form-node-status" and contains(.,"Published")]'), 'Node status field appears in the table');

    // Tests if edit and remove buttons appear.
    $this
      ->assertTrue((bool) $this
      ->xpath('//input[@type="submit" and @value="Edit"]'), 'Edit button appears in the table.');
    $this
      ->assertTrue((bool) $this
      ->xpath('//input[@type="submit" and @value="Remove"]'), 'Remove button appears in the table.');

    // Test edit functionality.
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @value="Edit"]'));
    $edit = array(
      'field_multiple_nodes[und][entities][0][form][title]' => 'Some changed reference',
    );
    $this
      ->drupalPostAjax(NULL, $edit, $this
      ->getButtonName('//input[@type="submit" and @value="Update node"]'));
    $this
      ->assertTrue((bool) $this
      ->xpath('//td[@class="inline-entity-form-node-title" and contains(.,"Some changed reference")]'), 'Node title field appears in the table');
    $this
      ->assertTrue((bool) $this
      ->xpath('//td[@class="inline-entity-form-node-status" and contains(.,"Published")]'), 'Node status field appears in the table');

    // Make sure unrelated AJAX submit doesn't save the referenced entity.
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @value="Upload"]'));
    $node = $this
      ->drupalGetNodeByTitle('Some changed reference');
    $this
      ->assertFalse($node, 'Referenced node was not saved during unrelated AJAX submit.');

    // Create ief_test_multiple node.
    $edit = array(
      'title' => 'Some title',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->assertResponse(200, 'Saving parent entity was successful.');

    // Checks values of created entities.
    $node = $this
      ->drupalGetNodeByTitle('Some changed reference');
    $this
      ->assertTrue($node, 'Created ief_reference_type node ' . $node->title);
    $this
      ->assertTrue($node->field_first_name[LANGUAGE_NONE][0]['value'] == 'John', 'First name in reference node set to John');
    $this
      ->assertTrue($node->field_last_name[LANGUAGE_NONE][0]['value'] == 'Doe', 'Last name in reference node set to Doe');
    $parent_node = $this
      ->drupalGetNodeByTitle('Some title');
    $this
      ->assertTrue($parent_node, 'Created ief_test_multiple node ' . $parent_node->title);
    $this
      ->assertTrue($parent_node->field_multiple_nodes[LANGUAGE_NONE][0]['target_id'] == $node->nid, 'Refererence node id set to ' . $node->nid);
  }

  /**
   * Tests the entity creation with different bundles nested in each other.
   *
   * ief_test_nested1 -> ief_test_nested2 -> ief_test_nested3
   */
  public function testNestedEntityCreationWithDifferentBundlesAjaxSubmit() {
    $required_possibilities = array(
      FALSE,
      TRUE,
    );
    foreach ($required_possibilities as $required) {
      $this
        ->setupNestedMultipleForm($required);
      $nested3_title = 'nested3 title steps ' . ($required ? 'required' : 'not required');
      $nested2_title = 'nested2 title steps ' . ($required ? 'required' : 'not required');
      $nested1_title = 'nested1 title steps ' . ($required ? 'required' : 'not required');
      $edit = array(
        'field_test_ref_nested1[und][form][field_test_ref_nested2][und][form][title]' => $nested3_title,
      );
      $this
        ->drupalPostAjax(NULL, $edit, $this
        ->getButtonName('//input[@type="submit" and @value="Create node 3"]'));
      $this
        ->assertText($nested3_title, 'Title of second nested node found.');
      $this
        ->assertFalse($this
        ->drupalGetNodeByTitle($nested3_title), 'Second nested entity is not saved yet.');
      $edit = array(
        'field_test_ref_nested1[und][form][title]' => $nested2_title,
      );
      $this
        ->drupalPostAjax(NULL, $edit, $this
        ->getButtonName('//input[@type="submit" and @value="Create node 2"]'));
      $this
        ->assertText($nested2_title, 'Title of first nested node found.');
      $this
        ->assertFalse($this
        ->drupalGetNodeByTitle($nested2_title), 'First nested entity is not saved yet.');
      $edit = array(
        'title' => $nested1_title,
      );
      $this
        ->drupalPost(NULL, $edit, t('Save'));
      $nested1_node = $this
        ->drupalGetNodeByTitle($nested1_title);
      $this
        ->assertEqual($nested1_title, $nested1_node->title, "First node's title looks correct.");
      $this
        ->assertEqual('ief_test_nested1', $nested1_node->type, "First node's type looks correct.");
      if ($this
        ->assertNotNull($nested1_node->field_test_ref_nested1[LANGUAGE_NONE][0]['target_id'], 'Second node was created.')) {
        $creatednested2 = node_load($nested1_node->field_test_ref_nested1[LANGUAGE_NONE][0]['target_id']);
        $this
          ->assertEqual($nested2_title, $creatednested2->title, "Second node's title looks correct.");
        $this
          ->assertEqual('ief_test_nested2', $creatednested2->type, "Second node's type looks correct.");
        if ($this
          ->assertNotNull($creatednested2->field_test_ref_nested2[LANGUAGE_NONE][0]['target_id'], 'Third node was created')) {
          $creatednested3 = node_load($creatednested2->field_test_ref_nested2[LANGUAGE_NONE][0]['target_id']);
          $this
            ->assertEqual($nested3_title, $creatednested3->title, "Third node's title looks correct.");
          $this
            ->assertEqual('ief_test_nested3', $creatednested3->type, "Third node's type looks correct.");
          $this
            ->checkNestedEntityEditing($nested1_node, TRUE);
        }
      }
    }
  }

  /**
   * Tests the entity creation with different bundles nested in each other.
   *
   * ief_test_nested1 -> ief_test_nested2 -> ief_test_nested3
   */
  public function testNestedEntityCreationWithDifferentBundlesNoAjaxSubmit() {
    $required_possibilities = array(
      FALSE,
      TRUE,
    );
    foreach ($required_possibilities as $required) {
      $this
        ->setupNestedMultipleForm($required);
      $nested3_title = 'nested3 title single ' . ($required ? 'required' : 'not required');
      $nested2_title = 'nested2 title single ' . ($required ? 'required' : 'not required');
      $nested1_title = 'nested1 title single ' . ($required ? 'required' : 'not required');
      $edit = array(
        'title' => $nested1_title,
        'field_test_ref_nested1[und][form][title]' => $nested2_title,
        'field_test_ref_nested1[und][form][field_test_ref_nested2][und][form][title]' => $nested3_title,
      );
      $this
        ->drupalPost(NULL, $edit, t('Save'));
      $nested1_node = $this
        ->drupalGetNodeByTitle($nested1_title);
      $this
        ->assertEqual($nested1_title, $nested1_node->title, "First node's title looks correct.");
      $this
        ->assertEqual('ief_test_nested1', $nested1_node->type, "First node's type looks correct.");
      $creatednested2 = node_load($nested1_node->field_test_ref_nested1[LANGUAGE_NONE][0]['target_id']);
      $this
        ->assertEqual($nested2_title, $creatednested2->title, "Second node's title looks correct.");
      $this
        ->assertEqual('ief_test_nested2', $creatednested2->type, "Second node's type looks correct.");
      $creatednested3 = node_load($creatednested2->field_test_ref_nested2[LANGUAGE_NONE][0]['target_id']);
      $this
        ->assertEqual($nested3_title, $creatednested3->title, "Third node's title looks correct.");
      $this
        ->assertEqual('ief_test_nested3', $creatednested3->type, "Third node's type looks correct.");
      $this
        ->checkNestedEntityEditing($nested1_node, FALSE);
    }
  }

  /**
   * Tests if editing and removing entities work.
   */
  public function testEntityEditingAndRemoving() {

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

    // Create three ief_reference_type entities.
    $referenceNodes = $this
      ->createReferenceContent(3);
    $field_multiple_nodes = array();
    foreach ($referenceNodes as $nid) {
      $field_multiple_nodes[LANGUAGE_NONE][] = array(
        'target_id' => $nid,
      );
    }
    $this
      ->drupalCreateNode(array(
      'type' => 'ief_test_multiple',
      'title' => 'Some title',
      'field_multiple_nodes' => $field_multiple_nodes,
    ));
    $parent_node = $this
      ->drupalGetNodeByTitle('Some title');

    // Edit the second entity.
    $this
      ->drupalGet('node/' . $parent_node->nid . '/edit');
    $cell = $this
      ->xpath('//table[@id="ief-entity-table-edit-field-multiple-nodes-und-entities"]/tbody/tr[@class="ief-row-entity draggable even"]/td[@class="inline-entity-form-node-title"]');
    $title = (string) $cell[0];
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @id="edit-field-multiple-nodes-und-entities-1-actions-ief-entity-edit"]'));
    $this
      ->assertResponse(200, 'Opening inline edit form was successful.');
    $edit = array(
      'field_multiple_nodes[und][entities][1][form][field_first_name][und][0][value]' => 'John',
      'field_multiple_nodes[und][entities][1][form][field_last_name][und][0][value]' => 'Doe',
    );
    $this
      ->drupalPostAjax(NULL, $edit, $this
      ->getButtonName('//input[@type="submit" and @id="edit-field-multiple-nodes-und-entities-1-form-actions-ief-edit-save"]'));
    $this
      ->assertResponse(200, 'Saving inline edit form was successful.');

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

    // Checks values of changed entities.
    $node = $this
      ->drupalGetNodeByTitle($title, TRUE);
    $this
      ->assertTrue($node->field_first_name[LANGUAGE_NONE][0]['value'] == 'John', 'First name in reference node changed to John');
    $this
      ->assertTrue($node->field_last_name[LANGUAGE_NONE][0]['value'] == 'Doe', 'Last name in reference node changed to Doe');

    // Delete the last entity.
    $this
      ->drupalGet('node/' . $parent_node->nid . '/edit');
    $cell = $this
      ->xpath('//table[@id="ief-entity-table-edit-field-multiple-nodes-und-entities"]/tbody//tr[3]//td[@class="inline-entity-form-node-title"]');
    $title = (string) $cell[0];
    $this
      ->drupalPost(NULL, array(), 'Remove');
    $this
      ->assertResponse(200, 'Opening inline remove confirm form was successful.');
    $this
      ->assertText('Are you sure you want to remove', 'Remove warning message is displayed.');
    $edit = array(
      'field_multiple_nodes[und][entities][2][form][delete]' => '1',
    );
    $this
      ->drupalPost(NULL, $edit, 'Remove');
    $this
      ->assertResponse(200, 'Removing inline entity was successful.');
    $this
      ->assertNoText($title, 'Deleted inline entity is not present on the page.');

    // Save the ief_test_multiple node.
    $this
      ->drupalPost(NULL, array(), t('Save'));
    $this
      ->assertResponse(200, 'Saving parent node was successful.');
    $deleted_node = $this
      ->drupalGetNodeByTitle($title);
    $this
      ->assertTrue(empty($deleted_node), 'The inline entity was deleted from the site.');

    // Checks that entity does nor appear in IEF.
    $this
      ->drupalGet('node/' . $parent_node->nid . '/edit');
    $this
      ->assertNoText($title, 'Deleted inline entity is not present on the page after saving parent.');

    // Delete the last entity reference only, don't delete the node. The last
    // entity now is second referenced entity because we already deleted one
    // in previous step.
    $this
      ->drupalGet('node/' . $parent_node->nid . '/edit');
    $cell = $this
      ->xpath('//table[@id="ief-entity-table-edit-field-multiple-nodes-und-entities"]/tbody//tr[2]//td[@class="inline-entity-form-node-title"]');
    $title = (string) $cell[0];
    $this
      ->drupalPost(NULL, array(), 'Remove');
    $this
      ->assertResponse(200, 'Opening inline remove confirm form was successful.');
    $this
      ->drupalPost(NULL, array(), 'Remove');
    $this
      ->assertResponse(200, 'Removing inline entity was successful.');

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

    // Checks that entity does nor appear in IEF.
    $this
      ->drupalGet('node/' . $parent_node->nid . '/edit');
    $this
      ->assertNoText($title, 'Deleted inline entity is not present on the page after saving parent.');

    // Checks that entity is not deleted.
    $node = $this
      ->drupalGetNodeByTitle($title, TRUE);
    $this
      ->assertTrue($node, 'Reference node not deleted');
  }

  /**
   * Tests if referencing existing entities work.
   */
  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++;
    }
  }

  /**
   * Tests if a referenced content can be edited while the referenced content is
   * newer than the referencing parent node.
   */
  public function testEditedInlineEntityValidation() {
    $this
      ->setAllowExisting(TRUE);

    // Create referenced content.
    $referenced_nodes = $this
      ->createReferenceContent(1);
    $field_multiple_nodes = array();
    foreach ($referenced_nodes as $nid) {
      $field_multiple_nodes[LANGUAGE_NONE][] = array(
        'target_id' => $nid,
      );
    }

    // Create first referencing node.
    $this
      ->drupalCreateNode(array(
      'type' => 'ief_test_multiple',
      'title' => 'First referencing node',
      'field_multiple_nodes' => $field_multiple_nodes,
    ));
    $first_node = $this
      ->drupalGetNodeByTitle('First referencing node');

    // Create second referencing node.
    $this
      ->drupalCreateNode(array(
      'type' => 'ief_test_multiple',
      'title' => 'Second referencing node',
      'field_multiple_nodes' => $field_multiple_nodes,
    ));
    $second_node = $this
      ->drupalGetNodeByTitle('Second referencing node');

    // Edit referenced content in first node.
    $this
      ->drupalGet('node/' . $first_node->nid . '/edit');

    // Edit referenced node.
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @value="Edit" and @id="edit-field-multiple-nodes-und-entities-0-actions-ief-entity-edit"]'));
    $edit = array(
      'field_multiple_nodes[und][entities][0][form][title]' => 'Some reference updated',
    );
    $this
      ->drupalPostAjax(NULL, $edit, $this
      ->getButtonName('//input[@type="submit" and @value="Update node" and @id="edit-field-multiple-nodes-und-entities-0-form-actions-ief-edit-save"]'));

    // Save the first node after editing the reference.
    $edit = array(
      'title' => 'First node updated',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));

    // The changed value of the referenced content is now newer than the
    // changed value of the second node.
    // Edit referenced content in second node.
    $this
      ->drupalGet('node/' . $second_node->nid . '/edit');

    // Edit referenced node.
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @value="Edit" and @id="edit-field-multiple-nodes-und-entities-0-actions-ief-entity-edit"]'));
    $edit = array(
      'field_multiple_nodes[und][entities][0][form][title]' => 'Some reference updated the second time',
    );
    $this
      ->drupalPostAjax(NULL, $edit, $this
      ->getButtonName('//input[@type="submit" and @value="Update node" and @id="edit-field-multiple-nodes-und-entities-0-form-actions-ief-edit-save"]'));

    // Save the second node after editing the reference.
    $edit = array(
      'title' => 'Second node updated',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));

    // Check if the referenced content could be edited.
    $this
      ->assertNoText('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.', 'The referenced content could be edited.');
  }

  /**
   * Tests create access on IEF Multiple content type.
   */
  public function testMultipleEntityCreate() {
    $user = $this
      ->drupalCreateUser(array(
      'create ief_test_multiple content',
    ));
    $this
      ->drupalLogin($user);
    $this
      ->drupalGet('node/add/ief-test-multiple');
    $this
      ->assertNoFieldByName('field_all_bundles[und][actions][bundle]', NULL, 'Bundle select is not shown when only one bundle is available.');
    $this
      ->assertNoFieldByName('field_multiple_nodes[und][form][title]', NULL);
    $user = $this
      ->drupalCreateUser(array(
      'create ief_test_multiple content',
      'create ief_reference_type content',
    ));
    $this
      ->drupalLogin($user);
    $this
      ->drupalGet('node/add/ief-test-multiple');
    $this
      ->assertFieldByName('field_all_bundles[und][actions][bundle]', NULL, 'Bundle select is shown when more than one bundle is available.');
    $this
      ->assertTrue((bool) $this
      ->xpath('//select[@id="edit-field-all-bundles-und-actions-bundle"]//option[@value="ief_reference_type"]'), 'IEF Reference type is available');
    $this
      ->assertTrue((bool) $this
      ->xpath('//select[@id="edit-field-all-bundles-und-actions-bundle"]//option[@value="ief_test_multiple"]'), 'IEF test multiple is available');
    $this
      ->assertFieldByName('field_multiple_nodes[und][form][title]');
  }

  /**
   * Tests entity create access is correct on nested IEF forms.
   */
  public function testNestedEntityCreateAccess() {
    $permissions = array(
      'create ief_test_nested1 content',
      'create ief_test_nested2 content',
    );
    $this
      ->setupNestedMultipleForm(TRUE, $permissions);
    $this
      ->assertFieldByName('title');
    $this
      ->assertFieldByName('field_test_ref_nested1[und][form][title]');
    $this
      ->assertNoFieldByName('field_test_ref_nested1[und][form][field_test_ref_nested2][und][form][title]', NULL);
    $this
      ->setupNestedMultipleForm(FALSE, $permissions);
    $this
      ->assertNoFieldByXPath('//input[@type="submit" and @value="Create node 3"]');
  }

  /**
   * Creates ief_reference_type nodes which shall serve as reference nodes.
   *
   * @param int $numNodes
   *   The number of nodes to create
   * @return array
   *   Array of created node ids keyed by labels.
   */
  protected function createReferenceContent($numNodes = 3) {
    $retval = array();
    for ($i = 1; $i <= $numNodes; $i++) {
      $this
        ->drupalCreateNode(array(
        'type' => 'ief_reference_type',
        'title' => 'Some reference ' . $i,
        'field_first_name' => 'First Name ' . $i,
        'field_last_name' => 'Last Name ' . $i,
      ));
      $node = $this
        ->drupalGetNodeByTitle('Some reference ' . $i);
      $this
        ->assertTrue($node, 'Created ief_reference_type node "' . $node->title . '"');
      $retval[$node->title] = $node->nid;
    }
    return $retval;
  }

  /**
   * Sets allow_existing IEF setting.
   *
   * @param bool $flag
   *   "allow_existing" flag to be set.
   */
  protected function setAllowExisting($flag) {
    $edit = array(
      'instance[widget][settings][type_settings][allow_existing]' => $flag,
    );
    $this
      ->drupalPost('admin/structure/types/manage/ief-test-multiple/fields/field_multiple_nodes', $edit, t('Save settings'));
    $this
      ->drupalGet('admin/structure/types/manage/ief-test-multiple/fields/field_multiple_nodes');
  }

  /**
   * Creates a node for every node bundle.
   *
   * @return array
   *   Array of node titles keyed by ids.
   */
  protected function createNodeForEveryBundle() {
    $retval = array();
    $bundles = node_type_get_types();
    foreach ($bundles as $id => $value) {
      $this
        ->drupalCreateNode(array(
        'type' => $id,
        'title' => $value->name,
      ));
      $node = $this
        ->drupalGetNodeByTitle($value->name);
      $this
        ->assertTrue($node, 'Created node "' . $node->title . '"');
      $retval[$node->nid] = $value->name;
    }
    return $retval;
  }

  /**
   * Set up the ief_test_nested1 node add form.
   *
   * Sets the nested fields' required settings.
   * Gets the form.
   * Opens the inline entity forms if they are not required.
   *
   * @param boolean $required
   *   Whether the fields are required.
   * @param array $permissions
   *   (optional) Permissions to sign testing user in with. You may pass in an
   *   empty array (default) to use the all the permissions necessary create and
   *   edit nodes on the form.
   */
  protected function setupNestedMultipleForm($required, $permissions = array()) {
    $this
      ->drupalLogin($this->administrator_user);
    $edit = array(
      'instance[required]' => $required,
    );
    $this
      ->drupalPost('admin/structure/types/manage/ief-test-nested1/fields/field_test_ref_nested1', $edit, t('Save settings'));
    $this
      ->drupalPost('admin/structure/types/manage/ief-test-nested2/fields/field_test_ref_nested2', $edit, t('Save settings'));
    if (!$permissions) {
      $permissions = array(
        'create ief_test_nested1 content',
        'create ief_test_nested2 content',
        'create ief_test_nested3 content',
        'edit any ief_test_nested1 content',
        'edit any ief_test_nested2 content',
        'edit any ief_test_nested3 content',
      );
    }
    $this->user = $this
      ->drupalCreateUser($permissions);
    $this
      ->drupalLogin($this->user);
    $this
      ->drupalGet('node/add/ief-test-nested1');
    if (!$required) {

      // Open inline forms if not required.
      if (in_array('create ief_test_nested2 content', $permissions)) {
        $this
          ->drupalPostAjax(NULL, array(), $this
          ->getButtonName('//input[@type="submit" and @value="Add new node 2"]'));
      }
      if (in_array('create ief_test_nested3 content', $permissions)) {
        $this
          ->drupalPostAjax(NULL, array(), $this
          ->getButtonName('//input[@type="submit" and @value="Add new node 3"]'));
      }
    }
  }

  /**
   * Closes the existing node form on the "multi" field.
   */
  protected function cancelExistingMultiForm($edit) {
    $this
      ->drupalPostAjax(NULL, $edit, $this
      ->getButtonName('//div[@id="edit-field-multiple-nodes"]//input[@type="submit" and @value="Cancel"]'));
    $this
      ->assertNoFieldByName('field_multiple_nodes[und][form][entity_id]', NULL, 'Existing entity reference autocomplete field removed.');
  }

  /**
   * Opens the existing node form on the "multi" field.
   */
  protected function openMultiExistingForm() {
    $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.');
    $this
      ->assertFieldByName('field_multiple_nodes[und][form][entity_id]', NULL, 'Existing entity reference autocomplete field found.');
  }

  /**
   * Checks that nested IEF entity references can be edit and saved.
   *
   * @param $node
   *  Top level node of type ief_test_nested1 to check.
   * @param bool $ajax_submit
   *  Whether IEF form widgets should be submitted via AJax or left open.
   *
   */
  protected function checkNestedEntityEditing($node, $ajax_submit = TRUE) {
    $this
      ->drupalGet("node/{$node->nid}/edit");
    $level_1_node = node_load($node->field_test_ref_nested1[LANGUAGE_NONE][0]['target_id']);
    $level_2_node = node_load($level_1_node->field_test_ref_nested2[LANGUAGE_NONE][0]['target_id']);
    $level_2_node_update_title = $level_2_node->title . ' - updated';

    //edit-test-ref-nested1-entities-0-actions-ief-entity-edit
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @id="edit-field-test-ref-nested1-und-entities-0-actions-ief-entity-edit"]'));

    //edit-test-ref-nested1-form-inline-entity-form-entities-0-form-test-ref-nested2-entities-0-actions-ief-entity-edit
    $this
      ->drupalPostAjax(NULL, array(), $this
      ->getButtonName('//input[@type="submit" and @id="edit-field-test-ref-nested1-und-entities-0-form-field-test-ref-nested2-und-entities-0-actions-ief-entity-edit"]'));
    $edit['field_test_ref_nested1[und][entities][0][form][field_test_ref_nested2][und][entities][0][form][title]'] = $level_2_node_update_title;
    if ($ajax_submit) {

      // Close IEF Forms with AJAX posts

      //edit-test-ref-nested1-form-inline-entity-form-entities-0-form-test-ref-nested2-form-inline-entity-form-entities-0-form-actions-ief-edit-save
      $this
        ->drupalPostAjax(NULL, $edit, $this
        ->getButtonName('//input[@type="submit" and @id="edit-field-test-ref-nested1-und-entities-0-form-field-test-ref-nested2-und-entities-0-form-actions-ief-edit-save"]'));
      $this
        ->drupalPostAjax(NULL, array(), $this
        ->getButtonName('//input[@type="submit" and @id="edit-field-test-ref-nested1-und-entities-0-form-actions-ief-edit-save"]'));
      $this
        ->drupalPost(NULL, array(), t('Save'));
    }
    else {
      $this
        ->drupalPost(NULL, $edit, t('Save'));
    }
    $level_2_node = node_load($level_2_node->nid, NULL, TRUE);
    $this
      ->assertEqual($level_2_node_update_title, $level_2_node->title);
  }

  /**
   * Checks that an invalid value for an existing node will be display the expected error.
   *
   * @param $existing_node_text
   *  The text to enter into the existing node text field.
   * @param $expected_error
   *  The error message that is expected to be shown.
   */
  protected function checkExistingValidationExpectation($existing_node_text, $expected_error) {
    $edit = array(
      'field_multiple_nodes[und][form][entity_id]' => $existing_node_text,
    );
    $this
      ->openMultiExistingForm();
    $this
      ->drupalPostAjax(NULL, $edit, $this
      ->getButtonName('//input[@type="submit" and @id="edit-field-multiple-nodes-und-form-actions-ief-reference-save"]'));
    $this
      ->assertText($expected_error);
    $this
      ->cancelExistingMultiForm($edit);
  }

}

Classes

Namesort descending Description
MultipleValuesWidgetTest IEF multiple values field widget tests.