Security update for Core, with self-updated composer
[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\entity_test\EntityTestListBuilder;
15 use Drupal\Tests\UnitTestCase;
16
17 /**
18  * @coversDefaultClass \Drupal\entity_test\EntityTestListBuilder
19  * @group Entity
20  */
21 class EntityListBuilderTest extends UnitTestCase {
22
23   /**
24    * The entity type used for testing.
25    *
26    * @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
27    */
28   protected $entityType;
29
30   /**
31    * The module handler used for testing.
32    *
33    * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
34    */
35   protected $moduleHandler;
36
37   /**
38    * The translation manager used for testing.
39    *
40    * @var \Drupal\Core\StringTranslation\TranslationInterface
41    */
42   protected $translationManager;
43
44   /**
45    * The role storage used for testing.
46    *
47    * @var \Drupal\user\RoleStorageInterface|\PHPUnit_Framework_MockObject_MockObject
48    */
49   protected $roleStorage;
50
51   /**
52    * The service container used for testing.
53    *
54    * @var \Drupal\Core\DependencyInjection\ContainerBuilder
55    */
56   protected $container;
57
58   /**
59    * The entity used to construct the EntityListBuilder.
60    *
61    * @var \Drupal\user\RoleInterface|\PHPUnit_Framework_MockObject_MockObject
62    */
63   protected $role;
64
65   /**
66    * The EntityListBuilder object to test.
67    *
68    * @var \Drupal\Core\Entity\EntityListBuilder
69    */
70   protected $entityListBuilder;
71
72   /**
73    * {@inheritdoc}
74    */
75   protected function setUp() {
76     parent::setUp();
77
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);
86   }
87
88   /**
89    * @covers ::getOperations
90    */
91   public function testGetOperations() {
92     $operation_name = $this->randomMachineName();
93     $operations = [
94       $operation_name => [
95         'title' => $this->randomMachineName(),
96       ],
97     ];
98     $this->moduleHandler->expects($this->once())
99       ->method('invokeAll')
100       ->with('entity_operation', [$this->role])
101       ->will($this->returnValue($operations));
102     $this->moduleHandler->expects($this->once())
103       ->method('alter')
104       ->with('entity_operation');
105
106     $this->container->set('module_handler', $this->moduleHandler);
107
108     $this->role->expects($this->any())
109       ->method('access')
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()
116       ->getMock();
117     $url->expects($this->any())
118       ->method('toArray')
119       ->will($this->returnValue([]));
120     $this->role->expects($this->any())
121       ->method('urlInfo')
122       ->will($this->returnValue($url));
123
124     $list = new EntityListBuilder($this->entityType, $this->roleStorage, $this->moduleHandler);
125     $list->setStringTranslation($this->translationManager);
126
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]);
138   }
139
140 }
141
142 class TestEntityListBuilder extends EntityTestListBuilder {
143   public function buildOperations(EntityInterface $entity) {
144     return [];
145   }
146
147 }