3 namespace Drupal\Tests\views\Unit\Plugin\views\field;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\views\Plugin\views\field\EntityOperations;
7 use Drupal\views\ResultRow;
10 * @coversDefaultClass \Drupal\views\Plugin\views\field\EntityOperations
13 class EntityOperationsUnitTest extends UnitTestCase {
18 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
20 protected $entityManager;
23 * The language manager.
25 * @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27 protected $languageManager;
30 * The plugin under test.
32 * @var \Drupal\views\Plugin\views\field\EntityOperations
39 * @covers ::__construct
41 protected function setUp() {
42 $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
43 $this->languageManager = $this->getMock('\Drupal\Core\Language\LanguageManagerInterface');
46 $plugin_id = $this->randomMachineName();
47 $plugin_definition = [
48 'title' => $this->randomMachineName(),
50 $this->plugin = new EntityOperations($configuration, $plugin_id, $plugin_definition, $this->entityManager, $this->languageManager);
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);
58 $view = $this->getMockBuilder('\Drupal\views\ViewExecutable')
59 ->disableOriginalConstructor()
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);
69 * @covers ::usesGroupBy
71 public function testUsesGroupBy() {
72 $this->assertFalse($this->plugin->usesGroupBy());
76 * @covers ::defineOptions
78 public function testDefineOptions() {
79 $options = $this->plugin->defineOptions();
80 $this->assertInternalType('array', $options);
81 $this->assertArrayHasKey('destination', $options);
87 public function testRenderWithDestination() {
88 $entity_type_id = $this->randomMachineName();
89 $entity = $this->getMockBuilder('\Drupal\user\Entity\Role')
90 ->disableOriginalConstructor()
92 $entity->expects($this->any())
93 ->method('getEntityTypeId')
94 ->will($this->returnValue($entity_type_id));
98 'title' => $this->randomMachineName(),
101 $list_builder = $this->getMock('\Drupal\Core\Entity\EntityListBuilderInterface');
102 $list_builder->expects($this->once())
103 ->method('getOperations')
105 ->will($this->returnValue($operations));
107 $this->entityManager->expects($this->once())
108 ->method('getListBuilder')
109 ->with($entity_type_id)
110 ->will($this->returnValue($list_builder));
112 $this->plugin->options['destination'] = TRUE;
114 $result = new ResultRow();
115 $result->_entity = $entity;
118 '#type' => 'operations',
119 '#links' => $operations
121 $expected_build['#links']['foo']['query'] = ['destination' => 'foobar'];
122 $build = $this->plugin->render($result);
123 $this->assertSame($expected_build, $build);
129 public function testRenderWithoutDestination() {
130 $entity_type_id = $this->randomMachineName();
131 $entity = $this->getMockBuilder('\Drupal\user\Entity\Role')
132 ->disableOriginalConstructor()
134 $entity->expects($this->any())
135 ->method('getEntityTypeId')
136 ->will($this->returnValue($entity_type_id));
140 'title' => $this->randomMachineName(),
143 $list_builder = $this->getMock('\Drupal\Core\Entity\EntityListBuilderInterface');
144 $list_builder->expects($this->once())
145 ->method('getOperations')
147 ->will($this->returnValue($operations));
149 $this->entityManager->expects($this->once())
150 ->method('getListBuilder')
151 ->with($entity_type_id)
152 ->will($this->returnValue($list_builder));
154 $this->plugin->options['destination'] = FALSE;
156 $result = new ResultRow();
157 $result->_entity = $entity;
160 '#type' => 'operations',
161 '#links' => $operations
163 $build = $this->plugin->render($result);
164 $this->assertSame($expected_build, $build);