function ContentEntityBaseUnitTest::testRequiredValidation

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php \Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest::testRequiredValidation()
  2. 8.9.x core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php \Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest::testRequiredValidation()
  3. 11.x core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php \Drupal\Tests\Core\Entity\ContentEntityBaseUnitTest::testRequiredValidation()

Tests required validation.

@covers ::validate @covers ::isValidationRequired @covers ::setValidationRequired @covers ::save @covers ::preSave

File

core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php, line 454

Class

ContentEntityBaseUnitTest
@coversDefaultClass \Drupal\Core\Entity\ContentEntityBase @group Entity @group Access

Namespace

Drupal\Tests\Core\Entity

Code

public function testRequiredValidation() : void {
    $validator = $this->createMock(ValidatorInterface::class);
    
    /** @var \Symfony\Component\Validator\ConstraintViolationList $empty_violation_list */
    $empty_violation_list = new ConstraintViolationList();
    $validator->expects($this->once())
        ->method('validate')
        ->with($this->entity
        ->getTypedData())
        ->willReturn($empty_violation_list);
    $this->typedDataManager
        ->expects($this->any())
        ->method('getValidator')
        ->willReturn($validator);
    
    /** @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit\Framework\MockObject\MockObject $storage */
    $storage = $this->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
    $storage->expects($this->any())
        ->method('save')
        ->willReturnCallback(function (ContentEntityInterface $entity) use ($storage) {
        $entity->preSave($storage);
    });
    $this->entityTypeManager
        ->expects($this->any())
        ->method('getStorage')
        ->with($this->entityTypeId)
        ->willReturn($storage);
    // Check that entities can be saved normally when validation is not
    // required.
    $this->assertFalse($this->entity
        ->isValidationRequired());
    $this->entity
        ->save();
    // Make validation required and check that if the entity is validated, it
    // can be saved normally.
    $this->entity
        ->setValidationRequired(TRUE);
    $this->assertTrue($this->entity
        ->isValidationRequired());
    $this->entity
        ->validate();
    $this->entity
        ->save();
    // Check that the "validated" status is reset after saving the entity and
    // that trying to save a non-validated entity when validation is required
    // results in an exception.
    $this->assertTrue($this->entity
        ->isValidationRequired());
    $this->expectException(\LogicException::class);
    $this->expectExceptionMessage('Entity validation is required, but was skipped.');
    $this->entity
        ->save();
}

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