class PdbBlockTest in Decoupled Blocks 8
@coversDefaultClass \Drupal\pdb\Plugin\Block\PdbBlock @group pdb
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\pdb\Unit\Plugin\Block\PdbBlockTest
Expanded class hierarchy of PdbBlockTest
File
- tests/
src/ Unit/ Plugin/ Block/ PdbBlockTest.php, line 17
Namespace
Drupal\Tests\pdb\Unit\Plugin\BlockView source
class PdbBlockTest extends UnitTestCase {
/**
* Instance of the Plugin.
*
* @var \Drupal\pdb\Plugin\Block\PdbBlock
*/
protected $plugin;
/**
* Create the setup for constants and plugin instance.
*/
protected function setUp() {
parent::setUp();
// Mock the UUID service.
$uuid = $this
->prophesize(UuidInterface::CLASS);
$uuid
->generate()
->willReturn('uuid');
$context_definition = $this
->prophesize(EntityContextDefinition::CLASS);
// Create a container needed by PdbBlock.
$container = new ContainerBuilder();
$container
->set('uuid', $uuid
->reveal());
\Drupal::setContainer($container);
$configuration = [
'pdb_configuration' => [
'testField' => 'test',
'second_field' => 1,
'formatted_field' => [
'value' => '<p>Formatted text</p>',
],
],
];
$plugin_id = 'pdb';
$plugin_definition = [
'provider' => 'pdb',
'admin_label' => 'test',
'info' => [
'machine_name' => 'example-1',
'add_js' => [
'footer' => [
'example-1.js' => [],
],
],
'settings' => [
'pdb' => [
'settings test',
],
],
'configuration' => [
'testField' => [
'type' => 'textfield',
'default_value' => 'test',
],
'formatted_field' => [
'type' => 'text_format',
'allowed_formats' => [
'basic_html',
],
],
],
],
'context_definitions' => [
'entity' => $context_definition
->reveal(),
],
];
// Create a new instance from the Abstract Class.
$anonymous_class_from_abstract = new class($configuration, $plugin_id, $plugin_definition) extends PdbBlock {
public function returnThis() {
return $this;
}
public function attachFramework(array $component) {
return [
'drupalSettings' => [
'pdb' => [
'webcomponents' => [],
],
],
];
}
public function attachPageHeader(array $component) {
return [
'page_attachment',
];
}
protected function getContextsValues(array $contexts) {
return [
'context_key' => 'context_value',
];
}
protected function getJsContexts(array $contexts) {
return [
'context_key' => 'context_value',
];
}
protected function addContextAssignmentElement(ContextAwarePluginInterface $plugin, array $contexts) {
return 'context';
}
};
$this->plugin = $anonymous_class_from_abstract
->returnThis();
// Create a translation stub for the t() method
$translator = $this
->getStringTranslationStub();
$this->plugin
->setStringTranslation($translator);
}
/**
* Tests the build() method.
*/
public function testBuild() {
$expected = [
'#attached' => [
'drupalSettings' => [
'pdb' => [
'settings test',
'webcomponents' => [],
'configuration' => [
'uuid' => [
'testField' => 'test',
'second_field' => 1,
'formatted_field' => [
'value' => '<p>Formatted text</p>',
],
],
],
'contexts' => [
'context_key' => 'context_value',
],
],
],
'pdb/example-1/footer',
'page_attachment',
],
];
$return = $this->plugin
->build();
$this
->assertEquals($expected, $return);
}
/**
* Tests the attachLibraries() method.
*
* @dataProvider attachLibrariesProvider
*/
public function testAttachLibraries($value, $expected) {
$component = [
'machine_name' => 'example-1',
];
$component = array_merge($component, $value);
$return = $this->plugin
->attachLibraries($component);
$this
->assertEquals($expected, $return);
}
/**
* Provider for testAttachLibraries().
*/
public function attachLibrariesProvider() {
return [
[
[
'add_js' => [
'header' => [
'example-1.js' => [],
],
],
],
[
'pdb/example-1/header',
],
],
[
[
'add_css' => [
'header' => [
'example-1.css' => [],
],
],
],
[
'pdb/example-1/header',
],
],
[
[
'add_css' => [
'header' => [
'example-1.css' => [],
],
],
'add_js' => [
'header' => [
'example-1.js' => [],
],
],
],
[
'pdb/example-1/header',
],
],
[
[
'add_js' => [
'footer' => [
'example-1.js' => [],
],
],
],
[
'pdb/example-1/footer',
],
],
[
[
'add_css' => [
'footer' => [
'example-1.css' => [],
],
],
],
[
'pdb/example-1/footer',
],
],
[
[
'add_css' => [
'footer' => [
'example-1.css' => [],
],
],
'add_js' => [
'footer' => [
'example-1.js' => [],
],
],
],
[
'pdb/example-1/footer',
],
],
[
[
'add_css' => [
'header' => [
'example-1.css' => [],
],
'footer' => [
'example-1.css' => [],
],
],
'add_js' => [
'header' => [
'example-1.js' => [],
],
'footer' => [
'example-1.js' => [],
],
],
],
[
'pdb/example-1/header',
'pdb/example-1/footer',
],
],
[
[
'add_css' => [
'header' => [
'css' => [
'example-1.css' => [],
],
'dependencies' => [
'pdb/example-2/header',
],
],
'footer' => [
'css' => [
'example-1.css' => [],
],
'dependencies' => [
'pdb/example-2/footer',
],
],
],
'add_js' => [
'header' => [
'js' => [
'example-1.js' => [],
],
'dependencies' => [
'pdb/example-2/header',
],
],
'footer' => [
'js' => [
'example-1.js' => [],
],
'dependencies' => [
'pdb/example-2/footer',
],
],
],
],
[
'pdb/example-1/header',
'pdb/example-1/footer',
],
],
];
}
/**
* Tests the attachSettings() method.
*/
public function testAttachSettings() {
$component = [
'settings' => [
'pdb' => [
'foobar',
],
],
];
$expected = [
'drupalSettings' => [
'pdb' => [
'foobar',
],
],
];
$return = $this->plugin
->attachSettings($component);
$this
->assertEquals($expected, $return);
}
/**
* Tests the buildConfigurationForm() method.
*/
public function testBuildConfigurationForm() {
$form_state = $this
->createMock(FormStateInterface::CLASS);
$return = $this->plugin
->buildConfigurationForm([], $form_state);
$this
->assertEquals('test', $return['pdb_configuration']['testField']['#default_value']);
$this
->assertEquals('<p>Formatted text</p>', $return['pdb_configuration']['formatted_field']['#default_value']);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PdbBlockTest:: |
protected | property | Instance of the Plugin. | |
PdbBlockTest:: |
public | function | Provider for testAttachLibraries(). | |
PdbBlockTest:: |
protected | function |
Create the setup for constants and plugin instance. Overrides UnitTestCase:: |
|
PdbBlockTest:: |
public | function | Tests the attachLibraries() method. | |
PdbBlockTest:: |
public | function | Tests the attachSettings() method. | |
PdbBlockTest:: |
public | function | Tests the build() method. | |
PdbBlockTest:: |
public | function | Tests the buildConfigurationForm() 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. |