X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Ftests%2FDrupal%2FKernelTests%2FCore%2FEntity%2FEntityDefinitionUpdateTest.php;fp=web%2Fcore%2Ftests%2FDrupal%2FKernelTests%2FCore%2FEntity%2FEntityDefinitionUpdateTest.php;h=1a743eceebed491d2e95ad1e5c5d71f9389fc550;hp=348c33cbb83d33dc183eb52cbd69d402cb91eb85;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php b/web/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php index 348c33cbb..1a743ecee 100644 --- a/web/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php +++ b/web/core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php @@ -15,11 +15,13 @@ use Drupal\Core\Field\FieldException; use Drupal\Core\Field\FieldStorageDefinitionEvents; use Drupal\Core\Language\LanguageInterface; use Drupal\entity_test_update\Entity\EntityTestUpdate; -use Drupal\system\Tests\Entity\EntityDefinitionTestTrait; +use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait; /** * Tests EntityDefinitionUpdateManager functionality. * + * @coversDefaultClass \Drupal\Core\Entity\EntityDefinitionUpdateManager + * * @group Entity */ class EntityDefinitionUpdateTest extends EntityKernelTestBase { @@ -1049,31 +1051,78 @@ class EntityDefinitionUpdateTest extends EntityKernelTestBase { /** * Tests adding a base field with initial values inherited from another field. + * + * @dataProvider initialValueFromFieldTestCases */ - public function testInitialValueFromField() { + public function testInitialValueFromField($default_initial_value, $expected_value) { $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update'); $db_schema = $this->database->schema(); // Create two entities before adding the base field. - /** @var \Drupal\entity_test\Entity\EntityTestUpdate $entity */ - $storage->create(['name' => 'First entity'])->save(); - $storage->create(['name' => 'Second entity'])->save(); + /** @var \Drupal\entity_test_update\Entity\EntityTestUpdate $entity */ + $storage->create([ + 'name' => 'First entity', + 'test_single_property' => 'test existing value', + ])->save(); + + // The second entity does not have any value for the 'test_single_property' + // field, allowing us to test the 'default_value' parameter of + // \Drupal\Core\Field\BaseFieldDefinition::setInitialValueFromField(). + $storage->create([ + 'name' => 'Second entity', + ])->save(); // Add a base field with an initial value inherited from another field. - $this->addBaseField(); - $storage_definition = BaseFieldDefinition::create('string') - ->setLabel(t('A new base field')) + $definitions['new_base_field'] = BaseFieldDefinition::create('string') + ->setName('new_base_field') + ->setLabel('A new base field') ->setInitialValueFromField('name'); + $definitions['another_base_field'] = BaseFieldDefinition::create('string') + ->setName('another_base_field') + ->setLabel('Another base field') + ->setInitialValueFromField('test_single_property', $default_initial_value); + + $this->state->set('entity_test_update.additional_base_field_definitions', $definitions); $this->assertFalse($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' does not exist before applying the update."); - $this->entityDefinitionUpdateManager->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $storage_definition); + $this->assertFalse($db_schema->fieldExists('entity_test_update', 'another_base_field'), "New field 'another_base_field' does not exist before applying the update."); + $this->entityDefinitionUpdateManager->installFieldStorageDefinition('new_base_field', 'entity_test_update', 'entity_test', $definitions['new_base_field']); + $this->entityDefinitionUpdateManager->installFieldStorageDefinition('another_base_field', 'entity_test_update', 'entity_test', $definitions['another_base_field']); $this->assertTrue($db_schema->fieldExists('entity_test_update', 'new_base_field'), "New field 'new_base_field' has been created on the 'entity_test_update' table."); + $this->assertTrue($db_schema->fieldExists('entity_test_update', 'another_base_field'), "New field 'another_base_field' has been created on the 'entity_test_update' table."); // Check that the initial values have been applied. $storage = \Drupal::entityTypeManager()->getStorage('entity_test_update'); $entities = $storage->loadMultiple(); $this->assertEquals('First entity', $entities[1]->get('new_base_field')->value); $this->assertEquals('Second entity', $entities[2]->get('new_base_field')->value); + + $this->assertEquals('test existing value', $entities[1]->get('another_base_field')->value); + $this->assertEquals($expected_value, $entities[2]->get('another_base_field')->value); + } + + /** + * Test cases for ::testInitialValueFromField. + */ + public function initialValueFromFieldTestCases() { + return [ + 'literal value' => [ + 'test initial value', + 'test initial value', + ], + 'indexed array' => [ + ['value' => 'test initial value'], + 'test initial value', + ], + 'empty array' => [ + [], + NULL, + ], + 'null' => [ + NULL, + NULL, + ], + ]; } /** @@ -1133,4 +1182,18 @@ class EntityDefinitionUpdateTest extends EntityKernelTestBase { } } + /** + * @covers ::getEntityTypes + */ + public function testGetEntityTypes() { + $entity_type_definitions = $this->entityDefinitionUpdateManager->getEntityTypes(); + + // Ensure that we have at least one entity type to check below. + $this->assertGreaterThanOrEqual(1, count($entity_type_definitions)); + + foreach ($entity_type_definitions as $entity_type_id => $entity_type) { + $this->assertEquals($this->entityDefinitionUpdateManager->getEntityType($entity_type_id), $entity_type); + } + } + }