You are here

public function EntityReferenceAutocreateTestCase::testViewBasedAutocomplete in Entityreference Autocreate 7

Enable and configure autocreate with a views-based lookup.

Use the GUID field as a lookup index. Add a target artist, verify that we can add a song and link to the artist by ID. Then add another song with an ID for an artist that has not yet been added.

File

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

Class

EntityReferenceAutocreateTestCase
Test for Entity Reference admin UI.

Code

public function testViewBasedAutocomplete() {

  // For this test (only) we need views.module also.
  module_enable(array(
    'views',
    'views_ui',
  ), TRUE);

  // Check that worked - that the view is available.
  $this
    ->drupalGet('admin/structure/views/view/artist_lookup/edit');
  $this
    ->assertText('Edit view');
  $this
    ->assertText('Entity Reference details');

  // Update the field edit properties.
  $admin_url = 'admin/structure/types/manage/song/fields/field_artist';

  // Change the entityselection mode to 'view'.
  $edit = array(
    'instance[widget][settings][entityreference_autocreate][active]' => TRUE,
    'instance[widget][settings][entityreference_autocreate][status]' => -1,
    'field[settings][handler]' => 'views',
  );
  $this
    ->drupalPost($admin_url, $edit, t('Save settings'));
  $this
    ->assertText('Saved Artist configuration.');

  // Need to submit the same form twice as the thing uses AJAX. Bah.
  // Also, due to  https://www.drupal.org/node/1502750 ?
  // Need to clear the cache or the lookup handler is unavailable still.

  #drupal_flush_all_caches();

  #cache_clear_all(NULL);

  // I can't figure out how to boot the fucking cache.
  // Try this old trick. Super-hacky work-around.
  $this
    ->drupalGet('update.php');
  $this
    ->drupalPost('update.php', array(), t('Continue'));

  // If cache was not cleared, we were seeing this error.
  $this
    ->drupalGet($admin_url);
  $this
    ->assertNoText('The selected selection handler is broken.');

  // Proceed to the Field (instance) settings.
  // Set the lookup view to the one we provide.
  $edit = array(
    'field[settings][handler]' => 'views',
    'field[settings][handler_settings][view][view_and_display]' => 'artist_lookup:by_mbid',
    'instance[widget][settings][entityreference_autocreate][bundle]' => 'artist',
  );
  $this
    ->drupalPost($admin_url, $edit, t('Save settings'));

  // Load this page again, just to review the results stick.
  $this
    ->drupalGet($admin_url);

  // We are not doing autocreate yet,
  // just testing that a view-based lookup works as expected first.
  // Add an artist node we will refer to.
  // Give this a MusicBrainz ID we will try to look up.
  $edit = array(
    'title' => 'Bonnie Tyler',
    'mbid[und][0][value]' => '0aa8294b-6332-4b65-b677-e3a1f8591d3b',
  );
  $this
    ->drupalPost('node/add/' . $this->testTargetBundle, $edit, t('Save'));
  $this
    ->assertText('Artist Bonnie Tyler has been created.');

  // Now make a song that will try to refer to it.
  $edit = array(
    'title' => 'Total Eclipse of the Heart',
    'field_artist[und][0][target_id]' => '0aa8294b-6332-4b65-b677-e3a1f8591d3b',
  );
  $this
    ->drupalPost('node/add/' . $this->testBundle, $edit, t('Save'));
  $this
    ->assertText('Song Total Eclipse of the Heart has been created.');

  // This should have created a link to the artist.
  $this
    ->clickLink('Bonnie Tyler');
}