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',
|
2012-12-19 14:20:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
public function init() {
|
|
|
|
parent::init();
|
|
|
|
|
2012-12-19 14:30:18 +01:00
|
|
|
$canAccess = (
|
|
|
|
!Director::isLive()
|
|
|
|
&& (Director::isDev() || Director::is_cli() || Permission::check("ADMIN"))
|
|
|
|
);
|
2012-12-19 14:20:06 +01:00
|
|
|
if(!$canAccess) return Security::permissionFailure($this);
|
|
|
|
}
|
2012-12-19 15:18:23 +01:00
|
|
|
|
|
|
|
public function Link($action = null) {
|
|
|
|
return Controller::join_links(Director::baseUrl(), 'dev/testsession', $action);
|
|
|
|
}
|
2012-12-19 14:20:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start a test session.
|
|
|
|
*/
|
2012-12-19 15:18:23 +01:00
|
|
|
public function start($request) {
|
|
|
|
if(SapphireTest::using_temp_db()) return $this->renderWith('TestSession_inprogress');
|
2012-12-19 14:30:18 +01:00
|
|
|
|
2012-12-19 15:18:23 +01:00
|
|
|
// Database
|
|
|
|
if(!$request->getVar('database')) {
|
|
|
|
// Create a new one with a randomized name
|
|
|
|
$dbname = SapphireTest::create_temp_db();
|
|
|
|
DB::set_alternative_database_name($dbname);
|
|
|
|
}
|
2012-12-19 14:20:06 +01:00
|
|
|
|
2012-12-19 15:18:23 +01:00
|
|
|
$this->setState($request->getVars());
|
|
|
|
|
|
|
|
return $this->renderWith('TestSession_start');
|
|
|
|
}
|
2012-12-19 14:20:06 +01:00
|
|
|
|
2012-12-19 15:18:23 +01:00
|
|
|
public function set($request) {
|
|
|
|
if(!SapphireTest::using_temp_db()) {
|
|
|
|
throw new LogicException(
|
|
|
|
"This command can only be used with a temporary database. "
|
|
|
|
. "Perhaps you should use dev/testsession/start first?"
|
|
|
|
);
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
2012-12-19 15:18:23 +01:00
|
|
|
|
|
|
|
$this->setState($request->getVars());
|
|
|
|
|
|
|
|
return $this->renderWith('TestSession_inprogress');
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
|
|
|
|
2012-12-19 15:18:23 +01:00
|
|
|
public function clear($request) {
|
|
|
|
if(!SapphireTest::using_temp_db()) {
|
|
|
|
throw new LogicException(
|
|
|
|
"This command can only be used with a temporary database. "
|
|
|
|
. "Perhaps you should use dev/testsession/start first?"
|
|
|
|
);
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
2012-12-19 15:18:23 +01:00
|
|
|
|
|
|
|
SapphireTest::empty_temp_db();
|
|
|
|
|
|
|
|
return "Cleared database and test state";
|
|
|
|
}
|
|
|
|
|
|
|
|
public function end() {
|
|
|
|
if(!SapphireTest::using_temp_db()) {
|
|
|
|
throw new LogicException(
|
|
|
|
"This command can only be used with a temporary database. "
|
|
|
|
. "Perhaps you should use dev/testsession/start first?"
|
|
|
|
);
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
|
|
|
|
2012-12-19 15:18:23 +01:00
|
|
|
SapphireTest::kill_temp_db();
|
|
|
|
DB::set_alternative_database_name(null);
|
|
|
|
Session::clear('testsession');
|
2012-12-19 14:20:06 +01:00
|
|
|
|
2012-12-19 15:18:23 +01:00
|
|
|
return $this->renderWith('TestSession_end');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function loadFixtureIntoDb($fixtureFile) {
|
|
|
|
$realFile = realpath(BASE_PATH.'/'.$fixtureFile);
|
|
|
|
$baseDir = realpath(Director::baseFolder());
|
|
|
|
if(!$realFile || !file_exists($realFile)) {
|
|
|
|
throw new LogicException("Fixture file doesn't exist");
|
|
|
|
} else if(substr($realFile,0,strlen($baseDir)) != $baseDir) {
|
|
|
|
throw new LogicException("Fixture file must be inside $baseDir");
|
|
|
|
} else if(substr($realFile,-4) != '.yml') {
|
|
|
|
throw new LogicException("Fixture file must be a .yml file");
|
|
|
|
} else if(!preg_match('/^([^\/.][^\/]+)\/tests\//', $fixtureFile)) {
|
|
|
|
throw new LogicException("Fixture file must be inside the tests subfolder of one of your modules.");
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
2012-12-19 15:18:23 +01:00
|
|
|
|
2013-04-09 01:08:34 +02:00
|
|
|
$factory = Injector::inst()->create('FixtureFactory');
|
2012-12-19 15:18:23 +01:00
|
|
|
$fixture = Injector::inst()->create('YamlFixture', $fixtureFile);
|
2013-04-09 01:08:34 +02:00
|
|
|
$fixture->writeInto($factory);
|
2012-12-19 15:18:23 +01:00
|
|
|
|
|
|
|
Session::add_to_array('testsession.fixtures', $fixtureFile);
|
|
|
|
|
|
|
|
return $fixture;
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
2012-12-19 15:18:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isTesting() {
|
|
|
|
return SapphireTest::using_temp_db();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setState($data) {
|
|
|
|
// Database
|
|
|
|
$dbname = (isset($data['database'])) ? $data['database'] : null;
|
|
|
|
if($dbname) {
|
|
|
|
// Set existing one, assumes it already has been created
|
|
|
|
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
|
|
|
|
$pattern = strtolower(sprintf('#^%stmpdb\d{7}#', $prefix));
|
|
|
|
if(!preg_match($pattern, $dbname)) {
|
|
|
|
throw new InvalidArgumentException("Invalid database name format");
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
2012-12-19 15:18:23 +01:00
|
|
|
DB::set_alternative_database_name($dbname);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fixtures
|
|
|
|
$fixtureFile = (isset($data['fixture'])) ? $data['fixture'] : null;
|
|
|
|
if($fixtureFile) {
|
|
|
|
$this->loadFixtureIntoDb($fixtureFile);
|
2012-12-19 14:20:06 +01:00
|
|
|
} else {
|
2012-12-19 15:18:23 +01:00
|
|
|
// If no fixture, then use defaults
|
|
|
|
$dataClasses = ClassInfo::subclassesFor('DataObject');
|
|
|
|
array_shift($dataClasses);
|
|
|
|
foreach($dataClasses as $dataClass) singleton($dataClass)->requireDefaultRecords();
|
2012-12-19 14:20:06 +01:00
|
|
|
}
|
2012-12-19 15:47:39 +01:00
|
|
|
|
|
|
|
// Mailer
|
|
|
|
$mailer = (isset($data['mailer'])) ? $data['mailer'] : null;
|
|
|
|
if($mailer) {
|
|
|
|
if(!class_exists($mailer) || !is_subclass_of($mailer, 'Mailer')) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'Class "%s" is not a valid class, or subclass of Mailer',
|
|
|
|
$mailer
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configured through testsession/_config.php
|
|
|
|
Session::set('testsession.mailer', $mailer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Date
|
|
|
|
$date = (isset($data['date'])) ? $data['date'] : null;
|
|
|
|
if($date) {
|
|
|
|
require_once 'Zend/Date.php';
|
|
|
|
if(!Zend_Date::isDate($date, 'yyyy-MM-dd HH:mm:ss')) {
|
|
|
|
throw new LogicException(sprintf(
|
|
|
|
'Invalid date format "%s", use yyyy-MM-dd HH:mm:ss',
|
|
|
|
$date
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configured through testsession/_config.php
|
|
|
|
Session::set('testsession.date', $date);
|
|
|
|
}
|
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() {
|
|
|
|
$state = array();
|
|
|
|
if($dbname = DB::get_alternative_database_name()) {
|
|
|
|
$state[] = new ArrayData(array(
|
|
|
|
'Name' => 'Database',
|
|
|
|
'Value' => $dbname,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if($fixtures = Session::get('testsession.fixtures')) {
|
|
|
|
$state[] = new ArrayData(array(
|
|
|
|
'Name' => 'Fixture',
|
|
|
|
'Value' => implode(',', array_unique($fixtures)),
|
|
|
|
));
|
|
|
|
}
|
2012-12-19 15:47:39 +01:00
|
|
|
if($mailer = Session::get('testsession.mailer')) {
|
|
|
|
$state[] = new ArrayData(array(
|
|
|
|
'Name' => 'Mailer Class',
|
|
|
|
'Value' => $mailer,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if($date = Session::get('testsession.date')) {
|
|
|
|
$state[] = new ArrayData(array(
|
|
|
|
'Name' => 'Date',
|
|
|
|
'Value' => $date,
|
|
|
|
));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|