Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Entity / EntityListBuilderTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Entity\EntityListBuilderTest.
6  */
7
8 namespace Drupal\Tests\Core\Entity;
9
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;
17
18 /**
19  * @coversDefaultClass \Drupal\entity_test\EntityTestListBuilder
20  * @group Entity
21  */
22 class EntityListBuilderTest extends UnitTestCase {
23
24   /**
25    * The entity type used for testing.
26    *
27    * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $entityType;
30
31   /**
32    * The module handler used for testing.
33    *
34    * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
35    */
36   protected $moduleHandler;
37
38   /**
39    * The translation manager used for testing.
40    *
41    * @var \Drupal\Core\StringTranslation\TranslationInterface
42    */
43   protected $translationManager;
44
45   /**
46    * The role storage used for testing.
47    *
48    * @var \Drupal\user\RoleStorageInterface|\PHPUnit_Framework_MockObject_MockObject
49    */
50   protected $roleStorage;
51
52   /**
53    * The service container used for testing.
54    *
55    * @var \Drupal\Core\DependencyInjection\ContainerBuilder
56    */
57   protected $container;
58
59   /**
60    * The entity used to construct the EntityListBuilder.
61    *
62    * @var \Drupal\user\RoleInterface|\PHPUnit_Framework_MockObject_MockObject
63    */
64   protected $role;
65
66   /**
67    * The redirect destination service.
68    *
69    * @var \Drupal\Core\Routing\RedirectDestinationInterface|\PHPUnit_Framework_MockObject_MockObject
70    */
71   protected $redirectDestination;
72
73   /**
74    * The EntityListBuilder object to test.
75    *
76    * @var \Drupal\Core\Entity\EntityListBuilder
77    */
78   protected $entityListBuilder;
79
80   /**
81    * {@inheritdoc}
82    */
83   protected function setUp() {
84     parent::setUp();
85
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);
95   }
96
97   /**
98    * @covers ::getOperations
99    */
100   public function testGetOperations() {
101     $operation_name = $this->randomMachineName();
102     $operations = [
103       $operation_name => [
104         'title' => $this->randomMachineName(),
105       ],
106     ];
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())
112       ->method('alter')
113       ->with('entity_operation');
114
115     $this->container->set('module_handler', $this->moduleHandler);
116
117     $this->role->expects($this->any())
118       ->method('access')
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()
125       ->getMock();
126     $url->expects($this->atLeastOnce())
127       ->method('mergeOptions')
128       ->with(['query' => ['destination' => '/foo/bar']]);
129     $this->role->expects($this->any())
130       ->method('toUrl')
131       ->will($this->returnValue($url));
132
133     $this->redirectDestination->expects($this->atLeastOnce())
134       ->method('getAsArray')
135       ->willReturn(['destination' => '/foo/bar']);
136
137     $list = new EntityListBuilder($this->entityType, $this->roleStorage);
138     $list->setStringTranslation($this->translationManager);
139     $list->setRedirectDestination($this->redirectDestination);
140
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]);
152   }
153
154 }
155
156 class TestEntityListBuilder extends EntityTestListBuilder {
157
158   public function buildOperations(EntityInterface $entity) {
159     return [];
160   }
161
162 }