class BlockStyleBaseTest in Block Style Plugins 8
Same name and namespace in other branches
- 8.2 tests/src/Unit/Plugin/BlockStyleBaseTest.php \Drupal\Tests\block_style_plugins\Unit\Plugin\BlockStyleBaseTest
@coversDefaultClass \Drupal\block_style_plugins\Plugin\BlockStyleBase @group block_style_plugins
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\block_style_plugins\Unit\Plugin\BlockStyleBaseTest
Expanded class hierarchy of BlockStyleBaseTest
File
- tests/
src/ Unit/ Plugin/ BlockStyleBaseTest.php, line 21
Namespace
Drupal\Tests\block_style_plugins\Unit\PluginView source
class BlockStyleBaseTest extends UnitTestCase {
/**
* Mocked entity repository service.
*
* @var \Drupal\Core\Entity\EntityRepository
*/
protected $entityRepository;
/**
* Mocked entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Mocked form state.
*
* @var \Drupal\Core\Form\FormStateInterface
*/
protected $formState;
/**
* Mocked Block Plugin.
*
* @var \Drupal\Core\Block\BlockPluginInterface
*/
protected $blockPlugin;
/**
* Instance of the BlockStyleBase plugin.
*
* @var \Drupal\block_style_plugins\Plugin\BlockStyleBase
*/
protected $plugin;
/**
* Create the setup for constants and configFactory stub.
*/
protected function setUp() {
parent::setUp();
// Stub the Iconset Finder Service.
$this->entityRepository = $this
->prophesize(EntityRepositoryInterface::CLASS);
// Stub the Entity Type Manager.
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::CLASS);
// Form state double.
$this->formState = $this
->prophesize(FormStateInterface::CLASS);
// Block plugin.
$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());
// Create a translation stub for the t() method.
$translator = $this
->getStringTranslationStub();
$this->plugin
->setStringTranslation($translator);
}
/**
* Tests the create method.
*
* @see ::create()
*/
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);
}
/**
* Tests the prepareForm() method.
*
* @see ::prepareForm()
*/
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());
// Check the callback function attached.
$return_callback = $return['actions']['submit']['#submit'][0];
$this
->assertInstanceOf('Drupal\\block_style_plugins\\Plugin\\BlockStyleBase', $return_callback[0]);
$this
->assertEquals('submitForm', $return_callback[1]);
// Check that a block_styles array is set.
$this
->assertArrayHasKey('block_styles', $return);
// Check that styles were set.
$styles = $this->plugin
->getConfiguration();
$expected_styles = [
'sample_class' => '',
'sample_checkbox' => '',
'test_style' => TRUE,
];
$this
->assertArrayEquals($expected_styles, $styles);
// Check third party settings.
$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']);
}
/**
* Tests the defaultConfiguration method.
*
* @see ::defaultConfiguration()
*/
public function testDefaultConfiguration() {
$expected = [
'sample_class' => '',
'sample_checkbox' => FALSE,
];
$default = $this->plugin
->defaultConfiguration();
$this
->assertArrayEquals($expected, $default);
}
/**
* Tests the buildConfigurationForm method.
*
* @see ::buildConfigurationForm()
*/
public function testBuildConfigurationForm() {
$form = [];
$return = $this->plugin
->buildConfigurationForm($form, $this->formState
->reveal());
$this
->assertArrayEquals([], $return);
}
/**
* Tests the formAlter method.
*
* @see ::formAlter()
*/
public function testFormAlter() {
$form = [
'test',
];
$return = $this->plugin
->formAlter($form, $this->formState
->reveal());
$this
->assertArrayEquals($form, $return);
}
/**
* Tests the validateForm method.
*
* @see ::validateForm()
*/
public function testValidateForm() {
$form = [
'third_party_settings' => [
'block_style_plugins' => [
$this->plugin
->getPluginId() => [],
],
],
];
$return = $this->plugin
->validateForm($form, $this->formState
->reveal());
$this
->assertNull($return);
}
/**
* Tests the submitForm method.
*
* @see ::submitForm()
*/
public function testSubmitForm() {
$form = [
'third_party_settings' => [
'block_style_plugins' => [
$this->plugin
->getPluginId() => [],
],
],
];
$return = $this->plugin
->submitForm($form, $this->formState
->reveal());
$this
->assertNull($return);
}
/**
* Tests the build method.
*
* @see ::build()
* @TODO Create a provider so that more combinations can be tested.
*/
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());
// No element ID is passed through the variables.
$variables = [];
$return = $this->plugin
->build($variables);
$this
->assertArrayEquals($variables, $return);
// No styles attached to the block.
$block
->getThirdPartySetting('block_style_plugins', 'block_style_plugins')
->willReturn(FALSE);
$variables = [
'elements' => [
'#id' => 1,
],
];
$return = $this->plugin
->build($variables);
$this
->assertArrayEquals($variables, $return);
// Return the third party styles set in the plugin.
$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);
// Don't set a class for integers.
$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);
}
/**
* Tests the getConfiguration method.
*
* @see ::getConfiguration()
*/
public function testGetConfiguration() {
$expected = [
'sample_class' => '',
'sample_checkbox' => FALSE,
];
$this->plugin
->setConfiguration([]);
$return = $this->plugin
->getConfiguration();
$this
->assertArrayEquals($expected, $return);
}
/**
* Tests the setConfiguration method.
*
* @see ::setConfiguration()
*/
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);
// Overwrite styles.
$expected = [
'sample_class' => 'class_name',
'sample_checkbox' => TRUE,
];
$this->plugin
->setConfiguration($expected);
$return = $this->plugin
->getConfiguration();
$this
->assertArrayEquals($expected, $return);
}
/**
* Tests the exclude method.
*
* @see ::exclude()
*
* @dataProvider excludeProvider
*/
public function testExclude($plugin, $bundle, $expected) {
// Stub the blockPlugin.
$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);
}
/**
* Provider for testExclude()
*/
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,
],
];
}
/**
* Tests the includeOnly method.
*
* @see ::includeOnly()
*
* @dataProvider includeOnlyProvider
*/
public function testIncludeOnly($plugin, $bundle, $expected) {
// Stub the blockPlugin.
$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);
}
/**
* Provider for testIncludeOnly()
*/
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,
],
];
}
/**
* Tests the setBlockContentBundle method.
*
* @see ::setBlockContentBundle()
*/
public function testSetBlockContentBundle() {
// Stub the blockPlugin.
$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);
}
/**
* Get a protected property on the plugin via reflection.
*
* @param string $property
* Property on instance.
*
* @return mixed
* Return the value of the protected property.
*/
public function getProtectedProperty($property) {
$reflection = new \ReflectionClass($this->plugin);
$reflection_property = $reflection
->getProperty($property);
$reflection_property
->setAccessible(TRUE);
return $reflection_property
->getValue($this->plugin);
}
/**
* Sets a protected property on the plugin via reflection.
*
* @param string $property
* Property on instance being modified.
* @param mixed $value
* New value of the property being modified.
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BlockStyleBaseTest:: |
protected | property | Mocked Block Plugin. | |
BlockStyleBaseTest:: |
protected | property | Mocked entity repository service. | |
BlockStyleBaseTest:: |
protected | property | Mocked entity type manager service. | |
BlockStyleBaseTest:: |
protected | property | Mocked form state. | |
BlockStyleBaseTest:: |
protected | property | Instance of the BlockStyleBase plugin. | |
BlockStyleBaseTest:: |
public | function | Provider for testExclude() | |
BlockStyleBaseTest:: |
public | function | Get a protected property on the plugin via reflection. | |
BlockStyleBaseTest:: |
public | function | Provider for testIncludeOnly() | |
BlockStyleBaseTest:: |
public | function | Sets a protected property on the plugin via reflection. | |
BlockStyleBaseTest:: |
protected | function |
Create the setup for constants and configFactory stub. Overrides UnitTestCase:: |
|
BlockStyleBaseTest:: |
public | function | Tests the build method. | |
BlockStyleBaseTest:: |
public | function | Tests the buildConfigurationForm method. | |
BlockStyleBaseTest:: |
public | function | Tests the create method. | |
BlockStyleBaseTest:: |
public | function | Tests the defaultConfiguration method. | |
BlockStyleBaseTest:: |
public | function | Tests the exclude method. | |
BlockStyleBaseTest:: |
public | function | Tests the formAlter method. | |
BlockStyleBaseTest:: |
public | function | Tests the getConfiguration method. | |
BlockStyleBaseTest:: |
public | function | Tests the includeOnly method. | |
BlockStyleBaseTest:: |
public | function | Tests the prepareForm() method. | |
BlockStyleBaseTest:: |
public | function | Tests the setBlockContentBundle method. | |
BlockStyleBaseTest:: |
public | function | Tests the setConfiguration method. | |
BlockStyleBaseTest:: |
public | function | Tests the submitForm method. | |
BlockStyleBaseTest:: |
public | function | Tests the validateForm method. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |