API Remove dev/tests/startsession etc, use new "testsession" module

Removed commands: startsession, endsession, sessionloadyml, setdb, emptydb.
See https://github.com/silverstripe-labs/silverstripe-testsession
This commit is contained in:
Ingo Schommer 2012-12-19 16:05:37 +01:00
parent f72a024af5
commit 2e1a5081fa
3 changed files with 1 additions and 220 deletions

View File

@ -79,9 +79,6 @@ class DevelopmentAdmin extends Controller {
"build" => "Build/rebuild this environment. Call this whenever you have updated your project sources", "build" => "Build/rebuild this environment. Call this whenever you have updated your project sources",
"tests" => "See a list of unit tests to run", "tests" => "See a list of unit tests to run",
"tests/all" => "Run all tests", "tests/all" => "Run all tests",
"tests/startsession" => "Start a test session in your browser"
. " (gives you a temporary database with default content)",
"tests/endsession" => "Ends a test session",
"jstests" => "See a list of JavaScript tests to run", "jstests" => "See a list of JavaScript tests to run",
"jstests/all" => "Run all JavaScript tests", "jstests/all" => "Run all JavaScript tests",
"tasks" => "See a list of build tasks to run" "tasks" => "See a list of build tasks to run"
@ -191,16 +188,6 @@ Config::inst()->update('Security', 'token', '$token');
TXT; TXT;
} }
public function reset() {
$link = BASE_URL.'/dev/tests/startsession';
return "<p>The dev/reset feature has been removed. If you are trying to test your site " .
"with a clean datababase, we recommend that you use " .
"<a href=\"$link\">dev/test/startsession</a> ".
"instead.</P>";
}
public function errors() { public function errors() {
$this->redirect("Debug_"); $this->redirect("Debug_");
} }

View File

@ -29,12 +29,7 @@ class TestRunner extends Controller {
'coverage/module/$ModuleName' => 'coverageModule', 'coverage/module/$ModuleName' => 'coverageModule',
'coverage/$TestCase!' => 'coverageOnly', 'coverage/$TestCase!' => 'coverageOnly',
'coverage' => 'coverageAll', 'coverage' => 'coverageAll',
'sessionloadyml' => 'sessionloadyml',
'startsession' => 'startsession',
'endsession' => 'endsession',
'setdb' => 'setdb',
'cleanupdb' => 'cleanupdb', 'cleanupdb' => 'cleanupdb',
'emptydb' => 'emptydb',
'module/$ModuleName' => 'module', 'module/$ModuleName' => 'module',
'all' => 'all', 'all' => 'all',
'build' => 'build', 'build' => 'build',
@ -48,9 +43,6 @@ class TestRunner extends Controller {
'coverageAll', 'coverageAll',
'coverageModule', 'coverageModule',
'coverageOnly', 'coverageOnly',
'startsession',
'endsession',
'setdb',
'cleanupdb', 'cleanupdb',
'module', 'module',
'all', 'all',
@ -341,204 +333,6 @@ class TestRunner extends Controller {
if(Director::is_cli() && ($results->failureCount() + $results->errorCount()) > 0) exit(2); if(Director::is_cli() && ($results->failureCount() + $results->errorCount()) > 0) exit(2);
} }
/**
* Start a test session.
* Usage: visit dev/tests/startsession?fixture=(fixturefile). A test database will be constructed, and your
* browser session will be amended to use this database. This can only be run on dev and test sites.
*
* See {@link setdb()} for an alternative approach which just sets a database
* name, and is used for more advanced use cases like interacting with test databases
* directly during functional tests.
*
* Requires PHP's mycrypt extension in order to set the database name
* as an encrypted cookie.
*/
public function startsession() {
if(!Director::isLive()) {
if(SapphireTest::using_temp_db()) {
$endLink = Director::baseURL() . "dev/tests/endsession";
return "<p><a id=\"end-session\" href=\"$endLink\">You're in the middle of a test session;"
. " click here to end it.</a></p>";
} else if(!isset($_GET['fixture'])) {
$me = Director::baseURL() . "dev/tests/startsession";
return <<<HTML
<form action="$me">
<p>Enter a fixture file name to start a new test session. Don't forget to visit dev/tests/endsession when
you're done!</p>
<p>Fixture file (leave blank to start with default set-up): <input id="fixture-file" name="fixture" /></p>
<input type="hidden" name="flush" value="1">
<p><input id="start-session" value="Start test session" type="submit" /></p>
</form>
HTML;
} else {
$fixtureFile = $_GET['fixture'];
if($fixtureFile) {
// Validate fixture file
$realFile = realpath(BASE_PATH.'/'.$fixtureFile);
$baseDir = realpath(Director::baseFolder());
if(!$realFile || !file_exists($realFile)) {
return "<p>Fixture file doesn't exist</p>";
} else if(substr($realFile,0,strlen($baseDir)) != $baseDir) {
return "<p>Fixture file must be inside $baseDir</p>";
} else if(substr($realFile,-4) != '.yml') {
return "<p>Fixture file must be a .yml file</p>";
} else if(!preg_match('/^([^\/.][^\/]+)\/tests\//', $fixtureFile)) {
return "<p>Fixture file must be inside the tests subfolder of one of your modules.</p>";
}
}
$dbname = SapphireTest::create_temp_db();
DB::set_alternative_database_name($dbname);
// Fixture
if($fixtureFile) {
$fixture = Injector::inst()->create('YamlFixture', $fixtureFile);
$fixture->saveIntoDatabase();
// If no fixture, then use defaults
} else {
$dataClasses = ClassInfo::subclassesFor('DataObject');
array_shift($dataClasses);
foreach($dataClasses as $dataClass) singleton($dataClass)->requireDefaultRecords();
}
return "<p>Started testing session with fixture '$fixtureFile'.
Time to start testing; where would you like to start?</p>
<ul>
<li><a id=\"home-link\" href=\"" .Director::baseURL() . "\">Homepage - published site</a></li>
<li><a id=\"draft-link\" href=\"" .Director::baseURL() . "?stage=Stage\">Homepage - draft site
</a></li>
<li><a id=\"admin-link\" href=\"" .Director::baseURL() . "admin/\">CMS Admin</a></li>
<li><a id=\"endsession-link\" href=\"" .Director::baseURL() . "dev/tests/endsession\">
End your test session</a></li>
</ul>";
}
} else {
return "<p>startession can only be used on dev and test sites</p>";
}
}
/**
* Set an alternative database name in the current browser session as a cookie.
* Useful for functional testing libraries like behat to create a "clean slate".
* Does not actually create the database, that's usually handled
* by {@link SapphireTest::create_temp_db()}.
*
* The database names are limited to a specific naming convention as a security measure:
* The "tmpdb" prefix and a random sequence of seven digits.
* This avoids the user gaining access to other production databases
* available on the same connection.
*
* See {@link startsession()} for a different approach which actually creates
* the DB and loads a fixture file instead.
*
* Requires PHP's mycrypt extension in order to set the database name
* as an encrypted cookie.
*/
public function setdb() {
if(Director::isLive()) {
return $this->httpError(403, "dev/tests/setdb can only be used on dev and test sites");
}
if(!isset($_GET['database'])) {
return $this->httpError(400, "dev/tests/setdb must be used with a 'database' parameter");
}
$name = $_GET['database'];
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
$pattern = strtolower(sprintf('#^%stmpdb\d{7}#', $prefix));
if($name && !preg_match($pattern, $name)) {
return $this->httpError(400, "Invalid database name format");
}
DB::set_alternative_database_name($name);
if($name) {
return "<p>Set database session to '$name'.</p>";
} else {
return "<p>Unset database session.</p>";
}
}
public function emptydb() {
if(SapphireTest::using_temp_db()) {
SapphireTest::empty_temp_db();
if(isset($_GET['fixture']) && ($fixtureFile = $_GET['fixture'])) {
$fixture = Injector::inst()->create('YamlFixture', $fixtureFile);
$fixture->saveIntoDatabase();
return "<p>Re-test the test database with fixture '$fixtureFile'. Time to start testing; where would"
. " you like to start?</p>";
} else {
return "<p>Re-test the test database. Time to start testing; where would you like to start?</p>";
}
} else {
return "<p>dev/tests/emptydb can only be used with a temporary database. Perhaps you should use"
. " dev/tests/startsession first?</p>";
}
}
public function endsession() {
SapphireTest::kill_temp_db();
DB::set_alternative_database_name(null);
return "<p>Test session ended.</p>
<ul>
<li><a id=\"home-link\" href=\"" .Director::baseURL() . "\">Return to your site</a></li>
<li><a id=\"startsession-link\" href=\"" .Director::baseURL() . "dev/tests/startsession\">
Start a new test session</a></li>
</ul>";
}
public function sessionloadyml() {
// Load incremental YAML fixtures
// TODO: We will probably have to filter out the admin member here,
// as it is supplied by Bare.yml
if(Director::isLive()) {
return "<p>sessionloadyml can only be used on dev and test sites</p>";
}
if (!SapphireTest::using_temp_db()) {
return "<p>Please load /dev/tests/startsession first</p>";
}
$fixtureFile = isset($_GET['fixture']) ? $_GET['fixture'] : null;
if (empty($fixtureFile)) {
$me = Director::baseURL() . "/dev/tests/sessionloadyml";
return <<<HTML
<form action="$me">
<p>Enter a fixture file name to load a new YAML fixture into the session.</p>
<p>Fixture file <input id="fixture-file" name="fixture" /></p>
<input type="hidden" name="flush" value="1">
<p><input id="session-load-yaml" value="Load yml fixture" type="submit" /></p>
</form>
HTML;
}
// Validate fixture file
$realFile = realpath(BASE_PATH.'/'.$fixtureFile);
$baseDir = realpath(Director::baseFolder());
if(!$realFile || !file_exists($realFile)) {
return "<p>Fixture file doesn't exist</p>";
} else if(substr($realFile,0,strlen($baseDir)) != $baseDir) {
return "<p>Fixture file must be inside $baseDir</p>";
} else if(substr($realFile,-4) != '.yml') {
return "<p>Fixture file must be a .yml file</p>";
} else if(!preg_match('/^([^\/.][^\/]+)\/tests\//', $fixtureFile)) {
return "<p>Fixture file must be inside the tests subfolder of one of your modules.</p>";
}
// Fixture
$fixture = Injector::inst()->create('YamlFixture', $fixtureFile);
$fixture->saveIntoDatabase();
return "<p>Loaded fixture '$fixtureFile' into session</p>";
}
public function setUp() { public function setUp() {
// The first DB test will sort out the DB, we don't have to // The first DB test will sort out the DB, we don't have to
SSViewer::flush_template_cache(); SSViewer::flush_template_cache();

View File

@ -153,7 +153,7 @@ class DB {
* rest of the options, see the specific class. * rest of the options, see the specific class.
*/ */
public static function connect($databaseConfig) { public static function connect($databaseConfig) {
// This is used by TestRunner::startsession() to test up a test session using an alt // This is used by the "testsession" module to test up a test session using an alternative name
if($name = self::get_alternative_database_name()) { if($name = self::get_alternative_database_name()) {
$databaseConfig['database'] = $name; $databaseConfig['database'] = $name;
} }