You are here

public function Select2BoxesTests::testEntityAutoCreationWithMultipleVocabularies in Select2 Boxes 8

Test entity auto-creation with multiple vocabularies.

File

tests/src/FunctionalJavascript/Select2BoxesTests.php, line 532

Class

Select2BoxesTests
Class for testing Select2Boxes.

Namespace

Drupal\Tests\select2boxes\FunctionalJavascript

Code

public function testEntityAutoCreationWithMultipleVocabularies() {
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();

  // Create new taxonomy vocabulary for testing.
  $voc = 'test_voc';
  Vocabulary::create([
    'vid' => $voc,
    'name' => $voc,
  ])
    ->save();

  // Check that the new taxonomy vocabulary has been created.
  $this
    ->assertNotNull(Vocabulary::load($voc));

  // Create new entity reference field field.
  $this
    ->drupalGet('admin/structure/types/manage/select2boxes_test_content_type/fields/add-field');
  $page
    ->selectFieldOption('edit-new-storage-type', 'field_ui:entity_reference:taxonomy_term');
  $page
    ->fillField('edit-label', 'Test term');
  $page
    ->pressButton('Save and continue');
  $page
    ->fillField('edit-field-name', 'test_term');
  $page
    ->pressButton('Save and continue');
  $page
    ->selectFieldOption('edit-cardinality', '-1');
  $page
    ->pressButton('Save field settings');
  $page
    ->checkField('settings[handler_settings][auto_create]');
  $page
    ->checkField("settings[handler_settings][target_bundles][{$voc}]");
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->checkField('settings[handler_settings][target_bundles][tags]');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->fillField('settings[handler_settings][auto_create_bundle]', $voc);
  $page
    ->pressButton('Save settings');

  // Go the the "Manage Form Display" form.
  $this
    ->drupalGet('admin/structure/types/manage/select2boxes_test_content_type/form-display');
  $assert_session
    ->elementExists('xpath', '//select[@name="fields[field_test_term][type]"]')
    ->setValue(static::$pluginIds[2]);
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->pressButton('Save');

  // Go to the node's creation form.
  $this
    ->drupalGet('node/add/select2boxes_test_content_type');
  $page
    ->fillField('title[0][value]', 'TESTTITLE');
  $this
    ->getSession()
    ->executeScript("jQuery('#edit-field-test-term').next().find('input.select2-search__field').val('TESTTERM').trigger('keyup');");
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->selectFieldOption('field_test_term[]', 'TESTTERM');
  $assert_session
    ->assertWaitOnAjaxRequest();
  $page
    ->pressButton('Save');
  $terms = Term::loadMultiple();
  $this
    ->assertNotEmpty($terms);

  /** @var \Drupal\taxonomy\Entity\Term $term */
  $term = reset($terms);
  $this
    ->assertEquals($voc, $term
    ->bundle());
}