Categories
CMS Drupal Mastering Development

How do I run a BrowserTestBase functional test with blocks?

I have a module that defines a custom block and places it using a block.block.block_name.yml file that I generated by exporting the placement of the block on a dev site. I’ve unit-tested the block code, but now I want a functional test that will tell me whether the block is correctly placed and rendering to the right users;

<?php

namespace Drupal\Tests\my_module\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\my_module\EnablerService;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\user\Entity\User;

/**
 * Main test class for the my_module module
 *
 * @group my_module
 */

class ImplementationTests extends BrowserTestBase {

  public $defaultTheme = 'bootstrap';

  /**
   * Modules to enable.
   *
   * @var array
   */

  public static $modules = ['block', 'node', 'my_module', 'user'];

  private NodeInterface $node;
  private User $user;

  # from https://www.drupal.org/project/bootstrap/issues/2860072#comment-12287718
  protected function getConfigSchemaExclusions() {
    return array_merge(parent::getConfigSchemaExclusions(), ['bootstrap.settings']);
  }
  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();
  }

  /**
   * some test
   */
  public function test_nothing() {
    $this->assertTrue(true);
  }
}

However, when I run this, I get this error:

Drupal\Tests\implementations\Functional\ImplementationTests::test_hookFormFormIdAlter_CreateImplementationForm
Trying to access array offset on value of type null

/opt/drupal/web/core/lib/Drupal/Core/Block/BlockPluginTrait.php:90
/opt/drupal/web/core/lib/Drupal/Core/Block/BlockPluginTrait.php:74
/opt/drupal/web/core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php:85
/opt/drupal/web/core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php:99
/opt/drupal/web/core/lib/Drupal/Core/Plugin/DefaultSingleLazyPluginCollection.php:55
/opt/drupal/web/core/modules/block/src/BlockPluginCollection.php:34
/opt/drupal/web/core/modules/block/src/Entity/Block.php:156
/opt/drupal/web/core/modules/block/src/Entity/Block.php:166
/opt/drupal/web/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php:291
/opt/drupal/web/core/modules/block/src/Entity/Block.php:344
/opt/drupal/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php:494
/opt/drupal/web/core/lib/Drupal/Core/Entity/EntityStorageBase.php:449
/opt/drupal/web/core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php:263
/opt/drupal/web/core/lib/Drupal/Core/Entity/EntityBase.php:339
/opt/drupal/web/core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php:591
/opt/drupal/web/core/lib/Drupal/Core/Config/ConfigInstaller.php:373
/opt/drupal/web/core/lib/Drupal/Core/Config/ConfigInstaller.php:248
/opt/drupal/web/core/lib/Drupal/Core/Config/ConfigInstaller.php:152
/opt/drupal/web/core/lib/Drupal/Core/ProxyClass/Config/ConfigInstaller.php:75
/opt/drupal/web/core/lib/Drupal/Core/Extension/ModuleInstaller.php:287
/opt/drupal/web/core/lib/Drupal/Core/ProxyClass/Extension/ModuleInstaller.php:83
/opt/drupal/web/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php:464
/opt/drupal/web/core/tests/Drupal/Tests/BrowserTestBase.php:581
/opt/drupal/web/core/tests/Drupal/Tests/BrowserTestBase.php:400
/opt/drupal/web/modules/custom/implementations/tests/src/Functional/ImplementationTests.php:40
/opt/drupal/vendor/phpunit/phpunit/src/Framework/TestResult.php:685

Looking at the first file in the stack (/opt/drupal/web/core/lib/Drupal/Core/Block/BlockPluginTrait.php:90), I see this:

 80   /**
 81    * Returns generic default configuration for block plugins.
 82    *
 83    * @return array
 84    *   An associative array with the default configuration.
 85    */
 86   protected function baseConfigurationDefaults() {
 87     return [
 88       'id' => $this->getPluginId(),
 89       'label' => '',
 90       'provider' => $this->pluginDefinition['provider'],
 91       'label_display' => BlockPluginInterface::BLOCK_LABEL_VISIBLE,
 92     ];
 93   }

My guess is that BrowserTestBase needs some additional code to properly set up the blocks module, but I’m not sure what else I need to do. Any ideas?

Leave a Reply

Your email address will not be published. Required fields are marked *