You are here

function EntityReferenceReferenceRemain::testScenarioReference in Entityreference prepopulate 7

Set various settings of the entity reference prepopulate settings.

File

./entityreference_prepopulate.test, line 136

Class

EntityReferenceReferenceRemain

Code

function testScenarioReference() {
  $this
    ->drupalLogin($this->user);

  // Verify the basic prepopulate option.
  $options = array(
    'query' => array(
      'node_ref' => $this->node2->nid,
    ),
  );
  $edit = array(
    'title' => 'Referencing node',
  );
  $this
    ->drupalPost('node/add/' . $this->node1->type, $edit, t('Save'), $options);
  $this
    ->assertText('Referenced node', 'The reference has been created');

  // Verify identifier works.
  $this
    ->changeInstanceSettings(array(
    'identifier' => 'friendly-ref',
  ));
  $options = array(
    'query' => array(
      'friendly-ref' => $this->node2->nid,
    ),
  );
  $this
    ->drupalPost('node/add/' . $this->node1->type, $edit, t('Save'), $options);
  $this
    ->assertText('Referenced node', 'The reference has been created');

  // Disable the field.
  $this
    ->changeInstanceSettings(array(
    'action' => 'disable',
  ));
  $this
    ->drupalGet('node/add/' . $this->node1->type, $options);
  $xpath = $this
    ->xpath('//input[@id="edit-node-ref-und-0-target-id" and @disabled="disabled" and @value="Referenced node (' . $this->node2->nid . ')"]');
  $this
    ->assertTrue(!empty($xpath), 'The field is disabled and default value is set to the node 2.');
  $this
    ->drupalPost('node/add/' . $this->node1->type, $edit, t('Save'), $options);
  $this
    ->assertText('Referenced node', 'The reference has been created');

  // Hide the field.
  $this
    ->changeInstanceSettings(array(
    'action' => 'hide',
  ));
  $xpath = $this
    ->xpath('//input[@id="edit-node-ref-und-0-target-id"]');
  $this
    ->assertTrue(empty($xpath), 'The field is not visible to the user.');
  $this
    ->drupalPost('node/add/' . $this->node1->type, $edit, t('Save'), $options);
  $this
    ->assertText('Referenced node', 'The reference has been created');

  // Set an error when the prepopulated value is missing.
  $this
    ->changeInstanceSettings(array(
    'fallback' => 'form_error',
  ));
  $this
    ->drupalGet('node/add/' . $this->node1->type);
  $this
    ->assertText('Field Node ref must be populated via URL.', 'The error of the missing  prepopulated value has been set.');

  // Redirect when there is the prepopulated field is missing.
  $this
    ->changeInstanceSettings(array(
    'fallback' => 'redirect',
  ));
  $this
    ->drupalGet('node/add/' . $this->node1->type);
  $this
    ->assertTrue($this
    ->getUrl() == url('<front>', array(
    'absolute' => TRUE,
  )), 'The redirect of due to non prepoulated value.');

  // Verify the basic edit.
  $this
    ->changeInstanceSettings(array(
    'action' => 'hide',
    'action_on_edit' => FALSE,
  ));
  $this
    ->drupalGet('node/' . $this->node1->nid . '/edit');
  $xpath = $this
    ->xpath('//input[@id="edit-node-ref-und-0-target-id"]');
  $this
    ->assertTrue(!empty($xpath), "The node reference is visible to the user.");
  $this
    ->drupalPost('node/' . $this->node1->nid . '/edit', array(
    'title' => 'Referencing node',
  ), t('Save'));
  $this
    ->verifyReferenceRemain();

  // Hide the field when editing.
  $this
    ->changeInstanceSettings(array(
    'action' => 'hide',
    'action_on_edit' => TRUE,
  ));
  $this
    ->drupalGet('node/' . $this->node1->nid . '/edit');
  $xpath = $this
    ->xpath('//input[@id="edit-node-ref-und-0-target-id"]');
  $this
    ->assertTrue(empty($xpath), "The node reference is invisible to the user.");
  $this
    ->drupalPost('node/' . $this->node1->nid . '/edit', array(
    'title' => 'Referencing node',
  ), t('Save'), $options);
  $this
    ->verifyReferenceRemain();

  // Disable the field.
  $this
    ->changeInstanceSettings(array(
    'action' => 'disable',
  ));
  $this
    ->drupalGet('node/' . $this->node1->nid . '/edit');
  $xpath = $this
    ->xpath('//input[@id="edit-node-ref-und-0-target-id" and @disabled="disabled" and @value="Referenced node (' . $this->node2->nid . ')"]');
  $this
    ->assertTrue(!empty($xpath), 'The field is disabled and default value is set to the node 2.');
  $this
    ->drupalPost('node/' . $this->node1->nid . '/edit', array(
    'title' => 'Referencing node',
  ), t('Save'));
  $this
    ->verifyReferenceRemain();
}