EntityListBuilderTest.php

Same filename in this branch
  1. main core/modules/system/tests/src/Functional/Entity/EntityListBuilderTest.php
Same filename and directory in other branches
  1. 11.x core/modules/system/tests/src/Functional/Entity/EntityListBuilderTest.php
  2. 11.x core/tests/Drupal/Tests/Core/Entity/EntityListBuilderTest.php
  3. 10 core/modules/system/tests/src/Functional/Entity/EntityListBuilderTest.php
  4. 10 core/tests/Drupal/Tests/Core/Entity/EntityListBuilderTest.php
  5. 9 core/modules/system/tests/src/Functional/Entity/EntityListBuilderTest.php
  6. 9 core/tests/Drupal/Tests/Core/Entity/EntityListBuilderTest.php
  7. 8.9.x core/modules/system/tests/src/Functional/Entity/EntityListBuilderTest.php
  8. 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityListBuilderTest.php

Namespace

Drupal\Tests\Core\Entity

File

core/tests/Drupal/Tests/Core/Entity/EntityListBuilderTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\Entity;

use Drupal\Component\Utility\SortArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Url;
use Drupal\entity_test\EntityTestListBuilder;
use Drupal\Tests\UnitTestCase;
use Drupal\user\RoleInterface;
use Drupal\user\RoleStorageInterface;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;

/**
 * Tests Drupal\Core\Entity\EntityListBuilder.
 */
class EntityListBuilderTest extends UnitTestCase {
  
  /**
   * The entity type used for testing.
   */
  protected EntityTypeInterface&Stub $entityType;
  
  /**
   * The module handler used for testing.
   */
  protected ModuleHandlerInterface&MockObject $moduleHandler;
  
  /**
   * The translation manager used for testing.
   */
  protected TranslationInterface&Stub $translationManager;
  
  /**
   * The role storage used for testing.
   */
  protected RoleStorageInterface&Stub $roleStorage;
  
  /**
   * The service container used for testing.
   *
   * @var \Drupal\Core\DependencyInjection\ContainerBuilder
   */
  protected $container;
  
  /**
   * The entity used to construct the EntityListBuilder.
   */
  protected RoleInterface&Stub $role;
  
  /**
   * The redirect destination service.
   */
  protected RedirectDestinationInterface&MockObject $redirectDestination;
  
  /**
   * The EntityListBuilder object to test.
   *
   * @var \Drupal\Core\Entity\EntityListBuilder
   */
  protected $entityListBuilder;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->role = $this->createStub(RoleInterface::class);
    $this->roleStorage = $this->createStub(RoleStorageInterface::class);
    $this->moduleHandler = $this->createMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $this->entityType = $this->createStub(EntityTypeInterface::class);
    $this->translationManager = $this->createStub(TranslationInterface::class);
    $this->entityListBuilder = new TestEntityListBuilder($this->entityType, $this->roleStorage);
    $this->redirectDestination = $this->createMock(RedirectDestinationInterface::class);
    $this->container = new ContainerBuilder();
    \Drupal::setContainer($this->container);
  }
  
  /**
   * Tests get operations.
   */
  public function testGetOperations() : void {
    $operation_name = $this->randomMachineName();
    $operations = [
      $operation_name => [
        'title' => $this->randomMachineName(),
      ],
    ];
    $this->moduleHandler
      ->expects($this->once())
      ->method('invokeAll')
      ->with('entity_operation', [
      $this->role,
      new CacheableMetadata(),
    ])
      ->willReturn($operations);
    $this->moduleHandler
      ->expects($this->once())
      ->method('alter')
      ->with('entity_operation');
    $this->container
      ->set('module_handler', $this->moduleHandler);
    $this->role
      ->method('access')
      ->willReturn(AccessResult::allowed());
    $this->role
      ->method('hasLinkTemplate')
      ->willReturn(TRUE);
    $url = Url::fromRoute('entity.user_role.collection');
    $this->role
      ->method('toUrl')
      ->willReturn($url);
    $this->redirectDestination
      ->expects($this->atLeastOnce())
      ->method('getAsArray')
      ->willReturn([
      'destination' => '/foo/bar',
    ]);
    $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('view', $operations);
    $this->assertIsArray($operations['view']);
    $this->assertArrayHasKey('edit', $operations);
    $this->assertIsArray($operations['edit']);
    $this->assertArrayHasKey('title', $operations['edit']);
    $this->assertArrayHasKey('delete', $operations);
    $this->assertIsArray($operations['delete']);
    $this->assertArrayHasKey('title', $operations['delete']);
    $this->assertArrayHasKey($operation_name, $operations);
    $this->assertIsArray($operations[$operation_name]);
    $this->assertArrayHasKey('title', $operations[$operation_name]);
    // Ensure the operations are in the correct relative order.
    uasort($operations, SortArray::sortByWeightElement(...));
    $this->assertSame([
      $operation_name,
      'edit',
      'delete',
      'view',
    ], array_keys($operations));
  }
  
  /**
   * Ensures entity operations handle entities without labels.
   */
  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
      ->method('access')
      ->willReturn(AccessResult::allowed());
    $this->role
      ->method('hasLinkTemplate')
      ->willReturn(TRUE);
    $this->role
      ->method('toUrl')
      ->willReturnCallback(static fn(): Url => Url::fromRoute('entity.user_role.collection'));
    $this->role
      ->method('label')
      ->willReturn(NULL);
    $this->role
      ->method('bundle')
      ->willReturn('role');
    $this->role
      ->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);
  }

}

/**
 * Stub class for testing EntityListBuilder.
 */
class TestEntityListBuilder extends EntityTestListBuilder {
  public function buildOperations(EntityInterface $entity) : array {
    return [];
  }

}

Classes

Title Deprecated Summary
EntityListBuilderTest Tests Drupal\Core\Entity\EntityListBuilder.
TestEntityListBuilder Stub class for testing EntityListBuilder.

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