You are here

public function WebformElementInputMaskTest::testInputMask in Webform 8.5

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

Test element input mask.

File

tests/src/Functional/Element/WebformElementInputMaskTest.php, line 31

Class

WebformElementInputMaskTest
Tests for element input mask.

Namespace

Drupal\Tests\webform\Functional\Element

Code

public function testInputMask() {
  $webform = Webform::load('test_element_input_mask');

  // Check default values.
  $this
    ->postSubmission($webform);
  $this
    ->assertRaw("currency: '\$ 1.00'\ncurrency_negative: '-\$ 1.00'\ncurrency_positive_negative: '\$ 1.00'\ndatetime: ''\ndecimal: ''\nemail: ''\nip: ''\nlicense_plate: ''\nmac: ''\npercentage: ''\nphone: ''\nssn: ''\nvin: ''\nzip: ''\nuppercase: ''\nlowercase: ''\ncustom: ''\nmodule: ''");

  // Check patterns.
  $edit = [
    'email' => 'example@example.com',
    'datetime' => '2007-06-09\'T\'17:46:21',
    'decimal' => '9.9',
    'ip' => '255.255.255.255',
    'currency' => '$ 9.99',
    'currency_negative' => '-$ 9.99',
    'currency_positive_negative' => '-$ 9.99',
    'percentage' => '99 %',
    'phone' => '(999) 999-9999',
    'license_plate' => '9-AAA-999',
    'mac' => '99-99-99-99-99-99',
    'ssn' => '999-99-9999',
    'vin' => 'JA3AY11A82U020534',
    'zip' => '99999-9999',
    'uppercase' => 'UPPERCASE',
    'lowercase' => 'lowercase',
    'module' => '999',
  ];
  $this
    ->postSubmission($webform, $edit);
  $this
    ->assertRaw("currency: '\$ 9.99'\ncurrency_negative: '-\$ 9.99'\ncurrency_positive_negative: '-\$ 9.99'\ndatetime: '2007-06-09''T''17:46:21'\ndecimal: '9.9'\nemail: example@example.com\nip: 255.255.255.255\nlicense_plate: 9-AAA-999\nmac: 99-99-99-99-99-99\npercentage: '99 %'\nphone: '(999) 999-9999'\nssn: 999-99-9999\nvin: JA3AY11A82U020534\nzip: 99999-9999\nuppercase: UPPERCASE\nlowercase: lowercase\ncustom: ''\nmodule: '999'");

  // Check pattern validation error messages.
  $edit = [
    'currency' => '$ 9.9_',
    'currency_negative' => '-$ 9.9_',
    'currency_positive_negative' => '-$ 9.9_',
    'decimal' => '9._',
    'ip' => '255.255.255.__',
    'mac' => '99-99-99-99-99-_)',
    'percentage' => '_ %',
    'phone' => '(999) 999-999_',
    'ssn' => '999-99-999_',
    'zip' => '99999-999_',
    'module' => '99_',
  ];
  $this
    ->postSubmission($webform, $edit);
  foreach ($edit as $name => $value) {
    $this
      ->assertRaw('<em class="placeholder">' . $name . '</em> field is not in the right format.');
  }

  // Check currency submitted as the default input (ie $ 0.00) triggers
  // required validation.
  // @see \Drupal\webform\Plugin\WebformElement\TextBase::validateInputMask
  $this
    ->postSubmission($webform, [
    'currency' => '$ 0.00',
  ]);
  $this
    ->assertRaw('currency field is required.');
}