You are here

WebformStatesPreviewTest.php in Webform 8.5

Same filename and directory in other branches
  1. 6.x tests/src/Functional/States/WebformStatesPreviewTest.php

File

tests/src/Functional/States/WebformStatesPreviewTest.php
View source
<?php

namespace Drupal\Tests\webform\Functional\States;

use Drupal\webform\Entity\Webform;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;

/**
 * Tests for webform states preview.
 *
 * @group webform
 */
class WebformStatesPreviewTest extends WebformBrowserTestBase {

  /**
   * Webforms to load.
   *
   * @var array
   */
  protected static $testWebforms = [
    'test_states_server_preview',
    'test_states_server_save',
    'test_states_server_clear',
  ];

  /**
   * Tests visible conditions (#states) validator for elements .
   */
  public function testStatesValidatorElementVisible() {
    $webform_preview = Webform::load('test_states_server_preview');

    // Check trigger unchecked and elements are conditionally hidden.
    $this
      ->postSubmission($webform_preview, [], 'Preview');
    $this
      ->assertRaw('trigger_checkbox');
    $this
      ->assertNoRaw('dependent_checkbox');
    $this
      ->assertNoRaw('dependent_markup');
    $this
      ->assertNoRaw('dependent_message');
    $this
      ->assertNoRaw('dependent_fieldset');
    $this
      ->assertNoRaw('nested_textfield');

    // Check trigger checked and elements are conditionally visible.
    $this
      ->postSubmission($webform_preview, [
      'trigger_checkbox' => TRUE,
    ], 'Preview');
    $this
      ->assertRaw('trigger_checkbox');
    $this
      ->assertRaw('dependent_checkbox');
    $this
      ->assertRaw('dependent_markup');
    $this
      ->assertRaw('dependent_message');
    $this
      ->assertRaw('dependent_fieldset');
    $this
      ->assertRaw('nested_textfield');
    $webform_save = Webform::load('test_states_server_save');

    // Check trigger unchecked and saved.
    $this
      ->postSubmission($webform_save, [
      'trigger_checkbox' => FALSE,
    ], 'Submit');
    $this
      ->assertRaw("trigger_checkbox: 0\ndependent_hidden: ''\ndependent_checkbox: ''\ndependent_value: ''\ndependent_textfield: ''\ndependent_textfield_multiple: {  }\ndependent_details_textfield: ''");

    // Check trigger checked and saved.
    $this
      ->postSubmission($webform_save, [
      'trigger_checkbox' => TRUE,
    ], 'Submit');
    $this
      ->assertRaw("trigger_checkbox: 1\ndependent_hidden: '{dependent_hidden}'\ndependent_checkbox: 0\ndependent_value: '{value}'\ndependent_textfield: '{dependent_textfield}'\ndependent_textfield_multiple:\n  - '{dependent_textfield}'\ndependent_details_textfield: '{dependent_details_textfield}'");
    $webform_clear = Webform::load('test_states_server_clear');

    // Check trigger unchecked and not cleared.
    $this
      ->postSubmission($webform_clear, [
      'trigger_checkbox' => FALSE,
    ], 'Submit');
    $this
      ->assertRaw("trigger_checkbox: 0\ndependent_hidden: '{dependent_hidden}'\ndependent_checkbox: 1\ndependent_radios: One\ndependent_value: '{value}'\ndependent_textfield: '{dependent_textfield}'\ndependent_textfield_multiple:\n  - '{dependent_textfield}'\ndependent_webform_name:\n  - title: ''\n    first: John\n    middle: ''\n    last: Smith\n    suffix: ''\n    degree: ''\ndependent_details_textfield: '{dependent_details_textfield}'");
  }

}

Classes

Namesort descending Description
WebformStatesPreviewTest Tests for webform states preview.