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;


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 {

   * some test
  public function test_nothing() {

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

Drupal\Tests\my_module\Functional\ImplementationTests::test_nothing 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   }

Here is the contents of my block.block.my_block.yml file:

langcode: en
status: true
    - node
    - bootstrap
id: my_block
theme: bootstrap
region: content
weight: 10
provider: null
plugin: my_module_my_block
  id: my_module_my_block
  label: 'My Block'
  provider: null
  label_display: '0'
    id: node_type
      idea: idea
    negate: false
      node: '@node.node_route_context:node'

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?


In setUp() you need $this->drupalPlaceBlock('block_name');

Possibly other things are wrong - you didn't show the code where the failure occurs so it's hard to say.

  • I've added the contents of the block's yml file, is that what you mean? Regarding your suggestion, the block is placed by the yml file, but regardless, it's crashing even without that drupalPlaceBlock line so I wanted to resolve that first.
    – quant
    Oct 18 '20 at 21:52
  • I just realised I had incorrect class names in the error messages. This was because I modified the error message to produce a mve, that's fixed now.
    – quant
    Oct 18 '20 at 21:56
  • Now you've edited your post and you changed a function name and changed the machine name of the block you're using and you don't show the code for your block plugin so it's not clear you're even doing the right things. If you used drupalPlaceBlock() like I suggested but did not modify it with your new block name, then of course it won't work. If your code works interactively, then drupalPlaceBlock() is the only "special" thing you need to add in the tests. Maybe you should start by using one of the core-provided blocks in your test (instead of your own block) and see if you can make that work.
    – anonymous
    Oct 18 '20 at 22:35
  • Ok so I tried to put together an MVE in response to your comment but in doing that I realised what my mistake was - I overrode the BlockBase constructor and that seems to break the Functional test (but for some reason the dev environment was fine). Regardless of why it behaved the way it did, I think it's probably a bad question and your answer is more likely to help the average visitor, so I'll mark it as the correct one. Sorry about that - I'll try to do an MVE from the get-go next time around.
    – quant
    Oct 19 '20 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.