You are here

function EditableViewsEntityReferenceWebTestCase::testNodeTitleBackwardsRelationshipView in Editable Views 7

Test node view 'test_editable_views_node_referenced_backwards_title'.

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

File

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

Class

EditableViewsEntityReferenceWebTestCase
Test editable views with editable fields on relationships.

Code

function testNodeTitleBackwardsRelationshipView() {

  // 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_node_title = 'a' . $this
    ->randomName();
  $first_node = $this
    ->drupalCreateNode(array(
    'title' => $first_node_title,
    'type' => 'editable_views_test_node',
  ));
  $second_node_title = 'z' . $this
    ->randomName();
  $second_node = $this
    ->drupalCreateNode(array(
    'title' => $second_node_title,
    'type' => 'editable_views_test_node',
  ));
  $first_pointer_node_title = $this
    ->randomName();
  $first_pointer_node = $this
    ->drupalCreateNode(array(
    'title' => $first_pointer_node_title,
    'type' => 'editable_views_test_referencing',
    'field_test_node_reference' => array(
      LANGUAGE_NONE => array(
        array(
          'target_id' => $first_node->nid,
        ),
      ),
    ),
  ));

  // Look at the editable view.
  $this
    ->drupalGet('test_editable_views_node_referenced_backwards_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_pointer_node_title_element_name = "node[{$first_pointer_node->nid}][title]";
  $this
    ->assertFieldByName($first_pointer_node_title_element_name, $first_pointer_node_title, t("View shows an edit form for the first pointer 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[reverse_field_test_node_reference_node: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 referencing nodes, i.e., the second field in the
  // view. This will create a new node.
  $edit = array(
    $first_pointer_node_title_element_name => $this
      ->randomName(),
    $empty_node_title_element_name => $this
      ->randomName(),
  );
  $this
    ->drupalPost('test_editable_views_node_referenced_backwards_title', $edit, t('Save'));

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

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

  // Check it points to the second node.
  $second_pointer_node_wrapper = entity_metadata_wrapper('node', $second_pointer_node);
  $this
    ->assertEqual($second_pointer_node_wrapper->field_test_node_reference
    ->raw(), $second_node->nid, t("The second referencing node points to the second node."));
}