3 namespace Drupal\Tests\views\Unit;
5 use Drupal\Core\Entity\EntityTypeManagerInterface;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\views\Views;
8 use Drupal\views\Entity\View;
9 use Drupal\views\ViewExecutableFactory;
10 use Drupal\Core\DependencyInjection\ContainerBuilder;
11 use Symfony\Component\HttpFoundation\Request;
12 use Symfony\Component\HttpFoundation\RequestStack;
15 * @coversDefaultClass \Drupal\views\Views
18 class ViewsTest extends UnitTestCase {
23 * @var \Drupal\Core\DependencyInjection\ContainerBuilder
30 protected function setUp() {
33 $this->container = new ContainerBuilder();
34 $user = $this->getMock('Drupal\Core\Session\AccountInterface');
35 $request_stack = new RequestStack();
36 $request_stack->push(new Request());
37 $views_data = $this->getMockBuilder('Drupal\views\ViewsData')
38 ->disableOriginalConstructor()
40 $route_provider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
41 $this->container->set('views.executable', new ViewExecutableFactory($user, $request_stack, $views_data, $route_provider));
43 \Drupal::setContainer($this->container);
47 * Tests the getView() method.
51 public function testGetView() {
52 $view = new View(['id' => 'test_view'], 'view');
54 $view_storage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigEntityStorage')
55 ->disableOriginalConstructor()
57 $view_storage->expects($this->once())
60 ->will($this->returnValue($view));
62 $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
63 $entity_manager->expects($this->once())
64 ->method('getStorage')
66 ->will($this->returnValue($view_storage));
67 $this->container->set('entity.manager', $entity_manager);
69 $executable = Views::getView('test_view');
70 $this->assertInstanceOf('Drupal\views\ViewExecutable', $executable);
71 $this->assertEquals($view->id(), $executable->storage->id());
72 $this->assertEquals(spl_object_hash($view), spl_object_hash($executable->storage));
76 * @covers ::getApplicableViews
78 * @dataProvider providerTestGetApplicableViews
80 public function testGetApplicableViews($applicable_type, $expected) {
82 'id' => 'test_view_1',
85 'display_plugin' => 'default',
86 'display_options' => [],
89 'display_plugin' => 'type_a',
90 'display_options' => [],
95 'id' => 'test_view_2',
98 'display_plugin' => 'default',
99 'display_options' => [],
102 'display_plugin' => 'type_b',
103 'display_options' => [
108 'display_plugin' => 'type_b',
109 'display_options' => [
116 'id' => 'test_view_3',
119 'display_plugin' => 'default',
120 'display_options' => [],
122 // Test with Type A but a disabled display.
124 'display_plugin' => 'type_a',
125 'display_options' => [
129 // Type D intentionally doesn't exist.
131 'display_plugin' => 'type_d',
132 'display_options' => [],
137 $query = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
138 $query->expects($this->exactly(2))
139 ->method('condition')
141 $query->expects($this->once())
143 ->willReturn(['test_view_1', 'test_view_2', 'test_view_3']);
145 $view_storage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigEntityStorage')
146 ->disableOriginalConstructor()
148 $view_storage->expects($this->once())
150 ->willReturn($query);
152 $view_storage->expects($this->once())
153 ->method('loadMultiple')
154 ->with(['test_view_1', 'test_view_2', 'test_view_3'])
155 ->will($this->returnValue(['test_view_1' => $view_1, 'test_view_2' => $view_2, 'test_view_3' => $view_3]));
157 $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
158 $entity_type_manager->expects($this->exactly(2))
159 ->method('getStorage')
161 ->will($this->returnValue($view_storage));
162 $this->container->set('entity_type.manager', $entity_type_manager);
175 $display_manager = $this->getMock('Drupal\Component\Plugin\PluginManagerInterface');
176 $display_manager->expects($this->once())
177 ->method('getDefinitions')
178 ->willReturn($definitions);
179 $this->container->set('plugin.manager.views.display', $display_manager);
181 $result = Views::getApplicableViews($applicable_type);
182 $this->assertEquals($expected, $result);
186 * Data provider for testGetApplicableViews.
190 public function providerTestGetApplicableViews() {
192 ['type_a', [['test_view_1', 'type_a']]],
193 ['type_b', [['test_view_2', 'type_b']]],