Verified Commit d0b94dfc authored by Dave Long's avatar Dave Long
Browse files

fix: #3319212 Entities without labels cause TypeError in EntityController title callbacks

By: joachim
By: andrew answer
By: dcam
(cherry picked from commit 20de7941)
parent 8c53c80e
Loading
Loading
Loading
Loading
Loading
+16 −3
Original line number Diff line number Diff line
@@ -239,9 +239,15 @@ public function title(RouteMatchInterface $route_match, ?EntityInterface $_entit
   *   The title for the entity edit page, if an entity was found.
   */
  public function editTitle(RouteMatchInterface $route_match, ?EntityInterface $_entity = NULL) {
    if ($entity = $this->doGetEntity($route_match, $_entity)) {
      return $this->t('Edit %label', ['%label' => $entity->label()]);
    $entity = $this->doGetEntity($route_match, $_entity);
    if ($entity === NULL) {
      return NULL;
    }
    $label = $entity->label();
    if ($label === NULL) {
      return $this->t('Edit');
    }
    return $this->t('Edit %label', ['%label' => $label]);
  }

  /**
@@ -258,7 +264,14 @@ public function editTitle(RouteMatchInterface $route_match, ?EntityInterface $_e
   */
  public function deleteTitle(RouteMatchInterface $route_match, ?EntityInterface $_entity = NULL) {
    $entity = $this->doGetEntity($route_match, $_entity);
    return $entity ? $this->t('Delete %label', ['%label' => $entity->label()]) : '';
    if ($entity === NULL) {
      return '';
    }
    $label = $entity->label();
    if ($label === NULL) {
      return $this->t('Delete');
    }
    return $this->t('Delete %label', ['%label' => $label]);
  }

  /**
+57 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\Core\Entity\Controller;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\Controller\EntityController;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\TestWith;

/**
 * Tests EntityController.
 */
#[CoversClass(EntityController::class)]
#[Group('Entity')]
class EntityControllerTest extends UnitTestCase {

  /**
   * Tests edit and delete title callbacks.
   *
   * @param string $callback
   *   The controller title callback name.
   * @param string|null $entity_label
   *   The label of the entity being displayed by the controller.
   * @param string $expected_title
   *   The expected title return value.
   */
  #[TestWith(['deleteTitle', 'example', 'Delete <em class="placeholder">example</em>'])]
  #[TestWith(['deleteTitle', NULL, 'Delete'])]
  #[TestWith(['editTitle', 'example', 'Edit <em class="placeholder">example</em>'])]
  #[TestWith(['editTitle', NULL, 'Edit'])]
  public function testDeleteEditTitleCallbacks(
    string $callback,
    ?string $entity_label,
    string $expected_title,
  ): void {
    $container = new ContainerBuilder();
    $container->set('string_translation', $this->getStringTranslationStub());
    \Drupal::setContainer($container);

    $route_match = $this->createStub(RouteMatchInterface::class);
    $entity = $this->createStub(EntityInterface::class);
    $entity->method('label')
      ->willReturn($entity_label);
    $controller = $this->createPartialMock(EntityController::class, ['doGetEntity']);
    $controller->expects($this->once())
      ->method('doGetEntity')
      ->willReturn($entity);
    $this->assertEquals($expected_title, (string) $controller->$callback($route_match, $entity));
  }

}