Use \Drupal consistently in tests

Created on 16 August 2013, about 11 years ago
Updated 25 July 2024, about 1 month ago

Problem/Motivation

Kernel and functional tests should use the \Drupal discovery class even though $this->container is also available.

Proposed resolution

Replace all calls to $this->container->get() with \Drupal::service(), in core module kernel and functional test classes.

Use git grep '\Drupal::' core/modules/*/src/Tests/* to find these. Get a list of files this way: git grep --name-only '\Drupal::' core/modules/*/src/Tests/*

Note

Originally, this issue proposed the exact opposite. After a lot of discussion here and on #2066993: Use \Drupal consistently in tests , it was decided to use \Drupal::service() consistently.

Remaining tasks

file issue per module
fix issues

Related Issues

Initially from #1957142-64: Replace config() with Drupal::config()
#2001206: Replace drupal_container() with Drupal::service()

@todo list of issues
#2067007: Replace \Drupal:: with $this->container->get() in test classes of aggregator module
#2066999: Replace \Drupal:: with $this->container->get() in test classes of system module

Original report by @andypost

Problem/Motivation

SimpleTest TestBase-based tests should use the fixture container, rather than relying on the \Drupal discovery class.

Currently, TestBase assigns the fixture container to \Drupal, since a lot of existing code is still not properly injected.

As more of core is injected, it will be more important to use the fixture for tests, so that we can draw a line of isolation between the test runner and the system under test. This will allow for greater latitude in mocking services and filesystems for core.

Proposed resolution

Replace all calls to \Drupal:: with corresponding $this->container->get(), in core module test classes.
Use git grep '\Drupal::' core/modules/*/src/Tests/* to find these. Get a list of files this way: git grep --name-only '\Drupal::' core/modules/*/src/Tests/*

🌱 Plan
Status

Needs work

Version

11.0 🔥

Component
PHPUnit 

Last updated about 11 hours ago

Created by

🇫🇷France andypost

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024