ENHANCEMENT: Removed dev/reset, instead encouraging the use of dev/tests/startsession for tests.

ENHANCEMENT: Let people use dev/tests/startsession without a fixture, instead calling requireDefaultRecords

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.3@98081 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-02-03 23:01:04 +00:00
parent cd64c943f7
commit 909de1f454
2 changed files with 40 additions and 94 deletions

View File

@ -63,9 +63,10 @@ class DevelopmentAdmin extends Controller {
function index() { function index() {
$actions = array( $actions = array(
"build" => "Build/rebuild this environment (formerly db/build). Call this whenever you have updated your project sources", "build" => "Build/rebuild this environment (formerly db/build). Call this whenever you have updated your project sources",
"reset" => "Reset this environment - truncate the database and rebuild. This is useful after testing to start with a fresh working copy",
"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",
"modules/add" => "Add a module, for example, 'sake dev/modules/add ecommerce'", "modules/add" => "Add a module, for example, 'sake dev/modules/add ecommerce'",
@ -140,84 +141,13 @@ class DevelopmentAdmin extends Controller {
} }
function reset() { function reset() {
global $databaseConfig; $link = BASE_URL.'/dev/tests/startsession';
$databaseName = $databaseConfig['database'];
if(Director::is_cli()) { return "<p>The dev/reset feature has been removed. If you are trying to test your site " .
echo "\nPlease run dev/reset from your web browser.\n"; "with a clean datababase, we recommend that you use " .
} else { "<a href=\"$link\">dev/test/startsession</a> ".
$renderer = new DebugView(); "instead.</P>";
$renderer->writeHeader();
$renderer->writeInfo('Database reset', 'Completely truncate and rebuild the current database');
echo '<div style="margin: 0 2em">';
if(isset($_GET['done'])) {
echo "<p style=\"color: green\"><b>$databaseName</b> has been completely truncated and rebuilt.</p>";
echo "<p>Note: If you had <i>SS_DEFAULT_ADMIN_USERNAME</i> and <i>SS_DEFAULT_ADMIN_PASSWORD</i>
defined in your <b>_ss_environment.php</b> file, a default admin Member record has been created
with those credentials.</p>";
} else {
echo $this->ResetForm()->renderWith('Form');
}
echo '</div>';
$renderer->writeFooter();
}
}
function ResetForm() {
global $databaseConfig;
$databaseName = $databaseConfig['database'];
if(!Session::get('devResetRandNumber')) {
$rand = rand(5,500);
Session::set('devResetRandNumber', $rand);
} else {
$rand = Session::get('devResetRandNumber');
}
$form = new Form(
$this,
'ResetForm',
new FieldSet(
new LiteralField('ResetWarning', "<p style=\"color: red\">WARNING: This will completely
destroy ALL existing data in <b>$databaseName</b>! &nbsp; Press the button below to
confirm this action.</p>"),
new HiddenField('devResetRandNumber', '', $rand)
),
new FieldSet(
new FormAction('doReset', 'Reset and completely rebuild the database')
)
);
$form->setFormAction(Director::absoluteBaseURL() . 'dev/ResetForm');
return $form;
}
function doReset($data, $form, $request) {
if(!isset($data['devResetRandNumber'])) {
Director::redirectBack();
return false;
}
// Avoid accidental database resets by checking the posted number to the one in session
if(Session::get('devResetRandNumber') != $data['devResetRandNumber']) {
Director::redirectBack();
return false;
}
$da = new DatabaseAdmin();
$da->clearAllData();
// If _ss_environment.php has some constants set for default admin, set these up in the request
$_REQUEST['username'] = defined('SS_DEFAULT_ADMIN_USERNAME') ? SS_DEFAULT_ADMIN_USERNAME : null;
$_REQUEST['password'] = defined('SS_DEFAULT_ADMIN_PASSWORD') ? SS_DEFAULT_ADMIN_PASSWORD : null;
$da->build();
Session::clear('devResetRandNumber');
Director::redirect(Director::absoluteBaseURL() . 'dev/reset?done=1');
} }
function errors() { function errors() {

View File

@ -214,39 +214,51 @@ class TestRunner extends Controller {
return <<<HTML return <<<HTML
<form action="$me"> <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>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: <input id="fixture-file" name="fixture" /></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"> <input type="hidden" name="flush" value="1">
<p><input id="start-session" value="Start test session" type="submit" /></p> <p><input id="start-session" value="Start test session" type="submit" /></p>
</form> </form>
HTML; HTML;
} else { } else {
$fixtureFile = $_GET['fixture']; $fixtureFile = $_GET['fixture'];
// Validate fixture file if($fixtureFile) {
$realFile = realpath('../' . $fixtureFile); // Validate fixture file
$baseDir = realpath(Director::baseFolder()); $realFile = realpath('../' . $fixtureFile);
if(!$realFile || !file_exists($realFile)) { $baseDir = realpath(Director::baseFolder());
return "<p>Fixture file doesn't exist</p>"; if(!$realFile || !file_exists($realFile)) {
} else if(substr($realFile,0,strlen($baseDir)) != $baseDir) { return "<p>Fixture file doesn't exist</p>";
return "<p>Fixture file must be inside $baseDir</p>"; } else if(substr($realFile,0,strlen($baseDir)) != $baseDir) {
} else if(substr($realFile,-4) != '.yml') { return "<p>Fixture file must be inside $baseDir</p>";
return "<p>Fixture file must be a .yml file</p>"; } else if(substr($realFile,-4) != '.yml') {
} else if(!preg_match('/^([^\/.][^\/]+)\/tests\//', $fixtureFile)) { return "<p>Fixture file must be a .yml file</p>";
return "<p>Fixture file must be inside the tests subfolder of one of your modules.</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(); $dbname = SapphireTest::create_temp_db();
DB::set_alternative_database_name($dbname); DB::set_alternative_database_name($dbname);
$fixture = new YamlFixture($_GET['fixture']); // Fixture
$fixture->saveIntoDatabase(); if($fixtureFile) {
$fixture = new 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> return "<p>Started testing session with fixture '$fixtureFile'. Time to start testing; where would you like to start?</p>
<ul> <ul>
<li><a id=\"home-link\" href=\"" .Director::baseURL() . "\">Homepage - published site</a></li> <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=\"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=\"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>"; </ul>";
} }
@ -259,7 +271,11 @@ HTML;
SapphireTest::kill_temp_db(); SapphireTest::kill_temp_db();
DB::set_alternative_database_name(null); DB::set_alternative_database_name(null);
return "<p>Test session ended.</p>"; 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>";
} }
function setUp() { function setUp() {