View source
<?php
namespace Drupal\Tests\webform\Functional;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Utility\WebformElementHelper;
use Drupal\webform\WebformSubmissionForm;
use Drupal\webform\WebformSubmissionInterface;
class WebformSubmissionApiTest extends WebformBrowserTestBase {
protected static $testWebforms = [
'test_form_wizard_advanced',
'test_form_limit',
];
public function testApi() {
$normal_user = $this
->drupalCreateUser();
$contact_webform = Webform::load('contact');
$values = [
'webform_id' => 'contact',
'data' => [
'name' => 'Dixisset',
'company' => 'Dixisset',
'email' => 'test@test.com',
'subject' => 'Testing contact webform from [site:name]',
'message' => 'Please ignore this email.',
],
];
$webform_submission = WebformSubmissionForm::submitFormValues($values);
$this
->assertEqual($webform_submission
->id(), $this
->getLastSubmissionId($contact_webform));
$values = [
'webform_id' => 'contact',
'data' => [
'email' => 'invalid',
],
];
$errors = WebformSubmissionForm::validateFormValues($values);
WebformElementHelper::convertRenderMarkupToStrings($errors);
$this
->assertEqual($errors, [
'name' => 'Your Name field is required.',
'email' => 'The email address <em class="placeholder">invalid</em> is not valid.',
'subject' => 'Subject field is required.',
'message' => 'Message field is required.',
]);
$values = [
'webform_id' => 'contact',
'in_draft' => TRUE,
'data' => [],
];
$errors = WebformSubmissionForm::validateFormValues($values);
if ($errors) {
WebformElementHelper::convertRenderMarkupToStrings($errors);
}
$this
->assertEqual($errors, [
'name' => 'Your Name field is required.',
'email' => 'Your Email field is required.',
'subject' => 'Subject field is required.',
'message' => 'Message field is required.',
]);
$values = [
'webform_id' => 'contact',
'in_draft' => TRUE,
'data' => [],
];
$webform_submission = WebformSubmissionForm::submitFormValues($values);
$this
->assertInstanceOf(WebformSubmissionInterface::class, $webform_submission);
$test_form_wizard_advanced_webform = Webform::load('test_form_wizard_advanced');
$values = [
'webform_id' => 'test_form_wizard_advanced',
'data' => [
'first_name' => 'Ringo',
'last_name' => 'Starr',
'gender' => 'Male',
'email' => 'example@example.com',
'phone' => '123-456-7890',
'comments' => 'Huius, Lyco, oratione locuples, rebus ipsis ielunior. Duo Reges: constructio interrete. Sed haec in pueris; Sed utrum hortandus es nobis, Luci, inquit, an etiam tua sponte propensus es? Sapiens autem semper beatus est et est aliquando in dolore; Immo videri fortasse. Paulum, cum regem Persem captum adduceret, eodem flumine invectio? Et ille ridens: Video, inquit, quid agas;',
],
];
$webform_submission = WebformSubmissionForm::submitFormValues($values);
$this
->assertEqual($webform_submission
->id(), $this
->getLastSubmissionId($test_form_wizard_advanced_webform));
$values = [
'webform_id' => 'test_form_wizard_advanced',
'data' => [
'email' => 'invalid',
],
];
$errors = WebformSubmissionForm::validateFormValues($values);
WebformElementHelper::convertRenderMarkupToStrings($errors);
$this
->assertEqual($errors, [
'email' => 'The email address <em class="placeholder">invalid</em> is not valid.',
]);
$values = [
'webform_id' => 'test_form_wizard_advanced',
'data' => [
'first_name' => 'Ringo',
'last_name' => 'Starr',
'gender' => 'INVALID',
'email' => 'example@example.com',
'phone' => '123-456-7890',
'comments' => 'Huius, Lyco, oratione locuples, rebus ipsis ielunior. Duo Reges: constructio interrete. Sed haec in pueris; Sed utrum hortandus es nobis, Luci, inquit, an etiam tua sponte propensus es? Sapiens autem semper beatus est et est aliquando in dolore; Immo videri fortasse. Paulum, cum regem Persem captum adduceret, eodem flumine invectio? Et ille ridens: Video, inquit, quid agas;',
],
];
$errors = WebformSubmissionForm::validateFormValues($values);
WebformElementHelper::convertRenderMarkupToStrings($errors);
$this
->assertEqual($errors, [
'gender' => 'An illegal choice has been detected. Please contact the site administrator.',
]);
$this
->drupalLogin($normal_user);
$test_form_limit_webform = Webform::load('test_form_limit');
$this
->assertTrue(WebformSubmissionForm::isOpen($test_form_limit_webform));
$values = [
'webform_id' => 'test_form_limit',
'data' => [
'name' => 'Oratione',
],
];
$webform_submission = WebformSubmissionForm::submitFormValues($values);
$this
->assertEqual($webform_submission
->id(), $this
->getLastSubmissionId($test_form_limit_webform));
$result = WebformSubmissionForm::isOpen($test_form_limit_webform);
$this
->assertEqual($result['#markup'], 'You are only allowed to have 1 submission for this webform.');
$this
->drupalLogin($this->rootUser);
WebformSubmissionForm::submitFormValues($values);
WebformSubmissionForm::submitFormValues($values);
WebformSubmissionForm::submitFormValues($values);
$result = WebformSubmissionForm::isOpen($test_form_limit_webform);
$this
->assertEqual($result['#markup'], 'Only 4 submissions are allowed.');
$test_form_limit_webform
->setStatus(FALSE)
->save();
$result = WebformSubmissionForm::isOpen($test_form_limit_webform);
$this
->assertEqual($result['#markup'], 'Sorry… This form is closed to new submissions.');
}
}