trait FunctionalTestSetupTrait (View source)

Defines a trait for shared functional test setup functionality.

Traits

SessionTestTrait

Provides a method to refresh in-memory configuration and state information.

Properties

protected AccountInterface $rootUser

The "#1" admin user.

protected Classloader $classLoader

The class loader to use for installation and initialization of setup.

protected bool $apcuEnsureUniquePrefix

The flag to set 'apcu_ensure_unique_prefix' setting.

Methods

refreshVariables()

Refreshes in-memory configuration and state information.

prepareSettings()

Prepares site settings and services before installation.

writeSettings(array $settings)

Rewrites the settings.php file of the test site.

setContainerParameter(string $name, string $value)

Changes parameters in the services.yml file.

rebuildContainer()

Rebuilds \Drupal::getContainer().

resetAll()

Resets all data structures after having enabled new modules.

Request
prepareRequestForGenerator(bool $clean_urls = TRUE, array $override_server_vars = [])

Creates a mock request and sets it on the generator.

doInstall()

Execute the non-interactive installer.

initSettings()

Initialize settings created during install.

initConfig(ContainerInterface $container)

Initialize various configurations post-installation.

initUserSession()

Initializes user 1 for the site to be installed.

ContainerInterface
initKernel(Request $request)

Initializes the kernel after installation.

installDefaultThemeFromClassProperty(ContainerInterface $container)

Installs the default theme defined by static::$defaultTheme when needed.

installModulesFromClassProperty(ContainerInterface $container)

Install modules defined by static::$modules.

rebuildAll()

Resets and rebuilds the environment after setup.

array
installParameters()

Returns the parameters that will be used when Simpletest installs Drupal.

setupBaseUrl()

Sets up the base URL based upon the environment variable.

prepareEnvironment()

Prepares the current environment for running the test.

Tasks[]
getDatabaseTypes()

Returns all supported database driver installer objects.

Details

protected refreshVariables()

Refreshes in-memory configuration and state information.

Useful after a page request is made that changes configuration or state in a different thread.

In other words calling a settings page with $this->submitForm() with a changed value would update configuration to reflect that change, but in the thread that made the call (thread running the test) the changed values would not be picked up.

This method clears the cache and loads a fresh copy.

protected prepareSettings()

Prepares site settings and services before installation.

protected writeSettings(array $settings)

Rewrites the settings.php file of the test site.

Parameters

array $settings

An array of settings to write out, in the format expected by drupal_rewrite_settings().

See also

drupal_rewrite_settings()

protected setContainerParameter(string $name, string $value)

Changes parameters in the services.yml file.

Parameters

string $name

The name of the parameter.

string $value

The value of the parameter.

protected rebuildContainer()

Rebuilds \Drupal::getContainer().

Use this to update the test process's kernel with a new service container. For example, when the list of enabled modules is changed via the internal browser the test process's kernel has a service container with an out of date module list.

Fix https://www.drupal.org/node/2941757 so that module enable/disable changes are immediately reflected in \Drupal::getContainer(). Until then, tests can invoke this workaround when requiring services from newly enabled modules to be immediately available in the same request.

See also

\Drupal\Core\Test\TestBase::prepareEnvironment()
\Drupal\Core\Test\TestBase::restoreEnvironment()

protected resetAll()

Resets all data structures after having enabled new modules.

This method is called by FunctionalTestSetupTrait::rebuildAll() after enabling the requested modules. It must be called again when additional modules are enabled later.

See also

FunctionalTestSetupTrait::rebuildAll
\Drupal\Tests\BrowserTestBase::installDrupal()

protected Request prepareRequestForGenerator(bool $clean_urls = TRUE, array $override_server_vars = [])

Creates a mock request and sets it on the generator.

This is used to manipulate how the generator generates paths during tests. It also ensures that calls to $this->drupalGet() will work when running from run-tests.sh because the url generator no longer looks at the global variables that are set there but relies on getting this information from a request object.

Parameters

bool $clean_urls

Whether to mock the request using clean urls.

array $override_server_vars

An array of server variables to override.

Return Value

Request

The mocked request object.

protected doInstall()

Execute the non-interactive installer.

See also

install_drupal()

protected initSettings()

Initialize settings created during install.

protected initConfig(ContainerInterface $container)

Initialize various configurations post-installation.

Parameters

ContainerInterface $container

The container.

protected initUserSession()

Initializes user 1 for the site to be installed.

protected ContainerInterface initKernel(Request $request)

Initializes the kernel after installation.

Parameters

Request $request

Request object.

Return Value

ContainerInterface

The container.

protected installDefaultThemeFromClassProperty(ContainerInterface $container)

Installs the default theme defined by static::$defaultTheme when needed.

To install a test theme outside of the testing environment, add

Parameters

ContainerInterface $container

The container.

Exceptions

Exception

protected installModulesFromClassProperty(ContainerInterface $container)

Install modules defined by static::$modules.

To install test modules outside of the testing environment, add

Parameters

ContainerInterface $container

The container.

protected rebuildAll()

Resets and rebuilds the environment after setup.

protected array installParameters()

Returns the parameters that will be used when Simpletest installs Drupal.

Return Value

array

Array of parameters for use in install_drupal().

See also

install_drupal()
install_state_defaults()

protected setupBaseUrl()

Sets up the base URL based upon the environment variable.

Exceptions

Exception

protected prepareEnvironment()

Prepares the current environment for running the test.

Also sets up new resources for the testing environment, such as the public filesystem and configuration directories.

This method is private as it must only be called once by BrowserTestBase::setUp() (multiple invocations for the same test would have unpredictable consequences) and it must not be callable or overridable by test classes.

protected Tasks[] getDatabaseTypes()

Returns all supported database driver installer objects.

This wraps drupal_get_database_types() for use without a current container.

Return Value

Tasks[]

An array of available database driver installer objects.