class EntityConstraintViolationListTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/EntityConstraintViolationListTest.php \Drupal\Tests\Core\Entity\EntityConstraintViolationListTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityConstraintViolationListTest.php \Drupal\Tests\Core\Entity\EntityConstraintViolationListTest
  3. 10 core/tests/Drupal/Tests/Core/Entity/EntityConstraintViolationListTest.php \Drupal\Tests\Core\Entity\EntityConstraintViolationListTest

@coversDefaultClass \Drupal\Core\Entity\EntityConstraintViolationList @group entity

Hierarchy

Expanded class hierarchy of EntityConstraintViolationListTest

File

core/tests/Drupal/Tests/Core/Entity/EntityConstraintViolationListTest.php, line 18

Namespace

Drupal\Tests\Core\Entity
View source
class EntityConstraintViolationListTest extends UnitTestCase {
    
    /**
     * @covers ::filterByFields
     */
    public function testFilterByFields() : void {
        $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() : void {
        $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() : void {
        $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() : void {
        $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],
        ]);
    }
    
    /**
     * @covers ::findByCodes
     */
    public function testFindByCodes() : void {
        $account = $this->prophesize('\\Drupal\\Core\\Session\\AccountInterface')
            ->reveal();
        $entity = $this->setupEntity($account);
        $constraint_list = $this->setupConstraintListWithoutCompositeConstraint($entity);
        $violations = iterator_to_array($constraint_list);
        $codes = [
            'test-code-violation-name',
            'test-code-violation2-name',
        ];
        $actual = $constraint_list->findByCodes($codes);
        $this->assertCount(2, $actual);
        $this->assertEquals(iterator_to_array($actual), [
            $violations[0],
            $violations[1],
        ]);
    }
    
    /**
     * 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', NULL, 'test-code-violation-name');
        $violations[] = new ConstraintViolation('test name violation2', '', [], '', 'name', 'invalid', NULL, 'test-code-violation2-name');
        $violations[] = new ConstraintViolation('test type violation', '', [], '', 'type', 'invalid', NULL, 'test-code-violation-type');
        $violations[] = new ConstraintViolation('test type violation2', '', [], '', 'type', 'invalid', NULL, 'test-code-violation2-type');
        // 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

Title Sort descending Modifiers Object type Summary Overrides
EntityConstraintViolationListTest::setupConstraintListWithCompositeConstraint protected function Builds an entity constraint violation list with composite constraints.
EntityConstraintViolationListTest::setupConstraintListWithoutCompositeConstraint protected function Builds an entity constraint violation list without composite constraints.
EntityConstraintViolationListTest::setupEntity protected function Builds the entity.
EntityConstraintViolationListTest::testFilterByFieldAccess public function @covers ::filterByFieldAccess
EntityConstraintViolationListTest::testFilterByFieldAccessWithCompositeConstraint public function @covers ::filterByFieldAccess
EntityConstraintViolationListTest::testFilterByFields public function @covers ::filterByFields
EntityConstraintViolationListTest::testFilterByFieldsWithCompositeConstraints public function @covers ::filterByFields
EntityConstraintViolationListTest::testFindByCodes public function @covers ::findByCodes
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
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::$root protected property The app root.
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
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 367
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.