2012-12-19 14:20:06 +01:00
|
|
|
# Browser Test Session Module
|
|
|
|
|
2022-07-17 23:55:08 +02:00
|
|
|
[![CI](https://github.com/silverstripe/silverstripe-testsession/actions/workflows/ci.yml/badge.svg)](https://github.com/silverstripe/silverstripe-testsession/actions/workflows/ci.yml)
|
2015-04-30 02:32:09 +02:00
|
|
|
|
2012-12-19 14:20:06 +01:00
|
|
|
## Overview
|
|
|
|
|
2014-02-27 03:10:18 +01:00
|
|
|
*IMPORTANT: This module is intended for development and testing, it poses a security risk if used on production servers.*
|
2014-09-03 07:23:57 +02:00
|
|
|
*It's completely possible to allow any user to become an admin, or do other nefarious things, if this is installed on a live site.*
|
2014-02-26 01:24:59 +01:00
|
|
|
|
2012-12-19 14:20:06 +01:00
|
|
|
This module starts a testing session in a browser,
|
|
|
|
in order to test a SilverStripe application in a clean state.
|
|
|
|
Usually the session is started on a fresh database with only default records loaded.
|
2013-12-12 14:18:18 +01:00
|
|
|
Further data can be loaded from YAML fixtures or database dumps.
|
2012-12-19 14:20:06 +01:00
|
|
|
|
2014-02-09 06:38:58 +01:00
|
|
|
The session is persisted in a file which is generated upon starting the session.
|
|
|
|
As long as this file exists, the test session is considered in progress,
|
|
|
|
both in web browsers and command-line execution. By default, the file
|
2014-09-03 07:23:57 +02:00
|
|
|
is stored in the webroot under `assets/TESTS_RUNNING-<id>.json`. The `<id>` value
|
2014-03-01 08:31:22 +01:00
|
|
|
is a random token stored in the browser session, in order to make the
|
|
|
|
test session specific to the executing browser, and allow multiple
|
|
|
|
people using their own test session in the same webroot.
|
2014-02-09 06:38:58 +01:00
|
|
|
|
2018-12-18 02:24:59 +01:00
|
|
|
The module also keeps some metadata about the session state in the database,
|
|
|
|
so that it may be available for the clients as well.
|
|
|
|
E.g. the silverstripe-behat-extension may use it through this module APIs,
|
|
|
|
allowing us to introduce some grey-box testing techniques.
|
|
|
|
|
2012-12-19 14:20:06 +01:00
|
|
|
The module also serves as an initializer for the
|
|
|
|
[SilverStripe Behat Extension](https://github.com/silverstripe-labs/silverstripe-behat-extension/).
|
|
|
|
It is required for Behat because the Behat CLI test runner needs to persist
|
|
|
|
test configuration just for the tested browser connection,
|
|
|
|
available on arbitary URL endpoints. For example,
|
|
|
|
we're setting up a test mailer which writes every email
|
|
|
|
into a temporary database table for inspection by the CLI-based process.
|
|
|
|
|
|
|
|
## Setup
|
|
|
|
|
2014-02-09 06:33:25 +01:00
|
|
|
Simply require the module in a SilverStripe webroot (3.0 or newer):
|
2012-12-19 14:20:06 +01:00
|
|
|
|
2014-09-03 01:20:07 +02:00
|
|
|
composer require --dev silverstripe/behat-extension
|
2012-12-19 14:20:06 +01:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
2012-12-19 15:18:23 +01:00
|
|
|
You start a test session by manually requesting a certain URL,
|
|
|
|
and interact with it through other URL endpoints.
|
|
|
|
|
|
|
|
Commands:
|
|
|
|
|
2013-12-10 18:53:26 +01:00
|
|
|
* `dev/testsession`: Shows options for starting a test session
|
2022-07-17 23:55:08 +02:00
|
|
|
* `dev/testsession/start`: Sets up test state, most commonly a test database will be constructed,
|
2012-12-19 15:18:23 +01:00
|
|
|
and your browser session will be amended to use this database. See "Parameters" documentation below.
|
|
|
|
* `dev/testsession/end`: Removes the test state, and resets to the original database.
|
|
|
|
* `dev/testsession/loadfixture?fixture=<path>`: Loads a fixture into an existing test state.
|
|
|
|
* `dev/testsession/clear`: Empties the test state.
|
2014-02-26 01:24:59 +01:00
|
|
|
* `dev/testsession/browsersessionstate`: Set or unset browser session state (different from test session state).
|
|
|
|
Use query parameters to define states.
|
2012-12-19 15:18:23 +01:00
|
|
|
|
2014-02-09 06:33:25 +01:00
|
|
|
While you can use the interface to set the test session state,
|
|
|
|
it can be useful to set them programmatically through query parameters
|
|
|
|
on "dev/testsession/start":
|
2012-12-19 15:18:23 +01:00
|
|
|
|
2022-07-17 23:55:08 +02:00
|
|
|
* `fixture`: Loads a YAML fixture in the format generally accepted by `SapphireTest`
|
|
|
|
(see [fixture format docs](http://doc.silverstripe.org/en/developer_guides/testing/fixtures/)).
|
2012-12-19 15:18:23 +01:00
|
|
|
The path should be relative to the webroot.
|
2013-12-10 18:53:26 +01:00
|
|
|
* `createDatabase`: Create a temporary database.
|
2015-10-18 03:11:33 +02:00
|
|
|
* `importDatabasePath`: Absolute path to a database dump to load into a newly created temporary database.
|
2015-10-19 04:04:14 +02:00
|
|
|
* `importDatabaseFilename`: File name for a database dump to load, relative to `TestSessionController.database_templates_path`
|
2014-02-19 10:38:32 +01:00
|
|
|
* `requireDefaultRecords`: Include default records as defined on the model classes (in PHP)
|
2022-07-17 23:55:08 +02:00
|
|
|
* `database`: Set an alternative database name in the current
|
|
|
|
browser session as a cookie. Does not actually create the database,
|
2012-12-19 15:18:23 +01:00
|
|
|
that's usually handled by `SapphireTest::create_temp_db()`.
|
|
|
|
Note: The database names are limited to a specific naming convention as a security measure:
|
2013-12-10 18:53:26 +01:00
|
|
|
The "ss_tmpdb" prefix and a random sequence of seven digits.
|
|
|
|
This avoids the user gaining access to other production databases available on the same connection.
|
2012-12-19 15:47:39 +01:00
|
|
|
* `mailer`: Subclass of `Mailer`, typically used to record emails instead of actually sending them.
|
2013-12-10 18:53:26 +01:00
|
|
|
* `datetime`: Sets a simulated date used for all framework operations.
|
2012-12-19 15:47:39 +01:00
|
|
|
Format as "yyyy-MM-dd HH:mm:ss" (Example: "2012-12-31 18:40:59").
|
2014-05-06 06:45:55 +02:00
|
|
|
* `globalTestSession`: Activate test session independently of the current browser session,
|
2022-07-17 23:55:08 +02:00
|
|
|
effectively setting the site into test session mode for all users across different browsers.
|
|
|
|
Only available in "dev" mode. For example, create a global test session in Chrome, then you can share
|
|
|
|
the session data in Firefox. But if you have started a non-global session in a browser before starting
|
2014-09-03 07:23:57 +02:00
|
|
|
a global session somewhere else, that non-global session will take priority in that browser.
|
2012-12-19 15:18:23 +01:00
|
|
|
|
|
|
|
Example usage with parameters:
|
|
|
|
|
|
|
|
dev/testsession/start?database=ss_tmpdb_1234567&fixture=cms/tests/controller/CMSMainTest.yml
|