View source
<?php
namespace Drupal\Tests\block_style_plugins\Unit\Plugin;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\block\BlockForm;
use Drupal\block\Entity\Block;
use Drupal\Core\Block\BlockPluginInterface;
class BlockStyleBaseTest extends UnitTestCase {
protected $entityRepository;
protected $entityTypeManager;
protected $formState;
protected $blockPlugin;
protected $plugin;
protected function setUp() {
parent::setUp();
$this->entityRepository = $this
->prophesize(EntityRepositoryInterface::CLASS);
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::CLASS);
$this->formState = $this
->prophesize(FormStateInterface::CLASS);
$this->blockPlugin = $this
->prophesize(BlockPluginInterface::CLASS);
$this->blockPlugin
->getBaseId()
->willReturn('block_content');
$this->blockPlugin
->getDerivativeId()
->willReturn('uuid-1234');
$this->blockPlugin
->getPluginId()
->willReturn('basic_block');
$configuration = [];
$plugin_id = 'block_style_plugins';
$plugin_definition['provider'] = 'block_style_plugins';
$this->plugin = new MockBlockStyleBase($configuration, $plugin_id, $plugin_definition, $this->entityRepository
->reveal(), $this->entityTypeManager
->reveal());
$translator = $this
->getStringTranslationStub();
$this->plugin
->setStringTranslation($translator);
}
public function testCreate() {
$configuration = [];
$plugin_id = 'block_style_plugins';
$plugin_definition['provider'] = 'block_style_plugins';
$container = $this
->prophesize(ContainerInterface::CLASS);
$container
->get('entity.repository')
->willReturn($this->entityRepository
->reveal());
$container
->get('entity_type.manager')
->willReturn($this->entityTypeManager
->reveal());
$instance = MockBlockStyleBase::create($container
->reveal(), $configuration, $plugin_id, $plugin_definition);
$this
->assertInstanceOf('Drupal\\block_style_plugins\\Plugin\\BlockStyleInterface', $instance);
}
public function testPrepareForm() {
$block = $this
->prophesize(Block::CLASS);
$block
->getPlugin()
->willReturn($this->blockPlugin
->reveal());
$block
->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
->willReturn([
'test_style' => TRUE,
]);
$blockForm = $this
->prophesize(BlockForm::CLASS);
$blockForm
->getEntity()
->willReturn($block
->reveal());
$this->formState
->getFormObject()
->willReturn($blockForm
->reveal());
$form = [];
$form['actions']['submit']['#submit'] = [];
$return = $this->plugin
->prepareForm($form, $this->formState
->reveal());
$return_callback = $return['actions']['submit']['#submit'][0];
$this
->assertInstanceOf('Drupal\\block_style_plugins\\Plugin\\BlockStyleBase', $return_callback[0]);
$this
->assertEquals('submitForm', $return_callback[1]);
$this
->assertArrayHasKey('block_styles', $return);
$styles = $this->plugin
->getConfiguration();
$expected_styles = [
'sample_class' => '',
'sample_checkbox' => '',
'test_style' => TRUE,
];
$this
->assertArrayEquals($expected_styles, $styles);
$expected_third_party_settings['block_style_plugins']['block_style_plugins'] = [
'#type' => 'container',
'#group' => 'block_styles',
];
$this
->assertArrayEquals($expected_third_party_settings, $return['third_party_settings']);
}
public function testDefaultConfiguration() {
$expected = [
'sample_class' => '',
'sample_checkbox' => FALSE,
];
$default = $this->plugin
->defaultConfiguration();
$this
->assertArrayEquals($expected, $default);
}
public function testBuildConfigurationForm() {
$form = [];
$return = $this->plugin
->buildConfigurationForm($form, $this->formState
->reveal());
$this
->assertArrayEquals([], $return);
}
public function testFormAlter() {
$form = [
'test',
];
$return = $this->plugin
->formAlter($form, $this->formState
->reveal());
$this
->assertArrayEquals($form, $return);
}
public function testValidateForm() {
$form = [
'third_party_settings' => [
'block_style_plugins' => [
$this->plugin
->getPluginId() => [],
],
],
];
$return = $this->plugin
->validateForm($form, $this->formState
->reveal());
$this
->assertNull($return);
}
public function testSubmitForm() {
$form = [
'third_party_settings' => [
'block_style_plugins' => [
$this->plugin
->getPluginId() => [],
],
],
];
$return = $this->plugin
->submitForm($form, $this->formState
->reveal());
$this
->assertNull($return);
}
public function testBuild() {
$block = $this
->prophesize(ConfigEntityInterface::CLASS);
$storage = $this
->prophesize(EntityStorageInterface::CLASS);
$storage
->load(1)
->willReturn($block
->reveal());
$this->entityTypeManager
->getStorage('block')
->willReturn($storage
->reveal());
$variables = [];
$return = $this->plugin
->build($variables);
$this
->assertArrayEquals($variables, $return);
$block
->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
->willReturn(FALSE);
$variables = [
'elements' => [
'#id' => 1,
],
];
$return = $this->plugin
->build($variables);
$this
->assertArrayEquals($variables, $return);
$block
->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
->willReturn([
'class1',
'class2',
]);
$variables = [
'elements' => [
'#id' => 1,
],
];
$expected = [
'elements' => [
'#id' => 1,
],
'block_styles' => [
'block_style_plugins' => [
'class1',
'class2',
],
],
'attributes' => [
'class' => [
'class1',
'class2',
],
],
];
$return = $this->plugin
->build($variables);
$this
->assertArrayEquals($expected, $return);
$block
->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
->willReturn([
'class1',
1,
'class2',
0,
]);
$variables = [
'elements' => [
'#id' => 1,
],
];
$expected = [
'elements' => [
'#id' => 1,
],
'block_styles' => [
'block_style_plugins' => [
'class1',
1,
'class2',
0,
],
],
'attributes' => [
'class' => [
'class1',
'class2',
],
],
];
$return = $this->plugin
->build($variables);
$this
->assertArrayEquals($expected, $return);
}
public function testGetConfiguration() {
$expected = [
'sample_class' => '',
'sample_checkbox' => FALSE,
];
$this->plugin
->setConfiguration([]);
$return = $this->plugin
->getConfiguration();
$this
->assertArrayEquals($expected, $return);
}
public function testSetConfiguration() {
$expected = [
'sample_class' => '',
'sample_checkbox' => FALSE,
'new_key' => 'new_val',
];
$new_styles = [
'new_key' => 'new_val',
];
$this->plugin
->setConfiguration($new_styles);
$return = $this->plugin
->getConfiguration();
$this
->assertArrayEquals($expected, $return);
$expected = [
'sample_class' => 'class_name',
'sample_checkbox' => TRUE,
];
$this->plugin
->setConfiguration($expected);
$return = $this->plugin
->getConfiguration();
$this
->assertArrayEquals($expected, $return);
}
public function testExclude($plugin, $bundle, $expected) {
$this
->setProtectedProperty('blockPlugin', $this->blockPlugin
->reveal());
if ($plugin) {
$this
->setProtectedProperty('pluginDefinition', [
'exclude' => [
$plugin,
],
]);
}
if ($bundle) {
$this
->setProtectedProperty('blockContentBundle', $bundle);
}
$return = $this->plugin
->exclude();
$this
->assertEquals($expected, $return);
}
public function excludeProvider() {
return [
'No exclude options are passed' => [
FALSE,
NULL,
FALSE,
],
'Exclude basic_block' => [
'basic_block',
NULL,
TRUE,
],
'Exclude a block that is not the current one' => [
'wrong_block',
NULL,
FALSE,
],
'Exclude a custom content block' => [
'custom_block',
'custom_block',
TRUE,
],
'Exclude a custom content block that is not the current block' => [
'wrong_custom_block',
'custom_block',
FALSE,
],
'Exclude all derivatives of a base_plugin_id' => [
'basic_block:*',
NULL,
TRUE,
],
];
}
public function testIncludeOnly($plugin, $bundle, $expected) {
$this
->setProtectedProperty('blockPlugin', $this->blockPlugin
->reveal());
if ($plugin) {
$this
->setProtectedProperty('pluginDefinition', [
'include' => [
$plugin,
],
]);
}
if ($bundle) {
$this
->setProtectedProperty('blockContentBundle', $bundle);
}
$return = $this->plugin
->includeOnly();
$this
->assertEquals($expected, $return);
}
public function includeOnlyProvider() {
return [
'No include options are passed' => [
NULL,
NULL,
TRUE,
],
'Include basic_block' => [
'basic_block',
NULL,
TRUE,
],
'Include only a sample_block' => [
'wrong_block',
NULL,
FALSE,
],
'Include a custom content block' => [
'custom_block',
'custom_block',
TRUE,
],
'Include a custom content block which is not the current one' => [
'wrong_custom_block',
'custom_block',
FALSE,
],
'Include all derivatives of a base_plugin_id' => [
'basic_block:*',
NULL,
TRUE,
],
];
}
public function testSetBlockContentBundle() {
$this
->setProtectedProperty('blockPlugin', $this->blockPlugin
->reveal());
$entity = $this
->prophesize(EntityInterface::CLASS);
$entity
->bundle()
->willReturn('basic_custom_block');
$this->entityRepository
->loadEntityByUuid('block_content', 'uuid-1234')
->willReturn($entity
->reveal());
$this->plugin
->setBlockContentBundle();
$bundle = $this
->getProtectedProperty('blockContentBundle');
$this
->assertEquals('basic_custom_block', $bundle);
}
public function getProtectedProperty($property) {
$reflection = new \ReflectionClass($this->plugin);
$reflection_property = $reflection
->getProperty($property);
$reflection_property
->setAccessible(TRUE);
return $reflection_property
->getValue($this->plugin);
}
public function setProtectedProperty($property, $value) {
$reflection = new \ReflectionClass($this->plugin);
$reflection_property = $reflection
->getProperty($property);
$reflection_property
->setAccessible(TRUE);
$reflection_property
->setValue($this->plugin, $value);
}
}