View source
<?php
namespace Drupal\Tests\webform\Functional\Settings;
use Drupal\Component\Utility\Html;
use Drupal\user\Entity\User;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;
class WebformSettingsDraftTest extends WebformBrowserTestBase {
protected static $testWebforms = [
'test_form_draft_authenticated',
'test_form_draft_anonymous',
'test_form_draft_multiple',
'test_form_preview',
];
public function testDraft() {
$webform_submission_storage = \Drupal::entityTypeManager()
->getStorage('webform_submission');
$normal_user = $this
->drupalCreateUser([
'view own webform submission',
]);
$admin_submission_user = $this
->drupalCreateUser([
'administer webform submission',
]);
$this
->drupalGet('/webform/contact/drafts');
$this
->assertResponse(403);
$this
->drupalGet('/webform/test_form_draft_authenticated/drafts');
$this
->assertResponse(403);
$this
->drupalGet('/webform/test_form_draft_anonymous/drafts');
$this
->assertResponse(200);
$webform_ids = [
'test_form_draft_authenticated' => 'Test: Webform: Draft authenticated',
'test_form_draft_anonymous' => 'Test: Webform: Draft anonymous',
];
foreach ($webform_ids as $webform_id => $webform_title) {
$is_authenticated = $webform_id === 'test_form_draft_authenticated' ? TRUE : FALSE;
$is_authenticated ? $this
->drupalLogin($normal_user) : $this
->drupalLogout();
$webform = Webform::load($webform_id);
$sid = $this
->postSubmission($webform, [
'name' => 'John Smith',
], 'Save Draft');
$webform_submission = WebformSubmission::load($sid);
$this
->assertRaw('Your draft has been saved');
$this
->assertNoRaw('You have an existing draft');
$this
->drupalGet("webform/{$webform_id}/drafts");
$this
->assertResponse(200);
$account = $is_authenticated ? $normal_user : User::getAnonymousUser();
$this
->assertRaw('<title>' . Html::escape('Drafts for ' . $webform
->label() . ' for ' . ($account
->getAccountName() ?: 'Anonymous') . ' | Drupal') . '</title>');
$this
->assertRaw('<div>1 draft</div>');
$this
->drupalGet("webform/{$webform_id}");
$this
->assertNoRaw('Your draft has been saved');
$this
->assertRaw('You have an existing draft');
$this
->assertFieldByName('name', 'John Smith');
$webform
->setStatus(FALSE)
->save();
$this
->drupalGet("webform/{$webform_id}");
$this
->assertNoRaw('You have an existing draft');
$this
->assertNoFieldByName('name', 'John Smith');
$this
->assertRaw('Sorry… This form is closed to new submissions.');
$webform
->setStatus(TRUE)
->save();
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet("admin/structure/webform/manage/{$webform_id}/submission/{$sid}");
$this
->assertRaw('<div><b>Is draft:</b> Yes</div>');
$is_authenticated ? $this
->drupalLogin($normal_user) : $this
->drupalLogout();
$this
->drupalPostForm("webform/{$webform_id}", [
'name' => '',
'comment' => 'Hello World!',
], 'Save Draft');
$this
->assertRaw('Your draft has been saved');
$this
->assertNoRaw('You have an existing draft');
$this
->assertFieldByName('name', '');
$this
->assertFieldByName('comment', 'Hello World!');
$this
->drupalPostForm("webform/{$webform_id}", [
'name' => 'John Smith',
'comment' => 'Hello World!',
], 'Preview');
$this
->assertNoRaw('Your draft has been saved');
$this
->assertNoRaw('You have an existing draft');
$this
->assertNoFieldByName('name', '');
$this
->assertNoFieldByName('comment', 'Hello World!');
$this
->assertRaw('<label>Name</label>');
$this
->assertRaw('<label>Comment</label>');
$this
->assertRaw('Please review your submission. Your submission is not complete until you press the "Submit" button!');
$this
->drupalPostForm("webform/{$webform_id}", [], 'Submit');
$this
->assertRaw("New submission added to {$webform_title}.");
$this
->drupalGet("webform/{$webform_id}");
$this
->assertNoRaw('Your draft has been saved');
$this
->assertNoRaw('You have an existing draft');
$this
->assertFieldByName('name', '');
$this
->assertFieldByName('comment', '');
}
$webform = Webform::load('test_form_draft_anonymous');
$sid = $this
->postSubmission($webform, [
'name' => 'John Smith',
], 'Save Draft');
$this
->assertRaw('Your draft has been saved');
$webform_submission_storage
->resetCache();
$webform_submission = WebformSubmission::load($sid);
$this
->assertEqual($webform_submission
->getOwnerId(), 0);
$this
->drupalGet('/webform/test_form_draft_anonymous');
$this
->assertRaw('You have an existing draft');
$this
->assertFieldByName('name', 'John Smith');
$this
->drupalLogin($normal_user);
$webform_submission_storage
->resetCache();
$webform_submission = WebformSubmission::load($sid);
$this
->assertEqual($webform_submission
->getOwnerId(), $normal_user
->id());
$this
->drupalLogout();
$webform
->setSetting('form_convert_anonymous', FALSE)
->save();
$sid = $this
->postSubmission($webform, [
'name' => 'John Smith',
]);
$this
->drupalLogin($normal_user);
$webform_submission_storage
->resetCache();
$webform_submission = WebformSubmission::load($sid);
$this
->assertEqual($webform_submission
->getOwnerId(), 0);
$this
->drupalLogout();
$webform
->setSetting('form_confidential', TRUE);
$sid = $this
->postSubmission($webform, [
'name' => 'John Smith',
], 'Save Draft');
$this
->assertRaw('Your draft has been saved');
$webform_submission_storage
->resetCache();
$webform_submission = WebformSubmission::load($sid);
$this
->assertEqual($webform_submission
->getOwnerId(), 0);
$this
->drupalGet('/webform/test_form_draft_anonymous');
$this
->assertRaw('You have an existing draft');
$this
->drupalLogin($normal_user);
$webform_submission_storage
->resetCache();
$webform_submission = WebformSubmission::load($sid);
$this
->assertNotEqual($webform_submission
->getOwnerId(), $normal_user
->id());
$this
->assertEqual($webform_submission
->getOwnerId(), 0);
$this
->drupalLogin($admin_submission_user);
$this
->drupalGet('/admin/structure/webform/manage/test_form_draft_authenticated/results/download');
$this
->assertFieldByName('state', 'all');
$this
->drupalGet('/admin/structure/webform/manage/test_form_preview/results/download');
$this
->assertNoFieldByName('state', 'all');
$this
->drupalPostForm('/webform/test_form_draft_authenticated', [], 'Submit');
$this
->assertRaw('Name field is required.');
$this
->drupalGet('/webform/test_form_draft_authenticated');
$this
->assertRaw('You have an existing draft');
$this
->drupalPostForm('/webform/test_form_draft_authenticated', [
'name' => 'John Smith',
], 'Preview');
$this
->assertRaw('Please review your submission.');
$this
->drupalGet('/webform/test_form_draft_authenticated');
$this
->assertRaw('You have an existing draft');
$this
->assertRaw('<label>Name</label>' . PHP_EOL . ' John Smith');
$config = \Drupal::configFactory()
->getEditable('webform.settings');
$this
->drupalLogin($normal_user);
$webform = Webform::load('test_form_draft_multiple');
$sid_1 = $this
->postSubmission($webform, [
'name' => 'John Smith',
], 'Save Draft');
$this
->assertRaw('Submission saved. You may return to this form later and it will restore the current values.');
$webform_submission_1 = WebformSubmission::load($sid_1);
$this
->drupalGet('/webform/test_form_draft_multiple');
$this
->assertNoRaw('You have saved drafts.');
$this
->assertRaw('You have a pending draft for this webform.');
$this
->assertFieldByName('name', '');
$default_draft_pending_single_message = $config
->get('settings.default_draft_pending_single_message');
$config
->set('settings.default_draft_pending_single_message', '{default_draft_pending_single_message}')
->save();
$this
->drupalGet('/webform/test_form_draft_multiple');
$this
->assertNoRaw('You have a pending draft for this webform.');
$this
->assertRaw('{default_draft_pending_single_message}');
$config
->set('settings.default_draft_pending_single_message', $default_draft_pending_single_message)
->save();
$webform
->setSetting('draft_pending_single_message', '{draft_pending_single_message}')
->save();
$this
->drupalGet('/webform/test_form_draft_multiple');
$this
->assertNoRaw('You have a pending draft for this webform.');
$this
->assertRaw('{draft_pending_single_message}');
$webform
->setSetting('draft_pending_single_message', '')
->save();
$this
->drupalGet('/webform/test_form_draft_multiple');
$this
->clickLink('Load your pending draft');
$this
->assertFieldByName('name', 'John Smith');
$this
->drupalGet('/webform/test_form_draft_multiple', [
'query' => [
'token' => $webform_submission_1
->getToken(),
],
]);
$this
->assertFieldByName('name', 'John Smith');
$this
->drupalGet('/webform/test_form_draft_multiple/drafts');
$this
->assertRaw('token=' . $webform_submission_1
->getToken());
$sid_2 = $this
->postSubmission($webform, [
'name' => 'John Smith',
], 'Save Draft');
$webform_submission_2 = WebformSubmission::load($sid_2);
$this
->assertRaw('Submission saved. You may return to this form later and it will restore the current values.');
$this
->drupalGet('/webform/test_form_draft_multiple');
$this
->assertNoRaw('You have a pending draft for this webform.');
$this
->assertRaw('You have pending drafts for this webform. <a href="' . base_path() . 'webform/test_form_draft_multiple/drafts">View your pending drafts</a>.');
$default_draft_pending_multiple_message = $config
->get('settings.default_draft_pending_multiple_message');
$config
->set('settings.default_draft_pending_multiple_message', '{default_draft_pending_multiple_message}')
->save();
$this
->drupalGet('/webform/test_form_draft_multiple');
$this
->assertNoRaw('You have pending drafts for this webform.');
$this
->assertRaw('{default_draft_pending_multiple_message}');
$config
->set('settings.default_draft_pending_multiple_message', $default_draft_pending_multiple_message)
->save();
$webform
->setSetting('draft_pending_multiple_message', '{draft_pending_multiple_message}')
->save();
$this
->drupalGet('/webform/test_form_draft_multiple');
$this
->assertNoRaw('You have pending drafts for this webform.');
$this
->assertRaw('{draft_pending_multiple_message}');
$webform
->setSetting('draft_pending_multiple_message', '')
->save();
$this
->drupalGet('/webform/test_form_draft_multiple/drafts');
$this
->assertRaw('token=' . $webform_submission_1
->getToken());
$this
->assertRaw('token=' . $webform_submission_2
->getToken());
$this
->drupalLogout();
$this
->drupalGet('/webform/test_form_draft_multiple', [
'query' => [
'token' => $webform_submission_1
->getToken(),
],
]);
$this
->assertFieldByName('name', '');
$this
->postSubmission($webform, [
'name' => 'Jane Doe',
], 'Save Draft');
$this
->assertRaw('Submission saved. You may return to this form later and it will restore the current values.');
$this
->drupalGet('/webform/test_form_draft_multiple');
$this
->assertNoRaw('You have saved drafts.');
$this
->assertRaw('You have a pending draft for this webform.');
$this
->assertFieldByName('name', '');
$this
->drupalGet('/webform/test_form_draft_multiple');
$this
->clickLink('Load your pending draft');
$this
->assertFieldByName('name', 'Jane Doe');
$total_drafts = $webform_submission_storage
->getTotal($webform, NULL, NULL, [
'in_draft' => TRUE,
]);
$this
->postSubmission($webform);
$this
->assertRaw('Name field is required.');
$total = $webform_submission_storage
->getTotal($webform, NULL, NULL, [
'in_draft' => TRUE,
]);
$this
->assertEqual($total_drafts + 1, $total);
$this
->drupalPostForm(NULL, [], 'Submit');
$this
->assertRaw('Name field is required.');
$total = $webform_submission_storage
->getTotal($webform, NULL, NULL, [
'in_draft' => TRUE,
]);
$this
->assertEqual($total_drafts + 1, $total);
$this
->drupalLogin($this->rootUser);
$webform = Webform::load('test_form_draft_authenticated');
$sid = $this
->postSubmission($webform, [
'name' => 'John Smith',
], 'Save Draft');
$this
->assertNotNull($sid);
$webform_submission = WebformSubmission::load($sid);
$this
->assertEqual($sid, $webform_submission
->id());
$this
->postSubmission($webform, [], 'Reset');
$webform_submission_storage
->resetCache();
$webform_submission = WebformSubmission::load($sid);
$this
->assertNull($webform_submission);
$sid = $this
->postSubmission($webform, [
'name' => 'John Smith',
'comment' => 'This is a comment',
], 'Save Draft');
$this
->postSubmission($webform);
$webform_submission_storage
->resetCache();
$webform_submission = WebformSubmission::load($sid);
$this
->assertEqual('This is a comment', $webform_submission
->getElementData('comment'));
$this
->drupalPostForm('/admin/structure/webform/manage/test_form_draft_authenticated/submission/' . $sid . '/edit', [
'comment' => 'This is ignored',
], 'Reset');
$webform_submission_storage
->resetCache();
$webform_submission = WebformSubmission::load($sid);
$this
->assertEqual($sid, $webform_submission
->id());
$this
->assertEqual('This is a comment', $webform_submission
->getElementData('comment'));
$this
->assertNotEqual('This is ignored', $webform_submission
->getElementData('comment'));
$total = $webform_submission_storage
->getTotal($webform, NULL, $this->rootUser, [
'in_draft' => TRUE,
]);
$this
->assertEqual(0, $total);
}
}