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\entity_test\EntityTestListBuilder;
15 use Drupal\Tests\UnitTestCase;
18 * @coversDefaultClass \Drupal\entity_test\EntityTestListBuilder
21 class EntityListBuilderTest extends UnitTestCase {
24 * The entity type used for testing.
26 * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
28 protected $entityType;
31 * The module handler used for testing.
33 * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
35 protected $moduleHandler;
38 * The translation manager used for testing.
40 * @var \Drupal\Core\StringTranslation\TranslationInterface
42 protected $translationManager;
45 * The role storage used for testing.
47 * @var \Drupal\user\RoleStorageInterface|\PHPUnit_Framework_MockObject_MockObject
49 protected $roleStorage;
52 * The service container used for testing.
54 * @var \Drupal\Core\DependencyInjection\ContainerBuilder
59 * The entity used to construct the EntityListBuilder.
61 * @var \Drupal\user\RoleInterface|\PHPUnit_Framework_MockObject_MockObject
66 * The EntityListBuilder object to test.
68 * @var \Drupal\Core\Entity\EntityListBuilder
70 protected $entityListBuilder;
75 protected function setUp() {
78 $this->role = $this->getMock('Drupal\user\RoleInterface');
79 $this->roleStorage = $this->getMock('\Drupal\user\RoleStorageInterface');
80 $this->moduleHandler = $this->getMock('\Drupal\Core\Extension\ModuleHandlerInterface');
81 $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
82 $this->translationManager = $this->getMock('\Drupal\Core\StringTranslation\TranslationInterface');
83 $this->entityListBuilder = new TestEntityListBuilder($this->entityType, $this->roleStorage, $this->moduleHandler);
84 $this->container = new ContainerBuilder();
85 \Drupal::setContainer($this->container);
89 * @covers ::getOperations
91 public function testGetOperations() {
92 $operation_name = $this->randomMachineName();
95 'title' => $this->randomMachineName(),
98 $this->moduleHandler->expects($this->once())
100 ->with('entity_operation', [$this->role])
101 ->will($this->returnValue($operations));
102 $this->moduleHandler->expects($this->once())
104 ->with('entity_operation');
106 $this->container->set('module_handler', $this->moduleHandler);
108 $this->role->expects($this->any())
110 ->will($this->returnValue(AccessResult::allowed()));
111 $this->role->expects($this->any())
112 ->method('hasLinkTemplate')
113 ->will($this->returnValue(TRUE));
114 $url = $this->getMockBuilder('\Drupal\Core\Url')
115 ->disableOriginalConstructor()
117 $url->expects($this->any())
119 ->will($this->returnValue([]));
120 $this->role->expects($this->any())
122 ->will($this->returnValue($url));
124 $list = new EntityListBuilder($this->entityType, $this->roleStorage, $this->moduleHandler);
125 $list->setStringTranslation($this->translationManager);
127 $operations = $list->getOperations($this->role);
128 $this->assertInternalType('array', $operations);
129 $this->assertArrayHasKey('edit', $operations);
130 $this->assertInternalType('array', $operations['edit']);
131 $this->assertArrayHasKey('title', $operations['edit']);
132 $this->assertArrayHasKey('delete', $operations);
133 $this->assertInternalType('array', $operations['delete']);
134 $this->assertArrayHasKey('title', $operations['delete']);
135 $this->assertArrayHasKey($operation_name, $operations);
136 $this->assertInternalType('array', $operations[$operation_name]);
137 $this->assertArrayHasKey('title', $operations[$operation_name]);
142 class TestEntityListBuilder extends EntityTestListBuilder {
143 public function buildOperations(EntityInterface $entity) {