ViewsBlockTest.php 6.4 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\views\Unit\Plugin\Block;
4

5
use Drupal\Core\DependencyInjection\ContainerBuilder;
6 7 8 9 10 11 12 13 14
use Drupal\Tests\UnitTestCase;
use Drupal\views\Plugin\Block\ViewsBlock;

// @todo Remove this once the constant got converted.
if (!defined('BLOCK_LABEL_VISIBLE')) {
  define('BLOCK_LABEL_VISIBLE', 'visible');
}

/**
15 16
 * @coversDefaultClass \Drupal\views\Plugin\block\ViewsBlock
 * @group views
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 */
class ViewsBlockTest extends UnitTestCase {

  /**
   * The view executable.
   *
   * @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $executable;

  /**
   * The view executable factory.
   *
   * @var \Drupal\views\ViewExecutableFactory|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $executableFactory;

  /**
   * The view entity.
   *
37
   * @var \Drupal\views\ViewEntityInterface|\PHPUnit_Framework_MockObject_MockObject
38 39 40 41
   */
  protected $view;

  /**
42
   * The view storage.
43
   *
44
   * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
45
   */
46
  protected $storage;
47

48 49 50 51 52 53 54
  /**
   * The mocked user account.
   *
   * @var \Drupal\Core\Session\AccountInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $account;

55 56 57 58 59 60 61
  /**
   * The mocked display handler.
   *
   * @var \Drupal\views\Plugin\views\display\Block|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $displayHandler;

62 63 64 65 66
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp(); // TODO: Change the autogenerated stub
67 68 69 70 71 72 73
    $condition_plugin_manager = $this->getMock('Drupal\Core\Executable\ExecutableManagerInterface');
    $condition_plugin_manager->expects($this->any())
      ->method('getDefinitions')
      ->will($this->returnValue(array()));
    $container = new ContainerBuilder();
    $container->set('plugin.manager.condition', $condition_plugin_manager);
    \Drupal::setContainer($container);
74 75 76

    $this->executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
      ->disableOriginalConstructor()
77
      ->setMethods(['buildRenderable', 'setDisplay', 'setItemsPerPage'])
78 79 80 81 82
      ->getMock();
    $this->executable->expects($this->any())
      ->method('setDisplay')
      ->with('block_1')
      ->will($this->returnValue(TRUE));
83 84 85
    $this->executable->expects($this->any())
      ->method('getShowAdminLinks')
      ->willReturn(FALSE);
86

87
    $this->executable->display_handler = $this->getMockBuilder('Drupal\views\Plugin\views\display\Block')
88 89 90 91
      ->disableOriginalConstructor()
      ->setMethods(NULL)
      ->getMock();

92
    $this->view = $this->getMockBuilder('Drupal\views\Entity\View')
93 94
      ->disableOriginalConstructor()
      ->getMock();
95 96 97 98
    $this->view->expects($this->any())
      ->method('id')
      ->willReturn('test_view');
    $this->executable->storage = $this->view;
99 100

    $this->executableFactory = $this->getMockBuilder('Drupal\views\ViewExecutableFactory')
101
      ->disableOriginalConstructor()
102
      ->getMock();
103
    $this->executableFactory->expects($this->any())
104 105 106 107
      ->method('get')
      ->with($this->view)
      ->will($this->returnValue($this->executable));

108 109 110 111 112 113 114 115 116 117 118 119 120
    $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->executable->display_handler = $this->displayHandler;

121
    $this->storage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigEntityStorage')
122 123 124
      ->disableOriginalConstructor()
      ->getMock();

125
    $this->storage->expects($this->any())
126 127 128
      ->method('load')
      ->with('test_view')
      ->will($this->returnValue($this->view));
129
    $this->account = $this->getMock('Drupal\Core\Session\AccountInterface');
130 131 132 133 134 135 136 137
  }

  /**
   * Tests the build method.
   *
   * @see \Drupal\views\Plugin\block\ViewsBlock::build()
   */
  public function testBuild() {
138
    $output = $this->randomMachineName(100);
139
    $build = array('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 142 143
      ->method('buildRenderable')
      ->with('block_1', [])
      ->willReturn($build);
144 145 146 147

    $block_id = 'views_block:test_view-block_1';
    $config = array();
    $definition = array();
148 149

    $definition['provider'] = 'views';
150
    $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
151 152 153 154

    $this->assertEquals($build, $plugin->build());
  }

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  /**
   * Tests the build method.
   *
   * @covers ::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());
  }

177 178 179 180 181 182 183 184
  /**
   * Tests the build method with a failed execution.
   *
   * @see \Drupal\views\Plugin\block\ViewsBlock::build()
   */
  public function testBuildFailed() {
    $output = FALSE;
    $this->executable->expects($this->once())
185 186 187
      ->method('buildRenderable')
      ->with('block_1', [])
      ->willReturn($output);
188 189 190 191

    $block_id = 'views_block:test_view-block_1';
    $config = array();
    $definition = array();
192 193

    $definition['provider'] = 'views';
194
    $plugin = new ViewsBlock($config, $block_id, $definition, $this->executableFactory, $this->storage, $this->account);
195 196 197 198 199 200

    $this->assertEquals(array(), $plugin->build());
  }

}

201 202 203
// @todo https://www.drupal.org/node/2571679 replace
//   views_add_contextual_links().
namespace Drupal\views\Plugin\Block;
204

205 206
if (!function_exists('views_add_contextual_links')) {
  function views_add_contextual_links() {
207 208
  }
}