You are here

public function EntityReferenceAutocreateTestCase::testEditWithoutAutocomplete in Entityreference Autocreate 7

Confirm normal behavior will fail if referring to a non-existent target.

Run through normal edit process, and confirm that referring to an entity that does not exist will normally fail.

File

./entityreference_autocreate.test, line 67
Tests for autocreation of entity references

Class

EntityReferenceAutocreateTestCase
Test for Entity Reference admin UI.

Code

public function testEditWithoutAutocomplete() {

  // Add a simple node with no reference.
  $this
    ->drupalPost('node/add/' . $this->testBundle, array(
    'title' => 'Hey Jude',
  ), t('Save'));
  $this
    ->assertText('Song Hey Jude has been created.');

  // Add a node with a non-existing reference.
  $edit = array(
    'title' => 'Helter Skelter',
    'field_artist[und][0][target_id]' => 'The Beatles',
  );
  $this
    ->drupalPost('node/add/' . $this->testBundle, $edit, t('Save'));

  // That should fail.
  $this
    ->assertText('There are no entities matching "The Beatles"');

  // Add the desired target node, then assert it works now.
  $artist = array(
    'type' => $this->testTargetBundle,
    'title' => 'The Beatles',
  );
  $artist = $this
    ->drupalCreateNode($artist);

  // Trying to re-save the same post again should now pass.
  // We are cheating the autocomplete here to give it a target nid.
  // So this is not a good test of normal behaviour.
  $autocomplete_value = $artist->title . ' (' . $artist->nid . ')';

  // A bug in entityreference that does this?
  $edit['field_artist[und][0][target_id]'] = $autocomplete_value;
  $this
    ->drupalPost(NULL, $edit, t('Save'));

  // Anyway,
  // We expect success this time.
  $this
    ->assertText('Song Helter Skelter has been created.');

  // I expect to see a link to the artist now.
  $this
    ->assertText('Artist:');
  $this
    ->clickLink('The Beatles');

  // If that was clickable, it means the link was established OK.
}