Go to file
madmatt 42be37db53 Refactor testsession module to use file-based session state storage.
This is a major refactoring of the testsession module to use a persistent file
storage instead of using $_SESSION storage. The primary reason for this is for
out-of-band tests (e.g. simplifying Behat tests, and testing modules like
silverstripe-resque (https://github.com/stojg/silverstripe-resque)). Testing
the silverstripe-resque module without this is impossible as the PHP code
running the job has been started and loaded into memory long before you started
a testsession.

By default, this will create a TESTS_RUNNING.json file in your webroot, which
means that tests need to be run as a user who has permission to create files
there. In practice, this means your webroot needs to be owned by your webserver
user. The reason we store the file here is that it will show up as a changed
file in version control, so it’s more prominent if developers can’t figure out
why there are issues with database content.

API CHANGES:
- Add persistent file storage (using webroot/TESTS_RUNNING.json) as a base.
- Update TestSessionController to use new TestSessionEnvironment class.
- Moved extension points from TestSessionController to TestSessionEnvironment.
- Moved loadFixtureIntoDb from TestSessionController to TestSessionEnvironment.
- Moved setState from TestSessionController to TestSessionEnvironment.
  - Deprecated the use of TestSessionController::setState()

FIXES:
- Fixes TestSessionRequestFilter to use new TestSessionEnvironment instead of
  $_SESSION.

MINOR:
- Renamed TestSesssionRequestFilter.php to fix spelling error (three ’S’s)
  - Class did not need renaming, just the file itself.
2014-02-09 18:48:25 +13:00
_config Refactor testsession module to use file-based session state storage. 2014-02-09 18:48:25 +13:00
code Refactor testsession module to use file-based session state storage. 2014-02-09 18:48:25 +13:00
css Better default styles, made form extensible 2013-12-10 19:34:35 +01:00
javascript Load test data from database dumps 2013-12-12 14:18:18 +01:00
templates API State setting within session, make tmpdb optional 2013-12-10 19:34:37 +01:00
tests/unit NEW Write PHP code for later inclusion 2013-11-27 19:29:44 +01:00
_config.php Refactor testsession module to use file-based session state storage. 2014-02-09 18:48:25 +13:00
composer.json Fixed framework dependency version 2013-08-13 12:25:14 +02:00
LICENSE Create LICENSE 2013-09-02 21:14:48 +12:00
README.md Added onRegister hook 2013-12-16 23:48:24 +01:00

Browser Test Session Module

Overview

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. Further data can be loaded from YAML fixtures or database dumps.

The module also serves as an initializer for the 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

In order to execute the commands, the environment must be in "dev mode", or you must be logged-in with administrative permissions.

Since the database name is stored as an encrypted cookie, you need to create a secure token for the encryption first:

sake dev/generatesecuretoken

The resulting configuration code needs to be placed in mysite/_config.php.

Usage

You start a test session by manually requesting a certain URL, and interact with it through other URL endpoints.

Commands:

  • dev/testsession: Shows options for starting a test session
  • dev/testsession/start: Sets up test state, most commonly a test database will be constructed, 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.

Parameters for "dev/testsession/start":

  • fixture: Loads a YAML fixture in the format generally accepted by SapphireTest (see fixture format docs). The path should be relative to the webroot.
  • createDatabase: Create a temporary database.
  • createDatabaseTemplate: Path to a database dump to load into a newly created temporary database.
  • database: Set an alternative database name in the current browser session as a cookie. Does not actually create the database, that's usually handled by SapphireTest::create_temp_db(). Note: The database names are limited to a specific naming convention as a security measure: 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.
  • mailer: Subclass of Mailer, typically used to record emails instead of actually sending them.
  • datetime: Sets a simulated date used for all framework operations. Format as "yyyy-MM-dd HH:mm:ss" (Example: "2012-12-31 18:40:59").

Example usage with parameters:

dev/testsession/start?database=ss_tmpdb_1234567&fixture=cms/tests/controller/CMSMainTest.yml