class NoFieldItemsExistWithHigherCardinalityTest
Same name in this branch
- main core/modules/field/tests/src/Kernel/Plugin/Validation/Constraint/NoFieldItemsExistWithHigherCardinalityTest.php \Drupal\Tests\field\Kernel\Plugin\Validation\Constraint\NoFieldItemsExistWithHigherCardinalityTest
Tests the NoFieldItemsExistWithHigherCardinality constraint.
Attributes
#[CoversClass(NoFieldItemsExistWithHigherCardinality::class)]
#[Group('field')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\field\Unit\Plugin\Validation\Constraint\NoFieldItemsExistWithHigherCardinalityTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of NoFieldItemsExistWithHigherCardinalityTest
File
-
core/
modules/ field/ tests/ src/ Unit/ Plugin/ Validation/ Constraint/ NoFieldItemsExistWithHigherCardinalityTest.php, line 16
Namespace
Drupal\Tests\field\Unit\Plugin\Validation\ConstraintView source
class NoFieldItemsExistWithHigherCardinalityTest extends UnitTestCase {
/**
* Tests the constraint initialization with valid options.
*/
public function testValidOptions() : void {
$options = [
'entityType' => 'node',
'fieldName' => 'field_test',
];
$constraint = new NoFieldItemsExistWithHigherCardinality(entityType: $options['entityType'], fieldName: $options['fieldName']);
$this->assertEquals('node', $constraint->entityType);
$this->assertEquals('field_test', $constraint->fieldName);
$this->assertEquals("The field '@field_name' of entity type '@entity_type' has more entries (@max_delta) than the cardinality (@cardinality) allows.", $constraint->message);
}
/**
* Tests the message template with different parameters.
*/
public function testMessageParameters(string $entityType, string $fieldName, int $maxDelta, int $cardinality, string $expectedMessage) : void {
$options = [
'entityType' => $entityType,
'fieldName' => $fieldName,
];
$constraint = new NoFieldItemsExistWithHigherCardinality(entityType: $options['entityType'], fieldName: $options['fieldName']);
// Simulate the violation building process.
$parameters = [
'@field_name' => $fieldName,
'@entity_type' => $entityType,
'@max_delta' => (string) $maxDelta,
'@cardinality' => (string) $cardinality,
];
$message = strtr($constraint->message, $parameters);
$this->assertEquals($expectedMessage, $message);
}
/**
* Data provider for testMessageParameters.
*/
public static function messageParametersProvider() : array {
return [
[
'node',
'field_body',
3,
2,
"The field 'field_body' of entity type 'node' has more entries (3) than the cardinality (2) allows.",
],
[
'user',
'field_address',
5,
1,
"The field 'field_address' of entity type 'user' has more entries (5) than the cardinality (1) allows.",
],
];
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overrides |
|---|---|---|---|---|---|
| DrupalTestCaseTrait::$root | protected | property | The Drupal root directory. | ||
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | 1 | |
| DrupalTestCaseTrait::getDrupalRoot | Deprecated | protected static | function | Returns the Drupal root directory. | 1 |
| DrupalTestCaseTrait::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | ||
| DrupalTestCaseTrait::setUpRoot | final protected | function | Ensure that the $root property is set initially. | ||
| NoFieldItemsExistWithHigherCardinalityTest::messageParametersProvider | public static | function | Data provider for testMessageParameters. | ||
| NoFieldItemsExistWithHigherCardinalityTest::testMessageParameters | public | function | Tests the message template with different parameters. | ||
| NoFieldItemsExistWithHigherCardinalityTest::testValidOptions | public | function | Tests the constraint initialization with valid options. | ||
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | ||
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | ||
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | ||
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | ||
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | ||
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | ||
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | ||
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | ||
| UnitTestCase::setUp | protected | function | 359 | ||
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.