You are here

public function MultipleValuesWidgetTest::testEntityEditingAndRemoving in Inline Entity Form 7

Tests if editing and removing entities work.

File

tests/multiple_values_widget.test, line 254

Class

MultipleValuesWidgetTest
IEF multiple values field widget tests.

Code

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');
}