2012-12-19 14:20:06 +01:00
|
|
|
<?php
|
2012-12-19 15:18:23 +01:00
|
|
|
/**
|
|
|
|
* Requires PHP's mycrypt extension in order to set the database name as an encrypted cookie.
|
|
|
|
*/
|
2012-12-19 14:20:06 +01:00
|
|
|
class TestSessionController extends Controller {
|
|
|
|
|
2013-04-09 01:08:34 +02:00
|
|
|
private static $allowed_actions = array(
|
2012-12-19 14:20:06 +01:00
|
|
|
'index',
|
|
|
|
'start',
|
2012-12-19 15:18:23 +01:00
|
|
|
'set',
|
2012-12-19 14:20:06 +01:00
|
|
|
'end',
|
2012-12-19 15:18:23 +01:00
|
|
|
'clear',
|
2014-02-26 01:24:59 +01:00
|
|
|
'browsersessionstate',
|
2013-12-10 18:53:26 +01:00
|
|
|
'StartForm',
|
|
|
|
'ProgressForm',
|
2012-12-19 14:20:06 +01:00
|
|
|
);
|
|
|
|
|
2013-06-07 01:36:55 +02:00
|
|
|
private static $alternative_database_name = -1;
|
|
|
|
|
2013-12-12 14:18:18 +01:00
|
|
|
/**
|
|
|
|
* @var String Absolute path to a folder containing *.sql dumps.
|
|
|
|
*/
|
|
|
|
private static $database_templates_path;
|
|
|
|
|
2014-02-09 06:33:43 +01:00
|
|
|
/**
|
|
|
|
* @var TestSessionEnvironment
|
|
|
|
*/
|
|
|
|
protected $environment;
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
|
2014-03-03 04:19:38 +01:00
|
|
|
$this->environment = Injector::inst()->get('TestSessionEnvironment');
|
2014-02-09 06:33:43 +01:00
|
|
|
}
|
|
|
|
|
2012-12-19 14:20:06 +01:00
|
|
|
public function init() {
|
|
|
|
parent::init();
|
2013-12-18 16:07:37 +01:00
|
|
|
|
|
|
|
$this->extend('init');
|
2012-12-19 14:20:06 +01:00
|
|
|
|
2012-12-19 14:30:18 +01:00
|
|
|
$canAccess = (
|
|
|
|
!Director::isLive()
|
2013-12-12 01:08:29 +01:00
|
|
|
&& (Director::isDev() || Director::isTest() || Director::is_cli() || Permission::check("ADMIN"))
|
2012-12-19 14:30:18 +01:00
|
|
|
);
|
2012-12-19 14:20:06 +01:00
|
|
|
if(!$canAccess) return Security::permissionFailure($this);
|
2013-12-12 14:18:18 +01:00
|
|
|
|
|
|
|
Requirements::javascript('framework/thirdparty/jquery/jquery.js');
|
|
|
|
Requirements::javascript('testsession/javascript/testsession.js');
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
2012-12-19 15:18:23 +01:00
|
|
|
|
|
|
|
public function Link($action = null) {
|
|
|
|
return Controller::join_links(Director::baseUrl(), 'dev/testsession', $action);
|
|
|
|
}
|
2013-12-10 18:53:26 +01:00
|
|
|
|
|
|
|
public function index() {
|
2014-02-09 06:33:43 +01:00
|
|
|
if($this->environment->isRunningTests()) {
|
2013-12-10 18:53:26 +01:00
|
|
|
return $this->renderWith('TestSession_inprogress');
|
|
|
|
} else {
|
|
|
|
return $this->renderWith('TestSession_start');
|
|
|
|
}
|
|
|
|
}
|
2012-12-19 14:20:06 +01:00
|
|
|
|
|
|
|
/**
|
2014-02-04 23:38:22 +01:00
|
|
|
* Start a test session. If you wish to extend how the test session is started (and add additional test state),
|
|
|
|
* then take a look at {@link TestSessionEnvironment::startTestSession()} and
|
|
|
|
* {@link TestSessionEnvironment::applyState()} to see the extension points.
|
2012-12-19 14:20:06 +01:00
|
|
|
*/
|
2013-12-10 18:53:26 +01:00
|
|
|
public function start() {
|
|
|
|
$params = $this->request->requestVars();
|
2014-02-04 23:38:22 +01:00
|
|
|
|
2014-03-01 08:31:22 +01:00
|
|
|
$generator = Injector::inst()->get('RandomGenerator');
|
|
|
|
$id = substr($generator->randomToken(), 0, 10);
|
|
|
|
Session::set('TestSessionId', $id);
|
|
|
|
|
2014-02-04 23:38:22 +01:00
|
|
|
// Convert datetime from form object into a single string
|
|
|
|
$params = $this->fixDatetimeFormField($params);
|
|
|
|
|
|
|
|
// Remove unnecessary items of form-specific data from being saved in the test session
|
|
|
|
$params = array_diff_key(
|
|
|
|
$params,
|
|
|
|
array(
|
|
|
|
'action_set' => true,
|
|
|
|
'action_start' => true,
|
|
|
|
'SecurityID' => true,
|
|
|
|
'url' => true,
|
|
|
|
'flush' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2014-03-01 08:31:22 +01:00
|
|
|
$this->environment->startTestSession($params, $id);
|
2012-12-19 15:18:23 +01:00
|
|
|
|
2013-12-10 18:53:26 +01:00
|
|
|
return $this->renderWith('TestSession_inprogress');
|
2012-12-19 15:18:23 +01:00
|
|
|
}
|
2012-12-19 14:20:06 +01:00
|
|
|
|
2014-02-26 01:24:59 +01:00
|
|
|
/**
|
|
|
|
* Set $_SESSION state for the current browser session.
|
|
|
|
*/
|
|
|
|
public function browsersessionstate($request) {
|
|
|
|
if(!$this->environment->isRunningTests()) {
|
|
|
|
throw new LogicException("No test session in progress.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$newSessionStates = array_diff_key($request->getVars(), array('url' => true));
|
|
|
|
if(!$newSessionStates) {
|
|
|
|
throw new LogicException('No query parameters detected');
|
|
|
|
}
|
|
|
|
|
|
|
|
$sessionStates = (array)Session::get('_TestSessionController.BrowserSessionState');
|
|
|
|
|
|
|
|
foreach($newSessionStates as $k => $v) {
|
|
|
|
Session::set($k, $v);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track which state we're setting so we can unset later in end()
|
|
|
|
Session::set('_TestSessionController.BrowserSessionState', array_merge($sessionStates, $newSessionStates));
|
|
|
|
}
|
|
|
|
|
2013-12-10 18:53:26 +01:00
|
|
|
public function StartForm() {
|
2013-12-12 14:18:18 +01:00
|
|
|
$databaseTemplates = $this->getDatabaseTemplates();
|
2013-12-10 18:53:26 +01:00
|
|
|
$fields = new FieldList(
|
|
|
|
new CheckboxField('createDatabase', 'Create temporary database?', 1)
|
|
|
|
);
|
2013-12-12 14:18:18 +01:00
|
|
|
if($databaseTemplates) {
|
|
|
|
$fields->push(
|
2014-02-06 23:39:36 +01:00
|
|
|
$dropdown = new DropdownField('createDatabaseTemplate', false)
|
2013-12-12 14:18:18 +01:00
|
|
|
);
|
2014-02-06 23:39:36 +01:00
|
|
|
|
|
|
|
$dropdown->setSource($databaseTemplates)
|
|
|
|
->setEmptyString('Empty database');
|
2013-12-12 14:18:18 +01:00
|
|
|
}
|
2014-02-19 10:38:32 +01:00
|
|
|
$fields->push(new CheckboxField('requireDefaultRecords', 'Create default data?'));
|
2013-12-10 18:53:26 +01:00
|
|
|
$fields->merge($this->getBaseFields());
|
2013-12-10 16:50:23 +01:00
|
|
|
$form = new Form(
|
|
|
|
$this,
|
2013-12-10 18:53:26 +01:00
|
|
|
'StartForm',
|
|
|
|
$fields,
|
2013-12-10 16:50:23 +01:00
|
|
|
new FieldList(
|
2013-12-10 18:53:26 +01:00
|
|
|
new FormAction('start', 'Start Session')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->extend('updateStartForm', $form);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows state which is allowed to be modified while a test session is in progress.
|
|
|
|
*/
|
|
|
|
public function ProgressForm() {
|
|
|
|
$fields = $this->getBaseFields();
|
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'ProgressForm',
|
|
|
|
$fields,
|
2013-12-10 16:50:23 +01:00
|
|
|
new FieldList(
|
|
|
|
new FormAction('set', 'Set testing state')
|
|
|
|
)
|
|
|
|
);
|
2013-12-10 18:53:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
$form->setFormAction($this->Link('set'));
|
|
|
|
|
|
|
|
$this->extend('updateProgressForm', $form);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getBaseFields() {
|
2014-02-09 06:33:43 +01:00
|
|
|
$testState = $this->environment->getState();
|
2014-02-04 23:38:22 +01:00
|
|
|
|
2013-12-10 18:53:26 +01:00
|
|
|
$fields = new FieldList(
|
2014-02-06 23:39:36 +01:00
|
|
|
$textfield = new TextField('fixture', 'Fixture YAML file path'),
|
2013-12-10 18:53:26 +01:00
|
|
|
$datetimeField = new DatetimeField('datetime', 'Custom date'),
|
|
|
|
new HiddenField('flush', null, 1)
|
|
|
|
);
|
2014-02-06 23:39:36 +01:00
|
|
|
$textfield->setAttribute('placeholder', 'Example: framework/tests/security/MemberTest.yml');
|
2013-12-10 16:50:23 +01:00
|
|
|
$datetimeField->getDateField()
|
|
|
|
->setConfig('dateformat', 'yyyy-MM-dd')
|
|
|
|
->setConfig('showcalendar', true)
|
2013-12-10 18:53:26 +01:00
|
|
|
->setAttribute('placeholder', 'Date (yyyy-MM-dd)');
|
2013-12-10 16:50:23 +01:00
|
|
|
$datetimeField->getTimeField()
|
2013-12-10 18:53:26 +01:00
|
|
|
->setConfig('timeformat', 'HH:mm:ss')
|
|
|
|
->setAttribute('placeholder', 'Time (HH:mm:ss)');
|
2014-02-04 23:38:22 +01:00
|
|
|
$datetimeField->setValue((isset($testState->datetime) ? $testState->datetime : null));
|
2013-12-10 16:50:23 +01:00
|
|
|
|
2013-12-10 18:53:26 +01:00
|
|
|
$this->extend('updateBaseFields', $fields);
|
2013-12-10 16:50:23 +01:00
|
|
|
|
2013-12-10 18:53:26 +01:00
|
|
|
return $fields;
|
2013-12-10 16:50:23 +01:00
|
|
|
}
|
|
|
|
|
2013-06-07 01:22:18 +02:00
|
|
|
public function DatabaseName() {
|
2014-02-04 23:38:22 +01:00
|
|
|
$db = DB::getConn();
|
|
|
|
if(method_exists($db, 'currentDatabase')) return $db->currentDatabase();
|
2013-06-07 01:22:18 +02:00
|
|
|
}
|
|
|
|
|
2014-02-04 23:38:22 +01:00
|
|
|
/**
|
|
|
|
* Updates an in-progress {@link TestSessionEnvironment} object with new details. This could be loading in new
|
|
|
|
* fixtures, setting the mocked date to another value etc.
|
|
|
|
*
|
|
|
|
* @return HTMLText Rendered Template
|
|
|
|
* @throws LogicException
|
|
|
|
*/
|
2013-12-10 18:53:26 +01:00
|
|
|
public function set() {
|
2014-02-09 06:33:43 +01:00
|
|
|
if(!$this->environment->isRunningTests()) {
|
2013-12-10 18:53:26 +01:00
|
|
|
throw new LogicException("No test session in progress.");
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
2012-12-19 15:18:23 +01:00
|
|
|
|
2013-12-10 18:53:26 +01:00
|
|
|
$params = $this->request->requestVars();
|
2014-02-04 23:38:22 +01:00
|
|
|
|
|
|
|
// Convert datetime from form object into a single string
|
|
|
|
$params = $this->fixDatetimeFormField($params);
|
|
|
|
|
|
|
|
// Remove unnecessary items of form-specific data from being saved in the test session
|
|
|
|
$params = array_diff_key(
|
|
|
|
$params,
|
|
|
|
array(
|
|
|
|
'action_set' => true,
|
|
|
|
'action_start' => true,
|
|
|
|
'SecurityID' => true,
|
|
|
|
'url' => true,
|
|
|
|
'flush' => true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2014-02-09 06:33:43 +01:00
|
|
|
$this->environment->updateTestSession($params);
|
2012-12-19 15:18:23 +01:00
|
|
|
|
|
|
|
return $this->renderWith('TestSession_inprogress');
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
|
|
|
|
2013-12-10 18:53:26 +01:00
|
|
|
public function clear() {
|
2014-02-09 06:33:43 +01:00
|
|
|
if(!$this->environment->isRunningTests()) {
|
2013-12-10 18:53:26 +01:00
|
|
|
throw new LogicException("No test session in progress.");
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
2012-12-19 15:18:23 +01:00
|
|
|
|
2013-12-09 13:29:46 +01:00
|
|
|
$this->extend('onBeforeClear');
|
|
|
|
|
2013-12-10 18:53:26 +01:00
|
|
|
if(SapphireTest::using_temp_db()) {
|
|
|
|
SapphireTest::empty_temp_db();
|
|
|
|
}
|
2013-11-27 19:29:44 +01:00
|
|
|
|
|
|
|
if(isset($_SESSION['_testsession_codeblocks'])) {
|
|
|
|
unset($_SESSION['_testsession_codeblocks']);
|
|
|
|
}
|
2012-12-19 15:18:23 +01:00
|
|
|
|
2013-12-09 13:29:46 +01:00
|
|
|
$this->extend('onAfterClear');
|
|
|
|
|
2012-12-19 15:18:23 +01:00
|
|
|
return "Cleared database and test state";
|
|
|
|
}
|
2014-02-04 23:38:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* As with {@link self::start()}, if you want to extend the functionality of this, then look at
|
|
|
|
* {@link TestSessionEnvironent::endTestSession()} as the extension points have moved to there now that the logic
|
|
|
|
* is there.
|
|
|
|
*/
|
2012-12-19 15:18:23 +01:00
|
|
|
public function end() {
|
2014-02-09 06:33:43 +01:00
|
|
|
if(!$this->environment->isRunningTests()) {
|
2013-12-10 18:53:26 +01:00
|
|
|
throw new LogicException("No test session in progress.");
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
|
|
|
|
2014-02-09 06:33:43 +01:00
|
|
|
$this->environment->endTestSession();
|
2014-03-01 08:31:22 +01:00
|
|
|
Session::clear('TestSessionId');
|
2013-12-09 13:29:46 +01:00
|
|
|
|
2014-02-26 01:24:59 +01:00
|
|
|
// Clear out all PHP session states which have been set previously
|
|
|
|
if($sessionStates = Session::get('_TestSessionController.BrowserSessionState')) {
|
|
|
|
foreach($sessionStates as $k => $v) {
|
|
|
|
Session::clear($k);
|
|
|
|
}
|
|
|
|
Session::clear('_TestSessionController');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-19 15:18:23 +01:00
|
|
|
return $this->renderWith('TestSession_end');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isTesting() {
|
|
|
|
return SapphireTest::using_temp_db();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setState($data) {
|
2014-02-04 23:38:22 +01:00
|
|
|
Deprecation::notice('3.1', 'TestSessionController::setState() is no longer used, please use '
|
|
|
|
. 'TestSessionEnvironment instead.');
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
2012-12-19 14:30:18 +01:00
|
|
|
|
2012-12-19 15:18:23 +01:00
|
|
|
/**
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function getState() {
|
2014-02-09 06:33:43 +01:00
|
|
|
$stateObj = $this->environment->getState();
|
2012-12-19 15:18:23 +01:00
|
|
|
$state = array();
|
2014-02-04 23:38:22 +01:00
|
|
|
|
|
|
|
// Convert the stdObject of state into ArrayData
|
|
|
|
foreach($stateObj as $k => $v) {
|
2012-12-19 15:18:23 +01:00
|
|
|
$state[] = new ArrayData(array(
|
2013-11-27 19:29:44 +01:00
|
|
|
'Name' => $k,
|
2013-12-10 16:50:07 +01:00
|
|
|
'Value' => var_export($v, true)
|
2013-11-27 19:29:44 +01:00
|
|
|
));
|
2012-12-19 15:47:39 +01:00
|
|
|
}
|
2012-12-19 14:20:06 +01:00
|
|
|
|
2012-12-19 15:18:23 +01:00
|
|
|
return new ArrayList($state);
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
|
|
|
|
2013-12-12 14:18:18 +01:00
|
|
|
/**
|
|
|
|
* Get all *.sql database files located in a specific path,
|
|
|
|
* keyed by their file name.
|
|
|
|
*
|
|
|
|
* @param String $path Absolute folder path
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getDatabaseTemplates($path = null) {
|
|
|
|
$templates = array();
|
|
|
|
|
|
|
|
if(!$path) {
|
|
|
|
$path = $this->config()->database_templates_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Remove once we can set BASE_PATH through the config layer
|
|
|
|
if($path && !Director::is_absolute($path)) {
|
|
|
|
$path = BASE_PATH . '/' . $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($path && file_exists($path)) {
|
|
|
|
$it = new FilesystemIterator($path);
|
|
|
|
foreach($it as $fileinfo) {
|
|
|
|
if($fileinfo->getExtension() != 'sql') continue;
|
|
|
|
$templates[$fileinfo->getRealPath()] = $fileinfo->getFilename();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $templates;
|
|
|
|
}
|
|
|
|
|
2014-02-04 23:38:22 +01:00
|
|
|
/**
|
|
|
|
* @param $params array The form fields as passed through from ->start() or ->set()
|
|
|
|
* @return array The form fields, after fixing the datetime field if necessary
|
|
|
|
*/
|
|
|
|
private function fixDatetimeFormField($params) {
|
|
|
|
if(isset($params['datetime']) && is_array($params['datetime']) && !empty($params['datetime']['date'])) {
|
|
|
|
// Convert DatetimeField format from array into string
|
|
|
|
$datetime = $params['datetime']['date'];
|
|
|
|
$datetime .= ' ';
|
|
|
|
$datetime .= (@$params['datetime']['time']) ? $params['datetime']['time'] : '00:00:00';
|
|
|
|
$params['datetime'] = $datetime;
|
|
|
|
} else if(isset($params['datetime']) && empty($params['datetime']['date'])) {
|
|
|
|
unset($params['datetime']); // No datetime, so remove the param entirely
|
|
|
|
}
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|