You are here

public function ConfigEntityTrackingTest::testWebformTracking in Entity Usage 8.4

Same name and namespace in other branches
  1. 8.2 tests/src/FunctionalJavascript/ConfigEntityTrackingTest.php \Drupal\Tests\entity_usage\FunctionalJavascript\ConfigEntityTrackingTest::testWebformTracking()
  2. 8.3 tests/src/FunctionalJavascript/ConfigEntityTrackingTest.php \Drupal\Tests\entity_usage\FunctionalJavascript\ConfigEntityTrackingTest::testWebformTracking()

Tests webform tracking.

File

tests/src/FunctionalJavascript/ConfigEntityTrackingTest.php, line 61

Class

ConfigEntityTrackingTest
Tests tracking of config entities.

Namespace

Drupal\Tests\entity_usage\FunctionalJavascript

Code

public function testWebformTracking() {

  // Create an entity reference field pointing to a webform.
  $storage = FieldStorageConfig::create([
    'field_name' => 'field_eu_test_related_webforms',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'settings' => [
      'target_type' => 'webform',
    ],
  ]);
  $storage
    ->save();
  FieldConfig::create([
    'bundle' => 'eu_test_ct',
    'entity_type' => 'node',
    'field_name' => 'field_eu_test_related_webforms',
    'label' => 'Related Webforms',
    'settings' => [
      'handler' => 'default:webform',
      'handler_settings' => [
        'target_bundles' => NULL,
        'auto_create' => FALSE,
      ],
    ],
  ])
    ->save();

  // Define our widget and formatter for this field.
  \Drupal::service('entity_display.repository')
    ->getFormDisplay('node', 'eu_test_ct', 'default')
    ->setComponent('field_eu_test_related_webforms', [
    'type' => 'entity_reference_autocomplete',
  ])
    ->save();
  \Drupal::service('entity_display.repository')
    ->getViewDisplay('node', 'eu_test_ct', 'default')
    ->setComponent('field_eu_test_related_webforms', [
    'type' => 'entity_reference_label',
  ])
    ->save();
  $this
    ->drupalPlaceBlock('local_tasks_block');
  $session = $this
    ->getSession();
  $page = $session
    ->getPage();
  $assert_session = $this
    ->assertSession();

  // Check some config-entity related settings on the config form.
  $this
    ->drupalGet('/admin/config/entity-usage/settings');

  // We should have an unchecked checkbox for a local tab.
  $webform_tab_checkbox = $assert_session
    ->fieldExists('local_task_enabled_entity_types[entity_types][webform]');
  $assert_session
    ->checkboxNotChecked('local_task_enabled_entity_types[entity_types][webform]');

  // Check it so we can test it later.
  $webform_tab_checkbox
    ->click();

  // We should have an unchecked checkbox for target entity type.
  $targets_fieldset_wrapper = $assert_session
    ->elementExists('css', '#edit-track-enabled-target-entity-types summary');
  $targets_fieldset_wrapper
    ->click();
  $webform_target_checkbox = $assert_session
    ->fieldExists('track_enabled_target_entity_types[entity_types][webform]');
  $assert_session
    ->checkboxNotChecked('track_enabled_target_entity_types[entity_types][webform]');

  // Check tracking webforms as targets.
  $webform_target_checkbox
    ->click();

  // Save configuration.
  $page
    ->pressButton('Save configuration');
  $this
    ->saveHtmlOutput();

  // Make sure the 'contact' webform exists.
  $this
    ->drupalGet('/form/contact');
  $page
    ->findField('email');
  $page
    ->findButton('Send message');

  // Create a node referencing this webform.
  $this
    ->drupalGet('/node/add/eu_test_ct');
  $page
    ->fillField('title[0][value]', 'Node that points to a webform');
  $page
    ->fillField('field_eu_test_related_webforms[0][target_id]', 'Contact (contact)');
  $page
    ->pressButton('Save');
  $this
    ->saveHtmlOutput();
  $this
    ->assertSession()
    ->pageTextContains('eu_test_ct Node that points to a webform has been created.');

  // Visit the webform page, check the usage tab is there.
  $webform_link = $assert_session
    ->elementExists('css', '.field--name-field-eu-test-related-webforms a');
  $webform_link
    ->click();
  $this
    ->saveHtmlOutput();

  // Click on the tab and verify if the usage was correctly tracked.
  $assert_session
    ->pageTextContains('Usage');
  $page
    ->clickLink('Usage');
  $this
    ->saveHtmlOutput();

  // We should be at /webform/contact/usage.
  $this
    ->assertStringContainsString("/webform/contact/usage", $session
    ->getCurrentUrl());
  $assert_session
    ->elementContains('css', 'main table', 'Node that points to a webform');
  $assert_session
    ->elementContains('css', 'main table', 'Related Webforms');
}