class ViewsBlockTest
@coversDefaultClass \Drupal\views\Plugin\block\ViewsBlock
      
    
@group views
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase- class \Drupal\Tests\views\Unit\Plugin\Block\ViewsBlockTest extends \Drupal\Tests\UnitTestCase
 
Expanded class hierarchy of ViewsBlockTest
File
- 
              core/modules/ views/ tests/ src/ Unit/ Plugin/ Block/ ViewsBlockTest.php, line 15 
Namespace
Drupal\Tests\views\Unit\Plugin\BlockView source
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.
   *
   * @var \Drupal\views\ViewEntityInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $view;
  
  /**
   * The view storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $storage;
  
  /**
   * The mocked user account.
   *
   * @var \Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $account;
  
  /**
   * The mocked display handler.
   *
   * @var \Drupal\views\Plugin\views\display\Block|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $displayHandler;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    // @todo Change the autogenerated stub.
    parent::setUp();
    $condition_plugin_manager = $this->createMock('Drupal\\Core\\Executable\\ExecutableManagerInterface');
    $condition_plugin_manager->expects($this->any())
      ->method('getDefinitions')
      ->willReturn([]);
    $container = new ContainerBuilder();
    $container->set('plugin.manager.condition', $condition_plugin_manager);
    \Drupal::setContainer($container);
    $this->executable = $this->getMockBuilder('Drupal\\views\\ViewExecutable')
      ->disableOriginalConstructor()
      ->onlyMethods([
      'buildRenderable',
      'setDisplay',
      'setItemsPerPage',
      'getShowAdminLinks',
    ])
      ->getMock();
    $this->executable
      ->expects($this->any())
      ->method('setDisplay')
      ->with('block_1')
      ->willReturn(TRUE);
    $this->executable
      ->expects($this->any())
      ->method('getShowAdminLinks')
      ->willReturn(FALSE);
    $this->executable->display_handler = $this->getMockBuilder('Drupal\\views\\Plugin\\views\\display\\Block')
      ->disableOriginalConstructor()
      ->onlyMethods([])
      ->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)
      ->willReturn($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')
      ->willReturn($this->view);
    $this->account = $this->createMock('Drupal\\Core\\Session\\AccountInterface');
  }
  
  /**
   * Tests the build method.
   *
   * @see \Drupal\views\Plugin\block\ViewsBlock::build()
   */
  public function testBuild() : void {
    $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());
  }
  
  /**
   * Tests the build method.
   *
   * @covers ::build
   */
  public function testBuildEmpty() : void {
    $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());
  }
  
  /**
   * Tests the build method with a failed execution.
   *
   * @see \Drupal\views\Plugin\block\ViewsBlock::build()
   */
  public function testBuildFailed() : void {
    $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());
  }
}Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides | 
|---|---|---|---|---|---|---|
| PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
| PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
| RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
| UnitTestCase::$root | protected | property | The app root. | 1 | ||
| UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |||
| UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |||
| UnitTestCase::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |||
| UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
| UnitTestCase::setUpBeforeClass | public static | function | ||||
| UnitTestCase::__get | public | function | ||||
| ViewsBlockTest::$account | protected | property | The mocked user account. | |||
| ViewsBlockTest::$displayHandler | protected | property | The mocked display handler. | |||
| ViewsBlockTest::$executable | protected | property | The view executable. | |||
| ViewsBlockTest::$executableFactory | protected | property | The view executable factory. | |||
| ViewsBlockTest::$storage | protected | property | The view storage. | |||
| ViewsBlockTest::$view | protected | property | The view entity. | |||
| ViewsBlockTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
| ViewsBlockTest::testBuild | public | function | Tests the build method. | |||
| ViewsBlockTest::testBuildEmpty | public | function | Tests the build method. | |||
| ViewsBlockTest::testBuildFailed | public | function | Tests the build method with a failed execution. | 
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.
