Enter a fixture file name to start a new test session. Don't forget to visit dev/testsession/end when you're done!
Fixture file (leave blank to start with default set-up):
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 "Fixture file doesn't exist
"; } else if(substr($realFile,0,strlen($baseDir)) != $baseDir) { return "Fixture file must be inside $baseDir
"; } else if(substr($realFile,-4) != '.yml') { return "Fixture file must be a .yml file
"; } else if(!preg_match('/^([^\/.][^\/]+)\/tests\//', $fixtureFile)) { return "Fixture file must be inside the tests subfolder of one of your modules.
"; } } $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 "Started testing session with fixture '$fixtureFile'. Time to start testing; where would you like to start?
"; } } else { return "startession can only be used on dev and test sites
"; } } /** * 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 "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. * * See {@link start()} 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/testsession/setdb can only be used on dev and test sites"); } if(!isset($_GET['database'])) { return $this->httpError(400, "dev/testsession/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 "Set database session to '$name'.
"; } else { return "Unset database session.
"; } } 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 "Re-test the test database with fixture '$fixtureFile'. Time to start testing; where would" . " you like to start?
"; } else { return "Re-test the test database. Time to start testing; where would you like to start?
"; } } else { return "dev/testsession/emptydb can only be used with a temporary database. Perhaps you should use" . " dev/testsession/start first?
"; } } public function end() { SapphireTest::kill_temp_db(); DB::set_alternative_database_name(null); return "Test session ended.
"; } }