You are here

public function WebformElementSameTest::testSame in Webform 8.5

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

Test same element.

File

tests/src/Functional/Element/WebformElementSameTest.php, line 25

Class

WebformElementSameTest
Tests for same element.

Namespace

Drupal\Tests\webform\Functional\Element

Code

public function testSame() {
  $webform = Webform::load('test_element_same');

  // Check same checked.
  $this
    ->postSubmission($webform);
  $this
    ->assertRaw("textfield_source: '{some value}'\ntextfield_same: 1\ntextfield_destination: '{some value}'\nwebform_name_source:\n  title: Mr\n  first: John\n  middle: Adam\n  last: Smith\n  suffix: Jr\n  degree: Dr\nwebform_name_same: 1\nwebform_name_destination:\n  title: Mr\n  first: John\n  middle: Adam\n  last: Smith\n  suffix: Jr\n  degree: Dr\ntextfield_multiple_source:\n  - '{one value}'\n  - '{two value}'\ntextfield_multiple_same: 1\ntextfield_multiple_destination:\n  - '{one value}'\n  - '{two value}'");

  // Check same not checked throw validate errors.
  $edit = [
    'textfield_same' => FALSE,
    'webform_name_same' => FALSE,
    'textfield_multiple_same' => FALSE,
  ];
  $this
    ->postSubmission($webform, $edit);
  $this
    ->assertRaw('textfield_destination field is required.');
  $this
    ->assertRaw('webform_name_destination field is required.');
  $this
    ->assertRaw('textfield_multiple_destination field is required.');

  // Check same not checked throw validate errors.
  $edit = [
    'textfield_same' => FALSE,
    'textfield_destination' => '{some other value}',
    'webform_name_same' => FALSE,
    'webform_name_destination[title][select]' => 'Mrs',
    'webform_name_destination[first]' => '{first}',
    'webform_name_destination[middle]' => '{middle}',
    'webform_name_destination[last]' => '{last}',
    'webform_name_destination[suffix]' => '{suffix}',
    'webform_name_destination[degree]' => '{degree}',
    'textfield_multiple_same' => FALSE,
    'textfield_multiple_destination[items][0][_item_]' => '{three value}',
  ];
  $sid = $this
    ->postSubmission($webform, $edit);
  $this
    ->assertRaw("textfield_source: '{some value}'\ntextfield_same: 0\ntextfield_destination: '{some other value}'\nwebform_name_source:\n  title: Mr\n  first: John\n  middle: Adam\n  last: Smith\n  suffix: Jr\n  degree: Dr\nwebform_name_same: 0\nwebform_name_destination:\n  title: Mrs\n  first: '{first}'\n  middle: '{middle}'\n  last: '{last}'\n  suffix: '{suffix}'\n  degree: '{degree}'\ntextfield_multiple_source:\n  - '{one value}'\n  - '{two value}'\ntextfield_multiple_same: 0\ntextfield_multiple_destination:\n  - '{three value}'");
  $webform_submission = WebformSubmission::load($sid);

  /**************************************************************************/

  // Check textfield source and destination are not equal.
  $this
    ->assertNotEqual($webform_submission
    ->getElementData('textfield_source'), $webform_submission
    ->getElementData('textfield_destination'));

  // Set textfield same as to TRUE.
  // @see \Drupal\webform\Plugin\WebformElement\WebformSame::preSave
  $webform_submission
    ->setElementData('textfield_same', TRUE);
  $webform_submission
    ->save();

  // Check textfield source and destination are now equal.
  $this
    ->assertEqual($webform_submission
    ->getElementData('textfield_source'), $webform_submission
    ->getElementData('textfield_destination'));
}