View source
<?php
namespace Drupal\Tests\block_style_plugins\Unit\Plugin;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Block\BlockPluginInterface;
class BlockStyleBaseTest extends UnitTestCase {
protected $entityTypeManager;
protected $formState;
protected $blockPlugin;
protected $plugin;
protected function setUp() : void {
parent::setUp();
$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());
$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_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 testDefaultConfiguration() {
$expected = [
'sample_class' => '',
'sample_checkbox' => FALSE,
];
$default = $this->plugin
->defaultConfiguration();
$this
->assertEquals($expected, $default);
}
public function testBuildConfigurationForm() {
$form = [];
$return = $this->plugin
->buildConfigurationForm($form, $this->formState
->reveal());
$this
->assertEquals([], $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
->assertEquals($variables, $return);
$block
->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
->willReturn(FALSE);
$variables = [
'elements' => [
'#id' => 1,
],
];
$return = $this->plugin
->build($variables);
$this
->assertEquals($variables, $return);
$block
->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
->willReturn([
'class1',
'class2',
]);
$variables = [
'elements' => [
'#id' => 1,
],
];
$expected = [
'elements' => [
'#id' => 1,
],
'configuration' => [
'block_styles' => [
'block_style_plugins' => [
'class1',
'class2',
],
],
],
'attributes' => [
'class' => [
'class1',
'class2',
],
],
];
$return = $this->plugin
->build($variables);
$this
->assertEquals($expected, $return);
$block
->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
->willReturn([
[
'class1',
'class2',
],
]);
$variables = [
'elements' => [
'#id' => 1,
],
];
$expected = [
'elements' => [
'#id' => 1,
],
'configuration' => [
'block_styles' => [
'block_style_plugins' => [
[
'class1',
'class2',
],
],
],
],
'attributes' => [
'class' => [
'class1',
'class2',
],
],
];
$return = $this->plugin
->build($variables);
$this
->assertEquals($expected, $return);
$block
->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
->willReturn([
'class1',
1,
'class2',
0,
]);
$variables = [
'elements' => [
'#id' => 1,
],
];
$expected = [
'elements' => [
'#id' => 1,
],
'configuration' => [
'block_styles' => [
'block_style_plugins' => [
'class1',
1,
'class2',
0,
],
],
],
'attributes' => [
'class' => [
'class1',
'class2',
],
],
];
$return = $this->plugin
->build($variables);
$this
->assertEquals($expected, $return);
$configuration = [];
$plugin_id = 'block_style_plugins';
$plugin_definition = [
'provider' => 'block_style_plugins',
'disable_auto_classes' => TRUE,
];
$plugin = new MockBlockStyleBase($configuration, $plugin_id, $plugin_definition, $this->entityTypeManager
->reveal());
$translator = $this
->getStringTranslationStub();
$plugin
->setStringTranslation($translator);
$block
->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
->willReturn([
'class1',
1,
'class2',
0,
]);
$variables = [
'elements' => [
'#id' => 1,
],
];
$expected = [
'elements' => [
'#id' => 1,
],
'configuration' => [
'block_styles' => [
'block_style_plugins' => [
'class1',
1,
'class2',
0,
],
],
],
];
$return = $plugin
->build($variables);
$this
->assertEquals($expected, $return);
}
public function testGetConfiguration() {
$expected = [
'sample_class' => '',
'sample_checkbox' => FALSE,
];
$this->plugin
->setConfiguration([]);
$return = $this->plugin
->getConfiguration();
$this
->assertEquals($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
->assertEquals($expected, $return);
$expected = [
'sample_class' => 'class_name',
'sample_checkbox' => TRUE,
];
$this->plugin
->setConfiguration($expected);
$return = $this->plugin
->getConfiguration();
$this
->assertEquals($expected, $return);
}
}