View source
<?php
namespace Drupal\Tests\webform\Functional\Settings;
use Drupal\webform\Entity\Webform;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;
class WebformSettingsPreviewTest extends WebformBrowserTestBase {
protected static $testWebforms = [
'test_form_preview',
];
protected function setUp() {
parent::setUp();
\Drupal::configFactory()
->getEditable('webform.settings')
->set('libraries.excluded_libraries', [
'progress-tracker',
])
->save();
}
public function testPreview() {
$this
->drupalLogin($this->rootUser);
$webform_preview = Webform::load('test_form_preview');
$this
->drupalGet('/webform/test_form_preview');
$this
->assertFieldByName('op', 'Submit');
$this
->assertFieldByName('op', 'Preview');
$this
->drupalPostForm('/webform/test_form_preview', [
'name' => 'test',
'email' => 'example@example.com',
'checkbox' => TRUE,
], 'Preview');
$this
->assertRaw('<h1>Test: Webform: Preview: Preview</h1>');
$this
->assertRaw('<b>Preview</b></li>');
$this
->assertRaw('Please review your submission. Your submission is not complete until you press the "Submit" button!');
$this
->assertFieldByName('op', 'Submit');
$this
->assertFieldByName('op', '< Previous');
$this
->assertRaw('<div class="webform-preview js-form-wrapper form-wrapper" data-drupal-selector="edit-preview" id="edit-preview">');
$this
->assertRaw('<div data-drupal-selector="edit-submission" class="webform-submission-data webform-submission-data--webform-test-form-preview webform-submission-data--view-mode-preview">');
$this
->assertRaw('<fieldset class="format-attributes-class webform-container webform-container-type-fieldset js-form-item form-item js-form-wrapper form-wrapper" id="test_form_preview--fieldset">');
$this
->assertRaw('<div class="format-attributes-class webform-element webform-element-type-textfield js-form-item form-item js-form-type-item form-item-name js-form-item-name" id="test_form_preview--name">');
$this
->assertRaw('<label>Name</label>' . PHP_EOL . ' test');
$this
->assertRaw('<section class="format-attributes-class js-form-item form-item js-form-wrapper form-wrapper webform-section" id="test_form_preview--container">');
$this
->assertRaw('<div class="format-attributes-class webform-element webform-element-type-email js-form-item form-item js-form-type-item form-item-email js-form-item-email" id="test_form_preview--email">');
$this
->assertRaw('<label>Email</label>' . PHP_EOL . ' <a href="mailto:example@example.com">example@example.com</a>');
$this
->assertRaw('<div class="format-attributes-class webform-element webform-element-type-checkbox js-form-item form-item js-form-type-item form-item-checkbox js-form-item-checkbox" id="test_form_preview--checkbox">');
$this
->assertRaw('<section class="format-attributes-class js-form-item form-item js-form-wrapper form-wrapper webform-section" id="test_form_preview--section">');
$this
->assertRaw('<label>Checkbox</label>' . PHP_EOL . ' Yes');
$this
->assertRaw('<div class="webform-preview js-form-wrapper form-wrapper" data-drupal-selector="edit-preview" id="edit-preview">');
$this
->drupalPostForm('/webform/test_form_preview', [], 'Preview');
$this
->assertNoRaw('<label>Name</label>');
$this
->assertNoRaw('<label>Email</label>');
$this
->assertNoRaw('<label>Checkbox</label>');
$sid = $this
->postSubmission($webform_preview);
$this
->drupalGet("admin/structure/webform/manage/test_form_preview/submission/{$sid}");
$this
->assertNoRaw('<label>Name</label>');
$this
->assertNoRaw('<label>Email</label>');
$this
->assertNoRaw('<label>Checkbox</label>');
$this
->drupalGet("admin/structure/webform/manage/test_form_preview/submission/{$sid}/table");
$this
->assertNoRaw('<th>Name</th>');
$this
->assertNoRaw('<th>Email</th>');
$this
->assertNoRaw('<th>Checkbox</th>');
$this
->assertNoRaw('<td>No</td>');
\Drupal::configFactory()
->getEditable('webform.settings')
->set('settings.default_preview_message', '')
->save();
$this
->drupalPostForm('/webform/test_form_preview', [
'name' => 'test',
'email' => 'example@example.com',
], 'Preview');
$this
->assertNoRaw('Please review your submission. Your submission is not complete until you press the "Submit" button!');
$webform_preview
->setSetting('preview_exclude_empty', FALSE);
$webform_preview
->setSetting('preview_exclude_empty_checkbox', FALSE);
$webform_preview
->setSetting('submission_exclude_empty', FALSE);
$webform_preview
->setSetting('submission_exclude_empty_checkbox', FALSE);
$webform_preview
->save();
$this
->drupalPostForm('/webform/test_form_preview', [
'name' => '',
'email' => '',
'checkbox' => FALSE,
], 'Preview');
$this
->assertRaw('<label>Name</label>' . PHP_EOL . ' {Empty}');
$this
->assertRaw('<div class="format-attributes-class webform-element webform-element-type-email js-form-item form-item js-form-type-item form-item-email js-form-item-email" id="test_form_preview--email">');
$this
->assertRaw('<label>Email</label>' . PHP_EOL . ' {Empty}');
$this
->assertRaw('<label>Checkbox</label>' . PHP_EOL . ' No');
$sid = $this
->postSubmission($webform_preview);
$this
->drupalGet("admin/structure/webform/manage/test_form_preview/submission/{$sid}");
$this
->assertRaw('<label>Name</label>');
$this
->assertRaw('<label>Email</label>');
$this
->assertRaw('<label>Checkbox</label>');
$this
->drupalGet("admin/structure/webform/manage/test_form_preview/submission/{$sid}/table");
$this
->assertRaw('<th>Name</th>');
$this
->assertRaw('<th>Email</th>');
$this
->assertRaw('<th>Checkbox</th>');
$this
->assertRaw('<td>No</td>');
$webform_preview
->set('title', "This has special characters. '<>\"&");
$webform_preview
->save();
$this
->drupalGet('/webform/test_form_preview');
$this
->assertRaw('<title>This has special characters. \'"& | Drupal</title>');
$this
->assertRaw('<h1>This has special characters. '<>"&</h1>');
$this
->drupalPostForm('/webform/test_form_preview', [
'name' => 'test',
], 'Preview');
$this
->assertRaw('<title>This has special characters. \'"&: Preview | Drupal</title>');
$this
->assertRaw('<h1>This has special characters. '<>"&: Preview</h1>');
$webform_preview
->setSettings([
'preview' => DRUPAL_REQUIRED,
'preview_label' => '{Label}',
'preview_title' => '{Title}',
'preview_message' => '{Message}',
'preview_attributes' => [
'class' => [
'preview-custom',
],
],
'preview_excluded_elements' => [
'email' => 'email',
],
]);
$webform_preview
->setElementProperties('actions', [
'#type' => 'webform_actions',
'#preview_next__label' => '{Preview}',
'#preview_prev__label' => '{Back}',
]);
$webform_preview
->save();
$this
->drupalPostForm('/webform/test_form_preview', [
'name' => 'test',
], '{Preview}');
$this
->assertRaw('<h1>{Title}</h1>');
$this
->assertRaw('<b>{Label}</b></li>');
$this
->assertRaw('{Message}');
$this
->assertFieldByName('op', 'Submit');
$this
->assertFieldByName('op', '{Back}');
$this
->assertRaw('<label>Name</label>' . PHP_EOL . ' test');
$this
->assertNoRaw('<label>Email</label>');
$this
->assertRaw('<div class="preview-custom webform-preview js-form-wrapper form-wrapper" data-drupal-selector="edit-preview" id="edit-preview">');
$this
->drupalGet('/webform/test_form_preview');
$this
->assertNoFieldByName('op', 'Submit');
$this
->assertFieldByName('op', '{Preview}');
$this
->drupalPostForm('/webform/test_form_preview', [
'name' => 'test',
'email' => '',
], '{Preview}');
$this
->assertRaw('<label>Name</label>' . PHP_EOL . ' test');
$this
->assertNoRaw('<label>Email</label>');
}
}