You are here

function EditableViewsBasicWebTestCase::testNodeFieldsView in Editable Views 7

Test basic node view 'editable_views_node_fields'.

File

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

Class

EditableViewsBasicWebTestCase
Test simple editable views with no editable fields on relationships.

Code

function testNodeFieldsView() {
  $first_node_title = $this
    ->randomName();
  $first_node = $this
    ->drupalCreateNode(array(
    'title' => $first_node_title,
    'type' => 'editable_views_test_node',
  ));
  $second_node_title = $this
    ->randomName();
  $second_node = $this
    ->drupalCreateNode(array(
    'title' => $second_node_title,
    'type' => 'editable_views_test_node',
  ));

  // The fields are defined in the feature module.
  $field_names = array(
    'field_test_editable_text',
    'field_test_editable_options',
  );

  // An array of form element names, keyed first by node id, and then by
  // field name.
  $field_element_names = array(
    $first_node->nid => array(
      'field_test_editable_text' => "node[{$first_node->nid}][field_test_editable_text][und][0][value]",
      'field_test_editable_options' => "node[{$first_node->nid}][field_test_editable_options][und]",
    ),
    $second_node->nid => array(
      'field_test_editable_text' => "node[{$second_node->nid}][field_test_editable_text][und][0][value]",
      'field_test_editable_options' => "node[{$second_node->nid}][field_test_editable_options][und]",
    ),
  );

  // Look at the node title editable view.
  $this
    ->drupalGet('test-editable-views-node-fields');

  // Check the field editable elements are shown in the view form.
  foreach ($field_names as $field_name) {
    $this
      ->assertFieldByName($field_element_names[$first_node->nid][$field_name], '', t("View shows an edit form for the first node's {$field_name} field."));
    $this
      ->assertFieldByName($field_element_names[$second_node->nid][$field_name], '', t("View shows an edit form for the second node's {$field_name} field."));
  }

  // Save the form with changes to the first node, but with a field value on
  // the second node that will fail validation, because
  // editableviews_test_field_attach_validate() will reject 'banana'.
  // Ensure this string is definitely NOT 'banana'.
  $new_value_field_test_editable_text = $this
    ->randomString(8);
  $new_value_field_test_editable_options = 1;
  $edit = array(
    "node[{$first_node->nid}][field_test_editable_text][und][0][value]" => $new_value_field_test_editable_text,
    "node[{$first_node->nid}][field_test_editable_options][und]" => $new_value_field_test_editable_options,
    "node[{$second_node->nid}][field_test_editable_text][und][0][value]" => 'banana',
  );
  $this
    ->drupalPost('test-editable-views-node-fields', $edit, t('Save'));

  // Form validation should fail...
  $this
    ->assertText('Text may not be "banana" (error 1).', "The form error message was shown for the editable text field.");

  // ...but only once.
  $this
    ->assertNoText('Text may not be "banana" (error 2).', "The form error message is shown only once.");

  // Neither node should be saved.
  entity_get_controller('node')
    ->resetCache();
  $first_node = node_load($first_node->nid);
  $first_node_wrapper = entity_metadata_wrapper('node', $first_node);
  $this
    ->assertNotEqual($first_node_wrapper->field_test_editable_text
    ->value(), $new_value_field_test_editable_text, t("The node's field value was not set to the new value."));
  $this
    ->assertNotEqual($first_node_wrapper->field_test_editable_options
    ->value(), $new_value_field_test_editable_options, t("The node's field value was not set to the new value."));
  $second_node = node_load($second_node->nid);
  $second_node_wrapper = entity_metadata_wrapper('node', $second_node);
  $this
    ->assertNotEqual($first_node_wrapper->field_test_editable_text
    ->value(), 'banana', t("The node's field value was not set to the new value."));

  // Save the form again, this time without a bad value.
  $edit = array(
    "node[{$first_node->nid}][field_test_editable_text][und][0][value]" => $new_value_field_test_editable_text,
    "node[{$first_node->nid}][field_test_editable_options][und]" => $new_value_field_test_editable_options,
  );
  $this
    ->drupalPost('test-editable-views-node-fields', $edit, t('Save'));

  // The first node should be saved and have new data in its fields.
  entity_get_controller('node')
    ->resetCache();
  $first_node = node_load($first_node->nid);
  $first_node_wrapper = entity_metadata_wrapper('node', $first_node);
  $this
    ->assertEqual($first_node_wrapper->field_test_editable_text
    ->value(), $new_value_field_test_editable_text, t("The node's field value is set to the new value by saving the view."));
  $this
    ->assertEqual($first_node_wrapper->field_test_editable_options
    ->value(), $new_value_field_test_editable_options, t("The node's field value is set to the new value by saving the view."));
}