You are here

protected function WebformBrowserTestTrait::loadWebform in Webform 6.x

Same name and namespace in other branches
  1. 8.5 tests/src/Traits/WebformBrowserTestTrait.php \Drupal\Tests\webform\Traits\WebformBrowserTestTrait::loadWebform()

Lazy load a test webform.

Parameters

string $id: Webform id.

Return value

\Drupal\webform\WebformInterface|null A webform.

See also

\Drupal\views\Tests\ViewTestData::createTestViews

2 calls to WebformBrowserTestTrait::loadWebform()
WebformBrowserTestTrait::loadWebforms in tests/src/Traits/WebformBrowserTestTrait.php
Lazy load a test webforms.
WebformVariantApplyTest::testVariantApply in tests/src/Functional/Variant/WebformVariantApplyTest.php
Test variant apply.

File

tests/src/Traits/WebformBrowserTestTrait.php, line 159

Class

WebformBrowserTestTrait
Provides convenience methods for webform assertions in browser tests.

Namespace

Drupal\Tests\webform\Traits

Code

protected function loadWebform($id) {
  $storage = \Drupal::entityTypeManager()
    ->getStorage('webform');
  if ($webform = $storage
    ->load($id)) {
    return $webform;
  }
  else {
    $config_name = 'webform.webform.' . $id;
    if (strpos($id, 'test_') === 0) {
      $config_directory = drupal_get_path('module', 'webform') . '/tests/modules/webform_test/config/install';
    }
    elseif (strpos($id, 'example_') === 0) {
      $config_directory = drupal_get_path('module', 'webform') . '/modules/webform_examples/config/install';
    }
    elseif (strpos($id, 'template_') === 0) {
      $config_directory = drupal_get_path('module', 'webform') . '/modules/webform_templates/config/install';
    }
    else {
      throw new \Exception("Webform {$id} not valid");
    }
    if (!file_exists("{$config_directory}/{$config_name}.yml")) {
      throw new \Exception("Webform {$id} does not exist in {$config_directory}");
    }
    $file_storage = new FileStorage($config_directory);
    $values = $file_storage
      ->read($config_name);
    $webform = $storage
      ->create($values);
    $webform
      ->save();
    return $webform;
  }
}