Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / tests / src / Unit / ViewsHandlerManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Unit;
4
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\views\Plugin\ViewsHandlerManager;
7
8 /**
9  * Tests the ViewsHandlerManager class.
10  *
11  * @group views
12  *
13  * @coversDefaultClass \Drupal\views\Plugin\ViewsHandlerManager
14  */
15 class ViewsHandlerManagerTest extends UnitTestCase {
16
17   /**
18    * @var \Drupal\views\Plugin\ViewsHandlerManager
19    */
20   protected $handlerManager;
21
22   /**
23    * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
24    */
25   protected $moduleHandler;
26
27   /**
28    * The mocked views data.
29    *
30    * @var \Drupal\views\ViewsData|\PHPUnit_Framework_MockObject_MockObject
31    */
32   protected $viewsData;
33
34   /**
35    * The mocked factory.
36    *
37    * @var \Drupal\Component\Plugin\Factory\FactoryInterface|\PHPUnit_Framework_MockObject_MockObject
38    */
39   protected $factory;
40
41   /**
42    * {@inheritdoc}
43    */
44   protected function setUp() {
45     parent::setUp();
46     $this->viewsData = $this->getMockBuilder('Drupal\views\ViewsData')
47       ->disableOriginalConstructor()
48       ->getMock();
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);
52   }
53
54   /**
55    * Setups of the plugin factory.
56    */
57   protected function setupMockedFactory() {
58     $this->factory = $this->getMock('Drupal\Component\Plugin\Factory\FactoryInterface');
59
60     $reflection = new \ReflectionClass($this->handlerManager);
61     $property = $reflection->getProperty('factory');
62     $property->setAccessible(TRUE);
63     $property->setValue($this->handlerManager, $this->factory);
64   }
65
66   /**
67    * Tests that hook_views_plugins_TYPE_alter() is invoked for a handler type.
68    *
69    * @covers ::__construct
70    * @covers ::getDefinitions
71    */
72   public function testAlterHookInvocation() {
73     $this->moduleHandler->expects($this->once())
74       ->method('alter')
75       ->with('views_plugins_test', []);
76
77     $this->handlerManager->getDefinitions();
78   }
79
80   /**
81    * Tests getHandler() and its base information propagation.
82    */
83   public function testGetHandlerBaseInformationPropagation() {
84     $this->setupMockedFactory();
85
86     $item = [];
87     $item['table'] = 'test_table';
88     $item['field'] = 'test_field';
89
90     $views_data = [];
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';
98
99     $this->viewsData->expects($this->once())
100       ->method('get')
101       ->with('test_table')
102       ->willReturn($views_data);
103
104     $expected_definition = [
105       'id' => 'test_id',
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',
112     ];
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);
118
119     $result = $this->handlerManager->getHandler($item);
120     $this->assertSame($plugin, $result);
121   }
122
123   /**
124    * Tests getHandler() with an override.
125    */
126   public function testGetHandlerOverride() {
127     $this->setupMockedFactory();
128
129     $item = [];
130     $item['table'] = 'test_table';
131     $item['field'] = 'test_field';
132
133     $views_data = [];
134     $views_data['test_field']['test']['id'] = 'test_id';
135
136     $this->viewsData->expects($this->once())
137       ->method('get')
138       ->with('test_table')
139       ->willReturn($views_data);
140
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);
146
147     $result = $this->handlerManager->getHandler($item, 'test_override');
148     $this->assertSame($plugin, $result);
149   }
150
151   /**
152    * Tests getHandler() without an override.
153    */
154   public function testGetHandlerNoOverride() {
155     $this->setupMockedFactory();
156
157     $item = [];
158     $item['table'] = 'test_table';
159     $item['field'] = 'test_field';
160
161     $views_data = [];
162     $views_data['test_field']['test']['id'] = 'test_id';
163
164     $this->viewsData->expects($this->once())
165       ->method('get')
166       ->with('test_table')
167       ->willReturn($views_data);
168
169     $plugin = $this->getMock('Drupal\views\Plugin\views\ViewsHandlerInterface');
170     $this->factory->expects($this->once())
171       ->method('createInstance')
172       ->with('test_id')
173       ->willReturn($plugin);
174
175     $result = $this->handlerManager->getHandler($item);
176     $this->assertSame($plugin, $result);
177   }
178
179 }