View source
<?php
namespace Drupal\Tests\block_style_plugins\Unit;
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\block\BlockForm;
use Drupal\block\Entity\Block;
use Drupal\block_style_plugins\BlockFormAlter;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\block_style_plugins\Plugin\BlockStyleManager;
use Drupal\Tests\block_style_plugins\Unit\Plugin\MockBlockStyleBase;
class BlockFormAlterTest extends UnitTestCase {
protected $entityRepository;
protected $entityTypeManager;
protected $blockStyleManager;
protected $formState;
protected $blockPlugin;
protected $plugin;
protected $classInstance;
protected function setUp() : void {
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->entityTypeManager
->reveal());
$this->blockStyleManager = $this
->prophesize(BlockStyleManager::class);
$this->blockStyleManager
->getBlockDefinitions()
->willReturn([
$plugin_id => $plugin_definition,
]);
$this->blockStyleManager
->createInstance('block_style_plugins')
->willReturn($this->plugin);
$this->classInstance = new BlockFormAlter($this->blockStyleManager
->reveal());
$reflectionObject = new \ReflectionObject($this->classInstance);
$property = $reflectionObject
->getProperty('entityRepository');
$property
->setAccessible(TRUE);
$property
->setValue($this->classInstance, $this->entityRepository
->reveal());
$translator = $this
->getStringTranslationStub();
$this->classInstance
->setStringTranslation($translator);
}
public function testCreate() {
$container = $this
->prophesize(ContainerInterface::class);
$container
->get('plugin.manager.block_style.processor')
->willReturn($this->blockStyleManager
->reveal());
$instance = BlockFormAlter::create($container
->reveal());
$this
->assertInstanceOf('Drupal\\block_style_plugins\\BlockFormAlter', $instance);
}
public function testFormAlter() {
$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 = [];
$this->classInstance
->alterForm($form, $this->formState
->reveal());
$this
->assertArrayHasKey('block_styles', $form);
$styles = $this->plugin
->getConfiguration();
$expected_styles = [
'sample_class' => '',
'sample_checkbox' => '',
'test_style' => TRUE,
];
$this
->assertEquals($expected_styles, $styles);
$expected_third_party_settings['block_style_plugins']['block_style_plugins'] = [
'#type' => 'container',
'#group' => 'block_styles',
];
$this
->assertEquals($expected_third_party_settings, $form['third_party_settings']);
$this
->assertInstanceOf('Drupal\\block_style_plugins\\BlockFormAlter', $form['#validate'][0][0]);
$this
->assertEquals('validateForm', $form['#validate'][0][1]);
$this
->assertInstanceOf('Drupal\\block_style_plugins\\BlockFormAlter', $form['#submit'][0][0]);
$this
->assertEquals('submitForm', $form['#submit'][0][1]);
}
public function testAllowStyles($type, $plugin, $expected) {
$plugin_definition = [];
if ($plugin) {
$plugin_definition = [
$type => [
$plugin,
],
];
}
$return = $this->classInstance
->allowStyles('basic_block', $plugin_definition);
$this
->assertEquals($expected, $return);
}
public function allowStylesProvider() {
return [
'No include options are passed' => [
NULL,
NULL,
TRUE,
],
'Include basic_block' => [
'include',
'basic_block',
TRUE,
],
'Include only a sample_block' => [
'include',
'wrong_block',
FALSE,
],
'Include all derivatives of a base_plugin_id' => [
'include',
'basic_block:*',
TRUE,
],
'No exclude options are passed' => [
NULL,
NULL,
TRUE,
],
'Exclude basic_block' => [
'exclude',
'basic_block',
FALSE,
],
'Exclude a block that is not the current one' => [
'exclude',
'wrong_block',
TRUE,
],
'Exclude all derivatives of a base_plugin_id' => [
'exclude',
'basic_block:*',
FALSE,
],
];
}
public function testAllowStylesDerivatives() {
$plugin_definition = [
'exclude' => [
'system_menu_block:*',
],
];
$return = $this->classInstance
->allowStyles('system_menu_block:main', $plugin_definition);
$this
->assertFalse($return);
}
public function testAllowStylesInlineBlocks() {
$plugin_definition = [
'exclude' => [
'block_type',
],
];
$return = $this->classInstance
->allowStyles('inline_block:block_type', $plugin_definition);
$this
->assertFalse($return);
}
public function testExclude($plugin_id, $expected) {
$plugin_definition = [];
if ($plugin_id) {
$plugin_definition = [
'exclude' => [
$plugin_id,
],
];
}
$return = $this->classInstance
->exclude('basic_block', $plugin_definition);
$this
->assertEquals($expected, $return);
}
public function excludeProvider() {
return [
'No exclude options are passed' => [
FALSE,
FALSE,
],
'Exclude basic_block' => [
'basic_block',
TRUE,
],
'Exclude a block that is not the current one' => [
'wrong_block',
FALSE,
],
'Exclude all derivatives of a base_plugin_id' => [
'basic_block:*',
TRUE,
],
];
}
public function testIncludeOnly($plugin_id, $expected) {
$plugin_definition = [];
if ($plugin_id) {
$plugin_definition = [
'include' => [
$plugin_id,
],
];
}
$return = $this->classInstance
->includeOnly('basic_block', $plugin_definition);
$this
->assertEquals($expected, $return);
}
public function includeOnlyProvider() {
return [
'No include options are passed' => [
NULL,
TRUE,
],
'Include basic_block' => [
'basic_block',
TRUE,
],
'Include only a sample_block' => [
'wrong_block',
FALSE,
],
'Include all derivatives of a base_plugin_id' => [
'basic_block:*',
TRUE,
],
];
}
public function testGetBlockContentBundle() {
$entity = $this
->prophesize(EntityInterface::class);
$entity
->bundle()
->willReturn('basic_custom_block');
$this->entityRepository
->loadEntityByUuid('block_content', 'uuid-1234')
->willReturn($entity
->reveal());
$block = $this
->prophesize(Block::class);
$block
->getPlugin()
->willReturn($this->blockPlugin
->reveal());
$blockForm = $this
->prophesize(BlockForm::class);
$blockForm
->getEntity()
->willReturn($block
->reveal());
$this->formState
->getFormObject()
->willReturn($blockForm
->reveal());
$bundle = $this->classInstance
->getBlockContentBundle($this->formState
->reveal());
$this
->assertEquals('basic_custom_block', $bundle);
}
}