5 * Contains \Drupal\Tests\Core\Entity\EntityListBuilderTest.
8 namespace Drupal\Tests\Core\Entity;
10 use Drupal\Core\Access\AccessResult;
11 use Drupal\Core\DependencyInjection\ContainerBuilder;
12 use Drupal\Core\Entity\EntityInterface;
13 use Drupal\Core\Entity\EntityListBuilder;
14 use Drupal\Core\Routing\RedirectDestinationInterface;
15 use Drupal\entity_test\EntityTestListBuilder;
16 use Drupal\Tests\UnitTestCase;
19 * @coversDefaultClass \Drupal\entity_test\EntityTestListBuilder
22 class EntityListBuilderTest extends UnitTestCase {
25 * The entity type used for testing.
27 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
29 protected $entityType;
32 * The module handler used for testing.
34 * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
36 protected $moduleHandler;
39 * The translation manager used for testing.
41 * @var \Drupal\Core\StringTranslation\TranslationInterface
43 protected $translationManager;
46 * The role storage used for testing.
48 * @var \Drupal\user\RoleStorageInterface|\PHPUnit_Framework_MockObject_MockObject
50 protected $roleStorage;
53 * The service container used for testing.
55 * @var \Drupal\Core\DependencyInjection\ContainerBuilder
60 * The entity used to construct the EntityListBuilder.
62 * @var \Drupal\user\RoleInterface|\PHPUnit_Framework_MockObject_MockObject
67 * The redirect destination service.
69 * @var \Drupal\Core\Routing\RedirectDestinationInterface|\PHPUnit_Framework_MockObject_MockObject
71 protected $redirectDestination;
74 * The EntityListBuilder object to test.
76 * @var \Drupal\Core\Entity\EntityListBuilder
78 protected $entityListBuilder;
83 protected function setUp() {
86 $this->role = $this->getMock('Drupal\user\RoleInterface');
87 $this->roleStorage = $this->getMock('\Drupal\user\RoleStorageInterface');
88 $this->moduleHandler = $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface');
89 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
90 $this->translationManager = $this->getMock('\Drupal\Core\StringTranslation\TranslationInterface');
91 $this->entityListBuilder = new TestEntityListBuilder($this->entityType, $this->roleStorage);
92 $this->redirectDestination = $this->getMock(RedirectDestinationInterface::class);
93 $this->container = new ContainerBuilder();
94 \Drupal::setContainer($this->container);
98 * @covers ::getOperations
100 public function testGetOperations() {
101 $operation_name = $this->randomMachineName();
104 'title' => $this->randomMachineName(),
107 $this->moduleHandler->expects($this->once())
108 ->method('invokeAll')
109 ->with('entity_operation', [$this->role])
110 ->will($this->returnValue($operations));
111 $this->moduleHandler->expects($this->once())
113 ->with('entity_operation');
115 $this->container->set('module_handler', $this->moduleHandler);
117 $this->role->expects($this->any())
119 ->will($this->returnValue(AccessResult::allowed()));
120 $this->role->expects($this->any())
121 ->method('hasLinkTemplate')
122 ->will($this->returnValue(TRUE));
123 $url = $this->getMockBuilder('\Drupal\Core\Url')
124 ->disableOriginalConstructor()
126 $url->expects($this->atLeastOnce())
127 ->method('mergeOptions')
128 ->with(['query' => ['destination' => '/foo/bar']]);
129 $this->role->expects($this->any())
131 ->will($this->returnValue($url));
133 $this->redirectDestination->expects($this->atLeastOnce())
134 ->method('getAsArray')
135 ->willReturn(['destination' => '/foo/bar']);
137 $list = new EntityListBuilder($this->entityType, $this->roleStorage);
138 $list->setStringTranslation($this->translationManager);
139 $list->setRedirectDestination($this->redirectDestination);
141 $operations = $list->getOperations($this->role);
142 $this->assertInternalType('array', $operations);
143 $this->assertArrayHasKey('edit', $operations);
144 $this->assertInternalType('array', $operations['edit']);
145 $this->assertArrayHasKey('title', $operations['edit']);
146 $this->assertArrayHasKey('delete', $operations);
147 $this->assertInternalType('array', $operations['delete']);
148 $this->assertArrayHasKey('title', $operations['delete']);
149 $this->assertArrayHasKey($operation_name, $operations);
150 $this->assertInternalType('array', $operations[$operation_name]);
151 $this->assertArrayHasKey('title', $operations[$operation_name]);
156 class TestEntityListBuilder extends EntityTestListBuilder {
158 public function buildOperations(EntityInterface $entity) {