Skip to content
This repository
Browse code

NEW create TestRunner setdb URL endpoint

  • Loading branch information...
commit 1432a8e8a160db2fa7c9110443740b98086cd4b4 1 parent 342f076
Michał Ochman authored chillu committed

Showing 1 changed file with 47 additions and 0 deletions. Show diff stats Hide diff stats

  1. 47  dev/TestRunner.php
47  dev/TestRunner.php
@@ -32,6 +32,7 @@ class TestRunner extends Controller {
32 32
 		'sessionloadyml' => 'sessionloadyml',
33 33
 		'startsession' => 'startsession',
34 34
 		'endsession' => 'endsession',
  35
+		'setdb' => 'setdb',
35 36
 		'cleanupdb' => 'cleanupdb',
36 37
 		'emptydb' => 'emptydb',
37 38
 		'module/$ModuleName' => 'module',
@@ -49,6 +50,7 @@ class TestRunner extends Controller {
49 50
 		'coverageOnly',
50 51
 		'startsession',
51 52
 		'endsession',
  53
+		'setdb',
52 54
 		'cleanupdb',
53 55
 		'module',
54 56
 		'all',
@@ -341,6 +343,10 @@ class_exists($className);
341 343
 	 * Start a test session.
342 344
 	 * Usage: visit dev/tests/startsession?fixture=(fixturefile).  A test database will be constructed, and your browser session will be amended
343 345
 	 * to use this database.  This can only be run on dev and test sites.
  346
+	 *
  347
+	 * See {@link setdb()} for an alternative approach which just sets a database
  348
+	 * name, and is used for more advanced use cases like interacting with test databases
  349
+	 * directly during functional tests.
344 350
 	 */
345 351
 	function startsession() {
346 352
 		if(!Director::isLive()) {
@@ -405,6 +411,47 @@ function startsession() {
405 411
 			return "<p>startession can only be used on dev and test sites</p>";
406 412
 		}
407 413
 	}
  414
+
  415
+	/**
  416
+	 * Set an alternative database name in the current browser session.
  417
+	 * Useful for functional testing libraries like behat to create a "clean slate". 
  418
+	 * Does not actually create the database, that's usually handled
  419
+	 * by {@link SapphireTest::create_temp_db()}.
  420
+	 *
  421
+	 * The database names are limited to a specific naming convention as a security measure:
  422
+	 * The "tmpdb" prefix and a random sequence of seven digits.
  423
+	 * This avoids the user gaining access to other production databases 
  424
+	 * available on the same connection.
  425
+	 *
  426
+	 * See {@link startsession()} for a different approach which actually creates
  427
+	 * the DB and loads a fixture file instead.
  428
+	 */
  429
+	function setdb() {
  430
+		if(Director::isLive()) {
  431
+			return $this->permissionFailure("dev/tests/setdb can only be used on dev and test sites");
  432
+		}
  433
+		
  434
+		if(!isset($_GET['database'])) {
  435
+			return $this->permissionFailure("dev/tests/setdb must be used with a 'database' parameter");
  436
+		}
  437
+		
  438
+		$database_name = $_GET['database'];
  439
+		$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
  440
+		$pattern = strtolower(sprintf('#^%stmpdb\d{7}#', $prefix));
  441
+		if(!preg_match($pattern, $database_name)) {
  442
+			return $this->permissionFailure("Invalid database name format");
  443
+		}
  444
+
  445
+		DB::set_alternative_database_name($database_name);
  446
+
  447
+		return "<p>Set database session to '$database_name'.  Time to start testing; where would you like to start?</p>
  448
+			<ul>
  449
+				<li><a id=\"home-link\" href=\"" .Director::baseURL() . "\">Homepage - published site</a></li>
  450
+				<li><a id=\"draft-link\" href=\"" .Director::baseURL() . "?stage=Stage\">Homepage - draft site</a></li>
  451
+				<li><a id=\"admin-link\" href=\"" .Director::baseURL() . "admin/\">CMS Admin</a></li>
  452
+				<li><a id=\"endsession-link\" href=\"" .Director::baseURL() . "dev/tests/endsession\">End your test session</a></li>
  453
+			</ul>";
  454
+	}
408 455
 	
409 456
 	function emptydb() {
410 457
 		if(SapphireTest::using_temp_db()) {

0 notes on commit 1432a8e

Please sign in to comment.
Something went wrong with that request. Please try again.