diff --git a/core/modules/system/src/Tests/Entity/EntityTranslationTest.php b/core/modules/system/src/Tests/Entity/EntityTranslationTest.php index 611ef65..6289c03 100644 --- a/core/modules/system/src/Tests/Entity/EntityTranslationTest.php +++ b/core/modules/system/src/Tests/Entity/EntityTranslationTest.php @@ -298,12 +298,12 @@ protected function doTestMultilingualProperties($entity_type) { function testEntityTranslationAPI() { // Test all entity variations with data table support. foreach (entity_test_entity_types(ENTITY_TEST_TYPES_MULTILINGUAL) as $entity_type) { - $this->doTestMultilingualProperties($entity_type); + $this->doTestEntityTranslationAPI($entity_type); } } /** - * Executes the Entity Translation API for the given entity type. + * Executes the Entity Translation API tests for the given entity type. * * @param string $entity_type * The entity type to run the tests with. @@ -311,7 +311,7 @@ function testEntityTranslationAPI() { protected function doTestEntityTranslationAPI($entity_type) { $default_langcode = $this->langcodes[0]; $langcode = $this->langcodes[1]; - $langcode_key = 'langcode'; + $langcode_key = $this->entityManager->getDefinition($entity_type)->getKey('langcode'); $entity = $this->entityManager ->getStorage($entity_type) ->create(array('name' => $this->randomMachineName(), $langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED)); @@ -349,7 +349,7 @@ protected function doTestEntityTranslationAPI($entity_type) { $translation->save(); $hooks = $this->getHooksInfo(); $this->assertEqual($hooks['entity_translation_insert'], $langcode, 'The generic entity translation insertion hook has fired.'); - $this->assertEqual($hooks['entity_test_mul_translation_insert'], $langcode, 'The entity-type-specific entity translation insertion hook has fired.'); + $this->assertEqual($hooks[$entity_type . '_translation_insert'], $langcode, 'The entity-type-specific entity translation insertion hook has fired.'); // Check that after loading an entity the language is the default one. $entity = $this->reloadEntity($entity); @@ -366,7 +366,7 @@ protected function doTestEntityTranslationAPI($entity_type) { $translation->save(); $hooks = $this->getHooksInfo(); $this->assertEqual($hooks['entity_translation_insert'], $langcode2, 'The generic entity translation insertion hook has fired.'); - $this->assertEqual($hooks['entity_test_mul_translation_insert'], $langcode2, 'The entity-type-specific entity translation insertion hook has fired.'); + $this->assertEqual($hooks[$entity_type . '_translation_insert'], $langcode2, 'The entity-type-specific entity translation insertion hook has fired.'); // Verify that trying to manipulate a translation object referring to a // removed translation results in exceptions being thrown. @@ -389,7 +389,7 @@ protected function doTestEntityTranslationAPI($entity_type) { $entity->save(); $hooks = $this->getHooksInfo(); $this->assertEqual($hooks['entity_translation_delete'], $langcode2, 'The generic entity translation deletion hook has fired.'); - $this->assertEqual($hooks['entity_test_mul_translation_delete'], $langcode2, 'The entity-type-specific entity translation deletion hook has fired.'); + $this->assertEqual($hooks[$entity_type . '_translation_delete'], $langcode2, 'The entity-type-specific entity translation deletion hook has fired.'); $entity = $this->reloadEntity($entity); $this->assertFalse($entity->hasTranslation($langcode2), 'The translation does not appear among available translations after saving the entity.'); @@ -478,7 +478,7 @@ protected function doTestEntityTranslationAPI($entity_type) { function testLanguageFallback() { // Test all entity variations with data table support. foreach (entity_test_entity_types(ENTITY_TEST_TYPES_MULTILINGUAL) as $entity_type) { - $this->doTestMultilingualProperties($entity_type); + $this->doTestLanguageFallback($entity_type); } } @@ -501,9 +501,15 @@ protected function doTestLanguageFallback($entity_type) { $default_langcode = $this->langcodes[0]; $langcode = $this->langcodes[1]; $langcode2 = $this->langcodes[2]; + $langcode_key = $this->entityManager->getDefinition($entity_type)->getKey('langcode'); + $languages = $this->languageManager->getLanguages(); + $language = ConfigurableLanguage::load($languages[$langcode]->getId()); + $language->set('weight', 1); + $language->save(); + $this->languageManager->reset(); $controller = $this->entityManager->getStorage($entity_type); - $entity = $controller->create(array('langcode' => $default_langcode) + $values[$default_langcode]); + $entity = $controller->create(array($langcode_key => $default_langcode) + $values[$default_langcode]); $entity->save(); $entity->addTranslation($langcode, $values[$langcode]); @@ -515,7 +521,6 @@ protected function doTestLanguageFallback($entity_type) { $this->assertEqual($translation->language()->getId(), $default_langcode, 'The current translation language matches the expected one.'); // Check that language fallback respects language weight by default. - $languages = $this->languageManager->getLanguages(); $language = ConfigurableLanguage::load($languages[$langcode]->getId()); $language->set('weight', -1); $language->save(); @@ -530,7 +535,7 @@ protected function doTestLanguageFallback($entity_type) { $this->assertEqual($current_langcode, $translation->language()->getId(), 'The current translation language matches the current language.'); // Check that if the entity has no translation no fallback is applied. - $entity2 = $controller->create(array('langcode' => $default_langcode)); + $entity2 = $controller->create(array($langcode_key => $default_langcode)); // Get an view builder. $controller = $this->entityManager->getViewBuilder($entity_type); $entity2_build = $controller->view($entity2); @@ -604,7 +609,7 @@ function testFieldDefinitions() { public function testLanguageChange() { // Test all entity variations with data table support. foreach (entity_test_entity_types(ENTITY_TEST_TYPES_MULTILINGUAL) as $entity_type) { - $this->doTestMultilingualProperties($entity_type); + $this->doTestLanguageChange($entity_type); } } @@ -622,7 +627,7 @@ protected function doTestLanguageChange($entity_type) { // check that field languages match entity language regardless of field // translatability. $values = array( - 'langcode' => $langcode, + $langcode_key => $langcode, $this->field_name => $this->randomMachineName(), $this->untranslatable_field_name => $this->randomMachineName(), ); diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index ff44c48..6a6c6e2 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -430,6 +430,34 @@ function entity_test_entity_test_mul_translation_delete(EntityInterface $transla } /** + * Implements hook_ENTITY_TYPE_translation_insert(). + */ +function entity_test_entity_test_mulrev_translation_insert(EntityInterface $translation) { + _entity_test_record_hooks('entity_test_mulrev_translation_insert', $translation->language()->getId()); +} + +/** + * Implements hook_ENTITY_TYPE_translation_delete(). + */ +function entity_test_entity_test_mulrev_translation_delete(EntityInterface $translation) { + _entity_test_record_hooks('entity_test_mulrev_translation_delete', $translation->language()->getId()); +} + +/** + * Implements hook_ENTITY_TYPE_translation_insert(). + */ +function entity_test_entity_test_mul_langcode_key_translation_insert(EntityInterface $translation) { + _entity_test_record_hooks('entity_test_mul_langcode_key_translation_insert', $translation->language()->getId()); +} + +/** + * Implements hook_ENTITY_TYPE_translation_delete(). + */ +function entity_test_entity_test_mul_langcode_key_translation_delete(EntityInterface $translation) { + _entity_test_record_hooks('entity_test_mul_langcode_key_translation_delete', $translation->language()->getId()); +} + +/** * Field default value callback. * * @param \Drupal\Core\Entity\FieldableEntityInterface $entity diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulLangcodeKey.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulLangcodeKey.php index 9401d3e..575d125 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulLangcodeKey.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulLangcodeKey.php @@ -10,7 +10,7 @@ use Drupal\Core\Entity\EntityTypeInterface; /** - * Defines the test entity class. + * Defines a test entity class using a custom langcode entity key. * * @ContentEntityType( * id = "entity_test_mul_langcode_key", diff --git a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRev.php b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRev.php index 3d4de93..7fa29a9 100644 --- a/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRev.php +++ b/core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRev.php @@ -18,6 +18,7 @@ * id = "entity_test_mulrev", * label = @Translation("Test entity - revisions and data table"), * handlers = { + * "view_builder" = "Drupal\entity_test\EntityTestViewBuilder", * "access" = "Drupal\entity_test\EntityTestAccessControlHandler", * "form" = { * "default" = "Drupal\entity_test\EntityTestForm", @@ -34,8 +35,9 @@ * entity_keys = { * "id" = "id", * "uuid" = "uuid", - * "revision" = "revision_id", * "bundle" = "type", + * "revision" = "revision_id", + * "label" = "name", * "langcode" = "langcode", * }, * links = {