Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / views / tests / src / Unit / ViewsTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Unit;
4
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;
13
14 /**
15  * @coversDefaultClass \Drupal\views\Views
16  * @group views
17  */
18 class ViewsTest extends UnitTestCase {
19
20   /**
21    * The test container.
22    *
23    * @var \Drupal\Core\DependencyInjection\ContainerBuilder
24    */
25   protected $container;
26
27   /**
28    * {@inheritdoc}
29    */
30   protected function setUp() {
31     parent::setUp();
32
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()
39       ->getMock();
40     $route_provider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
41     $this->container->set('views.executable', new ViewExecutableFactory($user, $request_stack, $views_data, $route_provider));
42
43     \Drupal::setContainer($this->container);
44   }
45
46   /**
47    * Tests the getView() method.
48    *
49    * @covers ::getView
50    */
51   public function testGetView() {
52     $view = new View(['id' => 'test_view'], 'view');
53
54     $view_storage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigEntityStorage')
55       ->disableOriginalConstructor()
56       ->getMock();
57     $view_storage->expects($this->once())
58       ->method('load')
59       ->with('test_view')
60       ->will($this->returnValue($view));
61
62     $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
63     $entity_manager->expects($this->once())
64       ->method('getStorage')
65       ->with('view')
66       ->will($this->returnValue($view_storage));
67     $this->container->set('entity.manager', $entity_manager);
68
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));
73   }
74
75   /**
76    * @covers ::getApplicableViews
77    *
78    * @dataProvider providerTestGetApplicableViews
79    */
80   public function testGetApplicableViews($applicable_type, $expected) {
81     $view_1 = new View([
82       'id' => 'test_view_1',
83       'display' => [
84         'default' => [
85           'display_plugin' => 'default',
86           'display_options' => [],
87         ],
88         'type_a' => [
89           'display_plugin' => 'type_a',
90           'display_options' => [],
91         ],
92       ],
93     ], 'view');
94     $view_2 = new View([
95       'id' => 'test_view_2',
96       'display' => [
97         'default' => [
98           'display_plugin' => 'default',
99           'display_options' => [],
100         ],
101         'type_b' => [
102           'display_plugin' => 'type_b',
103           'display_options' => [
104             'enabled' => TRUE,
105           ],
106         ],
107         'type_b_2' => [
108           'display_plugin' => 'type_b',
109           'display_options' => [
110             'enabled' => FALSE,
111           ],
112         ],
113       ],
114     ], 'view');
115     $view_3 = new View([
116       'id' => 'test_view_3',
117       'display' => [
118         'default' => [
119           'display_plugin' => 'default',
120           'display_options' => [],
121         ],
122         // Test with Type A but a disabled display.
123         'type_a' => [
124           'display_plugin' => 'type_a',
125           'display_options' => [
126             'enabled' => FALSE,
127           ],
128         ],
129         // Type D intentionally doesn't exist.
130         'type_d' => [
131           'display_plugin' => 'type_d',
132           'display_options' => [],
133         ],
134       ],
135     ], 'view');
136
137     $query = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
138     $query->expects($this->exactly(2))
139       ->method('condition')
140       ->willReturnSelf();
141     $query->expects($this->once())
142       ->method('execute')
143       ->willReturn(['test_view_1', 'test_view_2', 'test_view_3']);
144
145     $view_storage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigEntityStorage')
146       ->disableOriginalConstructor()
147       ->getMock();
148     $view_storage->expects($this->once())
149       ->method('getQuery')
150       ->willReturn($query);
151
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]));
156
157     $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
158     $entity_type_manager->expects($this->exactly(2))
159       ->method('getStorage')
160       ->with('view')
161       ->will($this->returnValue($view_storage));
162     $this->container->set('entity_type.manager', $entity_type_manager);
163
164     $definitions = [
165       'type_a' => [
166         'type_a' => TRUE,
167         'type_b' => FALSE,
168       ],
169       'type_b' => [
170         'type_a' => FALSE,
171         'type_b' => TRUE,
172       ],
173     ];
174
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);
180
181     $result = Views::getApplicableViews($applicable_type);
182     $this->assertEquals($expected, $result);
183   }
184
185   /**
186    * Data provider for testGetApplicableViews.
187    *
188    * @return array
189    */
190   public function providerTestGetApplicableViews() {
191     return [
192       ['type_a', [['test_view_1', 'type_a']]],
193       ['type_b', [['test_view_2', 'type_b']]],
194       ['type_c', []],
195     ];
196   }
197
198 }