You are here

public function WebformFieldTest::testWebformField in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/src/Functional/Field/WebformFieldTest.php \Drupal\Tests\webform\Functional\Field\WebformFieldTest::testWebformField()

Tests the webform (entity reference) field.

File

tests/src/Functional/Field/WebformFieldTest.php, line 25

Class

WebformFieldTest
Tests the webform (entity reference) field.

Namespace

Drupal\Tests\webform\Functional\Field

Code

public function testWebformField() {

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $this
    ->drupalCreateContentType([
    'type' => 'page',
  ]);
  FieldStorageConfig::create([
    'field_name' => 'field_webform',
    'type' => 'webform',
    'entity_type' => 'node',
    'cardinality' => 1,
  ])
    ->save();
  FieldConfig::create([
    'field_name' => 'field_webform',
    'entity_type' => 'node',
    'bundle' => 'page',
    'label' => 'webform',
  ])
    ->save();
  $form_display = $display_repository
    ->getFormDisplay('node', 'page');
  $form_display
    ->setComponent('field_webform', [
    'type' => 'webform_entity_reference_select',
    'settings' => [],
  ]);
  $form_display
    ->save();
  $this
    ->drupalLogin($this->rootUser);

  /**************************************************************************/

  // Check that webform select menu is visible.
  $this
    ->drupalGet('/node/add/page');
  $this
    ->assertNoCssSelect('#edit-field-webform-0-target-id optgroup');
  $this
    ->assertOption('edit-field-webform-0-target-id', 'contact');

  // Add category to 'contact' webform.

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = Webform::load('contact');
  $webform
    ->set('category', '{Some category}');
  $webform
    ->save();

  // Check that webform select menu included optgroup.
  $this
    ->drupalGet('/node/add/page');
  $this
    ->assertCssSelect('#edit-field-webform-0-target-id optgroup[label="{Some category}"]');

  // Create a second webform.
  $webform_2 = $this
    ->createWebform();

  // Check that webform 2 is included in the select menu.
  $this
    ->drupalGet('/node/add/page');
  $this
    ->assertOption('edit-field-webform-0-target-id', 'contact');
  $this
    ->assertOption('edit-field-webform-0-target-id', $webform_2
    ->id());

  // Limit the webform select menu to only the contact form.
  $this
    ->drupalGet('/admin/structure/types/manage/page/form-display');
  $this
    ->drupalPostForm('/admin/structure/types/manage/page/form-display', [], 'field_webform_settings_edit');
  $this
    ->drupalPostForm(NULL, [
    'fields[field_webform][settings_edit_form][settings][webforms][]' => [
      'contact',
    ],
  ], 'Save');

  // Check that webform 2 is NOT included in the select menu.
  $this
    ->drupalGet('/node/add/page');
  $this
    ->assertOption('edit-field-webform-0-target-id', 'contact');
  $this
    ->assertNoOption('edit-field-webform-0-target-id', $webform_2
    ->id());
}