3 namespace Drupal\Tests\views\Unit;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\views\Plugin\ViewsHandlerManager;
9 * Tests the ViewsHandlerManager class.
13 * @coversDefaultClass \Drupal\views\Plugin\ViewsHandlerManager
15 class ViewsHandlerManagerTest extends UnitTestCase {
18 * @var \Drupal\views\Plugin\ViewsHandlerManager
20 protected $handlerManager;
23 * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
25 protected $moduleHandler;
28 * The mocked views data.
30 * @var \Drupal\views\ViewsData|\PHPUnit_Framework_MockObject_MockObject
37 * @var \Drupal\Component\Plugin\Factory\FactoryInterface|\PHPUnit_Framework_MockObject_MockObject
44 protected function setUp() {
46 $this->viewsData = $this->getMockBuilder('Drupal\views\ViewsData')
47 ->disableOriginalConstructor()
49 $cache_backend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
50 $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
51 $this->handlerManager = new ViewsHandlerManager('test', new \ArrayObject([]), $this->viewsData, $cache_backend, $this->moduleHandler);
55 * Setups of the plugin factory.
57 protected function setupMockedFactory() {
58 $this->factory = $this->getMock('Drupal\Component\Plugin\Factory\FactoryInterface');
60 $reflection = new \ReflectionClass($this->handlerManager);
61 $property = $reflection->getProperty('factory');
62 $property->setAccessible(TRUE);
63 $property->setValue($this->handlerManager, $this->factory);
67 * Tests that hook_views_plugins_TYPE_alter() is invoked for a handler type.
69 * @covers ::__construct
70 * @covers ::getDefinitions
72 public function testAlterHookInvocation() {
73 $this->moduleHandler->expects($this->once())
75 ->with('views_plugins_test', []);
77 $this->handlerManager->getDefinitions();
81 * Tests getHandler() and its base information propagation.
83 public function testGetHandlerBaseInformationPropagation() {
84 $this->setupMockedFactory();
87 $item['table'] = 'test_table';
88 $item['field'] = 'test_field';
91 $views_data['test_field']['test']['id'] = 'test_id';
92 $views_data['test_field']['test']['more_information'] = 'test_id';
93 $views_data['test_field']['group'] = 'test_group';
94 $views_data['test_field']['title'] = 'test title';
95 $views_data['test_field']['real field'] = 'test real field';
96 $views_data['test_field']['real table'] = 'test real table';
97 $views_data['test_field']['entity field'] = 'test entity field';
99 $this->viewsData->expects($this->once())
102 ->willReturn($views_data);
104 $expected_definition = [
106 'more_information' => 'test_id',
107 'group' => 'test_group',
108 'title' => 'test title',
109 'real field' => 'test real field',
110 'real table' => 'test real table',
111 'entity field' => 'test entity field',
113 $plugin = $this->getMock('Drupal\views\Plugin\views\ViewsHandlerInterface');
114 $this->factory->expects($this->once())
115 ->method('createInstance')
116 ->with('test_id', $expected_definition)
117 ->willReturn($plugin);
119 $result = $this->handlerManager->getHandler($item);
120 $this->assertSame($plugin, $result);
124 * Tests getHandler() with an override.
126 public function testGetHandlerOverride() {
127 $this->setupMockedFactory();
130 $item['table'] = 'test_table';
131 $item['field'] = 'test_field';
134 $views_data['test_field']['test']['id'] = 'test_id';
136 $this->viewsData->expects($this->once())
139 ->willReturn($views_data);
141 $plugin = $this->getMock('Drupal\views\Plugin\views\ViewsHandlerInterface');
142 $this->factory->expects($this->once())
143 ->method('createInstance')
144 ->with('test_override')
145 ->willReturn($plugin);
147 $result = $this->handlerManager->getHandler($item, 'test_override');
148 $this->assertSame($plugin, $result);
152 * Tests getHandler() without an override.
154 public function testGetHandlerNoOverride() {
155 $this->setupMockedFactory();
158 $item['table'] = 'test_table';
159 $item['field'] = 'test_field';
162 $views_data['test_field']['test']['id'] = 'test_id';
164 $this->viewsData->expects($this->once())
167 ->willReturn($views_data);
169 $plugin = $this->getMock('Drupal\views\Plugin\views\ViewsHandlerInterface');
170 $this->factory->expects($this->once())
171 ->method('createInstance')
173 ->willReturn($plugin);
175 $result = $this->handlerManager->getHandler($item);
176 $this->assertSame($plugin, $result);