View source
<?php
namespace Drupal\Tests\webform\Kernel\Entity;
use Drupal\KernelTests\KernelTestBase;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformSubmissionEntityTest extends KernelTestBase {
public static $modules = [
'system',
'webform',
'user',
'field',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('webform', [
'webform',
]);
$this
->installConfig('webform');
$this
->installEntitySchema('webform_submission');
$this
->installEntitySchema('user');
}
public function testWebformMethods() {
$webform = Webform::create([
'id' => 'webform_test',
'title' => 'Test',
]);
$elements = [
'name' => [
'#type' => 'textfield',
'#title' => 'name',
],
'other' => [
'#type' => 'textfield',
'#title' => 'other',
],
];
$webform
->setElements($elements);
$webform
->save();
$webform
->save();
$values = [
'id' => 'webform_submission_test',
'webform_id' => $webform
->id(),
'data' => [
'name' => 'John Smith',
],
];
$webform_submission = WebformSubmission::create($values);
$webform_submission
->save();
$this
->assertEquals($webform
->uuid(), $webform_submission
->getWebform()
->uuid());
$this
->assertEquals($webform_submission
->getData(), [
'name' => 'John Smith',
]);
$this
->assertEquals($webform_submission
->getElementData('name'), 'John Smith');
$this
->assertEquals($webform_submission
->getElementData('name'), 'John Smith');
$webform_submission
->setElementData('other', 'Other');
$this
->assertEquals($webform_submission
->getElementData('other'), 'Other');
$this
->assertEquals($webform_submission
->getData(), [
'name' => 'John Smith',
'other' => 'Other',
]);
$this
->assertEquals($webform_submission
->label(), 'Test: Submission #1');
\Drupal::configFactory()
->getEditable('webform.settings')
->set('settings.default_submission_label', 'Submission #[webform_submission:serial]')
->save();
$this
->assertEquals($webform_submission
->label(), 'Submission #1');
$webform = $webform_submission
->getWebform();
$webform
->setSetting('submission_label', 'Submitted by [webform_submission:values:name]')
->save();
$this
->assertEquals($webform
->getSetting('submission_label'), 'Submitted by [webform_submission:values:name]');
$this
->assertEquals($webform_submission
->label(), 'Submitted by John Smith');
}
}