From 0e454b550fb136737d3da81204be7b99a202e964 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Fri, 4 Mar 2016 09:25:41 +0900 Subject: [PATCH] Issue #2502917 by dimaro, rakesh.gectcr, DuaelFr, suhel.rangnekar: Replace deprecated usage of entity_create used with variable entity types with a direct call to EntityManager::getStorage()->create() --- .../src/Tests/ConfigEntityStaticCacheTest.php | 5 +- .../src/Tests/ConfigEntityStorageTest.php | 5 +- .../src/Tests/ContentTranslationTestBase.php | 4 +- core/modules/field/field.module | 4 +- .../field/src/Tests/BulkDeleteTest.php | 4 +- .../EntityReferenceFormatterTest.php | 16 ++++-- .../field/src/Tests/FieldAttachOtherTest.php | 16 ++++-- .../src/Tests/FieldAttachStorageTest.php | 32 +++++++++--- .../field/src/Tests/FieldDataCountTest.php | 6 +-- core/modules/field/src/Tests/FormTest.php | 4 +- .../field/src/Tests/NestedFormTest.php | 8 ++- .../field/src/Tests/TranslationTest.php | 8 ++- .../field/src/Tests/TranslationWebTest.php | 4 +- .../Tests/Field/EntityReferenceRdfaTest.php | 8 ++- core/modules/rest/src/Tests/CsrfTest.php | 4 +- core/modules/rest/src/Tests/RESTTestBase.php | 4 +- core/modules/rest/src/Tests/UpdateTest.php | 4 +- .../system/src/Tests/Entity/EntityApiTest.php | 12 +++-- .../Tests/Entity/EntityAutocompleteTest.php | 14 +++-- .../Tests/Entity/EntityCacheTagsTestBase.php | 26 ++++++---- .../Entity/EntityFieldDefaultValueTest.php | 4 +- .../src/Tests/Entity/EntityFieldTest.php | 52 ++++++++++++------- .../Tests/Entity/EntityReferenceFieldTest.php | 28 +++++++--- .../src/Tests/Entity/EntityRevisionsTest.php | 10 ++-- .../Tests/Entity/EntityTranslationTest.php | 44 ++++++++++------ .../src/Tests/Entity/EntityUUIDTest.php | 14 +++-- .../src/Tests/Entity/EntityValidationTest.php | 4 +- .../src/Tests/Entity/FieldSqlStorageTest.php | 32 ++++++++---- .../FieldWidgetConstraintValidatorTest.php | 4 +- .../src/Tests/Formatter/TextFormatterTest.php | 4 +- .../src/Tests/TextWithSummaryItemTest.php | 8 ++- 31 files changed, 269 insertions(+), 123 deletions(-) diff --git a/core/modules/config/src/Tests/ConfigEntityStaticCacheTest.php b/core/modules/config/src/Tests/ConfigEntityStaticCacheTest.php index 6b302697f6..e36331cf7e 100644 --- a/core/modules/config/src/Tests/ConfigEntityStaticCacheTest.php +++ b/core/modules/config/src/Tests/ConfigEntityStaticCacheTest.php @@ -45,7 +45,10 @@ protected function setUp() { parent::setUp(); $this->entityTypeId = 'config_test'; $this->entityId = 'test_1'; - entity_create($this->entityTypeId, array('id' => $this->entityId, 'label' => 'Original label'))->save(); + $this->container->get('entity_type.manager') + ->getStorage($this->entityTypeId) + ->create(array('id' => $this->entityId, 'label' => 'Original label')) + ->save(); } /** diff --git a/core/modules/config/src/Tests/ConfigEntityStorageTest.php b/core/modules/config/src/Tests/ConfigEntityStorageTest.php index dea998b6a5..0eb1ab69b1 100644 --- a/core/modules/config/src/Tests/ConfigEntityStorageTest.php +++ b/core/modules/config/src/Tests/ConfigEntityStorageTest.php @@ -32,7 +32,10 @@ public function testUUIDConflict() { $entity_type = 'config_test'; $id = 'test_1'; // Load the original configuration entity. - entity_create($entity_type, array('id' => $id))->save(); + $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('id' => $id)) + ->save(); $entity = entity_load($entity_type, $id); $original_properties = $entity->toArray(); diff --git a/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php b/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php index 9a93ba2592..09c13cc5cc 100644 --- a/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php +++ b/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php @@ -234,7 +234,9 @@ protected function createEntity($values, $langcode, $bundle_name = NULL) { } } } - $entity = entity_create($this->entityTypeId, $entity_values); + $entity = $this->container->get('entity_type.manager') + ->getStorage($this->entityTypeId) + ->create($entity_values); $entity->save(); return $entity->id(); } diff --git a/core/modules/field/field.module b/core/modules/field/field.module index ce65c79835..e453c46cbb 100644 --- a/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -292,7 +292,9 @@ function _field_create_entity_from_ids($ids) { if (isset($ids->bundle) && $bundle_key = $entity_type->getKey('bundle')) { $id_properties[$bundle_key] = $ids->bundle; } - return entity_create($ids->entity_type, $id_properties); + return \Drupal::entityTypeManager() + ->getStorage($ids->entity_type) + ->create($id_properties); } /** diff --git a/core/modules/field/src/Tests/BulkDeleteTest.php b/core/modules/field/src/Tests/BulkDeleteTest.php index 2c7835f67f..2949867ac1 100644 --- a/core/modules/field/src/Tests/BulkDeleteTest.php +++ b/core/modules/field/src/Tests/BulkDeleteTest.php @@ -133,7 +133,9 @@ protected function setUp() { ])->save(); } for ($i = 0; $i < 10; $i++) { - $entity = entity_create($this->entityTypeId, array('type' => $bundle)); + $entity = $this->container->get('entity_type.manager') + ->getStorage($this->entityTypeId) + ->create(array('type' => $bundle)); foreach ($this->fieldStorages as $field_storage) { $entity->{$field_storage->getName()}->setValue($this->_generateTestFieldValues($field_storage->getCardinality())); } diff --git a/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php b/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php index 8e1948d47b..a53a2fb113 100644 --- a/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php +++ b/core/modules/field/src/Tests/EntityReference/EntityReferenceFormatterTest.php @@ -108,7 +108,9 @@ protected function setUp() { ))->save(); // Create the entity to be referenced. - $this->referencedEntity = entity_create($this->entityType, array('name' => $this->randomMachineName())); + $this->referencedEntity = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('name' => $this->randomMachineName())); $this->referencedEntity->body = array( 'value' => '

Hello, world!

', 'format' => 'full_html', @@ -116,7 +118,9 @@ protected function setUp() { $this->referencedEntity->save(); // Create another entity to be referenced but do not save it. - $this->unsavedReferencedEntity = entity_create($this->entityType, array('name' => $this->randomMachineName())); + $this->unsavedReferencedEntity = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('name' => $this->randomMachineName())); $this->unsavedReferencedEntity->body = array( 'value' => '

Hello, unsaved world!

', 'format' => 'full_html', @@ -134,7 +138,9 @@ public function testAccess() { $field_name = $this->fieldName; - $referencing_entity = entity_create($this->entityType, array('name' => $this->randomMachineName())); + $referencing_entity = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('name' => $this->randomMachineName())); $referencing_entity->save(); $referencing_entity->{$field_name}->entity = $this->referencedEntity; @@ -286,7 +292,9 @@ public function testLabelFormatter() { */ protected function buildRenderArray(array $referenced_entities, $formatter, $formatter_options = array()) { // Create the entity that will have the entity reference field. - $referencing_entity = entity_create($this->entityType, array('name' => $this->randomMachineName())); + $referencing_entity = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('name' => $this->randomMachineName())); $items = $referencing_entity->get($this->fieldName); diff --git a/core/modules/field/src/Tests/FieldAttachOtherTest.php b/core/modules/field/src/Tests/FieldAttachOtherTest.php index 37c6fecc10..6c67793e75 100644 --- a/core/modules/field/src/Tests/FieldAttachOtherTest.php +++ b/core/modules/field/src/Tests/FieldAttachOtherTest.php @@ -32,7 +32,9 @@ function testEntityDisplayBuild() { $this->createFieldWithStorage('_2'); $entity_type = 'entity_test'; - $entity_init = entity_create($entity_type); + $entity_init = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(); // Populate values to be displayed. $values = $this->_generateTestFieldValues($this->fieldTestData->field_storage->getCardinality()); @@ -186,9 +188,11 @@ function testEntityCache() { $entity_type = 'entity_test_rev'; $this->createFieldWithStorage('_2', $entity_type); - $entity_init = entity_create($entity_type, array( - 'type' => $entity_type, - )); + $entity_init = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'type' => $entity_type, + )); // Check that no initial cache entry is present. $cid = "values:$entity_type:" . $entity->id(); @@ -293,7 +297,9 @@ function testEntityFormDisplayExtractFormValues() { $this->createFieldWithStorage('_2'); $entity_type = 'entity_test'; - $entity_init = entity_create($entity_type, array('id' => 1, 'revision_id' => 1, 'type' => $this->fieldTestData->field->getTargetBundle())); + $entity_init = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('id' => 1, 'revision_id' => 1, 'type' => $this->fieldTestData->field->getTargetBundle())); // Build the form for all fields. $display = entity_get_form_display($entity_type, $this->fieldTestData->field->getTargetBundle(), 'default'); diff --git a/core/modules/field/src/Tests/FieldAttachStorageTest.php b/core/modules/field/src/Tests/FieldAttachStorageTest.php index d7003345df..596eb13a00 100644 --- a/core/modules/field/src/Tests/FieldAttachStorageTest.php +++ b/core/modules/field/src/Tests/FieldAttachStorageTest.php @@ -37,7 +37,9 @@ function testFieldAttachSaveLoad() { // TODO : test empty values filtering and "compression" (store consecutive deltas). // Preparation: create three revisions and store them in $revision array. $values = array(); - $entity = entity_create($entity_type); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(); for ($revision_id = 0; $revision_id < 3; $revision_id++) { // Note: we try to insert one extra value. $current_values = $this->_generateTestFieldValues($cardinality + 1); @@ -114,7 +116,9 @@ function testFieldAttachLoadMultiple() { // Create one test entity per bundle, with random values. foreach ($bundles as $index => $bundle) { - $entities[$index] = entity_create($entity_type, array('id' => $index, 'revision_id' => $index, 'type' => $bundle)); + $entities[$index] = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('id' => $index, 'revision_id' => $index, 'type' => $bundle)); $entity = clone($entities[$index]); foreach ($field_names as $field_name) { if (!$entity->hasField($field_name)) { @@ -149,7 +153,9 @@ function testFieldAttachSaveEmptyData() { $entity_type = 'entity_test'; $this->createFieldWithStorage('', $entity_type); - $entity_init = entity_create($entity_type, array('id' => 1)); + $entity_init = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('id' => 1)); // Insert: Field is NULL. $entity = clone $entity_init; @@ -200,7 +206,9 @@ function testFieldAttachSaveEmptyDataDefaultValue() { $this->fieldTestData->field->save(); // Verify that fields are populated with default values. - $entity_init = entity_create($entity_type, array('id' => 1, 'revision_id' => 1)); + $entity_init = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('id' => 1, 'revision_id' => 1)); $default = field_test_default_value($entity_init, $this->fieldTestData->field); $this->assertEqual($entity_init->{$this->fieldTestData->field_name}->getValue(), $default, 'Default field value correctly populated.'); @@ -213,7 +221,9 @@ function testFieldAttachSaveEmptyDataDefaultValue() { // Verify that prepopulated field values are not overwritten by defaults. $value = array(array('value' => $default[0]['value'] - mt_rand(1, 127))); - $entity = entity_create($entity_type, array('type' => $entity_init->bundle(), $this->fieldTestData->field_name => $value)); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('type' => $entity_init->bundle(), $this->fieldTestData->field_name => $value)); $this->assertEqual($entity->{$this->fieldTestData->field_name}->getValue(), $value, 'Prepopulated field value correctly maintained.'); } @@ -224,7 +234,9 @@ function testFieldAttachDelete() { $entity_type = 'entity_test_rev'; $this->createFieldWithStorage('', $entity_type); $cardinality = $this->fieldTestData->field_storage->getCardinality(); - $entity = entity_create($entity_type, array('type' => $this->fieldTestData->field->getTargetBundle())); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('type' => $this->fieldTestData->field->getTargetBundle())); $vids = array(); // Create revision 0 @@ -292,7 +304,9 @@ function testEntityCreateBundle() { FieldConfig::create($this->fieldTestData->field_definition)->save(); // Save an entity with data in the field. - $entity = entity_create($entity_type, array('type' => $this->fieldTestData->field->getTargetBundle())); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('type' => $this->fieldTestData->field->getTargetBundle())); $values = $this->_generateTestFieldValues($cardinality); $entity->{$this->fieldTestData->field_name} = $values; @@ -336,7 +350,9 @@ function testEntityDeleteBundle() { FieldConfig::create($field)->save(); // Save an entity with data for both fields - $entity = entity_create($entity_type, array('type' => $this->fieldTestData->field->getTargetBundle())); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('type' => $this->fieldTestData->field->getTargetBundle())); $values = $this->_generateTestFieldValues($this->fieldTestData->field_storage->getCardinality()); $entity->{$this->fieldTestData->field_name} = $values; $entity->{$field_name} = $this->_generateTestFieldValues(1); diff --git a/core/modules/field/src/Tests/FieldDataCountTest.php b/core/modules/field/src/Tests/FieldDataCountTest.php index a844b056a3..c146289bfa 100644 --- a/core/modules/field/src/Tests/FieldDataCountTest.php +++ b/core/modules/field/src/Tests/FieldDataCountTest.php @@ -115,9 +115,9 @@ public function testEntityCountAndHasData() { $entity_type = 'entity_test_rev'; $this->createFieldWithStorage('_2', $entity_type); - $entity_init = entity_create($entity_type, array( - 'type' => $entity_type, - )); + $entity_init = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('type' => $entity_type,)); $cardinality = $this->fieldTestData->field_storage_2->getCardinality(); $this->assertIdentical($this->fieldTestData->field_storage_2->hasData(), FALSE, 'There are no entities with field data.'); diff --git a/core/modules/field/src/Tests/FormTest.php b/core/modules/field/src/Tests/FormTest.php index c629ca7250..79ba7d1a74 100644 --- a/core/modules/field/src/Tests/FormTest.php +++ b/core/modules/field/src/Tests/FormTest.php @@ -543,7 +543,9 @@ function testFieldFormAccess() { // Test that the form structure includes full information for each delta // apart from #access. - $entity = entity_create($entity_type, array('id' => 0, 'revision_id' => 0)); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('id' => 0, 'revision_id' => 0)); $display = entity_get_form_display($entity_type, $entity_type, 'default'); $form = array(); diff --git a/core/modules/field/src/Tests/NestedFormTest.php b/core/modules/field/src/Tests/NestedFormTest.php index ac5dca4c0b..5d6b410066 100644 --- a/core/modules/field/src/Tests/NestedFormTest.php +++ b/core/modules/field/src/Tests/NestedFormTest.php @@ -76,13 +76,17 @@ function testNestedFieldForm() { // Create two entities. $entity_type = 'entity_test'; - $entity_1 = entity_create($entity_type, array('id' => 1)); + $entity_1 = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('id' => 1)); $entity_1->enforceIsNew(); $entity_1->field_single->value = 0; $entity_1->field_unlimited->value = 1; $entity_1->save(); - $entity_2 = entity_create($entity_type, array('id' => 2)); + $entity_2 = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('id' => 2)); $entity_2->enforceIsNew(); $entity_2->field_single->value = 10; $entity_2->field_unlimited->value = 11; diff --git a/core/modules/field/src/Tests/TranslationTest.php b/core/modules/field/src/Tests/TranslationTest.php index 359ef7d013..efe1b7abd9 100644 --- a/core/modules/field/src/Tests/TranslationTest.php +++ b/core/modules/field/src/Tests/TranslationTest.php @@ -120,7 +120,9 @@ function testTranslatableFieldSaveLoad() { // Prepare the field translations. $entity_type_id = 'entity_test'; field_test_entity_info_translatable($entity_type_id, TRUE); - $entity = entity_create($entity_type_id, array('type' => $this->field->getTargetBundle())); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type_id) + ->create(array('type' => $this->field->getTargetBundle())); $field_translations = array(); $available_langcodes = array_keys($this->container->get('language_manager')->getLanguages()); $entity->langcode->value = reset($available_langcodes); @@ -160,7 +162,9 @@ function testTranslatableFieldSaveLoad() { $translation_langcodes = array_values($translation_langcodes); $values = array('type' => $field->getTargetBundle(), 'langcode' => $translation_langcodes[0]); - $entity = entity_create($entity_type_id, $values); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type_id) + ->create($values); foreach ($translation_langcodes as $langcode) { $values[$this->fieldName][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality()); $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode); diff --git a/core/modules/field/src/Tests/TranslationWebTest.php b/core/modules/field/src/Tests/TranslationWebTest.php index 78530c04c5..1606ea051c 100644 --- a/core/modules/field/src/Tests/TranslationWebTest.php +++ b/core/modules/field/src/Tests/TranslationWebTest.php @@ -96,7 +96,9 @@ function testFieldFormTranslationRevisions() { // Prepare the field translations. field_test_entity_info_translatable($this->entityTypeId, TRUE); - $entity = entity_create($this->entityTypeId); + $entity = $this->container->get('entity_type.manager') + ->getStorage($this->entityTypeId) + ->create(); $available_langcodes = array_flip(array_keys($this->container->get('language_manager')->getLanguages())); $field_name = $this->fieldStorage->getName(); diff --git a/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php b/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php index 73e6e53727..9a4ac0573a 100644 --- a/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php +++ b/core/modules/rdf/src/Tests/Field/EntityReferenceRdfaTest.php @@ -70,11 +70,15 @@ protected function setUp() { ))->save(); // Create the entity to be referenced. - $this->targetEntity = entity_create($this->entityType, array('name' => $this->randomMachineName())); + $this->targetEntity = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('name' => $this->randomMachineName())); $this->targetEntity->save(); // Create the entity that will have the entity reference field. - $this->entity = entity_create($this->entityType, array('name' => $this->randomMachineName())); + $this->entity = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('name' => $this->randomMachineName())); $this->entity->save(); $this->entity->{$this->fieldName}->entity = $this->targetEntity; $this->uri = $this->getAbsoluteUri($this->entity); diff --git a/core/modules/rest/src/Tests/CsrfTest.php b/core/modules/rest/src/Tests/CsrfTest.php index af49a334c9..25beae1a94 100644 --- a/core/modules/rest/src/Tests/CsrfTest.php +++ b/core/modules/rest/src/Tests/CsrfTest.php @@ -54,7 +54,9 @@ protected function setUp() { // request. $serializer = $this->container->get('serializer'); $entity_values = $this->entityValues($this->testEntityType); - $entity = entity_create($this->testEntityType, $entity_values); + $entity = $this->container->get('entity_type.manager') + ->getStorage($this->testEntityType) + ->create($entity_values); $this->serialized = $serializer->serialize($entity, $this->defaultFormat); } diff --git a/core/modules/rest/src/Tests/RESTTestBase.php b/core/modules/rest/src/Tests/RESTTestBase.php index db7d1236e2..87d18bdc70 100644 --- a/core/modules/rest/src/Tests/RESTTestBase.php +++ b/core/modules/rest/src/Tests/RESTTestBase.php @@ -183,7 +183,9 @@ protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL) { * The new entity object. */ protected function entityCreate($entity_type) { - return entity_create($entity_type, $this->entityValues($entity_type)); + return $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create($this->entityValues($entity_type)); } /** diff --git a/core/modules/rest/src/Tests/UpdateTest.php b/core/modules/rest/src/Tests/UpdateTest.php index ef18a743dc..f407c1e11e 100644 --- a/core/modules/rest/src/Tests/UpdateTest.php +++ b/core/modules/rest/src/Tests/UpdateTest.php @@ -50,7 +50,9 @@ public function testPatchUpdate() { 'value' => $this->randomString(), 'format' => 'plain_text', )); - $patch_entity = entity_create($entity_type, $patch_values); + $patch_entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create($patch_values); // We don't want to overwrite the UUID. $patch_entity->set('uuid', NULL); $serialized = $serializer->serialize($patch_entity, $this->defaultFormat, $context); diff --git a/core/modules/system/src/Tests/Entity/EntityApiTest.php b/core/modules/system/src/Tests/Entity/EntityApiTest.php index 7445734107..396e4dc6bb 100644 --- a/core/modules/system/src/Tests/Entity/EntityApiTest.php +++ b/core/modules/system/src/Tests/Entity/EntityApiTest.php @@ -52,11 +52,17 @@ public function testCRUD() { */ protected function assertCRUD($entity_type, UserInterface $user1) { // Create some test entities. - $entity = entity_create($entity_type, array('name' => 'test', 'user_id' => $user1->id())); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('name' => 'test', 'user_id' => $user1->id())); $entity->save(); - $entity = entity_create($entity_type, array('name' => 'test2', 'user_id' => $user1->id())); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('name' => 'test2', 'user_id' => $user1->id())); $entity->save(); - $entity = entity_create($entity_type, array('name' => 'test', 'user_id' => NULL)); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('name' => 'test', 'user_id' => NULL)); $entity->save(); $entities = array_values(entity_load_multiple_by_properties($entity_type, array('name' => 'test'))); diff --git a/core/modules/system/src/Tests/Entity/EntityAutocompleteTest.php b/core/modules/system/src/Tests/Entity/EntityAutocompleteTest.php index d42a07a1de..a7f01ed0c0 100644 --- a/core/modules/system/src/Tests/Entity/EntityAutocompleteTest.php +++ b/core/modules/system/src/Tests/Entity/EntityAutocompleteTest.php @@ -50,15 +50,21 @@ protected function setUp() { */ function testEntityReferenceAutocompletion() { // Add an entity with a slash in its name. - $entity_1 = entity_create($this->entityType, array('name' => '10/16/2011')); + $entity_1 = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('name' => '10/16/2011')); $entity_1->save(); // Add another entity that differs after the slash character. - $entity_2 = entity_create($this->entityType, array('name' => '10/17/2011')); + $entity_2 = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('name' => '10/17/2011')); $entity_2->save(); // Add another entity that has both a comma, a slash and markup. - $entity_3 = entity_create($this->entityType, array('name' => 'label with, and / test')); + $entity_3 = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('name' => 'label with, and / test')); $entity_3->save(); // Try to autocomplete a entity label that matches both entities. @@ -85,7 +91,7 @@ function testEntityReferenceAutocompletion() { $this->assertIdentical($data[0]['label'], Html::escape($entity_2->name->value), 'Autocomplete returned the second matching entity'); // Try to autocomplete a entity label with both a comma, a slash and markup. - $input = '"label with, and / '; + $input = '"label with, and /"'; $data = $this->getAutocompleteResult($input); $n = $entity_3->name->value . ' (3)'; // Entity labels containing commas or quotes must be wrapped in quotes. diff --git a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php index 53df17505e..2c712b1955 100644 --- a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php @@ -291,20 +291,24 @@ protected function createReferenceTestEntities($referenced_entity) { // Create an entity that does reference the entity being tested. $label_key = \Drupal::entityManager()->getDefinition($entity_type)->getKey('label'); - $referencing_entity = entity_create($entity_type, array( - $label_key => 'Referencing ' . $entity_type, - 'status' => 1, - 'type' => $bundle, - $field_name => array('target_id' => $referenced_entity->id()), - )); + $referencing_entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + $label_key => 'Referencing ' . $entity_type, + 'status' => 1, + 'type' => $bundle, + $field_name => array('target_id' => $referenced_entity->id()), + )); $referencing_entity->save(); // Create an entity that does not reference the entity being tested. - $non_referencing_entity = entity_create($entity_type, array( - $label_key => 'Non-referencing ' . $entity_type, - 'status' => 1, - 'type' => $bundle, - )); + $non_referencing_entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + $label_key => 'Non-referencing ' . $entity_type, + 'status' => 1, + 'type' => $bundle, + )); $non_referencing_entity->save(); return array( diff --git a/core/modules/system/src/Tests/Entity/EntityFieldDefaultValueTest.php b/core/modules/system/src/Tests/Entity/EntityFieldDefaultValueTest.php index e8453ff9ca..fb9f10363f 100644 --- a/core/modules/system/src/Tests/Entity/EntityFieldDefaultValueTest.php +++ b/core/modules/system/src/Tests/Entity/EntityFieldDefaultValueTest.php @@ -47,7 +47,9 @@ public function testDefaultValues() { * The entity type to run the tests with. */ protected function assertDefaultValues($entity_type_id) { - $entity = entity_create($entity_type_id); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type_id) + ->create(); $definition = $this->entityManager->getDefinition($entity_type_id); $langcode_key = $definition->getKey('langcode'); $this->assertEqual($entity->{$langcode_key}->value, 'en', SafeMarkup::format('%entity_type: Default language', array('%entity_type' => $entity_type_id))); diff --git a/core/modules/system/src/Tests/Entity/EntityFieldTest.php b/core/modules/system/src/Tests/Entity/EntityFieldTest.php index 5c9161e3c8..8919326fd1 100644 --- a/core/modules/system/src/Tests/Entity/EntityFieldTest.php +++ b/core/modules/system/src/Tests/Entity/EntityFieldTest.php @@ -77,7 +77,9 @@ protected function createTestEntity($entity_type) { // Pass in the value of the name field when creating. With the user // field we test setting a field after creation. - $entity = entity_create($entity_type); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(); $entity->user_id->target_id = $this->entityUser->id(); $entity->name->value = $this->entityName; @@ -172,9 +174,11 @@ protected function doTestReadWrite($entity_type) { // Create a fresh entity so target_id does not get its property object // instantiated, then verify setting a new value via typed data API works. - $entity2 = entity_create($entity_type, array( - 'user_id' => array('target_id' => $new_user1->id()), - )); + $entity2 = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'user_id' => array('target_id' => $new_user1->id()), + )); // Access the property object, and set a value. $entity2->user_id->first()->get('target_id')->setValue($new_user2->id()); $this->assertEqual($new_user2->id(), $entity2->user_id->target_id, format_string('%entity_type: Updated user id can be read.', array('%entity_type' => $entity_type))); @@ -255,11 +259,13 @@ protected function doTestReadWrite($entity_type) { $this->entityFieldText = $this->randomMachineName(); $text_item[0]['value'] = $this->entityFieldText; - $entity = entity_create($entity_type, array( - 'name' => $name_item, - 'user_id' => $user_item, - 'field_test_text' => $text_item, - )); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'name' => $name_item, + 'user_id' => $user_item, + 'field_test_text' => $text_item, + )); $this->assertEqual($this->entityName, $entity->name->value, format_string('%entity_type: Name value can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entityUser->id(), $entity->user_id->target_id, format_string('%entity_type: User id can be read.', array('%entity_type' => $entity_type))); $this->assertEqual($this->entityUser->getUsername(), $entity->user_id->entity->name->value, format_string('%entity_type: User name can be read.', array('%entity_type' => $entity_type))); @@ -327,20 +333,24 @@ protected function doTestReadWrite($entity_type) { // Make sure the user id can be set to zero. $user_item[0]['target_id'] = 0; - $entity = entity_create($entity_type, array( - 'name' => $name_item, - 'user_id' => $user_item, - 'field_test_text' => $text_item, - )); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'name' => $name_item, + 'user_id' => $user_item, + 'field_test_text' => $text_item, + )); $this->assertNotNull($entity->user_id->target_id, format_string('%entity_type: User id is not NULL', array('%entity_type' => $entity_type))); $this->assertIdentical($entity->user_id->target_id, 0, format_string('%entity_type: User id has been set to 0', array('%entity_type' => $entity_type))); // Test setting the ID with the value only. - $entity = entity_create($entity_type, array( - 'name' => $name_item, - 'user_id' => 0, - 'field_test_text' => $text_item, - )); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'name' => $name_item, + 'user_id' => 0, + 'field_test_text' => $text_item, + )); $this->assertNotNull($entity->user_id->target_id, format_string('%entity_type: User id is not NULL', array('%entity_type' => $entity_type))); $this->assertIdentical($entity->user_id->target_id, 0, format_string('%entity_type: User id has been set to 0', array('%entity_type' => $entity_type))); } @@ -435,7 +445,9 @@ protected function doTestIntrospection($entity_type) { // Test introspecting an entity object. // @todo: Add bundles and test bundles as well. - $entity = entity_create($entity_type); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(); $definitions = $entity->getFieldDefinitions(); $this->assertEqual($definitions['name']->getType(), 'string', $entity_type .': Name field found.'); diff --git a/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php b/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php index 33379bc4d6..38a6653009 100644 --- a/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php +++ b/core/modules/system/src/Tests/Entity/EntityReferenceFieldTest.php @@ -93,10 +93,14 @@ protected function setUp() { */ public function testEntityReferenceFieldValidation() { // Test a valid reference. - $referenced_entity = entity_create($this->referencedEntityType, array('type' => $this->bundle)); + $referenced_entity = $this->container->get('entity_type.manager') + ->getStorage($this->referencedEntityType) + ->create(array('type' => $this->bundle)); $referenced_entity->save(); - $entity = entity_create($this->entityType, array('type' => $this->bundle)); + $entity = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('type' => $this->bundle)); $entity->{$this->fieldName}->target_id = $referenced_entity->id(); $violations = $entity->{$this->fieldName}->validate(); $this->assertEqual($violations->count(), 0, 'Validation passes.'); @@ -122,13 +126,17 @@ public function testEntityReferenceFieldValidation() { */ public function testReferencedEntitiesMultipleLoad() { // Create the parent entity. - $entity = entity_create($this->entityType, array('type' => $this->bundle)); + $entity = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('type' => $this->bundle)); // Create three target entities and attach them to parent field. $target_entities = array(); $reference_field = array(); for ($i = 0; $i < 3; $i++) { - $target_entity = entity_create($this->referencedEntityType, array('type' => $this->bundle)); + $target_entity = $this->container->get('entity_type.manager') + ->getStorage($this->referencedEntityType) + ->create(array('type' => $this->bundle)); $target_entity->save(); $target_entities[] = $target_entity; $reference_field[]['target_id'] = $target_entity->id(); @@ -149,7 +157,9 @@ public function testReferencedEntitiesMultipleLoad() { // Create a new target entity that is not saved, thus testing the // "autocreate" feature. - $target_entity_unsaved = entity_create($this->referencedEntityType, array('type' => $this->bundle, 'name' => $this->randomString())); + $target_entity_unsaved = $this->container->get('entity_type.manager') + ->getStorage($this->referencedEntityType) + ->create(array('type' => $this->bundle, 'name' => $this->randomString())); $reference_field[6]['entity'] = $target_entity_unsaved; $target_entities[6] = $target_entity_unsaved; @@ -200,7 +210,9 @@ public function testReferencedEntitiesStringId() { FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED ); // Create the parent entity. - $entity = entity_create($this->entityType, array('type' => $this->bundle)); + $entity = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('type' => $this->bundle)); // Create the default target entity. $target_entity = EntityTestStringId::create([ @@ -224,7 +236,9 @@ public function testReferencedEntitiesStringId() { $this->assertConfigSchema(\Drupal::service('config.typed'), 'field.field.' . $field->id(), $field->toArray()); // Test that the default value works. - $entity = entity_create($this->entityType, array('type' => $this->bundle)); + $entity = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('type' => $this->bundle)); $entities = $entity->{$field_name}->referencedEntities(); $this->assertEqual($entities[0]->id(), $target_entity->id()); } diff --git a/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php b/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php index 39f04c32d7..057d23af82 100644 --- a/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php +++ b/core/modules/system/src/Tests/Entity/EntityRevisionsTest.php @@ -62,10 +62,12 @@ public function testRevisions() { protected function runRevisionsTests($entity_type) { // Create initial entity. - $entity = entity_create($entity_type, array( - 'name' => 'foo', - 'user_id' => $this->webUser->id(), - )); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'name' => 'foo', + 'user_id' => $this->webUser->id(), + )); $entity->field_test_text->value = 'bar'; $entity->save(); diff --git a/core/modules/system/src/Tests/Entity/EntityTranslationTest.php b/core/modules/system/src/Tests/Entity/EntityTranslationTest.php index de62ba73b6..f867d98fb5 100644 --- a/core/modules/system/src/Tests/Entity/EntityTranslationTest.php +++ b/core/modules/system/src/Tests/Entity/EntityTranslationTest.php @@ -39,16 +39,21 @@ public function testEntityLanguageMethods() { */ protected function doTestEntityLanguageMethods($entity_type) { $langcode_key = $this->entityManager->getDefinition($entity_type)->getKey('langcode'); - $entity = entity_create($entity_type, array( - 'name' => 'test', - 'user_id' => $this->container->get('current_user')->id(), - )); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'name' => 'test', + 'user_id' => $this->container->get('current_user')->id(), + )); $this->assertEqual($entity->language()->getId(), $this->languageManager->getDefaultLanguage()->getId(), format_string('%entity_type: Entity created with API has default language.', array('%entity_type' => $entity_type))); - $entity = entity_create($entity_type, array( - 'name' => 'test', - 'user_id' => \Drupal::currentUser()->id(), - $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED, - )); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'name' => 'test', + 'user_id' => \Drupal::currentUser()->id(), + $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED, + )); + $this->assertEqual($entity->language()->getId(), LanguageInterface::LANGCODE_NOT_SPECIFIED, format_string('%entity_type: Entity language not specified.', array('%entity_type' => $entity_type))); $this->assertFalse($entity->getTranslationLanguages(FALSE), format_string('%entity_type: No translations are available', array('%entity_type' => $entity_type))); @@ -164,7 +169,9 @@ protected function doTestMultilingualProperties($entity_type) { // Create a language neutral entity and check that properties are stored // as language neutral. - $entity = entity_create($entity_type, array('name' => $name, 'user_id' => $uid, $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED)); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('name' => $name, 'user_id' => $uid, $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED)); $entity->save(); $entity = entity_load($entity_type, $entity->id()); $default_langcode = $entity->language()->getId(); @@ -186,7 +193,9 @@ protected function doTestMultilingualProperties($entity_type) { // Create a language-aware entity and check that properties are stored // as language-aware. - $entity = entity_create($entity_type, array('name' => $name, 'user_id' => $uid, $langcode_key => $langcode)); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('name' => $name, 'user_id' => $uid, $langcode_key => $langcode)); $entity->save(); $entity = entity_load($entity_type, $entity->id()); $default_langcode = $entity->language()->getId(); @@ -238,11 +247,14 @@ protected function doTestMultilingualProperties($entity_type) { // Create an additional entity with only the uid set. The uid for the // original language is the same of one used for a translation. $langcode = $this->langcodes[1]; - entity_create($entity_type, array( - 'user_id' => $properties[$langcode]['user_id'], - 'name' => 'some name', - $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED, - ))->save(); + $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'user_id' => $properties[$langcode]['user_id'], + 'name' => 'some name', + $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED, + )) + ->save(); $entities = entity_load_multiple($entity_type); $this->assertEqual(count($entities), 3, format_string('%entity_type: Three entities were created.', array('%entity_type' => $entity_type))); diff --git a/core/modules/system/src/Tests/Entity/EntityUUIDTest.php b/core/modules/system/src/Tests/Entity/EntityUUIDTest.php index 48a6e4e32c..570d55698c 100644 --- a/core/modules/system/src/Tests/Entity/EntityUUIDTest.php +++ b/core/modules/system/src/Tests/Entity/EntityUUIDTest.php @@ -45,16 +45,20 @@ protected function assertCRUD($entity_type) { // Verify that no UUID is auto-generated when passing one for creation. $uuid_service = $this->container->get('uuid'); $uuid = $uuid_service->generate(); - $custom_entity = entity_create($entity_type, array( - 'name' => $this->randomMachineName(), - 'uuid' => $uuid, - )); + $custom_entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'name' => $this->randomMachineName(), + 'uuid' => $uuid, + )); $this->assertIdentical($custom_entity->uuid(), $uuid); // Save this entity, so we have more than one later. $custom_entity->save(); // Verify that a new UUID is generated upon creating an entity. - $entity = entity_create($entity_type, array('name' => $this->randomMachineName())); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('name' => $this->randomMachineName())); $uuid = $entity->uuid(); $this->assertTrue($uuid); diff --git a/core/modules/system/src/Tests/Entity/EntityValidationTest.php b/core/modules/system/src/Tests/Entity/EntityValidationTest.php index aa85fa7b3b..7b5eb5803e 100644 --- a/core/modules/system/src/Tests/Entity/EntityValidationTest.php +++ b/core/modules/system/src/Tests/Entity/EntityValidationTest.php @@ -67,7 +67,9 @@ protected function createTestEntity($entity_type) { // Pass in the value of the name field when creating. With the user // field we test setting a field after creation. - $entity = entity_create($entity_type); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(); $entity->user_id->target_id = $this->entityUser->id(); $entity->name->value = $this->entityName; diff --git a/core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php b/core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php index 9f5028ffdc..6bfbe940bb 100644 --- a/core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php +++ b/core/modules/system/src/Tests/Entity/FieldSqlStorageTest.php @@ -116,7 +116,9 @@ function testFieldLoad() { // Create an entity with four revisions. $revision_ids = array(); - $entity = entity_create($entity_type); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(); $entity->save(); $revision_ids[] = $entity->getRevisionId(); for ($i = 0; $i < 4; $i++) { @@ -182,7 +184,9 @@ function testFieldLoad() { */ function testFieldWrite() { $entity_type = $bundle = 'entity_test_rev'; - $entity = entity_create($entity_type); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(); $revision_values = array(); @@ -297,7 +301,9 @@ function testLongNames() { } // Save an entity with values. - $entity = entity_create($entity_type, $values); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create($values); $entity->save(); // Load the entity back and check the values. @@ -325,10 +331,12 @@ function testUpdateFieldSchemaWithData() { 'bundle' => $entity_type, ]); $field->save(); - $entity = entity_create($entity_type, array( - 'id' => 0, - 'revision_id' => 0, - )); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'id' => 0, + 'revision_id' => 0, + )); $entity->decimal52->value = '1.235'; $entity->save(); @@ -408,10 +416,12 @@ function testFieldUpdateIndexesWithData() { } // Add data so the table cannot be dropped. - $entity = entity_create($entity_type, array( - 'id' => 1, - 'revision_id' => 1, - )); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array( + 'id' => 1, + 'revision_id' => 1, + )); $entity->$field_name->value = 'field data'; $entity->enforceIsNew(); $entity->save(); diff --git a/core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php b/core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php index 68bfee66de..34233c4902 100644 --- a/core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php +++ b/core/modules/system/src/Tests/Entity/FieldWidgetConstraintValidatorTest.php @@ -41,7 +41,9 @@ protected function setUp() { */ public function testValidation() { $entity_type = 'entity_test_constraint_violation'; - $entity = entity_create($entity_type, array('id' => 1, 'revision_id' => 1)); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(array('id' => 1, 'revision_id' => 1)); $display = entity_get_form_display($entity_type, $entity_type, 'default'); $form = array(); $form_state = new FormState(); diff --git a/core/modules/text/src/Tests/Formatter/TextFormatterTest.php b/core/modules/text/src/Tests/Formatter/TextFormatterTest.php index ca44407218..437b8b6382 100644 --- a/core/modules/text/src/Tests/Formatter/TextFormatterTest.php +++ b/core/modules/text/src/Tests/Formatter/TextFormatterTest.php @@ -82,7 +82,9 @@ public function testFormatters() { ); // Create the entity to be referenced. - $entity = entity_create($this->entityType, array('name' => $this->randomMachineName())); + $entity = $this->container->get('entity_type.manager') + ->getStorage($this->entityType) + ->create(array('name' => $this->randomMachineName())); $entity->formatted_text = array( 'value' => 'Hello, world!', 'format' => 'my_text_format', diff --git a/core/modules/text/src/Tests/TextWithSummaryItemTest.php b/core/modules/text/src/Tests/TextWithSummaryItemTest.php index cf42cbec0b..dcb765f870 100644 --- a/core/modules/text/src/Tests/TextWithSummaryItemTest.php +++ b/core/modules/text/src/Tests/TextWithSummaryItemTest.php @@ -64,7 +64,9 @@ public function testCrudAndUpdate() { $this->createField($entity_type); // Create an entity with a summary and no text format. - $entity = entity_create($entity_type); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(); $entity->summary_field->value = $value = $this->randomMachineName(); $entity->summary_field->summary = $summary = $this->randomMachineName(); $entity->summary_field->format = NULL; @@ -88,7 +90,9 @@ public function testCrudAndUpdate() { $this->assertEqual($entity->summary_field->summary_processed, $summary); // Test the generateSampleValue() method. - $entity = entity_create($entity_type); + $entity = $this->container->get('entity_type.manager') + ->getStorage($entity_type) + ->create(); $entity->summary_field->generateSampleItems(); $this->entityValidateAndSave($entity); } -- GitLab