Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
108
core/modules/block/tests/src/Unit/BlockConfigEntityUnitTest.php
Normal file
108
core/modules/block/tests/src/Unit/BlockConfigEntityUnitTest.php
Normal file
|
@ -0,0 +1,108 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\block\Unit\BlockConfigEntityUnitTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\block\Unit;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\block\Entity\Block
|
||||
* @group block
|
||||
*/
|
||||
class BlockConfigEntityUnitTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The entity type used for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityTypeInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $entityType;
|
||||
|
||||
/**
|
||||
* The entity manager used for testing.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* The ID of the type of the entity under test.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $entityTypeId;
|
||||
|
||||
/**
|
||||
* The UUID generator used for testing.
|
||||
*
|
||||
* @var \Drupal\Component\Uuid\UuidInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $uuid;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
$this->entityTypeId = $this->randomMachineName();
|
||||
|
||||
$this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
|
||||
$this->entityType->expects($this->any())
|
||||
->method('getProvider')
|
||||
->will($this->returnValue('block'));
|
||||
|
||||
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
|
||||
$this->entityManager->expects($this->any())
|
||||
->method('getDefinition')
|
||||
->with($this->entityTypeId)
|
||||
->will($this->returnValue($this->entityType));
|
||||
|
||||
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
|
||||
|
||||
$container = new ContainerBuilder();
|
||||
$container->set('entity.manager', $this->entityManager);
|
||||
$container->set('uuid', $this->uuid);
|
||||
\Drupal::setContainer($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::calculateDependencies
|
||||
*/
|
||||
public function testCalculateDependencies() {
|
||||
$values = array('theme' => 'stark');
|
||||
// Mock the entity under test so that we can mock getPluginCollections().
|
||||
$entity = $this->getMockBuilder('\Drupal\block\Entity\Block')
|
||||
->setConstructorArgs(array($values, $this->entityTypeId))
|
||||
->setMethods(array('getPluginCollections'))
|
||||
->getMock();
|
||||
// Create a configurable plugin that would add a dependency.
|
||||
$instance_id = $this->randomMachineName();
|
||||
$instance = new TestConfigurablePlugin(array(), $instance_id, array('provider' => 'test'));
|
||||
|
||||
// Create a plugin collection to contain the instance.
|
||||
$plugin_collection = $this->getMockBuilder('\Drupal\Core\Plugin\DefaultLazyPluginCollection')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(array('get'))
|
||||
->getMock();
|
||||
$plugin_collection->expects($this->atLeastOnce())
|
||||
->method('get')
|
||||
->with($instance_id)
|
||||
->will($this->returnValue($instance));
|
||||
$plugin_collection->addInstanceId($instance_id);
|
||||
|
||||
// Return the mocked plugin collection.
|
||||
$entity->expects($this->once())
|
||||
->method('getPluginCollections')
|
||||
->will($this->returnValue(array($plugin_collection)));
|
||||
|
||||
$dependencies = $entity->calculateDependencies();
|
||||
$this->assertContains('test', $dependencies['module']);
|
||||
$this->assertContains('stark', $dependencies['theme']);
|
||||
}
|
||||
|
||||
}
|
125
core/modules/block/tests/src/Unit/BlockFormTest.php
Normal file
125
core/modules/block/tests/src/Unit/BlockFormTest.php
Normal file
|
@ -0,0 +1,125 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\block\Unit\BlockFormTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\block\Unit;
|
||||
|
||||
use Drupal\block\BlockForm;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\block\BlockForm
|
||||
* @group block
|
||||
*/
|
||||
class BlockFormTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The condition plugin manager.
|
||||
*
|
||||
* @var \Drupal\Core\Executable\ExecutableManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $conditionManager;
|
||||
|
||||
/**
|
||||
* The block storage.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $storage;
|
||||
|
||||
/**
|
||||
* The event dispatcher service.
|
||||
*
|
||||
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $dispatcher;
|
||||
|
||||
/**
|
||||
* The language manager service.
|
||||
*
|
||||
* @var \Drupal\Core\Language\LanguageManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $language;
|
||||
|
||||
|
||||
/**
|
||||
* The theme handler.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $themeHandler;
|
||||
|
||||
/**
|
||||
* The entity manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->conditionManager = $this->getMock('Drupal\Core\Executable\ExecutableManagerInterface');
|
||||
$this->language = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
|
||||
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
|
||||
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
|
||||
$this->storage = $this->getMock('Drupal\Core\Config\Entity\ConfigEntityStorageInterface');
|
||||
$this->themeHandler = $this->getMock('Drupal\Core\Extension\ThemeHandlerInterface');
|
||||
$this->entityManager->expects($this->any())
|
||||
->method('getStorage')
|
||||
->will($this->returnValue($this->storage));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the unique machine name generator.
|
||||
*
|
||||
* @see \Drupal\block\BlockForm::getUniqueMachineName()
|
||||
*/
|
||||
public function testGetUniqueMachineName() {
|
||||
$blocks = array();
|
||||
|
||||
$blocks['test'] = $this->getBlockMockWithMachineName('test');
|
||||
$blocks['other_test'] = $this->getBlockMockWithMachineName('other_test');
|
||||
$blocks['other_test_1'] = $this->getBlockMockWithMachineName('other_test');
|
||||
$blocks['other_test_2'] = $this->getBlockMockWithMachineName('other_test');
|
||||
|
||||
$query = $this->getMock('Drupal\Core\Entity\Query\QueryInterface');
|
||||
$query->expects($this->exactly(5))
|
||||
->method('condition')
|
||||
->will($this->returnValue($query));
|
||||
|
||||
$query->expects($this->exactly(5))
|
||||
->method('execute')
|
||||
->will($this->returnValue(array('test', 'other_test', 'other_test_1', 'other_test_2')));
|
||||
|
||||
$this->storage->expects($this->exactly(5))
|
||||
->method('getQuery')
|
||||
->will($this->returnValue($query));
|
||||
|
||||
$block_form_controller = new BlockForm($this->entityManager, $this->conditionManager, $this->dispatcher, $this->language, $this->themeHandler);
|
||||
|
||||
// Ensure that the block with just one other instance gets the next available
|
||||
// name suggestion.
|
||||
$this->assertEquals('test_2', $block_form_controller->getUniqueMachineName($blocks['test']));
|
||||
|
||||
// Ensure that the block with already three instances (_0, _1, _2) gets the
|
||||
// 4th available name.
|
||||
$this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test']));
|
||||
$this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_1']));
|
||||
$this->assertEquals('other_test_3', $block_form_controller->getUniqueMachineName($blocks['other_test_2']));
|
||||
|
||||
// Ensure that a block without an instance yet gets the suggestion as
|
||||
// unique machine name.
|
||||
$last_block = $this->getBlockMockWithMachineName('last_test');
|
||||
$this->assertEquals('last_test', $block_form_controller->getUniqueMachineName($last_block));
|
||||
}
|
||||
|
||||
}
|
185
core/modules/block/tests/src/Unit/BlockRepositoryTest.php
Normal file
185
core/modules/block/tests/src/Unit/BlockRepositoryTest.php
Normal file
|
@ -0,0 +1,185 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\block\Unit\BlockRepositoryTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\block\Unit;
|
||||
|
||||
use Drupal\block\BlockRepository;
|
||||
use Drupal\Core\Block\BlockPluginInterface;
|
||||
use Drupal\Core\Plugin\ContextAwarePluginInterface;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\block\BlockRepository
|
||||
* @group block
|
||||
*/
|
||||
class BlockRepositoryTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* @var \Drupal\block\BlockRepository
|
||||
*/
|
||||
protected $blockRepository;
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $blockStorage;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $theme;
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $contextHandler;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$active_theme = $this->getMockBuilder('Drupal\Core\Theme\ActiveTheme')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->theme = $this->randomMachineName();
|
||||
$active_theme->expects($this->atLeastOnce())
|
||||
->method('getName')
|
||||
->willReturn($this->theme);
|
||||
$active_theme->expects($this->atLeastOnce())
|
||||
->method('getRegions')
|
||||
->willReturn([
|
||||
'top',
|
||||
'center',
|
||||
'bottom',
|
||||
]);
|
||||
|
||||
$theme_manager = $this->getMock('Drupal\Core\Theme\ThemeManagerInterface');
|
||||
$theme_manager->expects($this->once())
|
||||
->method('getActiveTheme')
|
||||
->will($this->returnValue($active_theme));
|
||||
|
||||
$this->contextHandler = $this->getMock('Drupal\Core\Plugin\Context\ContextHandlerInterface');
|
||||
$this->blockStorage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
|
||||
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
|
||||
$entity_manager->expects($this->any())
|
||||
->method('getStorage')
|
||||
->willReturn($this->blockStorage);
|
||||
|
||||
$this->blockRepository = new BlockRepository($entity_manager, $theme_manager, $this->contextHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the retrieval of block entities.
|
||||
*
|
||||
* @covers ::getVisibleBlocksPerRegion
|
||||
*
|
||||
* @dataProvider providerBlocksConfig
|
||||
*/
|
||||
public function testGetVisibleBlocksPerRegion(array $blocks_config, array $expected_blocks) {
|
||||
$blocks = [];
|
||||
foreach ($blocks_config as $block_id => $block_config) {
|
||||
$block = $this->getMock('Drupal\block\BlockInterface');
|
||||
$block->expects($this->once())
|
||||
->method('setContexts')
|
||||
->willReturnSelf();
|
||||
$block->expects($this->once())
|
||||
->method('access')
|
||||
->will($this->returnValue($block_config[0]));
|
||||
$block->expects($block_config[0] ? $this->atLeastOnce() : $this->never())
|
||||
->method('getRegion')
|
||||
->willReturn($block_config[1]);
|
||||
$blocks[$block_id] = $block;
|
||||
}
|
||||
|
||||
$this->blockStorage->expects($this->once())
|
||||
->method('loadByProperties')
|
||||
->with(['theme' => $this->theme])
|
||||
->willReturn($blocks);
|
||||
$result = [];
|
||||
foreach ($this->blockRepository->getVisibleBlocksPerRegion([]) as $region => $resulting_blocks) {
|
||||
$result[$region] = [];
|
||||
foreach ($resulting_blocks as $plugin_id => $block) {
|
||||
$result[$region][] = $plugin_id;
|
||||
}
|
||||
}
|
||||
$this->assertSame($result, $expected_blocks);
|
||||
}
|
||||
|
||||
public function providerBlocksConfig() {
|
||||
$blocks_config = array(
|
||||
'block1' => array(
|
||||
TRUE, 'top', 0
|
||||
),
|
||||
// Test a block without access.
|
||||
'block2' => array(
|
||||
FALSE, 'bottom', 0
|
||||
),
|
||||
// Test two blocks in the same region with specific weight.
|
||||
'block3' => array(
|
||||
TRUE, 'bottom', 5
|
||||
),
|
||||
'block4' => array(
|
||||
TRUE, 'bottom', -5
|
||||
),
|
||||
);
|
||||
|
||||
$test_cases = [];
|
||||
$test_cases[] = [$blocks_config,
|
||||
[
|
||||
'top' => ['block1'],
|
||||
'center' => [],
|
||||
'bottom' => ['block4', 'block3'],
|
||||
]
|
||||
];
|
||||
return $test_cases;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the retrieval of block entities that are context-aware.
|
||||
*
|
||||
* @covers ::getVisibleBlocksPerRegion
|
||||
*/
|
||||
public function testGetVisibleBlocksPerRegionWithContext() {
|
||||
$block = $this->getMock('Drupal\block\BlockInterface');
|
||||
$block->expects($this->once())
|
||||
->method('setContexts')
|
||||
->willReturnSelf();
|
||||
$block->expects($this->once())
|
||||
->method('access')
|
||||
->willReturn(TRUE);
|
||||
$block->expects($this->once())
|
||||
->method('getRegion')
|
||||
->willReturn('top');
|
||||
$blocks['block_id'] = $block;
|
||||
|
||||
$contexts = [];
|
||||
$this->blockStorage->expects($this->once())
|
||||
->method('loadByProperties')
|
||||
->with(['theme' => $this->theme])
|
||||
->willReturn($blocks);
|
||||
$result = [];
|
||||
foreach ($this->blockRepository->getVisibleBlocksPerRegion($contexts) as $region => $resulting_blocks) {
|
||||
$result[$region] = [];
|
||||
foreach ($resulting_blocks as $plugin_id => $block) {
|
||||
$result[$region][] = $plugin_id;
|
||||
}
|
||||
}
|
||||
$expected = [
|
||||
'top' => [
|
||||
'block_id',
|
||||
],
|
||||
'center' => [],
|
||||
'bottom' => [],
|
||||
];
|
||||
$this->assertSame($expected, $result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface TestContextAwareBlockInterface extends BlockPluginInterface, ContextAwarePluginInterface {
|
||||
}
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\block\Unit\CategoryAutocompleteTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\block\Unit;
|
||||
|
||||
use Drupal\block\Controller\CategoryAutocompleteController;
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\block\Controller\CategoryAutocompleteController
|
||||
* @group block
|
||||
*/
|
||||
class CategoryAutocompleteTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The autocomplete controller.
|
||||
*
|
||||
* @var \Drupal\block\Controller\CategoryAutocompleteController
|
||||
*/
|
||||
protected $autocompleteController;
|
||||
|
||||
protected function setUp() {
|
||||
$block_manager = $this->getMock('Drupal\Core\Block\BlockManagerInterface');
|
||||
$block_manager->expects($this->any())
|
||||
->method('getCategories')
|
||||
->will($this->returnValue(array('Comment', 'Node', 'None & Such', 'User')));
|
||||
|
||||
$this->autocompleteController = new CategoryAutocompleteController($block_manager);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the autocomplete method.
|
||||
*
|
||||
* @param string $string
|
||||
* The string entered into the autocomplete.
|
||||
* @param array $suggestions
|
||||
* The array of expected suggestions.
|
||||
*
|
||||
* @see \Drupal\block\Controller\CategoryAutocompleteController::autocomplete()
|
||||
*
|
||||
* @dataProvider providerTestAutocompleteSuggestions
|
||||
*/
|
||||
public function testAutocompleteSuggestions($string, $suggestions) {
|
||||
$suggestions = array_map(function ($suggestion) {
|
||||
return array('value' => $suggestion, 'label' => SafeMarkup::checkPlain($suggestion));
|
||||
}, $suggestions);
|
||||
$result = $this->autocompleteController->autocomplete(new Request(array('q' => $string)));
|
||||
$this->assertSame($suggestions, json_decode($result->getContent(), TRUE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for testAutocompleteSuggestions().
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function providerTestAutocompleteSuggestions() {
|
||||
$test_parameters = array();
|
||||
$test_parameters[] = array(
|
||||
'string' => 'Com',
|
||||
'suggestions' => array(
|
||||
'Comment',
|
||||
),
|
||||
);
|
||||
$test_parameters[] = array(
|
||||
'string' => 'No',
|
||||
'suggestions' => array(
|
||||
'Node',
|
||||
'None & Such',
|
||||
),
|
||||
);
|
||||
$test_parameters[] = array(
|
||||
'string' => 'us',
|
||||
'suggestions' => array(
|
||||
'User',
|
||||
),
|
||||
);
|
||||
$test_parameters[] = array(
|
||||
'string' => 'Banana',
|
||||
'suggestions' => array(),
|
||||
);
|
||||
return $test_parameters;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\block\Unit\Menu\BlockLocalTasksTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\block\Unit\Menu;
|
||||
|
||||
use Drupal\Tests\Core\Menu\LocalTaskIntegrationTestBase;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
|
||||
/**
|
||||
* Tests block local tasks.
|
||||
*
|
||||
* @group block
|
||||
*/
|
||||
class BlockLocalTasksTest extends LocalTaskIntegrationTestBase {
|
||||
|
||||
protected function setUp() {
|
||||
$this->directoryList = array('block' => 'core/modules/block');
|
||||
parent::setUp();
|
||||
|
||||
$config_factory = $this->getConfigFactoryStub(array('system.theme' => array(
|
||||
'default' => 'test_c',
|
||||
)));
|
||||
|
||||
$themes = array();
|
||||
$themes['test_a'] = (object) array(
|
||||
'status' => 0,
|
||||
);
|
||||
$themes['test_b'] = (object) array(
|
||||
'status' => 1,
|
||||
'info' => array(
|
||||
'name' => 'test_b',
|
||||
),
|
||||
);
|
||||
$themes['test_c'] = (object) array(
|
||||
'status' => 1,
|
||||
'info' => array(
|
||||
'name' => 'test_c',
|
||||
),
|
||||
);
|
||||
$theme_handler = $this->getMock('Drupal\Core\Extension\ThemeHandlerInterface');
|
||||
$theme_handler->expects($this->any())
|
||||
->method('listInfo')
|
||||
->will($this->returnValue($themes));
|
||||
|
||||
$container = new ContainerBuilder();
|
||||
$container->set('config.factory', $config_factory);
|
||||
$container->set('theme_handler', $theme_handler);
|
||||
$container->set('app.root', $this->root);
|
||||
\Drupal::setContainer($container);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the admin edit local task.
|
||||
*/
|
||||
public function testBlockAdminLocalTasks() {
|
||||
$this->assertLocalTasks('entity.block.edit_form', array(array('entity.block.edit_form')));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the block admin display local tasks.
|
||||
*
|
||||
* @dataProvider providerTestBlockAdminDisplay
|
||||
*/
|
||||
public function testBlockAdminDisplay($route, $expected) {
|
||||
$this->assertLocalTasks($route, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a list of routes to test.
|
||||
*/
|
||||
public function providerTestBlockAdminDisplay() {
|
||||
return array(
|
||||
array('block.admin_display', array(array('block.admin_display'), array('block.admin_display_theme:test_b', 'block.admin_display_theme:test_c'))),
|
||||
array('block.admin_display_theme', array(array('block.admin_display'), array('block.admin_display_theme:test_b', 'block.admin_display_theme:test_c'))),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,241 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\block\Unit\Plugin\DisplayVariant\BlockPageVariantTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\block\Unit\Plugin\DisplayVariant;
|
||||
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\block\Plugin\DisplayVariant\BlockPageVariant
|
||||
* @group block
|
||||
*/
|
||||
class BlockPageVariantTest extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* The block repository.
|
||||
*
|
||||
* @var \Drupal\block\BlockRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $blockRepository;
|
||||
|
||||
/**
|
||||
* The block view builder.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityViewBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $blockViewBuilder;
|
||||
|
||||
/**
|
||||
* The event dispatcher.
|
||||
*
|
||||
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $dispatcher;
|
||||
|
||||
/**
|
||||
* The plugin context handler.
|
||||
*
|
||||
* @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
|
||||
*/
|
||||
protected $contextHandler;
|
||||
|
||||
/**
|
||||
* Sets up a display variant plugin for testing.
|
||||
*
|
||||
* @param array $configuration
|
||||
* An array of plugin configuration.
|
||||
* @param array $definition
|
||||
* The plugin definition array.
|
||||
*
|
||||
* @return \Drupal\block\Plugin\DisplayVariant\BlockPageVariant|\PHPUnit_Framework_MockObject_MockObject
|
||||
* A mocked display variant plugin.
|
||||
*/
|
||||
public function setUpDisplayVariant($configuration = array(), $definition = array()) {
|
||||
$this->blockRepository = $this->getMock('Drupal\block\BlockRepositoryInterface');
|
||||
$this->blockViewBuilder = $this->getMock('Drupal\Core\Entity\EntityViewBuilderInterface');
|
||||
$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
|
||||
$this->dispatcher->expects($this->any())
|
||||
->method('dispatch')
|
||||
->willReturnArgument(1);
|
||||
return $this->getMockBuilder('Drupal\block\Plugin\DisplayVariant\BlockPageVariant')
|
||||
->setConstructorArgs(array($configuration, 'test', $definition, $this->blockRepository, $this->blockViewBuilder, $this->dispatcher, ['config:block_list']))
|
||||
->setMethods(array('getRegionNames'))
|
||||
->getMock();
|
||||
}
|
||||
|
||||
public function providerBuild() {
|
||||
$blocks_config = array(
|
||||
'block1' => array(
|
||||
// region, is main content block, is messages block
|
||||
'top', FALSE, FALSE,
|
||||
),
|
||||
// Test multiple blocks in the same region.
|
||||
'block2' => array(
|
||||
'bottom', FALSE, FALSE,
|
||||
),
|
||||
'block3' => array(
|
||||
'bottom', FALSE, FALSE,
|
||||
),
|
||||
// Test a block implementing MainContentBlockPluginInterface.
|
||||
'block4' => array(
|
||||
'center', TRUE, FALSE,
|
||||
),
|
||||
// Test a block implementing MessagesBlockPluginInterface.
|
||||
'block5' => array(
|
||||
'center', FALSE, TRUE,
|
||||
),
|
||||
);
|
||||
|
||||
$test_cases = [];
|
||||
$test_cases[] = [$blocks_config, 5,
|
||||
[
|
||||
'#cache' => [
|
||||
'tags' => [
|
||||
'config:block_list',
|
||||
],
|
||||
],
|
||||
'top' => [
|
||||
'block1' => [],
|
||||
'#sorted' => TRUE,
|
||||
],
|
||||
// The main content was rendered via a block.
|
||||
'center' => [
|
||||
'block4' => [],
|
||||
'block5' => [],
|
||||
'#sorted' => TRUE,
|
||||
],
|
||||
'bottom' => [
|
||||
'block2' => [],
|
||||
'block3' => [],
|
||||
'#sorted' => TRUE,
|
||||
],
|
||||
],
|
||||
];
|
||||
unset($blocks_config['block5']);
|
||||
$test_cases[] = [$blocks_config, 4,
|
||||
[
|
||||
'#cache' => [
|
||||
'tags' => [
|
||||
'config:block_list',
|
||||
],
|
||||
],
|
||||
'top' => [
|
||||
'block1' => [],
|
||||
'#sorted' => TRUE,
|
||||
],
|
||||
'center' => [
|
||||
'block4' => [],
|
||||
'#sorted' => TRUE,
|
||||
],
|
||||
'bottom' => [
|
||||
'block2' => [],
|
||||
'block3' => [],
|
||||
'#sorted' => TRUE,
|
||||
],
|
||||
// The messages are rendered via the fallback in case there is no block
|
||||
// rendering the main content.
|
||||
'content' => [
|
||||
'messages' => [
|
||||
'#weight' => -1000,
|
||||
'#type' => 'status_messages',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
unset($blocks_config['block4']);
|
||||
$test_cases[] = [$blocks_config, 3,
|
||||
[
|
||||
'#cache' => [
|
||||
'tags' => [
|
||||
'config:block_list',
|
||||
],
|
||||
],
|
||||
'top' => [
|
||||
'block1' => [],
|
||||
'#sorted' => TRUE,
|
||||
],
|
||||
'bottom' => [
|
||||
'block2' => [],
|
||||
'block3' => [],
|
||||
'#sorted' => TRUE,
|
||||
],
|
||||
// The main content & messages are rendered via the fallback in case
|
||||
// there are no blocks rendering them.
|
||||
'content' => [
|
||||
'system_main' => ['#markup' => 'Hello kittens!'],
|
||||
'messages' => [
|
||||
'#weight' => -1000,
|
||||
'#type' => 'status_messages',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
return $test_cases;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the building of a full page variant.
|
||||
*
|
||||
* @covers ::build
|
||||
*
|
||||
* @dataProvider providerBuild
|
||||
*/
|
||||
public function testBuild(array $blocks_config, $visible_block_count, array $expected_render_array) {
|
||||
$display_variant = $this->setUpDisplayVariant();
|
||||
$display_variant->setMainContent(['#markup' => 'Hello kittens!']);
|
||||
|
||||
$blocks = ['top' => [], 'center' => [], 'bottom' => []];
|
||||
$block_plugin = $this->getMock('Drupal\Core\Block\BlockPluginInterface');
|
||||
$main_content_block_plugin = $this->getMock('Drupal\Core\Block\MainContentBlockPluginInterface');
|
||||
$messages_block_plugin = $this->getMock('Drupal\Core\Block\MessagesBlockPluginInterface');
|
||||
foreach ($blocks_config as $block_id => $block_config) {
|
||||
$block = $this->getMock('Drupal\block\BlockInterface');
|
||||
$block->expects($this->atLeastOnce())
|
||||
->method('getPlugin')
|
||||
->willReturn($block_config[1] ? $main_content_block_plugin : ($block_config[2] ? $messages_block_plugin : $block_plugin));
|
||||
$blocks[$block_config[0]][$block_id] = $block;
|
||||
}
|
||||
|
||||
$this->blockViewBuilder->expects($this->exactly($visible_block_count))
|
||||
->method('view')
|
||||
->will($this->returnValue(array()));
|
||||
$this->blockRepository->expects($this->once())
|
||||
->method('getVisibleBlocksPerRegion')
|
||||
->will($this->returnValue($blocks));
|
||||
|
||||
$this->assertSame($expected_render_array, $display_variant->build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the building of a full page variant with no main content set.
|
||||
*
|
||||
* @covers ::build
|
||||
*/
|
||||
public function testBuildWithoutMainContent() {
|
||||
$display_variant = $this->setUpDisplayVariant();
|
||||
$this->blockRepository->expects($this->once())
|
||||
->method('getVisibleBlocksPerRegion')
|
||||
->willReturn([]);
|
||||
|
||||
$expected = [
|
||||
'#cache' => [
|
||||
'tags' => [
|
||||
'config:block_list',
|
||||
],
|
||||
],
|
||||
'content' => [
|
||||
'system_main' => [],
|
||||
'messages' => [
|
||||
'#weight' => -1000,
|
||||
'#type' => 'status_messages',
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->assertSame($expected, $display_variant->build());
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue