You are here

public function FieldTypeTest::testFieldStorageSettings 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::testFieldStorageSettings()

Test field storage settings.

File

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

Class

FieldTypeTest
A test for Double Field type.

Namespace

Drupal\Tests\double_field\Functional

Code

public function testFieldStorageSettings() : void {

  // Not random to avoid different plural form in messages.
  $maxlength = 50;

  // -- Boolean and varchar.
  $storage_settings['storage']['first']['type'] = 'boolean';
  $storage_settings['storage']['second']['type'] = 'string';
  $storage_settings['storage']['second']['maxlength'] = $maxlength;
  $this
    ->saveFieldStorageSettings($storage_settings);
  $values = [
    // The valid boolean value is 0 or 1.
    2,
    $this
      ->randomString($maxlength + 1),
  ];
  $expected_messages = [
    'This value should be of the correct primitive type.',
    new FormattableMarkup('This value is too long. It should have @maxlength characters or less.', [
      '@maxlength' => $maxlength,
    ]),
  ];
  $this
    ->assertViolations($values, $expected_messages);

  // Set the field optional as zero value will cause 'No blank' violation.
  $settings['first']['required'] = FALSE;
  $this
    ->saveFieldSettings($settings);
  $values = [
    mt_rand(0, 1),
    $this
      ->randomString($maxlength),
  ];
  $this
    ->assertNoViolations($values);

  // -- Text (long) and integer.
  $storage_settings['storage']['first']['type'] = 'text';
  $storage_settings['storage']['second']['type'] = 'integer';
  $this
    ->saveFieldStorageSettings($storage_settings);
  $values = [
    // Text storage has no restrictions.
    $this
      ->randomString(1000),
    // Float value should not be accepted.
    123.456,
  ];
  $expected_messages = [
    'This value should be of the correct primitive type.',
  ];
  $this
    ->assertViolations($values, $expected_messages);
  $values = [
    $this
      ->randomString(1000),
    mt_rand(0, 1000),
  ];
  $this
    ->assertNoViolations($values);

  // -- Float and numeric.
  $storage_settings['storage']['first']['type'] = 'float';
  $storage_settings['storage']['second']['type'] = 'numeric';
  $this
    ->saveFieldStorageSettings($storage_settings);
  $values = [
    'abc',
    'abc',
  ];
  $expected_messages = [
    'This value should be of the correct primitive type.',
    'This value should be of the correct primitive type.',
  ];
  $this
    ->assertViolations($values, $expected_messages);
  $values = [
    mt_rand(0, 1000) + mt_rand(),
    mt_rand(0, 1000) + mt_rand(),
  ];
  $this
    ->assertNoViolations($values);

  // -- Email and URI.
  $storage_settings['storage']['first']['type'] = 'email';
  $storage_settings['storage']['second']['type'] = 'uri';
  $this
    ->saveFieldStorageSettings($storage_settings);
  $values = [
    'abc',
    'abc',
  ];
  $expected_messages = [
    'This value is not a valid email address.',
    'This value should be of the correct primitive type.',
  ];
  $this
    ->assertViolations($values, $expected_messages);
  $values = [
    'qwe@rty.ui',
    'http://example.com',
  ];
  $this
    ->assertNoViolations($values);

  // -- Datetime.
  $storage_settings['storage']['first']['type'] = 'datetime_iso8601';
  $storage_settings['storage']['second']['type'] = 'string';
  $this
    ->saveFieldStorageSettings($storage_settings);
  $values = [
    'abc',
    'abc',
  ];
  $expected_messages = [
    'This value should be of the correct primitive type.',
  ];
  $this
    ->assertViolations($values, $expected_messages);
  $values = [
    '2017-10-10T01:00:00',
    'abc',
  ];
  $this
    ->assertNoViolations($values);
}