View source
<?php
namespace Drupal\Tests\webform\Functional\Field;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;
use Drupal\webform\Entity\Webform;
class WebformFieldTest extends WebformBrowserTestBase {
public static $modules = [
'node',
'field_ui',
];
public function testWebformField() {
$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);
$this
->drupalGet('/node/add/page');
$this
->assertNoCssSelect('#edit-field-webform-0-target-id optgroup');
$this
->assertOption('edit-field-webform-0-target-id', 'contact');
$webform = Webform::load('contact');
$webform
->set('category', '{Some category}');
$webform
->save();
$this
->drupalGet('/node/add/page');
$this
->assertCssSelect('#edit-field-webform-0-target-id optgroup[label="{Some category}"]');
$webform_2 = $this
->createWebform();
$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());
$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');
$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());
}
}