View source
<?php
namespace Drupal\Tests\views\Unit\Plugin\Block;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Plugin\Block\ViewsBlock;
class ViewsBlockTest extends UnitTestCase {
protected $executable;
protected $executableFactory;
protected $view;
protected $storage;
protected $account;
protected $displayHandler;
protected function setUp() {
parent::setUp();
$condition_plugin_manager = $this
->createMock('Drupal\\Core\\Executable\\ExecutableManagerInterface');
$condition_plugin_manager
->expects($this
->any())
->method('getDefinitions')
->will($this
->returnValue([]));
$container = new ContainerBuilder();
$container
->set('plugin.manager.condition', $condition_plugin_manager);
\Drupal::setContainer($container);
$this->executable = $this
->getMockBuilder('Drupal\\views\\ViewExecutable')
->disableOriginalConstructor()
->setMethods([
'buildRenderable',
'setDisplay',
'setItemsPerPage',
'getShowAdminLinks',
])
->getMock();
$this->executable
->expects($this
->any())
->method('setDisplay')
->with('block_1')
->will($this
->returnValue(TRUE));
$this->executable
->expects($this
->any())
->method('getShowAdminLinks')
->willReturn(FALSE);
$this->executable->display_handler = $this
->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\Block')
->disableOriginalConstructor()
->setMethods(NULL)
->getMock();
$this->view = $this
->getMockBuilder('Drupal\\views\\Entity\\View')
->disableOriginalConstructor()
->getMock();
$this->view
->expects($this
->any())
->method('id')
->willReturn('test_view');
$this->executable->storage = $this->view;
$this->executableFactory = $this
->getMockBuilder('Drupal\\views\\ViewExecutableFactory')
->disableOriginalConstructor()
->getMock();
$this->executableFactory
->expects($this
->any())
->method('get')
->with($this->view)
->will($this
->returnValue($this->executable));
$this->displayHandler = $this
->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\Block')
->disableOriginalConstructor()
->getMock();
$this->displayHandler
->expects($this
->any())
->method('blockSettings')
->willReturn([]);
$this->displayHandler
->expects($this
->any())
->method('getPluginId')
->willReturn('block');
$this->displayHandler
->expects($this
->any())
->method('getHandlers')
->willReturn([]);
$this->executable->display_handler = $this->displayHandler;
$this->storage = $this
->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityStorage')
->disableOriginalConstructor()
->getMock();
$this->storage
->expects($this
->any())
->method('load')
->with('test_view')
->will($this
->returnValue($this->view));
$this->account = $this
->createMock('Drupal\\Core\\Session\\AccountInterface');
}
public function testBuild() {
$output = $this
->randomMachineName(100);
$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,
];
$this->executable
->expects($this
->once())
->method('buildRenderable')
->with('block_1', [])
->willReturn($build);
$block_id = 'views_block:test_view-block_1';
$config = [];
$definition = [];
$definition['provider'] = 'views';
$plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
$this
->assertEquals($build, $plugin
->build());
}
public function testBuildEmpty() {
$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',
],
],
];
$this->executable
->expects($this
->once())
->method('buildRenderable')
->with('block_1', [])
->willReturn($build);
$block_id = 'views_block:test_view-block_1';
$config = [];
$definition = [];
$definition['provider'] = 'views';
$plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
$this
->assertEquals(array_intersect_key($build, [
'#cache' => TRUE,
]), $plugin
->build());
}
public function testBuildFailed() {
$output = FALSE;
$this->executable
->expects($this
->once())
->method('buildRenderable')
->with('block_1', [])
->willReturn($output);
$block_id = 'views_block:test_view-block_1';
$config = [];
$definition = [];
$definition['provider'] = 'views';
$plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
$this
->assertEquals([], $plugin
->build());
}
}
namespace Drupal\views\Plugin\Block;
if (!function_exists('views_add_contextual_links')) {
function views_add_contextual_links() {
}
}