public function FieldTypeTest::testFieldSettings in Double Field 8.3
Same name and namespace in other branches
- 4.x tests/src/Functional/FieldTypeTest.php \Drupal\Tests\double_field\Functional\FieldTypeTest::testFieldSettings()
Test field settings.
File
- tests/
src/ Functional/ FieldTypeTest.php, line 191
Class
- FieldTypeTest
- A test for Double Field type.
Namespace
Drupal\Tests\double_field\FunctionalCode
public function testFieldSettings() : void {
// -- Boolean and string.
$storage_settings['storage']['first']['type'] = 'boolean';
$storage_settings['storage']['second']['type'] = 'string';
$this
->saveFieldStorageSettings($storage_settings);
$settings['second']['list'] = TRUE;
$settings['second']['allowed_values'] = [
'aaa' => 'Aaa',
'bbb' => 'Bbb',
'ccc' => 'Ccc',
];
$this
->saveFieldSettings($settings);
$expected_messages = [
'The value you selected is not a valid choice.',
];
$this
->assertViolations([
1,
'abc',
], $expected_messages);
$values = [
// Boolean has no field level settings that may cause violations.
1,
array_rand($settings['second']['allowed_values']),
];
$this
->assertNoViolations($values);
// -- Integer.
$storage_settings['storage']['first']['type'] = 'integer';
$storage_settings['storage']['second']['type'] = 'integer';
$this
->saveFieldStorageSettings($storage_settings);
$min_limit = mt_rand(-1000, 1000);
$max_limit = mt_rand($min_limit, $min_limit + 1000);
foreach ([
'first',
'second',
] as $subfield) {
$settings[$subfield]['list'] = FALSE;
$settings[$subfield]['min'] = $min_limit;
$settings[$subfield]['max'] = $max_limit;
}
$this
->saveFieldSettings($settings);
$values = [
$min_limit - 1,
$max_limit + 1,
];
$expected_messages = [
new FormattableMarkup('This value should be @min_limit or more.', [
'@min_limit' => $min_limit,
]),
new FormattableMarkup('This value should be @max_limit or less.', [
'@max_limit' => $max_limit,
]),
];
$this
->assertViolations($values, $expected_messages);
$values = [
mt_rand($min_limit, $max_limit),
mt_rand($min_limit, $max_limit),
];
$this
->assertNoViolations($values);
// -- Float and numeric.
$storage_settings['storage']['first']['type'] = 'float';
$storage_settings['storage']['second']['type'] = 'numeric';
$this
->saveFieldStorageSettings($storage_settings);
$min_limit = mt_rand(-1000, 1000);
$max_limit = mt_rand($min_limit, $min_limit + 1000);
$settings = $this->field
->getSettings();
foreach ([
'first',
'second',
] as $subfield) {
$settings[$subfield]['list'] = FALSE;
$settings[$subfield]['min'] = $min_limit;
$settings[$subfield]['max'] = $max_limit;
}
$this
->saveFieldSettings($settings);
$values = [
$min_limit - mt_rand(1, 100),
$max_limit + mt_rand(1, 100),
];
$expected_messages = [
new FormattableMarkup('This value should be @min_limit or more.', [
'@min_limit' => $min_limit,
]),
new FormattableMarkup('This value should be @max_limit or less.', [
'@max_limit' => $max_limit,
]),
];
$this
->assertViolations($values, $expected_messages);
// Test allowed values.
$settings['first']['list'] = TRUE;
$settings['first']['allowed_values'] = [
'-12.379' => 'Aaa',
'4565' => 'Bbb',
'93577285' => 'Ccc',
];
$settings['second']['list'] = TRUE;
$settings['second']['allowed_values'] = [
'-245' => 'Aaa',
'sssssss' => 'Bbb',
'7738854' => 'Ccc',
];
$settings['second']['max'] = $max_limit;
$this
->saveFieldSettings($settings);
$values = [
123.356,
300.12,
];
$expected_messages = [
'The value you selected is not a valid choice.',
'The value you selected is not a valid choice.',
];
$this
->assertViolations($values, $expected_messages);
$this
->assertNoViolations([
4565,
7738854,
]);
// -- Email and telephone.
$storage_settings['storage']['first']['type'] = 'email';
$storage_settings['storage']['second']['type'] = 'telephone';
foreach ([
'first',
'second',
] as $subfield) {
$settings[$subfield]['list'] = FALSE;
}
$this
->saveFieldSettings($settings);
$this
->saveFieldStorageSettings($storage_settings);
$values = [
'aaa',
str_repeat('x', 51),
];
$expected_messages = [
'This value is not a valid email address.',
'This value is too long. It should have 50 characters or less.',
];
$this
->assertViolations($values, $expected_messages);
$values = [
'abc@example.com',
str_repeat('x', 50),
];
$this
->assertNoViolations($values);
// -- Uri and date.
$storage_settings['storage']['first']['type'] = 'uri';
$storage_settings['storage']['second']['type'] = 'datetime_iso8601';
foreach ([
'first',
'second',
] as $subfield) {
$settings[$subfield]['list'] = FALSE;
}
$this
->saveFieldSettings($settings);
$this
->saveFieldStorageSettings($storage_settings);
$values = [
'aaa',
'bbb',
];
$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 = [
'http://example.com',
'2016-10-11T01:12:14',
];
$this
->assertNoViolations($values);
}