You are here

public function FieldTypeTest::testAllowedValuesValidation in Double Field 4.x

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/FieldTypeTest.php \Drupal\Tests\double_field\Functional\FieldTypeTest::testAllowedValuesValidation()

Test allowed values validation.

File

tests/src/Functional/FieldTypeTest.php, line 475

Class

FieldTypeTest
A test for Double Field type.

Namespace

Drupal\Tests\double_field\Functional

Code

public function testAllowedValuesValidation() : void {

  // --
  $maxlength = 50;
  $storage_settings['storage']['first']['type'] = 'string';
  $storage_settings['storage']['first']['maxlength'] = $maxlength;
  $storage_settings['storage']['second']['type'] = 'float';
  $this
    ->saveFieldStorageSettings($storage_settings);
  $edit = [
    'settings[first][list]' => 1,
    // Random sting may content '|' character.
    'settings[first][allowed_values]' => str_repeat('a', $maxlength + 1),
    'settings[second][list]' => 1,
    'settings[second][allowed_values]' => implode("\n", [
      123,
      'abc',
      789,
    ]),
  ];
  $this
    ->drupalGet($this->fieldAdminPath);
  $this
    ->submitForm($edit, 'Save settings');
  $this
    ->assertErrorMessage(new FormattableMarkup('Allowed values list: each key must be a string at most @maxlength characters long.', [
    '@maxlength' => $maxlength,
  ]));
  $this
    ->assertErrorMessage('Allowed values list: each key must be a valid integer or decimal.');
  $edit = [
    'settings[first][allowed_values]' => str_repeat('a', $maxlength),
    'settings[second][allowed_values]' => implode("\n", [
      123,
      456,
      789,
    ]),
  ];
  $this
    ->drupalGet($this->fieldAdminPath);
  $this
    ->submitForm($edit, 'Save settings');
  self::assertCount(0, $this
    ->getMessages('error'), 'No error messages were found.');
  $this
    ->assertStatusMessage(new FormattableMarkup('Saved %field_name configuration.', [
    '%field_name' => $this->fieldName,
  ]));

  // --
  $storage_settings['storage']['first']['type'] = 'integer';
  $storage_settings['storage']['second']['type'] = 'numeric';
  $this
    ->saveFieldStorageSettings($storage_settings);
  $edit = [
    'settings[first][allowed_values]' => implode("\n", [
      123,
      'abc',
      789,
    ]),
    'settings[second][allowed_values]' => implode("\n", [
      123,
      'abc',
      789,
    ]),
  ];
  $this
    ->drupalGet($this->fieldAdminPath);
  $this
    ->submitForm($edit, 'Save settings');
  $this
    ->assertErrorMessage('Allowed values list: keys must be integers.');
  $this
    ->assertErrorMessage('Allowed values list: each key must be a valid integer or decimal.');
  $edit = [
    'settings[first][allowed_values]' => implode("\n", [
      123,
      456,
      789,
    ]),
    'settings[second][allowed_values]' => implode("\n", [
      123,
      456,
      789,
    ]),
  ];
  $this
    ->drupalGet($this->fieldAdminPath);
  $this
    ->submitForm($edit, 'Save settings');
  self::assertCount(0, $this
    ->getMessages('error'), 'No error messages were found.');
  $this
    ->assertStatusMessage(new FormattableMarkup('Saved %field_name configuration.', [
    '%field_name' => $this->fieldName,
  ]));
}