Skip to main content

Inside the Pyramid: Hiding Entities from Your Domain Code

Photo of Francesco Placella
Francesco Placella - Senior Architect | Technical Lead
December 2, 2026
Francesco Placella, Tag1 Senior Architect and Technical Lead, continues the four-part Inside the Pyramid series with a small newsroom module showing how an article interface, a bundle class, a thin hook, and a Unit test fit together without the domain code ever touching an entity.

This is the third post in Inside the Pyramid, a four-part series on reshaping Drupal’s test suite so it runs faster and costs less. The series comes from my DrupalCon Rotterdam 2026 session and supports the initiative to adopt the Test Pyramid approach in Drupal core, which is looking for contributors. The first post, The Ideal Shape for Drupal’s Test Suite, looked at how the suite got its current shape and why the Test Pyramid is a better target. The second, Guiding Principles for a Faster Drupal Test Suite, laid out the principles behind the guidelines I propose. This one shows what those principles look like in code, using a small newsroom module.

Articles, Not Nodes

Everything starts from an article interface. Note that it does not extend NodeInterface: it is an interface speaking our language.

interface ArticleInterface {
  public function getHeadline(): string;
  public function getPublicationTime(): int;
  public function getLastReviewTime(): ?int;
  public function isEvergreen(): bool;
}

The implementation, of course, relies on an entity: it is a bundle class. Whenever a node is loaded and it is an article, this class is instantiated and it implements our interface, which knows nothing about nodes, entities, or fields, so the rest of our code doesn’t need to either.

#[Bundle(
  entityType: 'node',
  bundle: 'article',
)]
class Article extends Node implements ArticleInterface {
  public function getPublicationTime(): int {
    return (int) $this->get('field_publication_date')->value;
  }
  public function getLastReviewTime(): ?int {
    $value = $this->get('field_last_review_date')->value;
    return $value !== NULL ? (int) $value : NULL;
  }
  // ...
}

Hooks are an integration point, so they do know about entities, but this is an exception, not the rule. Here is a very thin hook implementation performing an Entity Access check (only editors should see the notice) and delegating all the rest of the logic to a service working with articles, not nodes:

#[Hook('node_view')]
public function nodeView(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, string $view_mode): void {
  if (!$node instanceof ArticleInterface || $view_mode !== 'full') {
    return;
  }
  $access = $node->access('update', NULL, TRUE);
  if ($access->isAllowed()) {
    $build['newsroom_review_notice'] = $this->articleViewBuilder->buildReviewNotice($node);
  }
  // ...
}

The view builder only deals with articles and reaches out to another service working the same way: just look at the use section and you’ll notice that there is no trace of Entity/Field system classes. Here is the relevant bit of the review policy:

class ReviewPolicy {
  // ...
  public function getReviewDueTime(ArticleInterface $article): int {
    $interval = $article->isEvergreen() ? static::EVERGREEN_REVIEW_INTERVAL : static::REVIEW_INTERVAL;
    $reference = $article->getLastReviewTime() ?? $article->getPublicationTime();
    return $reference + $interval;
  }
  public function needsReview(ArticleInterface $article): bool {
    return $this->getReviewDueTime($article) <= $this->time->getCurrentTime();
  }
}

Let’s have a look at the tests now. As you can see, the setup of the review policy test is very thin: we do not need to mock much and we can exercise the whole logic without ever instantiating an entity. Note the TimeInterface stub providing a test double for the time service: as we mentioned before, this is perfectly fine, since there is in practice zero chance of introducing a mismatch with the real implementation. As a matter of fact, we are making no assumptions about the internal logic (no expectation configured): we are just stubbing its behavior based on the public contract.

#[CoversClass(ReviewPolicy::class)]
#[Group('newsroom')]
class ReviewPolicyTest extends UnitTestCase {
  protected const int NOW = 1_790_000_000;
  protected ReviewPolicy $policy;
  protected function setUp(): void {
    parent::setUp();
    $time = $this->prophesize(TimeInterface::class);
    $time->getCurrentTime()->willReturn(static::NOW);
    $this->policy = new ReviewPolicy($time->reveal());
  }
  public function testReviewedArticle(): void {
    $article = new TestArticle(
      published: static::NOW - 2 * ReviewPolicy::REVIEW_INTERVAL,
      reviewed: static::NOW - 86400,
    );
    $this->assertFalse($this->policy->needsReview($article));
  }
  // ...
}

You may have noticed that we are relying on a trick here: TestArticle is a fake implementation of ArticleInterface that doesn’t depend on the Entity system, so it is trivial to instantiate:

class TestArticle implements ArticleInterface {
  public function __construct(
    protected readonly int $published,
    protected readonly ?int $reviewed = NULL,
    protected readonly bool $evergreen = FALSE,
    protected readonly string $headline = 'Headline',
  ) {}
  public function getHeadline(): string {
    return $this->headline;
  }
  // ...
}

This is an example of a sociable Unit test, one where additional real objects (collaborators) are instantiated alongside the tested class, as opposed to solitary Unit tests, where everything aside from the tested class is a test double.

The view builder test follows the same pattern. As long as you can hide entities, you can Unit-test all your domain-specific code and get a big performance boost, together with all the other benefits we talked about before.

Bundle Classes vs Entity Wrappers

Bundle classes are very useful when you are writing custom code; however, when you are writing core or contrib code, dealing with bundle-specific logic is much less common. How can we hide entities in our code and write domain-specific code in this scenario? An effective way to achieve this is via an entity wrapper: an object wrapping an entity and implementing a domain-specific interface. All the module code then interacts with the wrapper object(s) and does not need to know about the Entity system, aside from when persistence is involved.

Up Next

Entities do get stored and loaded, and persistence needs Integration tests. In the final post, I cover how the repository pattern keeps storage details out of your domain logic, how to treat plugin classes as adapters, and how the principles discussed previously apply to real Drupal core code.

Want a faster, leaner test suite for your Drupal project? Talk to our team.

Work With Tag1

Be in Capable Digital Hands

Gain confidence and clarity with expert guidance that turns complex technical decisions into clear, informed choices—without the uncertainty.