class EntityConstraintViolationListTest in Drupal 10
Same name and namespace in other branches
- 8 core/tests/Drupal/Tests/Core/Entity/EntityConstraintViolationListTest.php \Drupal\Tests\Core\Entity\EntityConstraintViolationListTest
- 9 core/tests/Drupal/Tests/Core/Entity/EntityConstraintViolationListTest.php \Drupal\Tests\Core\Entity\EntityConstraintViolationListTest
@coversDefaultClass \Drupal\Core\Entity\EntityConstraintViolationList @group entity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitWarnings
- class \Drupal\Tests\Core\Entity\EntityConstraintViolationListTest
Expanded class hierarchy of EntityConstraintViolationListTest
File
- core/
tests/ Drupal/ Tests/ Core/ Entity/ EntityConstraintViolationListTest.php, line 16
Namespace
Drupal\Tests\Core\EntityView source
class EntityConstraintViolationListTest extends UnitTestCase {
/**
* @covers ::filterByFields
*/
public function testFilterByFields() {
$account = $this
->prophesize('\\Drupal\\Core\\Session\\AccountInterface')
->reveal();
$entity = $this
->setupEntity($account);
$constraint_list = $this
->setupConstraintListWithoutCompositeConstraint($entity);
$violations = iterator_to_array($constraint_list);
$this
->assertSame($constraint_list
->filterByFields([
'name',
]), $constraint_list);
$this
->assertCount(4, $constraint_list);
$this
->assertEquals(array_values(iterator_to_array($constraint_list)), [
$violations[2],
$violations[3],
$violations[4],
$violations[5],
]);
}
/**
* @covers ::filterByFields
*/
public function testFilterByFieldsWithCompositeConstraints() {
$account = $this
->prophesize('\\Drupal\\Core\\Session\\AccountInterface')
->reveal();
$entity = $this
->setupEntity($account);
$constraint_list = $this
->setupConstraintListWithCompositeConstraint($entity);
$violations = iterator_to_array($constraint_list);
$this
->assertSame($constraint_list
->filterByFields([
'name',
]), $constraint_list);
$this
->assertCount(4, $constraint_list);
$this
->assertEquals(array_values(iterator_to_array($constraint_list)), [
$violations[2],
$violations[3],
$violations[4],
$violations[5],
]);
}
/**
* @covers ::filterByFieldAccess
*/
public function testFilterByFieldAccess() {
$account = $this
->prophesize('\\Drupal\\Core\\Session\\AccountInterface')
->reveal();
$entity = $this
->setupEntity($account);
$constraint_list = $this
->setupConstraintListWithoutCompositeConstraint($entity);
$violations = iterator_to_array($constraint_list);
$this
->assertSame($constraint_list
->filterByFieldAccess($account), $constraint_list);
$this
->assertCount(4, $constraint_list);
$this
->assertEquals(array_values(iterator_to_array($constraint_list)), [
$violations[2],
$violations[3],
$violations[4],
$violations[5],
]);
}
/**
* @covers ::filterByFieldAccess
*/
public function testFilterByFieldAccessWithCompositeConstraint() {
$account = $this
->prophesize('\\Drupal\\Core\\Session\\AccountInterface')
->reveal();
$entity = $this
->setupEntity($account);
$constraint_list = $this
->setupConstraintListWithCompositeConstraint($entity);
$violations = iterator_to_array($constraint_list);
$this
->assertSame($constraint_list
->filterByFieldAccess($account), $constraint_list);
$this
->assertCount(4, $constraint_list);
$this
->assertEquals(array_values(iterator_to_array($constraint_list)), [
$violations[2],
$violations[3],
$violations[4],
$violations[5],
]);
}
/**
* Builds the entity.
*
* @param \Drupal\Core\Session\AccountInterface $account
* An account.
*
* @return \Drupal\Core\Field\FieldItemListInterface
* A fieldable entity.
*/
protected function setupEntity(AccountInterface $account) {
$prophecy = $this
->prophesize('\\Drupal\\Core\\Field\\FieldItemListInterface');
$prophecy
->access('edit', $account)
->willReturn(FALSE);
$name_field_item_list = $prophecy
->reveal();
$prophecy = $this
->prophesize('\\Drupal\\Core\\Field\\FieldItemListInterface');
$prophecy
->access('edit', $account)
->willReturn(TRUE);
$type_field_item_list = $prophecy
->reveal();
$prophecy = $this
->prophesize('\\Drupal\\Core\\Entity\\FieldableEntityInterface');
$prophecy
->hasField('name')
->willReturn(TRUE);
$prophecy
->hasField('type')
->willReturn(TRUE);
$prophecy
->get('name')
->willReturn($name_field_item_list);
$prophecy
->get('type')
->willReturn($type_field_item_list);
return $prophecy
->reveal();
}
/**
* Builds an entity constraint violation list without composite constraints.
*
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* A fieldable entity.
*
* @return \Drupal\Core\Entity\EntityConstraintViolationList
* The entity constraint violation list.
*/
protected function setupConstraintListWithoutCompositeConstraint(FieldableEntityInterface $entity) {
$violations = [];
// Add two violations to two specific fields.
$violations[] = new ConstraintViolation('test name violation', '', [], '', 'name', 'invalid');
$violations[] = new ConstraintViolation('test name violation2', '', [], '', 'name', 'invalid');
$violations[] = new ConstraintViolation('test type violation', '', [], '', 'type', 'invalid');
$violations[] = new ConstraintViolation('test type violation2', '', [], '', 'type', 'invalid');
// Add two entity level specific violations.
$violations[] = new ConstraintViolation('test entity violation', '', [], '', '', 'invalid');
$violations[] = new ConstraintViolation('test entity violation2', '', [], '', '', 'invalid');
return new EntityConstraintViolationList($entity, $violations);
}
/**
* Builds an entity constraint violation list with composite constraints.
*
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* A fieldable entity.
*
* @return \Drupal\Core\Entity\EntityConstraintViolationList
* The entity constraint violation list.
*/
protected function setupConstraintListWithCompositeConstraint(FieldableEntityInterface $entity) {
$violations = [];
// Add two violations to two specific fields.
$violations[] = new ConstraintViolation('test name violation', '', [], '', 'name', 'invalid');
$violations[] = new ConstraintViolation('test name violation2', '', [], '', 'name', 'invalid');
$violations[] = new ConstraintViolation('test type violation', '', [], '', 'type', 'invalid');
$violations[] = new ConstraintViolation('test type violation2', '', [], '', 'type', 'invalid');
// Add two entity level specific violations with a compound constraint.
$composite_constraint = new EntityTestCompositeConstraint();
$violations[] = new ConstraintViolation('test composite violation', '', [], '', '', 'invalid', NULL, NULL, $composite_constraint);
$violations[] = new ConstraintViolation('test composite violation2', '', [], '', '', 'invalid', NULL, NULL, $composite_constraint);
return new EntityConstraintViolationList($entity, $violations);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityConstraintViolationListTest:: |
protected | function | Builds an entity constraint violation list with composite constraints. | |
EntityConstraintViolationListTest:: |
protected | function | Builds an entity constraint violation list without composite constraints. | |
EntityConstraintViolationListTest:: |
protected | function | Builds the entity. | |
EntityConstraintViolationListTest:: |
public | function | @covers ::filterByFieldAccess | |
EntityConstraintViolationListTest:: |
public | function | @covers ::filterByFieldAccess | |
EntityConstraintViolationListTest:: |
public | function | @covers ::filterByFields | |
EntityConstraintViolationListTest:: |
public | function | @covers ::filterByFields | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 206 | |
UnitTestCase:: |
public static | function |