Version 1
[yaffs-website] / web / core / modules / views / tests / src / Unit / Plugin / views / field / EntityOperationsUnitTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Unit\Plugin\views\field;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\views\Plugin\views\field\EntityOperations;
7 use Drupal\views\ResultRow;
8
9 /**
10  * @coversDefaultClass \Drupal\views\Plugin\views\field\EntityOperations
11  * @group Views
12  */
13 class EntityOperationsUnitTest extends UnitTestCase {
14
15   /**
16    * The entity manager.
17    *
18    * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
19    */
20   protected $entityManager;
21
22   /**
23    * The language manager.
24    *
25    * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $languageManager;
28
29   /**
30    * The plugin under test.
31    *
32    * @var \Drupal\views\Plugin\views\field\EntityOperations
33    */
34   protected $plugin;
35
36   /**
37    * {@inheritdoc}
38    *
39    * @covers ::__construct
40    */
41   protected function setUp() {
42     $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
43     $this->languageManager = $this->getMock('\Drupal\Core\Language\LanguageManagerInterface');
44
45     $configuration = [];
46     $plugin_id = $this->randomMachineName();
47     $plugin_definition = [
48       'title' => $this->randomMachineName(),
49     ];
50     $this->plugin = new EntityOperations($configuration, $plugin_id, $plugin_definition, $this->entityManager, $this->languageManager);
51
52     $redirect_service = $this->getMock('Drupal\Core\Routing\RedirectDestinationInterface');
53     $redirect_service->expects($this->any())
54       ->method('getAsArray')
55       ->willReturn(['destination' => 'foobar']);
56     $this->plugin->setRedirectDestination($redirect_service);
57
58     $view = $this->getMockBuilder('\Drupal\views\ViewExecutable')
59       ->disableOriginalConstructor()
60       ->getMock();
61     $display = $this->getMockBuilder('\Drupal\views\Plugin\views\display\DisplayPluginBase')
62       ->disableOriginalConstructor()
63       ->getMockForAbstractClass();
64     $view->display_handler = $display;
65     $this->plugin->init($view, $display);
66   }
67
68   /**
69    * @covers ::usesGroupBy
70    */
71   public function testUsesGroupBy() {
72     $this->assertFalse($this->plugin->usesGroupBy());
73   }
74
75   /**
76    * @covers ::defineOptions
77    */
78   public function testDefineOptions() {
79     $options = $this->plugin->defineOptions();
80     $this->assertInternalType('array', $options);
81     $this->assertArrayHasKey('destination', $options);
82   }
83
84   /**
85    * @covers ::render
86    */
87   public function testRenderWithDestination() {
88     $entity_type_id = $this->randomMachineName();
89     $entity = $this->getMockBuilder('\Drupal\user\Entity\Role')
90       ->disableOriginalConstructor()
91       ->getMock();
92     $entity->expects($this->any())
93       ->method('getEntityTypeId')
94       ->will($this->returnValue($entity_type_id));
95
96     $operations = [
97       'foo' => [
98         'title' => $this->randomMachineName(),
99       ],
100     ];
101     $list_builder = $this->getMock('\Drupal\Core\Entity\EntityListBuilderInterface');
102     $list_builder->expects($this->once())
103       ->method('getOperations')
104       ->with($entity)
105       ->will($this->returnValue($operations));
106
107     $this->entityManager->expects($this->once())
108       ->method('getListBuilder')
109       ->with($entity_type_id)
110       ->will($this->returnValue($list_builder));
111
112     $this->plugin->options['destination'] = TRUE;
113
114     $result = new ResultRow();
115     $result->_entity = $entity;
116
117     $expected_build = [
118       '#type' => 'operations',
119       '#links' => $operations
120     ];
121     $expected_build['#links']['foo']['query'] = ['destination' => 'foobar'];
122     $build = $this->plugin->render($result);
123     $this->assertSame($expected_build, $build);
124   }
125
126   /**
127    * @covers ::render
128    */
129   public function testRenderWithoutDestination() {
130     $entity_type_id = $this->randomMachineName();
131     $entity = $this->getMockBuilder('\Drupal\user\Entity\Role')
132       ->disableOriginalConstructor()
133       ->getMock();
134     $entity->expects($this->any())
135       ->method('getEntityTypeId')
136       ->will($this->returnValue($entity_type_id));
137
138     $operations = [
139       'foo' => [
140         'title' => $this->randomMachineName(),
141       ],
142     ];
143     $list_builder = $this->getMock('\Drupal\Core\Entity\EntityListBuilderInterface');
144     $list_builder->expects($this->once())
145       ->method('getOperations')
146       ->with($entity)
147       ->will($this->returnValue($operations));
148
149     $this->entityManager->expects($this->once())
150       ->method('getListBuilder')
151       ->with($entity_type_id)
152       ->will($this->returnValue($list_builder));
153
154     $this->plugin->options['destination'] = FALSE;
155
156     $result = new ResultRow();
157     $result->_entity = $entity;
158
159     $expected_build = [
160       '#type' => 'operations',
161       '#links' => $operations
162     ];
163     $build = $this->plugin->render($result);
164     $this->assertSame($expected_build, $build);
165   }
166
167 }