You are here

protected function PdbBlockTest::setUp in Decoupled Blocks 8

Create the setup for constants and plugin instance.

Overrides UnitTestCase::setUp

File

tests/src/Unit/Plugin/Block/PdbBlockTest.php, line 29

Class

PdbBlockTest
@coversDefaultClass \Drupal\pdb\Plugin\Block\PdbBlock @group pdb

Namespace

Drupal\Tests\pdb\Unit\Plugin\Block

Code

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);
}