X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Ftests%2FDrupal%2FKernelTests%2FCore%2FEntity%2FEntityHasFieldConstraintValidatorTest.php;fp=web%2Fcore%2Ftests%2FDrupal%2FKernelTests%2FCore%2FEntity%2FEntityHasFieldConstraintValidatorTest.php;h=eebc94f52358bed0fd66272bc39284b98e7b89e0;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hp=0000000000000000000000000000000000000000;hpb=74df008bdbb3a11eeea356744f39b802369bda3c;p=yaffs-website diff --git a/web/core/tests/Drupal/KernelTests/Core/Entity/EntityHasFieldConstraintValidatorTest.php b/web/core/tests/Drupal/KernelTests/Core/Entity/EntityHasFieldConstraintValidatorTest.php new file mode 100644 index 000000000..eebc94f52 --- /dev/null +++ b/web/core/tests/Drupal/KernelTests/Core/Entity/EntityHasFieldConstraintValidatorTest.php @@ -0,0 +1,70 @@ +installEntitySchema('entity_test_constraints'); + $this->createUser(); + } + + public function testValidation() { + $this->state->set('entity_test_constraints.build', [ + 'EntityHasField' => 'body', + ]); + + /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ + $entity_type_manager = $this->container->get('entity_type.manager'); + $entity_type_manager->clearCachedDefinitions(); + + // Clear the typed data cache so that the entity has the correct constraints + // during validation. + $this->container->get('typed_data_manager')->clearCachedDefinitions(); + + $storage = $entity_type_manager->getStorage('entity_test_constraints'); + + /** @var \Drupal\entity_test\Entity\EntityTestConstraints $entity */ + $entity = $storage->create(); + // We should get a violation if we try to validate the entity before the + // field has been created. + $violations = $entity->validate(); + $this->assertCount(1, $violations); + $this->assertEquals($violations[0]->getMessage(), 'The entity must have the body field.'); + $storage->save($entity); + + // Create the field. + $field_storage = FieldStorageConfig::create([ + 'type' => 'string', + 'entity_type' => $entity->getEntityTypeId(), + 'field_name' => 'body', + ]); + $field_storage->save(); + + FieldConfig::create([ + 'field_storage' => $field_storage, + 'bundle' => $entity->bundle(), + ])->save(); + + // Now that the field has been created, there should be no violations. + $this->assertCount(0, $storage->loadUnchanged(1)->validate()); + } + +}