View source
<?php
namespace Drupal\Tests\webform\Functional\Settings;
use Drupal\webform\Entity\Webform;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;
class WebformSettingsBehaviorsTest extends WebformBrowserTestBase {
protected static $testWebforms = [
'test_form_submit_once',
'test_form_disable_back',
'test_form_submit_back',
'test_form_unsaved',
'test_form_disable_autocomplete',
'test_form_novalidate',
'test_form_disable_inline_errors',
'test_form_required',
'test_form_autofocus',
'test_form_details_toggle',
];
protected function setUp() {
parent::setUp();
$this
->config('webform.settings')
->set('ui.description_help', FALSE)
->save();
}
public function testSettings() {
$this
->drupalLogin($this->rootUser);
$webform_form_submit_once = Webform::load('test_form_submit_once');
$this
->drupalGet('/webform/test_form_submit_once');
$this
->assertRaw('webform.form.submit_once.js');
$webform_form_submit_once
->setSetting('form_submit_once', FALSE);
$webform_form_submit_once
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_submit_once/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-submit-once" aria-describedby="edit-form-submit-once--description" type="checkbox" id="edit-form-submit-once" name="form_submit_once" value class="form-checkbox" />');
$this
->drupalGet('/webform/test_form_submit_once');
$this
->assertNoRaw('webform.form.submit_once.js');
\Drupal::configFactory()
->getEditable('webform.settings')
->set('settings.default_form_submit_once', TRUE)
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_submit_once/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-submit-once-disabled" aria-describedby="edit-form-submit-once-disabled--description" disabled="disabled" type="checkbox" id="edit-form-submit-once-disabled" name="form_submit_once_disabled" value="1" checked="checked" class="form-checkbox" />');
$this
->assertRaw('Submit button is disabled immediately after it is clicked for all forms.');
$this
->drupalGet('/webform/test_form_submit_once');
$this
->assertRaw('webform.form.submit_once.js');
$webform_form_disable_back = Webform::load('test_form_disable_back');
$this
->drupalGet('/webform/test_form_disable_back');
$this
->assertRaw('webform.form.disable_back.js');
$webform_form_disable_back
->setSetting('form_disable_back', FALSE);
$webform_form_disable_back
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_disable_back/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-disable-back" aria-describedby="edit-form-disable-back--description" type="checkbox" id="edit-form-disable-back" name="form_disable_back" value class="form-checkbox" />');
$this
->drupalGet('/webform/test_form_disable_back');
$this
->assertNoRaw('webform.form.disable_back.js');
\Drupal::configFactory()
->getEditable('webform.settings')
->set('settings.default_form_disable_back', TRUE)
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_disable_back/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-disable-back-disabled" aria-describedby="edit-form-disable-back-disabled--description" disabled="disabled" type="checkbox" id="edit-form-disable-back-disabled" name="form_disable_back_disabled" value="1" checked="checked" class="form-checkbox" />');
$this
->assertRaw('Back button is disabled for all forms.');
$this
->drupalGet('/webform/test_form_disable_back');
$this
->assertRaw('webform.form.disable_back.js');
$webform_form_submit_back = Webform::load('test_form_submit_back');
$this
->drupalGet('/webform/test_form_submit_back');
$this
->assertRaw('webform.form.submit_back.js');
$webform_form_submit_back
->setSetting('form_submit_back', FALSE);
$webform_form_submit_back
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_submit_back/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-submit-back" aria-describedby="edit-form-submit-back--description" type="checkbox" id="edit-form-submit-back" name="form_submit_back" value class="form-checkbox" />');
$this
->drupalGet('/webform/test_form_submit_back');
$this
->assertNoRaw('webform.form.submit_back.js');
\Drupal::configFactory()
->getEditable('webform.settings')
->set('settings.default_form_submit_back', TRUE)
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_submit_back/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-submit-back-disabled" aria-describedby="edit-form-submit-back-disabled--description" disabled="disabled" type="checkbox" id="edit-form-submit-back-disabled" name="form_submit_back_disabled" value="1" checked="checked" class="form-checkbox" />');
$this
->assertRaw('Browser back button submits the previous page for all forms.');
$this
->drupalGet('/webform/test_form_submit_back');
$this
->assertRaw('webform.form.submit_back.js');
$webform_form_submit_back
->setSetting('ajax', TRUE);
$webform_form_submit_back
->save();
$this
->drupalGet('/webform/test_form_submit_back');
$this
->assertNoRaw('webform.form.submit_back.js');
$webform_form_unsaved = Webform::load('test_form_unsaved');
$this
->drupalGet('/webform/test_form_unsaved');
$this
->assertCssSelect('form.js-webform-unsaved', 'Form has .js-webform-unsaved class.');
$webform_form_unsaved
->setSetting('form_unsaved', FALSE);
$webform_form_unsaved
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_unsaved/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-unsaved" aria-describedby="edit-form-unsaved--description" type="checkbox" id="edit-form-unsaved" name="form_unsaved" value class="form-checkbox" />');
$this
->drupalGet('/webform/test_form_novalidate');
$this
->assertNoCssSelect('webform.js-webform-unsaved', 'Webform does not have .js-webform-unsaved class.');
\Drupal::configFactory()
->getEditable('webform.settings')
->set('settings.default_form_unsaved', TRUE)
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_unsaved/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-unsaved-disabled" aria-describedby="edit-form-unsaved-disabled--description" disabled="disabled" type="checkbox" id="edit-form-unsaved-disabled" name="form_unsaved_disabled" value="1" checked="checked" class="form-checkbox" />');
$this
->assertRaw('Unsaved warning is enabled for all forms.');
$this
->drupalGet('/webform/test_form_unsaved');
$this
->assertCssSelect('form.js-webform-unsaved', 'Form has .js-webform-unsaved class.');
$this
->drupalGet('/webform/test_form_disable_autocomplete');
$this
->assertCssSelect('form[autocomplete="off"]', 'Form has autocomplete=off attribute.');
$webform_form_novalidate = Webform::load('test_form_novalidate');
$this
->drupalGet('/webform/test_form_novalidate');
$this
->assertCssSelect('form[novalidate="novalidate"]', 'Form has the proper novalidate attribute.');
$webform_form_novalidate
->setSetting('form_novalidate', FALSE);
$webform_form_novalidate
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_novalidate/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-novalidate" aria-describedby="edit-form-novalidate--description" type="checkbox" id="edit-form-novalidate" name="form_novalidate" value class="form-checkbox" />');
$this
->assertRaw('If checked, the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form">novalidate</a> attribute, which disables client-side validation, will be added to this form.');
$this
->drupalGet('/webform/test_form_novalidate');
$this
->assertNoCssSelect('form[novalidate="novalidate"]', 'Webform have client-side validation enabled.');
\Drupal::configFactory()
->getEditable('webform.settings')
->set('settings.default_form_novalidate', TRUE)
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_novalidate/settings/form');
$this
->assertNoRaw('If checked, the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form">novalidate</a> attribute, which disables client-side validation, will be added to this form.');
$this
->assertRaw('<input data-drupal-selector="edit-form-novalidate-disabled" aria-describedby="edit-form-novalidate-disabled--description" disabled="disabled" type="checkbox" id="edit-form-novalidate-disabled" name="form_novalidate_disabled" value="1" checked="checked" class="form-checkbox" />');
$this
->assertRaw('Client-side validation is disabled for all forms.');
$this
->drupalGet('/webform/test_form_novalidate');
$this
->assertCssSelect('form[novalidate="novalidate"]', 'Form has the proper novalidate attribute.');
$webform_form_required = Webform::load('test_form_required');
$this
->drupalGet('/webform/test_form_required');
$this
->assertRaw('Indicates required field');
$webform_form_required
->setSetting('form_required', FALSE);
$webform_form_required
->save();
$this
->drupalGet('/webform/test_form_required');
$this
->assertNoRaw('Indicates required field');
\Drupal::configFactory()
->getEditable('webform.settings')
->set('settings.default_form_required', TRUE)
->set('settings.default_form_required_label', 'Custom required field')
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_required/settings/form');
$this
->assertRaw('Required indicator is displayed on all forms.');
$this
->assertRaw('<input data-drupal-selector="edit-form-required-disabled" aria-describedby="edit-form-required-disabled--description" disabled="disabled" type="checkbox" id="edit-form-required-disabled" name="form_required_disabled" value="1" checked="checked" class="form-checkbox" />');
$this
->drupalGet('/webform/test_form_required');
$this
->assertRaw('Custom required field');
$elements = $webform_form_required
->getElementsDecoded();
unset($elements['textfield']['#required']);
$webform_form_required
->setElements($elements);
$webform_form_required
->save();
$this
->drupalGet('/webform/test_form_required');
$this
->assertNoRaw('Custom required field');
$this
->drupalGet('/webform/test_form_autofocus');
$this
->assertCssSelect('.js-webform-autofocus');
$webform_form_details_toggle = Webform::load('test_form_details_toggle');
$this
->drupalGet('/webform/test_form_details_toggle');
$this
->assertCssSelect('form.webform-details-toggle', 'Form has the .webform-details-toggle class.');
$this
->drupalGet('/admin/structure/webform/manage/test_form_details_toggle/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-details-toggle-disabled" aria-describedby="edit-form-details-toggle-disabled--description" disabled="disabled" type="checkbox" id="edit-form-details-toggle-disabled" name="form_details_toggle_disabled" value="1" checked="checked" class="form-checkbox" />');
$this
->assertRaw('Expand/collapse all (details) link is automatically added to all forms.');
\Drupal::configFactory()
->getEditable('webform.settings')
->set('settings.default_form_details_toggle', FALSE)
->save();
$this
->drupalGet('/webform/test_form_details_toggle');
$this
->assertCssSelect('form.webform-details-toggle', 'Form has the .webform-details-toggle class.');
$this
->drupalGet('/admin/structure/webform/manage/test_form_details_toggle/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-details-toggle" aria-describedby="edit-form-details-toggle--description" type="checkbox" id="edit-form-details-toggle" name="form_details_toggle" value checked="checked" class="form-checkbox" />');
$this
->assertRaw('If checked, an expand/collapse all (details) link will be added to this webform when there are two or more details elements available on the webform.');
$webform_form_details_toggle
->setSetting('form_details_toggle', FALSE);
$webform_form_details_toggle
->save();
$this
->drupalGet('/webform/test_form_details_toggle');
$this
->assertNoCssSelect('webform.webform-details-toggle', 'Webform does not have the .webform-details-toggle class.');
$webform_form_inline_errors = Webform::load('test_form_disable_inline_errors');
$this
->postSubmission($webform_form_inline_errors);
$this
->assertPattern('#<h2 class="visually-hidden">Error message</h2>\\s+textfield field is required.#m');
\Drupal::service('module_installer')
->install([
'inline_form_errors',
]);
$this
->postSubmission($webform_form_inline_errors);
$this
->assertPattern('#<h2 class="visually-hidden">Error message</h2>\\s+textfield field is required.#m');
$webform_form_inline_errors
->setSetting('form_disable_inline_errors', FALSE);
$webform_form_inline_errors
->save();
$this
->postSubmission($webform_form_inline_errors);
$this
->assertNoPattern('#<h2 class="visually-hidden">Error message</h2>\\s+textfield field is required.#m');
$this
->assertRaw('1 error has been found: <ul class="item-list__comma-list"><li><a href="#edit-textfield">textfield</a></li></ul>');
$this
->assertRaw('textfield field is required.');
$this
->drupalGet('/admin/structure/webform/manage/test_form_disable_inline_errors/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-disable-inline-errors" aria-describedby="edit-form-disable-inline-errors--description" type="checkbox" id="edit-form-disable-inline-errors" name="form_disable_inline_errors" value class="form-checkbox" />');
$this
->assertRaw('If checked, <a href="https://www.drupal.org/docs/8/core/modules/inline-form-errors/inline-form-errors-module-overview">inline form errors</a> will be disabled for this form.');
\Drupal::configFactory()
->getEditable('webform.settings')
->set('settings.default_form_disable_inline_errors', TRUE)
->save();
$this
->drupalGet('/admin/structure/webform/manage/test_form_disable_inline_errors/settings/form');
$this
->assertRaw('<input data-drupal-selector="edit-form-disable-inline-errors-disabled" aria-describedby="edit-form-disable-inline-errors-disabled--description" disabled="disabled" type="checkbox" id="edit-form-disable-inline-errors-disabled" name="form_disable_inline_errors_disabled" value="1" checked="checked" class="form-checkbox" />');
$this
->assertRaw('Inline form errors is disabled for all forms.');
$this
->assertNoRaw('1 error has been found: <ul class="item-list__comma-list"><li><a href="#edit-textfield">textfield</a></li></ul>');
$this
->assertNoRaw('textfield field is required.');
}
}