You are here

public function WebformEntityTest::testElementsCrud in Webform 6.x

Same name and namespace in other branches
  1. 8.5 tests/src/Kernel/Entity/WebformEntityTest.php \Drupal\Tests\webform\Kernel\Entity\WebformEntityTest::testElementsCrud()

Test elements CRUD operations.

File

tests/src/Kernel/Entity/WebformEntityTest.php, line 354

Class

WebformEntityTest
Tests the webform entity class.

Namespace

Drupal\Tests\webform\Kernel\Entity

Code

public function testElementsCrud() {
  $this
    ->installEntitySchema('path_alias');
  $this
    ->installSchema('webform', [
    'webform',
  ]);
  $this
    ->installEntitySchema('webform_submission');

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = Webform::create([
    'id' => 'webform_test',
  ]);
  $webform
    ->save();

  // Check set new root element.
  $elements = [
    'root' => [
      '#type' => 'container',
      '#title' => 'root',
    ],
  ];
  $webform
    ->setElementProperties('root', $elements['root']);
  $this
    ->assertEquals($webform
    ->getElementsRaw(), WebformYaml::encode($elements));

  // Check add new container to root.
  $elements['root']['container'] = [
    '#type' => 'container',
    '#title' => 'container',
  ];
  $webform
    ->setElementProperties('container', $elements['root']['container'], 'root');
  $this
    ->assertEquals($webform
    ->getElementsRaw(), WebformYaml::encode($elements));

  // Check add new element to container.
  $elements['root']['container']['element'] = [
    '#type' => 'textfield',
    '#title' => 'element',
  ];
  $webform
    ->setElementProperties('element', $elements['root']['container']['element'], 'container');
  $this
    ->assertEquals($webform
    ->getElementsRaw(), WebformYaml::encode($elements));

  // Check delete container with al recursively delete all children.
  $elements = [
    'root' => [
      '#type' => 'container',
      '#title' => 'root',
    ],
  ];
  $webform
    ->deleteElement('container');
  $this
    ->assertEquals($webform
    ->getElementsRaw(), WebformYaml::encode($elements));
}