Commit c1cf742e authored by jrockowitz's avatar jrockowitz Committed by jrockowitz

Issue #2912197 by jrockowitz: Add set/get submission element value methods

parent 8226da92
......@@ -53,13 +53,13 @@ class WebformExampleCompositeTest extends WebformTestBase {
];
$sid = $this->postSubmission($webform, $edit);
$webform_submission = WebformSubmission::load($sid);
$this->assertEqual($webform_submission->getData('webform_example_composite'), [
$this->assertEqual($webform_submission->getElementData('webform_example_composite'), [
'first_name' => 'John',
'last_name' => 'Smith',
'gender' => 'Male',
'date_of_birth' => '1910-01-01',
]);
$this->assertEqual($webform_submission->getData('webform_example_composite_multiple'), [
$this->assertEqual($webform_submission->getElementData('webform_example_composite_multiple'), [
[
'first_name' => 'Jane',
'last_name' => 'Doe',
......
......@@ -42,8 +42,8 @@ class WebformExampleElementTest extends WebformTestBase {
];
$sid = $this->postSubmission($webform, $edit);
$webform_submission = WebformSubmission::load($sid);
$this->assertEqual($webform_submission->getData('webform_example_element'), '{Test}');
$this->assertEqual($webform_submission->getData('webform_example_element_multiple'), ['{Test 01}']);
$this->assertEqual($webform_submission->getElementData('webform_example_element'), '{Test}');
$this->assertEqual($webform_submission->getElementData('webform_example_element_multiple'), ['{Test 01}']);
}
}
......@@ -340,13 +340,26 @@ class WebformSubmission extends ContentEntityBase implements WebformSubmissionIn
/**
* {@inheritdoc}
*/
public function getData($key = NULL) {
if ($key !== NULL) {
return (isset($this->data[$key])) ? $this->data[$key] : NULL;
}
else {
return $this->data;
public function getElementData($key) {
return (isset($this->data[$key])) ? $this->data[$key] : NULL;
}
/**
* {@inheritdoc}
*/
public function setElementData($key, $value) {
// Make sure the element exists before setting its value.
if ($this->getWebform()->getElement($key)) {
$this->data[$key] = $value;
}
return $this;
}
/**
* {@inheritdoc}
*/
public function getData() {
return $this->data;
}
/**
......@@ -360,13 +373,8 @@ class WebformSubmission extends ContentEntityBase implements WebformSubmissionIn
/**
* {@inheritdoc}
*/
public function getOriginalData($key = NULL) {
if ($key !== NULL) {
return (isset($this->originalData[$key])) ? $this->originalData[$key] : NULL;
}
else {
return $this->originalData;
}
public function getOriginalData() {
return $this->originalData;
}
/**
......
......@@ -93,7 +93,7 @@ abstract class WebformComputedBase extends WebformElementBase implements Webform
public function getValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
if (!empty($element['#store'])) {
// Get stored value if it is set.
$value = $webform_submission->getData($element['#webform_key']);
$value = $webform_submission->getElementData($element['#webform_key']);
if (isset($value)) {
return $value;
}
......
......@@ -1181,7 +1181,7 @@ class WebformElementBase extends PluginBase implements WebformElementInterface {
}
$webform_key = (isset($options['webform_key'])) ? $options['webform_key'] : $element['#webform_key'];
$value = $webform_submission->getData($webform_key);
$value = $webform_submission->getElementData($webform_key);
// Is value is NULL and there is a #default_value, then use it.
if ($value === NULL && isset($element['#default_value'])) {
$value = $element['#default_value'];
......
......@@ -712,7 +712,7 @@ class EmailWebformHandler extends WebformHandlerBase implements WebformHandlerMe
}
// Get submission email addresseses as an array.
$options_element_value = $webform_submission->getData($element_name);
$options_element_value = $webform_submission->getElementData($element_name);
if (is_array($options_element_value)) {
$options_values = $options_element_value;
}
......@@ -798,7 +798,7 @@ class EmailWebformHandler extends WebformHandlerBase implements WebformHandlerMe
}
// Get file ids.
$fids = $webform_submission->getData($configuration_key);
$fids = $webform_submission->getElementData($configuration_key);
if (empty($fids)) {
continue;
}
......
......@@ -37,7 +37,7 @@ class WebformElementManagedFilePrivateTest extends WebformElementManagedFileTest
$file = File::load($fid);
// Check that test file 3 was uploaded to the current submission.
$this->assertEqual($submission->getData('managed_file_single'), $fid, 'Test file 3 was upload to the current submission');
$this->assertEqual($submission->getElementData('managed_file_single'), $fid, 'Test file 3 was upload to the current submission');
// Check test file 3 file usage.
$this->assertIdentical(['webform' => ['webform_submission' => [$sid => '1']]], $this->fileUsage->listUsage($file), 'The file has 3 usage.');
......
......@@ -80,7 +80,7 @@ class WebformElementManagedFileTest extends WebformElementManagedFileTestBase {
// Check that test file was uploaded to the current submission.
$second = ($type == 'multiple') ? [$fid] : $fid;
$this->assertEqual($submission->getData($key), $second, 'Test file was upload to the current submission');
$this->assertEqual($submission->getElementData($key), $second, 'Test file was upload to the current submission');
// Check test file file usage.
$this->assertIdentical(['webform' => ['webform_submission' => [$sid => '1']]], $this->fileUsage->listUsage($file), 'The file has 1 usage.');
......@@ -131,7 +131,7 @@ class WebformElementManagedFileTest extends WebformElementManagedFileTestBase {
// Check that test new file was uploaded to the current submission.
$second = ($type == 'multiple') ? [$new_fid] : $new_fid;
$this->assertEqual($submission->getData($key), $second, 'Test new file was upload to the current submission');
$this->assertEqual($submission->getElementData($key), $second, 'Test new file was upload to the current submission');
// Check that test file was deleted from the disk and database.
$this->assert(!file_exists($file->getFileUri()), 'Test file deleted from disk');
......
......@@ -99,7 +99,7 @@ class WebformElementMediaFileTest extends WebformElementManagedFileTestBase {
// Check that test file was uploaded to the current submission.
$second = ($type == 'multiple') ? [$fid] : $fid;
$this->assertEqual($submission->getData($key), $second, 'Test file was upload to the current submission');
$this->assertEqual($submission->getElementData($key), $second, 'Test file was upload to the current submission');
// Check test file file usage.
$this->assertIdentical(['webform' => ['webform_submission' => [$sid => '1']]], $this->fileUsage->listUsage($file), 'The file has 1 usage.');
......@@ -150,7 +150,7 @@ class WebformElementMediaFileTest extends WebformElementManagedFileTestBase {
// Check that test new file was uploaded to the current submission.
$second = ($type == 'multiple') ? [$new_fid] : $new_fid;
$this->assertEqual($submission->getData($key), $second, 'Test new file was upload to the current submission');
$this->assertEqual($submission->getElementData($key), $second, 'Test new file was upload to the current submission');
// Check that test file was deleted from the disk and database.
$this->assert(!file_exists($file->getFileUri()), 'Test file deleted from disk');
......
......@@ -49,7 +49,7 @@ class WebformHandlerRemotePostTest extends WebformTestBase {
// Check confirmation number is set via the
// [webform:handler:remote_post:completed:confirmation_number] token.
$this->assertRaw('Your confirmation number is ' . $webform_submission->getData('confirmation_number') . '.');
$this->assertRaw('Your confirmation number is ' . $webform_submission->getElementData('confirmation_number') . '.');
// Check custom header.
$this->assertRaw('{"custom_header":"true"}');
......
......@@ -79,7 +79,7 @@ class WebformResultsExportDownloadTest extends WebformTestBase {
$submissions = WebformSubmission::loadMultiple($sids);
foreach ($submissions as $submission) {
$serial = $submission->serial();
$fid = $submission->getData('managed_file_single');
$fid = $submission->getElementData('managed_file_single');
$filename = File::load($fid)->getFilename();
$this->assert(isset($files["submission-$serial/$filename"]));
......@@ -110,7 +110,7 @@ class WebformResultsExportDownloadTest extends WebformTestBase {
$submissions = WebformSubmission::loadMultiple($sids);
foreach ($submissions as $submission) {
$serial = $submission->serial();
$fid = $submission->getData('managed_file_single');
$fid = $submission->getElementData('managed_file_single');
$filename = File::load($fid)->getFilename();
$this->assert(isset($files["submission-$serial.yml"]));
......
......@@ -65,26 +65,26 @@ class WebformSubmissionListBuilderTest extends WebformTestBase {
$this->assertLinkByHref($submissions[0]->toUrl()->toString());
$this->assertLinkByHref($submissions[1]->toUrl()->toString());
$this->assertLinkByHref($submissions[2]->toUrl()->toString());
$this->assertRaw($submissions[0]->getData('first_name'));
$this->assertRaw($submissions[1]->getData('first_name'));
$this->assertRaw($submissions[2]->getData('first_name'));
$this->assertRaw($submissions[0]->getElementData('first_name'));
$this->assertRaw($submissions[1]->getElementData('first_name'));
$this->assertRaw($submissions[2]->getElementData('first_name'));
$this->assertNoFieldById('edit-reset', 'reset');
// Check results filtered by key(word).
$this->drupalPostForm('admin/structure/webform/manage/' . $webform->id() . '/results/submissions', ['search' => $submissions[0]->getData('first_name')], t('Filter'));
$this->assertUrl('admin/structure/webform/manage/' . $webform->id() . '/results/submissions?search=' . $submissions[0]->getData('first_name') . '&state=');
$this->assertRaw($submissions[0]->getData('first_name'));
$this->assertNoRaw($submissions[1]->getData('first_name'));
$this->assertNoRaw($submissions[2]->getData('first_name'));
$this->drupalPostForm('admin/structure/webform/manage/' . $webform->id() . '/results/submissions', ['search' => $submissions[0]->getElementData('first_name')], t('Filter'));
$this->assertUrl('admin/structure/webform/manage/' . $webform->id() . '/results/submissions?search=' . $submissions[0]->getElementData('first_name') . '&state=');
$this->assertRaw($submissions[0]->getElementData('first_name'));
$this->assertNoRaw($submissions[1]->getElementData('first_name'));
$this->assertNoRaw($submissions[2]->getElementData('first_name'));
$this->assertFieldById('edit-reset', 'Reset');
// Check results filtered by state.
$this->drupalPostForm('admin/structure/webform/manage/' . $webform->id() . '/results/submissions', ['state' => 'starred'], t('Filter'));
$this->assertUrl('admin/structure/webform/manage/' . $webform->id() . '/results/submissions?search=&state=starred');
$this->assertRaw('<option value="starred" selected="selected">Starred [1]</option>');
$this->assertNoRaw($submissions[0]->getData('first_name'));
$this->assertRaw($submissions[1]->getData('first_name'));
$this->assertNoRaw($submissions[2]->getData('first_name'));
$this->assertNoRaw($submissions[0]->getElementData('first_name'));
$this->assertRaw($submissions[1]->getElementData('first_name'));
$this->assertNoRaw($submissions[2]->getElementData('first_name'));
$this->assertFieldById('edit-reset', 'Reset');
/**************************************************************************/
......@@ -137,9 +137,9 @@ class WebformSubmissionListBuilderTest extends WebformTestBase {
// Check that only one result (Hillary #2) is displayed with pager.
$this->drupalGet('admin/structure/webform/manage/' . $webform->id() . '/results/submissions');
$this->assertNoRaw($submissions[0]->getData('first_name'));
$this->assertNoRaw($submissions[1]->getData('first_name'));
$this->assertRaw($submissions[2]->getData('first_name'));
$this->assertNoRaw($submissions[0]->getElementData('first_name'));
$this->assertNoRaw($submissions[1]->getElementData('first_name'));
$this->assertRaw($submissions[2]->getElementData('first_name'));
$this->assertRaw('<nav class="pager" role="navigation" aria-labelledby="pagination-heading">');
// Reset the limit to 20.
......
......@@ -106,8 +106,8 @@ class WebformSubmissionTest extends WebformTestBase {
// Check duplicate submission.
$this->assertNotEqual($sid, $duplicate_sid);
$this->assertEqual($duplicate_submission->getData('subject'), '{Duplicate Subject}');
$this->assertEqual($duplicate_submission->getData('message'), '{Original Message}');
$this->assertEqual($duplicate_submission->getElementData('subject'), '{Duplicate Subject}');
$this->assertEqual($duplicate_submission->getElementData('message'), '{Original Message}');
}
}
......@@ -45,7 +45,7 @@ class WebformSubmissionTokenUpdateTest extends WebformTestBase {
$this->drupalGet($webform_submission->getTokenUrl());
$this->assertResponse(200);
$this->assertRaw('Submission information');
$this->assertFieldByName('textfield', $webform_submission->getData('textfield'));
$this->assertFieldByName('textfield', $webform_submission->getElementData('textfield'));
// Check token update access denied.
$webform->setSetting('token_update', FALSE)->save();
......@@ -53,7 +53,7 @@ class WebformSubmissionTokenUpdateTest extends WebformTestBase {
$this->drupalGet($webform_submission->getTokenUrl());
$this->assertResponse(200);
$this->assertNoRaw('Submission information');
$this->assertNoFieldByName('textfield', $webform_submission->getData('textfield'));
$this->assertNoFieldByName('textfield', $webform_submission->getElementData('textfield'));
// Logout and switch to anonymous user.
$this->drupalLogout();
......
......@@ -193,7 +193,7 @@ class WebformSubmissionConditionsValidator implements WebformSubmissionCondition
$is_required = ($state == 'optional') ? !$is_required : $is_required;
if (isset($element['#webform_key'])) {
$value = $webform_submission->getData($element['#webform_key']);
$value = $webform_submission->getElementData($element['#webform_key']);
}
else {
$value = $element['#value'];
......
......@@ -234,36 +234,53 @@ interface WebformSubmissionInterface extends ContentEntityInterface, EntityOwner
public function getState();
/**
* Gets the webform submission's data.
* Get a webform submission element's data.
*
* @param string $key
* A string that maps to a key in the submission's data.
* If no key is specified, then the entire data array is returned.
* An webform submission element's key
*
* @return mixed
* An webform submission element's data/value.
*/
public function getElementData($key);
/**
* Set a webform submission element's data.
*
* @param string $key
* An webform submission element's key
* @param mixed $value
* A value.
*
* @return $this
*/
public function setElementData($key, $value);
/**
* Gets the webform submission's data.
*
* @return array
* The webform submission data.
*/
public function getData($key = NULL);
public function getData();
/**
* Set the webform submission's data.
*
* @param array $data
* The webform submission data.
*
* @return $this
*/
public function setData(array $data);
/**
* Gets the webform submission's original data before any changes.
*
* @param string $key
* A string that maps to a key in the submission's original data.
* If no key is specified, then the entire data array is returned.
*
* @return array
* The webform submission original data.
*/
public function getOriginalData($key = NULL);
public function getOriginalData();
/**
* Set the webform submission's original data.
......
......@@ -44,6 +44,10 @@ class WebformSubmissionEntityTest extends KernelTestBase {
'#type' => 'textfield',
'#title' => 'name',
],
'other' => [
'#type' => 'textfield',
'#title' => 'other',
],
];
$webform->setElements($elements);
$webform->save();
......@@ -60,6 +64,20 @@ class WebformSubmissionEntityTest extends KernelTestBase {
$webform_submission->save();
$this->assertEquals($webform->uuid(), $webform_submission->getWebform()->uuid());
// Check get data.
$this->assertEquals($webform_submission->getData(), ['name' => 'John Smith']);
// Check get element data.
$this->assertEquals($webform_submission->getElementData('name'), 'John Smith');
// Check get element data.
$this->assertEquals($webform_submission->getElementData('name'), 'John Smith');
// Check set element data.
$webform_submission->setElementData('other', 'Other');
$this->assertEquals($webform_submission->getElementData('other'), 'Other');
$this->assertEquals($webform_submission->getData(), ['name' => 'John Smith', 'other' => 'Other']);
// Check default submission label.
$this->assertEquals($webform_submission->label(), 'Test: Submission #1');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment