From 3e59aced0c232f526f16f88f5c6f3d2c380ee97c Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Mon, 28 Sep 2015 00:54:01 +0200 Subject: [PATCH] Issue #2575493 by Berdir, dawehner: Remove string cast of EntityType::getLabel() and getGroupLabel() --- core/lib/Drupal/Core/Entity/EntityType.php | 4 +- .../src/ConfigurableEntityReferenceItem.php | 2 +- .../Tests/Core/Entity/EntityTypeTest.php | 40 +++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/EntityType.php b/core/lib/Drupal/Core/Entity/EntityType.php index 7f49c1876da6..7072ef5701d6 100644 --- a/core/lib/Drupal/Core/Entity/EntityType.php +++ b/core/lib/Drupal/Core/Entity/EntityType.php @@ -696,7 +696,7 @@ public function getDataTable() { * {@inheritdoc} */ public function getLabel() { - return (string) $this->label; + return $this->label; } /** @@ -733,7 +733,7 @@ public function getGroup() { * {@inheritdoc} */ public function getGroupLabel() { - return !empty($this->group_label) ? (string) $this->group_label : $this->t('Other', array(), array('context' => 'Entity type group')); + return !empty($this->group_label) ? $this->group_label : $this->t('Other', array(), array('context' => 'Entity type group')); } /** diff --git a/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php b/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php index 3ce16ab56783..376fef859100 100644 --- a/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php +++ b/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php @@ -83,7 +83,7 @@ public function getSettableOptions(AccountInterface $account = NULL) { // The label does not need sanitizing since it is used as an optgroup // which is only supported by select elements and auto-escaped. $bundle_label = $bundles[$bundle]['label']; - $return[$bundle_label] = $entity_ids; + $return[(string) $bundle_label] = $entity_ids; } return count($return) == 1 ? reset($return) : $return; diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php index 3b47dbe56789..84076c5c2ee4 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityTypeTest.php @@ -9,6 +9,8 @@ use Drupal\Core\Entity\EntityType; use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\StringTranslation\TranslatableString; +use Drupal\Core\StringTranslation\TranslationInterface; use Drupal\Tests\UnitTestCase; /** @@ -283,6 +285,44 @@ public function testId() { $this->assertEquals($id, $entity_type->id()); } + /** + * @covers ::getLabel + */ + public function testGetLabel() { + $translatable_label = new TranslatableString($this->randomMachineName()); + $entity_type = $this->setUpEntityType(array('label' => $translatable_label)); + $this->assertSame($translatable_label, $entity_type->getLabel()); + + $label = $this->randomMachineName(); + $entity_type = $this->setUpEntityType(array('label' => $label)); + $this->assertSame($label, $entity_type->getLabel()); + } + + /** + * @covers ::getGroupLabel + */ + public function testGetGroupLabel() { + $translatable_group_label = new TranslatableString($this->randomMachineName()); + $entity_type = $this->setUpEntityType(array('group_label' => $translatable_group_label)); + $this->assertSame($translatable_group_label, $entity_type->getGroupLabel()); + + $default_label = $this->randomMachineName(); + $entity_type = $this->setUpEntityType(array('group_label' => $default_label)); + $this->assertSame($default_label, $entity_type->getGroupLabel()); + + $default_label = new TranslatableString('Other', array(), array('context' => 'Entity type group')); + $entity_type = $this->setUpEntityType([]); + + $string_translation = $this->getMock(TranslationInterface::class); + $string_translation->expects($this->atLeastOnce()) + ->method('translate') + ->with('Other', array(), array('context' => 'Entity type group')) + ->willReturn($default_label); + $entity_type->setStringTranslation($string_translation); + + $this->assertSame($default_label, $entity_type->getGroupLabel()); + } + /** * Gets a mock controller class name. * -- GitLab