Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / views / tests / src / Unit / Plugin / Block / ViewsBlockTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Unit\Plugin\Block;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\views\Plugin\Block\ViewsBlock;
8
9 /**
10  * @coversDefaultClass \Drupal\views\Plugin\block\ViewsBlock
11  * @group views
12  */
13 class ViewsBlockTest extends UnitTestCase {
14
15   /**
16    * The view executable.
17    *
18    * @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
19    */
20   protected $executable;
21
22   /**
23    * The view executable factory.
24    *
25    * @var \Drupal\views\ViewExecutableFactory|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $executableFactory;
28
29   /**
30    * The view entity.
31    *
32    * @var \Drupal\views\ViewEntityInterface|\PHPUnit_Framework_MockObject_MockObject
33    */
34   protected $view;
35
36   /**
37    * The view storage.
38    *
39    * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
40    */
41   protected $storage;
42
43   /**
44    * The mocked user account.
45    *
46    * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
47    */
48   protected $account;
49
50   /**
51    * The mocked display handler.
52    *
53    * @var \Drupal\views\Plugin\views\display\Block|\PHPUnit_Framework_MockObject_MockObject
54    */
55   protected $displayHandler;
56
57   /**
58    * {@inheritdoc}
59    */
60   protected function setUp() {
61     parent::setUp(); // TODO: Change the autogenerated stub
62     $condition_plugin_manager = $this->getMock('Drupal\Core\Executable\ExecutableManagerInterface');
63     $condition_plugin_manager->expects($this->any())
64       ->method('getDefinitions')
65       ->will($this->returnValue([]));
66     $container = new ContainerBuilder();
67     $container->set('plugin.manager.condition', $condition_plugin_manager);
68     \Drupal::setContainer($container);
69
70     $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
71       ->disableOriginalConstructor()
72       ->setMethods(['buildRenderable', 'setDisplay', 'setItemsPerPage'])
73       ->getMock();
74     $this->executable->expects($this->any())
75       ->method('setDisplay')
76       ->with('block_1')
77       ->will($this->returnValue(TRUE));
78     $this->executable->expects($this->any())
79       ->method('getShowAdminLinks')
80       ->willReturn(FALSE);
81
82     $this->executable->display_handler = $this->getMockBuilder('Drupal\views\Plugin\views\display\Block')
83       ->disableOriginalConstructor()
84       ->setMethods(NULL)
85       ->getMock();
86
87     $this->view = $this->getMockBuilder('Drupal\views\Entity\View')
88       ->disableOriginalConstructor()
89       ->getMock();
90     $this->view->expects($this->any())
91       ->method('id')
92       ->willReturn('test_view');
93     $this->executable->storage = $this->view;
94
95     $this->executableFactory = $this->getMockBuilder('Drupal\views\ViewExecutableFactory')
96       ->disableOriginalConstructor()
97       ->getMock();
98     $this->executableFactory->expects($this->any())
99       ->method('get')
100       ->with($this->view)
101       ->will($this->returnValue($this->executable));
102
103     $this->displayHandler = $this->getMockBuilder('Drupal\views\Plugin\views\display\Block')
104       ->disableOriginalConstructor()
105       ->getMock();
106
107     $this->displayHandler->expects($this->any())
108       ->method('blockSettings')
109       ->willReturn([]);
110
111     $this->displayHandler->expects($this->any())
112       ->method('getPluginId')
113       ->willReturn('block');
114
115     $this->displayHandler->expects($this->any())
116       ->method('getHandlers')
117       ->willReturn([]);
118
119     $this->executable->display_handler = $this->displayHandler;
120
121     $this->storage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigEntityStorage')
122       ->disableOriginalConstructor()
123       ->getMock();
124
125     $this->storage->expects($this->any())
126       ->method('load')
127       ->with('test_view')
128       ->will($this->returnValue($this->view));
129     $this->account = $this->getMock('Drupal\Core\Session\AccountInterface');
130   }
131
132   /**
133    * Tests the build method.
134    *
135    * @see \Drupal\views\Plugin\block\ViewsBlock::build()
136    */
137   public function testBuild() {
138     $output = $this->randomMachineName(100);
139     $build = ['view_build' => $output, '#view_id' => 'test_view', '#view_display_plugin_class' => '\Drupal\views\Plugin\views\display\Block', '#view_display_show_admin_links' => FALSE, '#view_display_plugin_id' => 'block', '#pre_rendered' => TRUE];
140     $this->executable->expects($this->once())
141       ->method('buildRenderable')
142       ->with('block_1', [])
143       ->willReturn($build);
144
145     $block_id = 'views_block:test_view-block_1';
146     $config = [];
147     $definition = [];
148
149     $definition['provider'] = 'views';
150     $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
151
152     $this->assertEquals($build, $plugin->build());
153   }
154
155   /**
156    * Tests the build method.
157    *
158    * @covers ::build
159    */
160   public function testBuildEmpty() {
161     $build = ['view_build' => [], '#view_id' => 'test_view', '#view_display_plugin_class' => '\Drupal\views\Plugin\views\display\Block', '#view_display_show_admin_links' => FALSE, '#view_display_plugin_id' => 'block', '#pre_rendered' => TRUE, '#cache' => ['contexts' => ['user']]];
162     $this->executable->expects($this->once())
163       ->method('buildRenderable')
164       ->with('block_1', [])
165       ->willReturn($build);
166
167     $block_id = 'views_block:test_view-block_1';
168     $config = [];
169     $definition = [];
170
171     $definition['provider'] = 'views';
172     $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
173
174     $this->assertEquals(array_intersect_key($build, ['#cache' => TRUE]), $plugin->build());
175   }
176
177   /**
178    * Tests the build method with a failed execution.
179    *
180    * @see \Drupal\views\Plugin\block\ViewsBlock::build()
181    */
182   public function testBuildFailed() {
183     $output = FALSE;
184     $this->executable->expects($this->once())
185       ->method('buildRenderable')
186       ->with('block_1', [])
187       ->willReturn($output);
188
189     $block_id = 'views_block:test_view-block_1';
190     $config = [];
191     $definition = [];
192
193     $definition['provider'] = 'views';
194     $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
195
196     $this->assertEquals([], $plugin->build());
197   }
198
199 }
200
201 // @todo https://www.drupal.org/node/2571679 replace
202 //   views_add_contextual_links().
203 namespace Drupal\views\Plugin\Block;
204
205 if (!function_exists('views_add_contextual_links')) {
206   function views_add_contextual_links() {
207   }
208 }