Drupal 8  8.0.2
BrowserTestBase Class Reference
Inheritance diagram for BrowserTestBase:

Public Member Functions

 getSession ($name=NULL)
 
 assertSession ($name=NULL)
 
 installDrupal ()
 
 __sleep ()
 

Static Public Member Functions

static filePreDeleteCallback ($path)
 
static getDatabaseConnection ()
 

Protected Member Functions

 initMink ()
 
 getDefaultDriverInstance ()
 
 registerSessions ()
 
 setUp ()
 
 cleanupEnvironment ()
 
 tearDown ()
 
 prepareRequest ()
 
 drupalGet ($path, array $options=array())
 
 getAbsoluteUrl ($path)
 
 drupalCreateUser (array $permissions=array(), $name=NULL)
 
 drupalCreateRole (array $permissions, $rid=NULL, $name=NULL, $weight=NULL)
 
 checkPermissions (array $permissions)
 
 drupalLogin (AccountInterface $account)
 
 drupalLogout ()
 
 submitForm (array $edit, $submit, $form_html_id=NULL)
 
 getOptions ($select, Element $container=NULL)
 
 installParameters ()
 
 prepareEnvironment ()
 
 writeSettings (array $settings)
 
 rebuildContainer ()
 
 prepareRequestForGenerator ($clean_urls=TRUE, $override_server_vars=array())
 
 resetAll ()
 
 refreshVariables ()
 
 drupalUserIsLoggedIn (UserInterface $account)
 

Protected Attributes

 $classLoader
 
 $siteDirectory
 
 $databasePrefix
 
 $originalSiteDirectory
 
 $timeLimit = 500
 
 $publicFilesDirectory
 
 $privateFilesDirectory
 
 $tempFilesDirectory
 
 $translationFilesDirectory
 
 $kernel
 
 $container
 
 $configImporter
 
 $profile = 'testing'
 
 $loggedInUser = FALSE
 
 $rootUser
 
 $configDirectories = array()
 
 $customTranslations
 
 $minkDefaultDriverClass = '\Behat\Mink\Driver\GoutteDriver'
 
 $minkDefaultDriverArgs
 
 $mink
 
 $runTestInSeparateProcess = TRUE
 
 $preserveGlobalState = FALSE
 

Detailed Description

Provides a test case for functional Drupal tests.

Note that this class does not yet have feature parity with WebTestBase, so WebTestBase should be used where possible. In particular, this class does not yet have the following features:

Tests extending BrowserTestBase must exist in the Drupal namespace and live in the modules/yourmodule/Tests/Functional directory.

See Also

Member Function Documentation

__sleep ( )

Prevents serializing any properties.

Browser tests are run in a separate process. To do this PHPUnit creates a script to run the test. If it fails, the test result object will contain a stack trace which includes the test object. It will attempt to serialize it. Returning an empty array prevents it from serializing anything it should not.

Returns
array An empty array.
See Also
vendor/phpunit/phpunit/src/Util/PHP/Template/TestCaseMethod.tpl.dist
assertSession (   $name = NULL)

Returns WebAssert object.

Parameters
string$name(optional) Name of the session. Defaults to the active session.
Returns
A new web-assert option for asserting the presence of elements with.

References BrowserTestBase\getSession().

Referenced by BrowserTestBase\drupalLogin(), BrowserTestBase\drupalLogout(), BrowserTestBase\getOptions(), BrowserTestBase\submitForm(), BrowserTestBaseTest\testForm(), and BrowserTestBaseTest\testGoTo().

Here is the call graph for this function:

Here is the caller graph for this function:

checkPermissions ( array  $permissions)
protected

Checks whether a given list of permission names is valid.

Parameters
array$permissionsThe permission names to check.
Returns
bool TRUE if the permissions are valid, FALSE otherwise.

References SafeMarkup\format(), and Drupal\service().

Referenced by BrowserTestBase\drupalCreateRole().

Here is the call graph for this function:

Here is the caller graph for this function:

cleanupEnvironment ( )
protected

Clean up the Simpletest environment.

References file_unmanaged_delete_recursive(), Database\getConnection(), and Database\getConnectionInfo().

Referenced by BrowserTestBase\tearDown().

Here is the call graph for this function:

Here is the caller graph for this function:

drupalCreateRole ( array  $permissions,
  $rid = NULL,
  $name = NULL,
  $weight = NULL 
)
protected

Creates a role with specified permissions.

Parameters
array$permissionsArray of permission names to assign to role.
string$rid(optional) The role ID (machine name). Defaults to a random name.
string$name(optional) The label for the role. Defaults to a random string.
int$weight(optional) The weight for the role. Defaults NULL so that entity_create() sets the weight to maximum + 1.
Returns
string Role ID of newly created role, or FALSE if role creation failed.

References BrowserTestBase\checkPermissions(), and SafeMarkup\format().

Referenced by BrowserTestBase\drupalCreateUser().

Here is the call graph for this function:

Here is the caller graph for this function:

drupalCreateUser ( array  $permissions = array(),
  $name = NULL 
)
protected

Creates a user with a given set of permissions.

Parameters
array$permissions(optional) Array of permission names to assign to user. Note that the user always has the default permissions derived from the "authenticated users" role.
string$name(optional) The user name.
Returns
|false A fully loaded user object with passRaw property, or FALSE if account creation fails.

References BrowserTestBase\drupalCreateRole(), and SafeMarkup\format().

Referenced by BrowserTestBaseTest\testGoTo().

Here is the call graph for this function:

Here is the caller graph for this function:

drupalGet (   $path,
array  $options = array() 
)
protected

Retrieves a Drupal path or an absolute path.

Parameters
string$pathDrupal path or URL to load into Mink controlled browser.
array$options(optional) Options to be forwarded to the url generator.
Returns
string The retrieved HTML string, also available as $this->getRawContent()

References Url\fromUri(), BrowserTestBase\getAbsoluteUrl(), BrowserTestBase\getSession(), UrlHelper\isExternal(), BrowserTestBase\prepareRequest(), and BrowserTestBase\refreshVariables().

Referenced by BrowserTestBase\drupalLogin(), BrowserTestBase\drupalLogout(), BrowserTestBaseTest\testForm(), and BrowserTestBaseTest\testGoTo().

Here is the call graph for this function:

Here is the caller graph for this function:

drupalLogin ( AccountInterface  $account)
protected

Logs in a user using the Mink controlled browser.

If a user is already logged in, then the current user is logged out before logging in the specified user.

Please note that neither the current user nor the passed-in user object is populated with data of the logged in user. If you need full access to the user object after logging in, it must be updated manually. If you also need access to the plain-text password of the user (set by drupalCreateUser()), e.g. to log in the same user again, then it must be re-assigned manually. For example:

// Create a user.
$account = $this->drupalCreateUser(array());
$this->drupalLogin($account);
// Load real user object.
$pass_raw = $account->passRaw;
$account = User::load($account->id());
$account->passRaw = $pass_raw;
Parameters
\Drupal\Core\Session\AccountInterface$accountUser object representing the user to log in.
See Also
drupalCreateUser()

References BrowserTestBase\assertSession(), BrowserTestBase\drupalGet(), BrowserTestBase\drupalLogout(), BrowserTestBase\drupalUserIsLoggedIn(), SafeMarkup\format(), BrowserTestBase\getSession(), BrowserTestBase\submitForm(), and t().

Referenced by BrowserTestBaseTest\testGoTo().

Here is the call graph for this function:

Here is the caller graph for this function:

drupalLogout ( )
protected

Logs a user out of the Mink controlled browser and confirms.

Confirms logout by checking the login page.

References BrowserTestBase\assertSession(), and BrowserTestBase\drupalGet().

Referenced by BrowserTestBase\drupalLogin().

Here is the call graph for this function:

Here is the caller graph for this function:

drupalUserIsLoggedIn ( UserInterface  $account)
protected

Returns whether a given user account is logged in.

Parameters
\Drupal\user\UserInterface$accountThe user account object to check.
Returns
bool Return TRUE if the user is logged in, FALSE otherwise.

Referenced by BrowserTestBase\drupalLogin().

Here is the caller graph for this function:

static filePreDeleteCallback (   $path)
static

Ensures test files are deletable within file_unmanaged_delete_recursive().

Some tests chmod generated files to be read only. During BrowserTestBase::cleanupEnvironment() and other cleanup operations, these files need to get deleted too.

Parameters
string$pathThe file path.
getAbsoluteUrl (   $path)
protected

Takes a path and returns an absolute path.

Parameters
string$pathA path from the Mink controlled browser content.
Returns
string The $path with $base_url prepended, if necessary.

Referenced by BrowserTestBase\drupalGet().

Here is the caller graph for this function:

static getDatabaseConnection ( )
static

Returns the database connection to the site running Simpletest.

Returns
The database connection to use for inserting assertions.

References Database\getConnection().

Here is the call graph for this function:

getDefaultDriverInstance ( )
protected

Gets an instance of the default Mink driver.

Returns
Behat Instance of default Mink driver.
Exceptions
\InvalidArgumentExceptionWhen provided default Mink driver class can't be instantiated.

Referenced by BrowserTestBase\initMink().

Here is the caller graph for this function:

getOptions (   $select,
Element  $container = NULL 
)
protected

Helper function to get the options of select field.

Parameters
\Behat\Mink\Element\NodeElement | string$selectName, ID, or Label of select field to assert.
\Behat\Mink\Element\Element$container(optional) Container element to check against. Defaults to current page.
Returns
array Associative array of option keys and values.

References BrowserTestBase\assertSession().

Here is the call graph for this function:

getSession (   $name = NULL)

Returns Mink session.

Parameters
string$name(optional) Name of the session. Defaults to the active session.
Returns
The active Mink session object.

Referenced by BrowserTestBase\assertSession(), BrowserTestBase\drupalGet(), BrowserTestBase\drupalLogin(), and BrowserTestBase\prepareRequest().

Here is the caller graph for this function:

initMink ( )
protected

Initializes Mink sessions.

References BrowserTestBase\getDefaultDriverInstance(), and BrowserTestBase\registerSessions().

Referenced by BrowserTestBase\setUp().

Here is the call graph for this function:

Here is the caller graph for this function:

installDrupal ( )

Installs Drupal into the Simpletest site.

References DrupalKernel\createFromRequest(), FILE_CREATE_DIRECTORY, file_prepare_directory(), SafeMarkup\format(), Settings\initialize(), BrowserTestBase\installParameters(), BrowserTestBase\rebuildContainer(), Drupal\request(), BrowserTestBase\resetAll(), and BrowserTestBase\writeSettings().

Referenced by BrowserTestBase\setUp().

Here is the call graph for this function:

Here is the caller graph for this function:

installParameters ( )
protected

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

See Also
install_drupal()
install_state_defaults()

References Database\getConnectionInfo().

Referenced by BrowserTestBase\installDrupal().

Here is the call graph for this function:

Here is the caller graph for this function:

prepareEnvironment ( )
protected

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.

References StreamWrapperInterface\ALL, TestRunnerKernel\createFromRequest(), drupal_set_time_limit(), FILE_CREATE_DIRECTORY, FILE_MODIFY_PERMISSIONS, file_prepare_directory(), Drupal\service(), and Drupal\unsetContainer().

Referenced by BrowserTestBase\setUp().

Here is the call graph for this function:

Here is the caller graph for this function:

prepareRequest ( )
protected

Prepare for a request to testing site.

The testing site is protected via a SIMPLETEST_USER_AGENT cookie that is checked by drupal_valid_test_ua().

See Also
drupal_valid_test_ua()

References BrowserTestBase\getSession().

Referenced by BrowserTestBase\drupalGet(), and BrowserTestBase\submitForm().

Here is the call graph for this function:

Here is the caller graph for this function:

prepareRequestForGenerator (   $clean_urls = TRUE,
  $override_server_vars = array() 
)
protected

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_urlsWhether to mock the request using clean urls.
array$override_server_varsAn array of server variables to override.
Returns
Request The mocked request object.

Referenced by BrowserTestBase\rebuildContainer().

Here is the caller graph for this function:

rebuildContainer ( )
protected

Rebuilds ::getContainer().

Use this to build a new kernel and service container. For example, when the list of enabled modules is changed via the Mink controlled browser, in which case the test process still contains an old kernel and service container with an old module list.

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

References BrowserTestBase\prepareRequestForGenerator().

Referenced by BrowserTestBase\installDrupal().

Here is the call graph for this function:

Here is the caller graph for this function:

refreshVariables ( )
protected

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.

References Cache\getBins().

Referenced by BrowserTestBase\drupalGet(), BrowserTestBase\resetAll(), and BrowserTestBase\submitForm().

Here is the call graph for this function:

Here is the caller graph for this function:

registerSessions ( )
protected

Registers additional Mink sessions.

Tests wishing to use a different driver or change the default driver should override this method.

// Register a new session that uses the MinkPonyDriver.
$pony = new MinkPonyDriver();
$session = new Session($pony);
$this->mink->registerSession('pony', $session);

Referenced by BrowserTestBase\initMink().

Here is the caller graph for this function:

resetAll ( )
protected

Resets all data structures after having enabled new modules.

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

References Drupal\getContainer(), and BrowserTestBase\refreshVariables().

Referenced by BrowserTestBase\installDrupal().

Here is the call graph for this function:

Here is the caller graph for this function:

setUp ( )
protected

{}

References BrowserTestBase\initMink(), BrowserTestBase\installDrupal(), BrowserTestBase\prepareEnvironment(), and Drupal\request().

Here is the call graph for this function:

submitForm ( array  $edit,
  $submit,
  $form_html_id = NULL 
)
protected

Fills and submits a form.

Parameters
array$editField data in an associative array. Changes the current input fields (where possible) to the values indicated.

A checkbox can be set to TRUE to be checked and should be set to FALSE to be unchecked.

Parameters
string$submitValue of the submit button whose click is to be emulated. For example, t('Save'). The processing of the request depends on this value. For example, a form may have one button with the value t('Save') and another button with the value t('Delete'), and execute different code depending on which one is clicked.
string$form_html_id(optional) HTML ID of the form to be submitted. On some pages there are many identical forms, so just using the value of the submit button is not enough. For example: 'trigger-node-presave-assign-form'. Note that this is not the Drupal $form_id, but rather the HTML ID of the form, which is typically the same thing but with hyphens replacing the underscores.

References BrowserTestBase\assertSession(), BrowserTestBase\prepareRequest(), and BrowserTestBase\refreshVariables().

Referenced by BrowserTestBase\drupalLogin(), and BrowserTestBaseTest\testForm().

Here is the call graph for this function:

Here is the caller graph for this function:

tearDown ( )
protected

{}

References BrowserTestBase\cleanupEnvironment().

Here is the call graph for this function:

writeSettings ( array  $settings)
protected

Rewrites the settings.php file of the test site.

Parameters
array$settingsAn array of settings to write out, in the format expected by drupal_rewrite_settings().
See Also
drupal_rewrite_settings()

Referenced by BrowserTestBase\installDrupal().

Here is the caller graph for this function:

Field Documentation

$preserveGlobalState = FALSE
protected

{}

$runTestInSeparateProcess = TRUE
protected

{}

Browser tests are run in separate processes to prevent collisions between code that may be loaded by tests.


The documentation for this class was generated from the following file: