View source
<?php
namespace Drupal\Tests\webform\Functional\Element;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformElementPrepopulateTest extends WebformElementBrowserTestBase {
use TestFileCreationTrait;
public static $modules = [
'file',
'webform',
];
protected static $testWebforms = [
'test_element_prepopulate',
];
public function testElementPrepopulate() {
$webform = Webform::load('test_element_prepopulate');
$files = $this
->getTestFiles('text');
$this
->drupalGet('/webform/test_element_prepopulate');
$this
->assertFieldByName('textfield_01', '');
$this
->assertFieldByName('textfield_prepopulate_01', '{default_value_01}');
$this
->assertFieldByName('files[managed_file_prepopulate_01]', '');
$this
->drupalPostForm('/webform/test_element_prepopulate', [], 'Next >');
$this
->assertFieldByName('textfield_02', '');
$this
->assertFieldByName('textfield_prepopulate_02', '{default_value_02}');
$this
->drupalGet('/webform/test_element_prepopulate', [
'query' => [
'textfield_01' => 'value',
],
]);
$this
->assertNoFieldByName('textfield_0', 'value');
$options = [
'query' => [
'textfield_prepopulate_01' => 'value_01',
'textfield_prepopulate_02' => 'value_02',
],
];
$this
->drupalGet('/webform/test_element_prepopulate', $options);
$this
->assertFieldByName('textfield_prepopulate_01', 'value_01');
$this
->drupalPostForm('/webform/test_element_prepopulate', [], 'Next >', $options);
$this
->assertFieldByName('textfield_prepopulate_02', 'value_02');
$options = [
'query' => [
'textfield_prepopulate_01' => 'value_01',
'textfield_prepopulate_02' => 'value_02',
],
];
$this
->drupalGet('/webform/test_element_prepopulate', $options);
$this
->assertFieldByName('textfield_prepopulate_01', 'value_01');
$this
->drupalPostForm('/webform/test_element_prepopulate', [
'textfield_prepopulate_01' => 'edit_01',
], 'Next >', $options);
$this
->assertFieldByName('textfield_prepopulate_02', 'value_02');
$this
->drupalPostForm(NULL, [], '< Previous', $options);
$this
->assertNoFieldByName('textfield_prepopulate_01', 'value_01');
$this
->assertFieldByName('textfield_prepopulate_01', 'edit_01');
$edit = [
'files[managed_file_prepopulate_01]' => \Drupal::service('file_system')
->realpath($files[0]->uri),
];
$this
->drupalPostForm('/webform/test_element_prepopulate', $edit, 'Next >');
$this
->drupalPostForm(NULL, [], 'Submit');
$sid = $this
->getLastSubmissionId($webform);
$webform_submission = WebformSubmission::load($sid);
$fid = $webform_submission
->getElementData('managed_file_prepopulate_01');
$this
->drupalGet('/webform/test_element_prepopulate', [
'query' => [
'managed_file_prepopulate_01' => $fid,
],
]);
$this
->assertFieldByName('files[managed_file_prepopulate_01]', '');
}
}