View source
<?php
namespace Drupal\Tests\webform\Functional;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformSubmissionTest extends WebformBrowserTestBase {
public static $modules = [
'node',
'webform',
'webform_test_submissions',
];
protected static $testWebforms = [
'test_submissions',
];
public function testWebformSubmission() {
$normal_user = $this
->drupalCreateUser();
$webform = Webform::load('test_submissions');
$submissions = array_values(\Drupal::entityTypeManager()
->getStorage('webform_submission')
->loadByProperties([
'webform_id' => 'test_submissions',
]));
$webform_submission = reset($submissions);
$this
->assertInstanceOf(WebformSubmission::class, $webform_submission);
$this
->assertEqual($webform_submission
->getWebform()
->id(), $webform
->id());
$this
->assertNull($webform_submission
->getSourceEntity());
$webform_submission
->set('uri', NULL)
->save();
$this
->assertEqual($webform_submission
->getSourceUrl()
->toString(), $webform
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString());
$webform_submission
->set('entity_type', 'user')
->set('entity_id', $normal_user
->id())
->save();
$this
->assertEqual($webform_submission
->getSourceUrl()
->toString(), $normal_user
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString());
try {
WebformSubmission::create();
$this
->fail('Webform id (webform_id) is required to create a webform submission.');
} catch (\Exception $exception) {
$this
->pass('Webform id (webform_id) is required to create a webform submission.');
}
$webform_submission = WebformSubmission::create([
'webform_id' => $webform
->id(),
'data' => [
'custom' => 'value',
],
]);
$this
->assertEqual($webform_submission
->getData(), [
'custom' => 'value',
]);
$webform_submission
->save();
$this
->assertEqual($webform_submission
->label(), $webform
->label() . ': Submission #' . $webform_submission
->serial());
$this
->drupalLogin($this->rootUser);
$sid = $this
->postSubmissionTest($webform);
$webform_submission = WebformSubmission::load($sid);
$this
->assertEqual($webform_submission
->getSourceUrl()
->toString(), $webform
->toUrl('canonical', [
'absolute' => TRUE,
])
->toString());
$this
->drupalLogout();
}
public function testDuplicate() {
$admin_submission_user = $this
->drupalCreateUser([
'administer webform submission',
]);
$this
->drupalLogin($admin_submission_user);
$webform = Webform::load('contact');
$sid = $this
->postSubmission($webform, [
'subject' => '{Original Subject}',
'message' => '{Original Message}',
]);
$webform_submission = WebformSubmission::load($sid);
$this
->drupalGet("admin/structure/webform/manage/contact/submission/{$sid}/duplicate");
$this
->assertRaw('Duplicate Contact: Submission #' . $webform_submission
->serial());
$this
->drupalPostForm("admin/structure/webform/manage/contact/submission/{$sid}/duplicate", [
'subject' => '{Duplicate Subject}',
], 'Send message');
$duplicate_sid = $this
->getLastSubmissionId($webform);
$duplicate_submission = WebformSubmission::load($duplicate_sid);
$this
->assertNotEqual($sid, $duplicate_sid);
$this
->assertEqual($duplicate_submission
->getElementData('subject'), '{Duplicate Subject}');
$this
->assertEqual($duplicate_submission
->getElementData('message'), '{Original Message}');
}
}