function EntityListBuilderTest::testGetOperationsWithNullLabel

Ensures entity operations handle entities without labels.

File

core/tests/Drupal/Tests/Core/Entity/EntityListBuilderTest.php, line 164

Class

EntityListBuilderTest
Tests Drupal\Core\Entity\EntityListBuilder.

Namespace

Drupal\Tests\Core\Entity

Code

public function testGetOperationsWithNullLabel() : void {
  $this->moduleHandler
    ->expects($this->once())
    ->method('invokeAll')
    ->with('entity_operation', [
    $this->role,
    new CacheableMetadata(),
  ])
    ->willReturn([]);
  $this->moduleHandler
    ->expects($this->once())
    ->method('alter')
    ->with('entity_operation');
  $this->container
    ->set('module_handler', $this->moduleHandler);
  $this->role
    ->expects($this->any())
    ->method('access')
    ->willReturn(AccessResult::allowed());
  $this->role
    ->expects($this->any())
    ->method('hasLinkTemplate')
    ->willReturn(TRUE);
  $this->role
    ->expects($this->any())
    ->method('toUrl')
    ->willReturnCallback(static fn(): Url => Url::fromRoute('entity.user_role.collection'));
  $this->role
    ->expects($this->any())
    ->method('label')
    ->willReturn(NULL);
  $this->role
    ->expects($this->any())
    ->method('bundle')
    ->willReturn('role');
  $this->role
    ->expects($this->any())
    ->method('id')
    ->willReturn('role_id');
  $this->redirectDestination
    ->expects($this->atLeastOnce())
    ->method('getAsArray')
    ->willReturn([
    'destination' => '/foo/bar',
  ]);
  $this->translationManager
    ->method('translateString')
    ->willReturnCallback(static function (TranslatableMarkup $string) : string {
    return $string->getUntranslatedString();
  });
  $list = new EntityListBuilder($this->entityType, $this->roleStorage);
  $list->setStringTranslation($this->translationManager);
  $list->setRedirectDestination($this->redirectDestination);
  $operations = $list->getOperations($this->role);
  $this->assertIsArray($operations);
  $this->assertArrayHasKey('edit', $operations);
  $edit_label = $operations['edit']['url']->getOption('attributes')['aria-label'];
  $this->assertInstanceOf(TranslatableMarkup::class, $edit_label);
  $this->assertSame('', $edit_label->getArguments()['@entity_label']);
  $this->assertSame('Edit role role_id', (string) $edit_label);
  $this->assertArrayHasKey('delete', $operations);
  $delete_label = $operations['delete']['url']->getOption('attributes')['aria-label'];
  $this->assertInstanceOf(TranslatableMarkup::class, $delete_label);
  $this->assertSame('', $delete_label->getArguments()['@entity_label']);
  $this->assertSame('Delete role role_id', (string) $delete_label);
  $this->assertArrayHasKey('view', $operations);
  $view_label = $operations['view']['url']->getOption('attributes')['aria-label'];
  $this->assertInstanceOf(TranslatableMarkup::class, $view_label);
  $this->assertSame('', $view_label->getArguments()['@entity_label']);
  $this->assertSame('View role role_id', (string) $view_label);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.