You are here

public function WebformElementPrepopulateTest::testElementPrepopulate in Webform 8.5

Same name and namespace in other branches
  1. 6.x tests/src/Functional/Element/WebformElementPrepopulateTest.php \Drupal\Tests\webform\Functional\Element\WebformElementPrepopulateTest::testElementPrepopulate()

Test element prepopulate.

File

tests/src/Functional/Element/WebformElementPrepopulateTest.php, line 35

Class

WebformElementPrepopulateTest
Tests for webform element prepopulate.

Namespace

Drupal\Tests\webform\Functional\Element

Code

public function testElementPrepopulate() {
  $webform = Webform::load('test_element_prepopulate');
  $files = $this
    ->getTestFiles('text');

  // Check default value of elements on multiple_values.
  $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}');

  // Check 'textfield' can not be prepopulated.
  $this
    ->drupalGet('/webform/test_element_prepopulate', [
    'query' => [
      'textfield_01' => 'value',
    ],
  ]);
  $this
    ->assertNoFieldByName('textfield_0', 'value');

  // Check prepopulating textfield on multiple pages.
  $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');

  // Check prepopulating textfield on multiple pages and changing the 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', [
    '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');

  // Check 'managed_file_prepopulate' can not be prepopulated.
  // The #prepopulate property is not available to managed file elements.
  // @see \Drupal\webform\Plugin\WebformElement\WebformManagedFileBase::defaultProperties
  $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]', '');
}