View source
<?php
namespace Drupal\Tests\block_style_plugins\Unit\Plugin;
use Drupal\Tests\UnitTestCase;
use Drupal\block_style_plugins\Plugin\BlockStyle;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
class BlockStyleTest extends UnitTestCase {
protected $entityTypeManager;
protected $formState;
protected $plugin;
protected function setUp() : void {
parent::setUp();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->formState = $this
->prophesize(FormStateInterface::class);
$configuration = [];
$plugin_id = 'block_style_plugins';
$plugin_definition = [
'provider' => 'block_style_plugins',
'form' => [
'test_field' => [
'#type' => 'textfield',
'#title' => 'this is a title',
'#default_value' => 'default text',
],
'second_field' => [
'#type' => 'checkbox',
'#title' => 'Checkbox title',
'#default_value' => 1,
],
'third_field' => [
'#type' => 'textfield',
'#title' => 'Third Box',
],
],
];
$this->plugin = new BlockStyle($configuration, $plugin_id, $plugin_definition, $this->entityTypeManager
->reveal());
$translator = $this
->getStringTranslationStub();
$this->plugin
->setStringTranslation($translator);
}
public function testDefaultConfiguration() {
$expected = [
'test_field' => 'default text',
'second_field' => 1,
];
$default = $this->plugin
->defaultConfiguration();
$this
->assertEquals($expected, $default);
}
public function testBuildConfigurationForm() {
$expected = [
'test_field' => [
'#type' => 'textfield',
'#title' => 'this is a title',
'#default_value' => 'default text',
],
'second_field' => [
'#type' => 'checkbox',
'#title' => 'Checkbox title',
'#default_value' => 1,
],
'third_field' => [
'#type' => 'textfield',
'#title' => 'Third Box',
'#default_value' => 'user set value',
],
];
$reflectionObject = new \ReflectionObject($this->plugin);
$property = $reflectionObject
->getProperty('configuration');
$property
->setAccessible(TRUE);
$property
->setValue($this->plugin, [
'third_field' => 'user set value',
]);
$form = [];
$return = $this->plugin
->buildConfigurationForm($form, $this->formState
->reveal());
$this
->assertEquals($expected, $return);
}
public function testThemeSuggestion() {
$block = $this
->prophesize(ConfigEntityInterface::class);
$storage = $this
->prophesize(EntityStorageInterface::class);
$storage
->load(1)
->willReturn($block
->reveal());
$this->entityTypeManager
->getStorage('block')
->willReturn($storage
->reveal());
$block
->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
->willReturn([
'class1',
'class2',
]);
$reflectionObject = new \ReflectionObject($this->plugin);
$property = $reflectionObject
->getProperty('pluginDefinition');
$property
->setAccessible(TRUE);
$property
->setValue($this->plugin, [
'template' => 'custom_template',
]);
$suggestions = [];
$variables = [
'elements' => [
'#id' => 1,
],
];
$expected = [
'custom_template',
];
$return = $this->plugin
->themeSuggestion($suggestions, $variables);
$this
->assertEquals($expected, $return);
}
}