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 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);
70 $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
71 ->disableOriginalConstructor()
72 ->setMethods(['buildRenderable', 'setDisplay', 'setItemsPerPage'])
74 $this->executable->expects($this->any())
75 ->method('setDisplay')
77 ->will($this->returnValue(TRUE));
78 $this->executable->expects($this->any())
79 ->method('getShowAdminLinks')
82 $this->executable->display_handler = $this->getMockBuilder('Drupal\views\Plugin\views\display\Block')
83 ->disableOriginalConstructor()
87 $this->view = $this->getMockBuilder('Drupal\views\Entity\View')
88 ->disableOriginalConstructor()
90 $this->view->expects($this->any())
92 ->willReturn('test_view');
93 $this->executable->storage = $this->view;
95 $this->executableFactory = $this->getMockBuilder('Drupal\views\ViewExecutableFactory')
96 ->disableOriginalConstructor()
98 $this->executableFactory->expects($this->any())
101 ->will($this->returnValue($this->executable));
103 $this->displayHandler = $this->getMockBuilder('Drupal\views\Plugin\views\display\Block')
104 ->disableOriginalConstructor()
107 $this->displayHandler->expects($this->any())
108 ->method('blockSettings')
111 $this->displayHandler->expects($this->any())
112 ->method('getPluginId')
113 ->willReturn('block');
115 $this->displayHandler->expects($this->any())
116 ->method('getHandlers')
119 $this->executable->display_handler = $this->displayHandler;
121 $this->storage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigEntityStorage')
122 ->disableOriginalConstructor()
125 $this->storage->expects($this->any())
128 ->will($this->returnValue($this->view));
129 $this->account = $this->getMock('Drupal\Core\Session\AccountInterface');
133 * Tests the build method.
135 * @see \Drupal\views\Plugin\block\ViewsBlock::build()
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);
145 $block_id = 'views_block:test_view-block_1';
149 $definition['provider'] = 'views';
150 $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
152 $this->assertEquals($build, $plugin->build());
156 * Tests the build method.
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);
167 $block_id = 'views_block:test_view-block_1';
171 $definition['provider'] = 'views';
172 $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
174 $this->assertEquals(array_intersect_key($build, ['#cache' => TRUE]), $plugin->build());
178 * Tests the build method with a failed execution.
180 * @see \Drupal\views\Plugin\block\ViewsBlock::build()
182 public function testBuildFailed() {
184 $this->executable->expects($this->once())
185 ->method('buildRenderable')
186 ->with('block_1', [])
187 ->willReturn($output);
189 $block_id = 'views_block:test_view-block_1';
193 $definition['provider'] = 'views';
194 $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
196 $this->assertEquals([], $plugin->build());
201 // @todo https://www.drupal.org/node/2571679 replace
202 // views_add_contextual_links().
203 namespace Drupal\views\Plugin\Block;
205 if (!function_exists('views_add_contextual_links')) {
206 function views_add_contextual_links() {