3 namespace Drupal\Tests\views\Unit\Plugin\Block;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Tests\UnitTestCase;
7 use Drupal\views\Plugin\Block\ViewsBlock;
10 * @coversDefaultClass \Drupal\views\Plugin\block\ViewsBlock
13 class ViewsBlockTest extends UnitTestCase {
16 * The view executable.
18 * @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
20 protected $executable;
23 * The view executable factory.
25 * @var \Drupal\views\ViewExecutableFactory|\PHPUnit_Framework_MockObject_MockObject
27 protected $executableFactory;
32 * @var \Drupal\views\ViewEntityInterface|\PHPUnit_Framework_MockObject_MockObject
39 * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
44 * The mocked user account.
46 * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
51 * The mocked display handler.
53 * @var \Drupal\views\Plugin\views\display\Block|\PHPUnit_Framework_MockObject_MockObject
55 protected $displayHandler;
60 protected function setUp() {
61 // TODO: Change the autogenerated stub.
63 $condition_plugin_manager = $this->getMock('Drupal\Core\Executable\ExecutableManagerInterface');
64 $condition_plugin_manager->expects($this->any())
65 ->method('getDefinitions')
66 ->will($this->returnValue([]));
67 $container = new ContainerBuilder();
68 $container->set('plugin.manager.condition', $condition_plugin_manager);
69 \Drupal::setContainer($container);
71 $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
72 ->disableOriginalConstructor()
73 ->setMethods(['buildRenderable', 'setDisplay', 'setItemsPerPage', 'getShowAdminLinks'])
75 $this->executable->expects($this->any())
76 ->method('setDisplay')
78 ->will($this->returnValue(TRUE));
79 $this->executable->expects($this->any())
80 ->method('getShowAdminLinks')
83 $this->executable->display_handler = $this->getMockBuilder('Drupal\views\Plugin\views\display\Block')
84 ->disableOriginalConstructor()
88 $this->view = $this->getMockBuilder('Drupal\views\Entity\View')
89 ->disableOriginalConstructor()
91 $this->view->expects($this->any())
93 ->willReturn('test_view');
94 $this->executable->storage = $this->view;
96 $this->executableFactory = $this->getMockBuilder('Drupal\views\ViewExecutableFactory')
97 ->disableOriginalConstructor()
99 $this->executableFactory->expects($this->any())
102 ->will($this->returnValue($this->executable));
104 $this->displayHandler = $this->getMockBuilder('Drupal\views\Plugin\views\display\Block')
105 ->disableOriginalConstructor()
108 $this->displayHandler->expects($this->any())
109 ->method('blockSettings')
112 $this->displayHandler->expects($this->any())
113 ->method('getPluginId')
114 ->willReturn('block');
116 $this->displayHandler->expects($this->any())
117 ->method('getHandlers')
120 $this->executable->display_handler = $this->displayHandler;
122 $this->storage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigEntityStorage')
123 ->disableOriginalConstructor()
126 $this->storage->expects($this->any())
129 ->will($this->returnValue($this->view));
130 $this->account = $this->getMock('Drupal\Core\Session\AccountInterface');
134 * Tests the build method.
136 * @see \Drupal\views\Plugin\block\ViewsBlock::build()
138 public function testBuild() {
139 $output = $this->randomMachineName(100);
140 $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];
141 $this->executable->expects($this->once())
142 ->method('buildRenderable')
143 ->with('block_1', [])
144 ->willReturn($build);
146 $block_id = 'views_block:test_view-block_1';
150 $definition['provider'] = 'views';
151 $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
153 $this->assertEquals($build, $plugin->build());
157 * Tests the build method.
161 public function testBuildEmpty() {
162 $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']]];
163 $this->executable->expects($this->once())
164 ->method('buildRenderable')
165 ->with('block_1', [])
166 ->willReturn($build);
168 $block_id = 'views_block:test_view-block_1';
172 $definition['provider'] = 'views';
173 $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
175 $this->assertEquals(array_intersect_key($build, ['#cache' => TRUE]), $plugin->build());
179 * Tests the build method with a failed execution.
181 * @see \Drupal\views\Plugin\block\ViewsBlock::build()
183 public function testBuildFailed() {
185 $this->executable->expects($this->once())
186 ->method('buildRenderable')
187 ->with('block_1', [])
188 ->willReturn($output);
190 $block_id = 'views_block:test_view-block_1';
194 $definition['provider'] = 'views';
195 $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
197 $this->assertEquals([], $plugin->build());
202 // @todo https://www.drupal.org/node/2571679 replace
203 // views_add_contextual_links().
204 namespace Drupal\views\Plugin\Block;
206 if (!function_exists('views_add_contextual_links')) {
208 function views_add_contextual_links() {