EntityListBuilderTest.php in Drupal 9
File
core/tests/Drupal/Tests/Core/Entity/EntityListBuilderTest.php
View source
<?php
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\entity_test\EntityTestListBuilder;
use Drupal\Tests\UnitTestCase;
class EntityListBuilderTest extends UnitTestCase {
protected $entityType;
protected $moduleHandler;
protected $translationManager;
protected $roleStorage;
protected $container;
protected $role;
protected $redirectDestination;
protected $entityListBuilder;
protected function setUp() : void {
parent::setUp();
$this->role = $this
->createMock('Drupal\\user\\RoleInterface');
$this->roleStorage = $this
->createMock('\\Drupal\\user\\RoleStorageInterface');
$this->moduleHandler = $this
->createMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
$this->entityType = $this
->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
$this->translationManager = $this
->createMock('\\Drupal\\Core\\StringTranslation\\TranslationInterface');
$this->entityListBuilder = new TestEntityListBuilder($this->entityType, $this->roleStorage);
$this->redirectDestination = $this
->createMock(RedirectDestinationInterface::class);
$this->container = new ContainerBuilder();
\Drupal::setContainer($this->container);
}
public function testGetOperations() {
$operation_name = $this
->randomMachineName();
$operations = [
$operation_name => [
'title' => $this
->randomMachineName(),
],
];
$this->moduleHandler
->expects($this
->once())
->method('invokeAll')
->with('entity_operation', [
$this->role,
])
->will($this
->returnValue($operations));
$this->moduleHandler
->expects($this
->once())
->method('alter')
->with('entity_operation');
$this->container
->set('module_handler', $this->moduleHandler);
$this->role
->expects($this
->any())
->method('access')
->will($this
->returnValue(AccessResult::allowed()));
$this->role
->expects($this
->any())
->method('hasLinkTemplate')
->will($this
->returnValue(TRUE));
$url = $this
->getMockBuilder('\\Drupal\\Core\\Url')
->disableOriginalConstructor()
->getMock();
$url
->expects($this
->atLeastOnce())
->method('mergeOptions')
->with([
'query' => [
'destination' => '/foo/bar',
],
]);
$this->role
->expects($this
->any())
->method('toUrl')
->will($this
->returnValue($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('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]);
}
}
class TestEntityListBuilder extends EntityTestListBuilder {
public function buildOperations(EntityInterface $entity) {
return [];
}
}