You are here

function EditableViewsEntityReferenceWebTestCase::testNodeTitleForwardsRelationshipView in Editable Views 7

Test node view 'test_editable_views_node_referenced_forwards_title'.

Contains a relationship to another node type via a forwards reference.

File

tests/editableviews.test, line 343
Tests for editableviews.module.

Class

EditableViewsEntityReferenceWebTestCase
Test editable views with editable fields on relationships.

Code

function testNodeTitleForwardsRelationshipView() {

  // We prefix the node titles to force the order of items in the View to be
  // predictable. Otherwise, we can't reliably (easily!) test the presence of
  // form elements for the empty nodes.
  $first_pointed_node_title = $this
    ->randomName();
  $first_pointed_node = $this
    ->drupalCreateNode(array(
    'title' => $first_pointed_node_title,
    'type' => 'editable_views_test_node',
  ));
  $first_node_title = 'a' . $this
    ->randomName();
  $first_node = $this
    ->drupalCreateNode(array(
    'title' => $first_node_title,
    'type' => 'editable_views_test_referencing',
    'field_test_node_reference' => array(
      LANGUAGE_NONE => array(
        array(
          'target_id' => $first_pointed_node->nid,
        ),
      ),
    ),
  ));
  $second_node_title = 'z' . $this
    ->randomName();
  $second_node = $this
    ->drupalCreateNode(array(
    'title' => $second_node_title,
    'type' => 'editable_views_test_referencing',
  ));

  // Look at the editable view.
  $this
    ->drupalGet('test_editable_views_node_referenced_forwards_title');

  // Check there are form elements for all three nodes' titles as well as the
  // empty node.
  $first_node_title_element_name = "node[{$first_node->nid}][title]";
  $this
    ->assertFieldByName($first_node_title_element_name, $first_node_title, t("View shows an edit form for the first node's title."));
  $second_node_title_element_name = "node[{$second_node->nid}][title]";
  $this
    ->assertFieldByName($second_node_title_element_name, $second_node_title, t("View shows an edit form for the second node's title."));
  $first_pointed_node_title_element_name = "node[{$first_pointed_node->nid}][title]";
  $this
    ->assertFieldByName($first_pointed_node_title_element_name, $first_pointed_node_title, t("View shows an edit form for the first pointed node's title."));

  // The empty node is forced to be in the second row, by the prefixes on the
  // node titles.
  $empty_node_title_element_name = "node[field_test_node_reference_target_id:1][title]";
  $this
    ->assertFieldByName($empty_node_title_element_name, '', t("View shows an edit form with an empty value for the empty node."));

  // Change the titles of the referenced nodes, i.e., the second field in the
  // view. This will create a new node.
  $edit = array(
    $first_pointed_node_title_element_name => $this
      ->randomName(),
    $empty_node_title_element_name => $this
      ->randomName(),
  );
  $this
    ->drupalPost('test_editable_views_node_referenced_forwards_title', $edit, t('Save'));

  // Check the referenced node's title is changed.
  entity_get_controller('node')
    ->resetCache();
  $first_pointed_node = node_load($first_pointed_node->nid);
  $this
    ->assertEqual($first_pointed_node->title, $edit[$first_pointed_node_title_element_name], t("The referenced node's title has been changed by saving the editable view."));

  // Check a new node was created.
  $query = new EntityFieldQuery();
  $entities = $query
    ->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'editable_views_test_node')
    ->propertyCondition('title', $edit[$empty_node_title_element_name])
    ->execute();
  $this
    ->assertFalse(empty($entities['node']), t("A new referenced node has been created."));
  $second_pointed_node = node_load(array_shift(array_keys($entities['node'])));
  $this
    ->assertEqual($second_pointed_node->title, $edit[$empty_node_title_element_name], t("The second referenced node's title has been set by saving the editable view."));

  // Check the second node points to it.
  $second_node = node_load($second_node->nid);
  $second_node_wrapper = entity_metadata_wrapper('node', $second_node);
  $this
    ->assertEqual($second_node_wrapper->field_test_node_reference
    ->raw(), $second_pointed_node->nid, t("The second node points to the new second referenced node."));
}