2007-08-15 08:38:41 +02:00
|
|
|
<?php
|
2008-07-16 05:29:47 +02:00
|
|
|
require_once 'TestRunner.php';
|
2007-08-15 08:38:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test case class for the Sapphire framework.
|
|
|
|
* Sapphire unit testing is based on PHPUnit, but provides a number of hooks into our data model that make it easier to work with.
|
2008-08-11 01:17:51 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-01-10 01:33:18 +01:00
|
|
|
* @subpackage testing
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
|
|
|
class SapphireTest extends PHPUnit_Framework_TestCase {
|
2008-08-11 00:49:59 +02:00
|
|
|
/**
|
2008-08-11 01:17:51 +02:00
|
|
|
* Path to fixture data for this test run.
|
2009-07-08 02:06:16 +02:00
|
|
|
* If passed as an array, multiple fixture files will be loaded.
|
|
|
|
* Please note that you won't be able to refer with "=>" notation
|
|
|
|
* between the fixtures, they act independent of each other.
|
2008-08-11 01:17:51 +02:00
|
|
|
*
|
2009-07-08 02:06:16 +02:00
|
|
|
* @var string|array
|
2008-08-11 00:49:59 +02:00
|
|
|
*/
|
2008-08-11 01:29:30 +02:00
|
|
|
static $fixture_file = null;
|
2008-03-17 03:04:58 +01:00
|
|
|
|
2011-09-26 05:08:41 +02:00
|
|
|
/**
|
|
|
|
* Set whether to include this test in the TestRunner or to skip this.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2011-09-26 06:11:49 +02:00
|
|
|
protected $skipTest = false;
|
2011-09-26 05:08:41 +02:00
|
|
|
|
2009-11-30 02:48:26 +01:00
|
|
|
/**
|
|
|
|
* @var Boolean If set to TRUE, this will force a test database to be generated
|
|
|
|
* in {@link setUp()}. Note that this flag is overruled by the presence of a
|
|
|
|
* {@link $fixture_file}, which always forces a database build.
|
|
|
|
*/
|
|
|
|
protected $usesDatabase = null;
|
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
protected $originalMailer;
|
2008-09-18 05:59:00 +02:00
|
|
|
protected $originalMemberPasswordValidator;
|
2008-11-10 02:13:42 +01:00
|
|
|
protected $originalRequirements;
|
2008-11-22 04:33:00 +01:00
|
|
|
protected $originalIsRunningTest;
|
2010-04-13 03:44:45 +02:00
|
|
|
protected $originalTheme;
|
2010-04-14 05:49:06 +02:00
|
|
|
protected $originalNestedURLsState;
|
2010-10-15 04:58:02 +02:00
|
|
|
protected $originalMemoryLimit;
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
protected $mailer;
|
|
|
|
|
2010-10-15 05:43:30 +02:00
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-11-22 04:33:00 +01:00
|
|
|
protected static $is_running_test = false;
|
|
|
|
|
2010-10-04 06:31:58 +02:00
|
|
|
/**
|
|
|
|
* By default, setUp() does not require default records. Pass
|
|
|
|
* class names in here, and the require/augment default records
|
|
|
|
* function will be called on them.
|
|
|
|
*/
|
|
|
|
protected $requireDefaultRecordsFrom = array();
|
|
|
|
|
2010-04-12 03:52:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of extensions that can't be applied during the execution of this run. If they are
|
|
|
|
* applied, they will be temporarily removed and a database migration called.
|
|
|
|
*
|
|
|
|
* The keys of the are the classes that the extensions can't be applied the extensions to, and
|
|
|
|
* the values are an array of illegal extensions on that class.
|
|
|
|
*/
|
|
|
|
protected $illegalExtensions = array(
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of extensions that must be applied during the execution of this run. If they are
|
|
|
|
* not applied, they will be temporarily added and a database migration called.
|
|
|
|
*
|
|
|
|
* The keys of the are the classes to apply the extensions to, and the values are an array
|
2010-10-19 04:46:26 +02:00
|
|
|
* of required extensions on that class.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* <code>
|
|
|
|
* array("MyTreeDataObject" => array("Versioned", "Hierarchy"))
|
|
|
|
* </code>
|
2010-04-12 03:52:39 +02:00
|
|
|
*/
|
|
|
|
protected $requiredExtensions = array(
|
|
|
|
);
|
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
/**
|
|
|
|
* By default, the test database won't contain any DataObjects that have the interface TestOnly.
|
|
|
|
* This variable lets you define additional TestOnly DataObjects to set up for this test.
|
|
|
|
* Set it to an array of DataObject subclass names.
|
|
|
|
*/
|
|
|
|
protected $extraDataObjects = array();
|
|
|
|
|
2009-08-27 06:45:58 +02:00
|
|
|
/**
|
|
|
|
* We need to disabling backing up of globals to avoid overriding
|
|
|
|
* the few globals SilverStripe relies on, like $lang for the i18n subsystem.
|
|
|
|
*
|
|
|
|
* @see http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html
|
|
|
|
*/
|
|
|
|
protected $backupGlobals = FALSE;
|
2010-04-12 03:52:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper arrays for illegalExtensions/requiredExtensions code
|
|
|
|
*/
|
|
|
|
private $extensionsToReapply = array(), $extensionsToRemove = array();
|
2011-09-26 05:08:41 +02:00
|
|
|
|
2009-08-27 06:45:58 +02:00
|
|
|
|
2010-10-15 05:43:30 +02:00
|
|
|
/**
|
|
|
|
* Determines if unit tests are currently run (via {@link TestRunner}).
|
|
|
|
* This is used as a cheap replacement for fully mockable state
|
|
|
|
* in certain contiditions (e.g. access checks).
|
|
|
|
* Caution: When set to FALSE, certain controllers might bypass
|
|
|
|
* access checks, so this is a very security sensitive setting.
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-11-22 04:33:00 +01:00
|
|
|
public static function is_running_test() {
|
|
|
|
return self::$is_running_test;
|
|
|
|
}
|
|
|
|
|
2008-08-11 01:17:51 +02:00
|
|
|
/**
|
2009-07-08 02:06:16 +02:00
|
|
|
* @var array $fixtures Array of {@link YamlFixture} instances
|
2008-08-11 01:17:51 +02:00
|
|
|
*/
|
2009-07-08 02:06:16 +02:00
|
|
|
protected $fixtures;
|
2008-08-11 01:17:51 +02:00
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
protected $model;
|
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
function setUp() {
|
2011-09-26 05:08:41 +02:00
|
|
|
// We cannot run the tests on this abstract class.
|
2011-09-26 06:11:49 +02:00
|
|
|
if(get_class($this) == "SapphireTest") $this->skipTest = true;
|
2011-09-26 05:08:41 +02:00
|
|
|
|
2011-09-26 06:11:49 +02:00
|
|
|
if($this->skipTest) {
|
2011-09-26 05:08:41 +02:00
|
|
|
$this->markTestSkipped(sprintf(
|
|
|
|
'Skipping %s ', get_class($this)
|
|
|
|
));
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
// Mark test as being run
|
|
|
|
$this->originalIsRunningTest = self::$is_running_test;
|
|
|
|
self::$is_running_test = true;
|
|
|
|
|
2010-10-19 02:37:55 +02:00
|
|
|
// i18n needs to be set to the defaults or tests fail
|
|
|
|
i18n::set_locale(i18n::default_locale());
|
2010-10-15 05:23:02 +02:00
|
|
|
i18n::set_date_format(null);
|
|
|
|
i18n::set_time_format(null);
|
|
|
|
|
2012-03-11 17:59:09 +01:00
|
|
|
// Set default timezone consistently to avoid NZ-specific dependencies
|
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
|
2008-09-18 05:59:00 +02:00
|
|
|
// Remove password validation
|
|
|
|
$this->originalMemberPasswordValidator = Member::password_validator();
|
2008-11-10 02:13:42 +01:00
|
|
|
$this->originalRequirements = Requirements::backend();
|
2008-09-18 05:59:00 +02:00
|
|
|
Member::set_password_validator(null);
|
2008-11-22 04:33:00 +01:00
|
|
|
Cookie::set_report_errors(false);
|
2009-08-11 06:45:54 +02:00
|
|
|
|
2011-03-22 23:49:44 +01:00
|
|
|
if(class_exists('RootURLController')) RootURLController::reset();
|
2011-03-22 09:30:34 +01:00
|
|
|
if(class_exists('Translatable')) Translatable::reset();
|
2009-08-11 06:45:54 +02:00
|
|
|
Versioned::reset();
|
|
|
|
DataObject::reset();
|
2011-03-22 23:49:44 +01:00
|
|
|
if(class_exists('SiteTree')) SiteTree::reset();
|
2009-11-30 03:24:46 +01:00
|
|
|
Hierarchy::reset();
|
2010-10-19 05:36:12 +02:00
|
|
|
if(Controller::has_curr()) Controller::curr()->setSession(new Session(array()));
|
2012-04-11 17:22:55 +02:00
|
|
|
Security::$database_is_ready = null;
|
2010-04-13 03:44:45 +02:00
|
|
|
|
|
|
|
$this->originalTheme = SSViewer::current_theme();
|
2010-04-14 05:49:06 +02:00
|
|
|
|
2011-03-22 23:49:44 +01:00
|
|
|
if(class_exists('SiteTree')) {
|
|
|
|
// Save nested_urls state, so we can restore it later
|
|
|
|
$this->originalNestedURLsState = SiteTree::nested_urls();
|
|
|
|
}
|
2008-09-18 05:59:00 +02:00
|
|
|
|
2008-03-17 03:04:58 +01:00
|
|
|
$className = get_class($this);
|
|
|
|
$fixtureFile = eval("return {$className}::\$fixture_file;");
|
2010-12-21 05:46:43 +01:00
|
|
|
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
|
2011-05-01 07:33:02 +02:00
|
|
|
|
|
|
|
// Todo: this could be a special test model
|
|
|
|
$this->model = DataModel::inst();
|
2010-12-11 06:27:34 +01:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
// Set up fixture
|
2010-04-12 10:28:06 +02:00
|
|
|
if($fixtureFile || $this->usesDatabase || !self::using_temp_db()) {
|
2010-12-21 05:46:43 +01:00
|
|
|
if(substr(DB::getConn()->currentDatabase(), 0, strlen($prefix) + 5) != strtolower(sprintf('%stmpdb', $prefix))) {
|
2008-08-13 05:41:54 +02:00
|
|
|
//echo "Re-creating temp database... ";
|
2008-08-13 04:47:14 +02:00
|
|
|
self::create_temp_db();
|
2008-08-13 05:41:54 +02:00
|
|
|
//echo "done.\n";
|
2008-03-17 03:04:58 +01:00
|
|
|
}
|
2008-08-13 04:47:14 +02:00
|
|
|
|
2008-03-17 03:04:58 +01:00
|
|
|
singleton('DataObject')->flushCache();
|
2009-03-04 04:44:11 +01:00
|
|
|
|
2010-04-12 05:21:30 +02:00
|
|
|
self::empty_temp_db();
|
2010-10-04 06:31:58 +02:00
|
|
|
|
|
|
|
foreach($this->requireDefaultRecordsFrom as $className) {
|
|
|
|
$instance = singleton($className);
|
|
|
|
if (method_exists($instance, 'requireDefaultRecords')) $instance->requireDefaultRecords();
|
|
|
|
if (method_exists($instance, 'augmentDefaultRecords')) $instance->augmentDefaultRecords();
|
|
|
|
}
|
2010-04-12 05:21:30 +02:00
|
|
|
|
2010-05-25 06:17:21 +02:00
|
|
|
if($fixtureFile) {
|
2011-03-30 08:49:11 +02:00
|
|
|
$pathForClass = $this->getCurrentAbsolutePath();
|
2010-05-25 06:17:21 +02:00
|
|
|
$fixtureFiles = (is_array($fixtureFile)) ? $fixtureFile : array($fixtureFile);
|
|
|
|
|
|
|
|
$i = 0;
|
|
|
|
foreach($fixtureFiles as $fixtureFilePath) {
|
2011-03-30 08:47:30 +02:00
|
|
|
// Support fixture paths relative to the test class, rather than relative to webroot
|
|
|
|
// String checking is faster than file_exists() calls.
|
|
|
|
$isRelativeToFile = (strpos('/', $fixtureFilePath) === false || preg_match('/^\.\./', $fixtureFilePath));
|
|
|
|
if($isRelativeToFile) {
|
|
|
|
$resolvedPath = realpath($pathForClass . '/' . $fixtureFilePath);
|
|
|
|
if($resolvedPath) $fixtureFilePath = $resolvedPath;
|
|
|
|
}
|
|
|
|
|
2010-05-25 06:17:21 +02:00
|
|
|
$fixture = new YamlFixture($fixtureFilePath);
|
2011-05-01 07:33:02 +02:00
|
|
|
$fixture->saveIntoDatabase($this->model);
|
2010-05-25 06:17:21 +02:00
|
|
|
$this->fixtures[] = $fixture;
|
|
|
|
|
|
|
|
// backwards compatibility: Load first fixture into $this->fixture
|
|
|
|
if($i == 0) $this->fixture = $fixture;
|
|
|
|
$i++;
|
|
|
|
}
|
2009-07-08 02:06:16 +02:00
|
|
|
}
|
|
|
|
|
2010-04-14 06:41:30 +02:00
|
|
|
$this->logInWithPermission("ADMIN");
|
2008-03-17 03:04:58 +01:00
|
|
|
}
|
2008-04-26 08:32:31 +02:00
|
|
|
|
|
|
|
// Set up email
|
|
|
|
$this->originalMailer = Email::mailer();
|
|
|
|
$this->mailer = new TestMailer();
|
|
|
|
Email::set_mailer($this->mailer);
|
2009-04-29 01:52:15 +02:00
|
|
|
Email::send_all_emails_to(null);
|
2010-10-15 04:58:02 +02:00
|
|
|
|
|
|
|
// Preserve memory settings
|
|
|
|
$this->originalMemoryLimit = ini_get('memory_limit');
|
2012-04-13 23:47:17 +02:00
|
|
|
|
|
|
|
// turn off template debugging
|
|
|
|
SSViewer::set_source_file_comments(false);
|
|
|
|
|
2012-01-31 22:36:53 +01:00
|
|
|
// Clear requirements
|
|
|
|
Requirements::clear();
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-22 05:22:09 +02:00
|
|
|
* Called once per test case ({@link SapphireTest} subclass).
|
|
|
|
* This is different to {@link setUp()}, which gets called once
|
|
|
|
* per method. Useful to initialize expensive operations which
|
|
|
|
* don't change state for any called method inside the test,
|
2010-04-12 03:52:39 +02:00
|
|
|
* e.g. dynamically adding an extension. See {@link tearDownOnce()}
|
2009-04-22 05:22:09 +02:00
|
|
|
* for tearing down the state again.
|
|
|
|
*/
|
2010-04-12 03:52:39 +02:00
|
|
|
function setUpOnce() {
|
|
|
|
// Remove any illegal extensions that are present
|
|
|
|
foreach($this->illegalExtensions as $class => $extensions) {
|
|
|
|
foreach($extensions as $extension) {
|
|
|
|
if (Object::has_extension($class, $extension)) {
|
|
|
|
if(!isset($this->extensionsToReapply[$class])) $this->extensionsToReapply[$class] = array();
|
|
|
|
$this->extensionsToReapply[$class][] = $extension;
|
|
|
|
Object::remove_extension($class, $extension);
|
|
|
|
$isAltered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any required extensions that aren't present
|
|
|
|
foreach($this->requiredExtensions as $class => $extensions) {
|
|
|
|
$this->extensionsToRemove[$class] = array();
|
|
|
|
foreach($extensions as $extension) {
|
|
|
|
if(!Object::has_extension($class, $extension)) {
|
|
|
|
if(!isset($this->extensionsToRemove[$class])) $this->extensionsToReapply[$class] = array();
|
|
|
|
$this->extensionsToRemove[$class][] = $extension;
|
|
|
|
Object::add_extension($class, $extension);
|
|
|
|
$isAltered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have made changes to the extensions present, then migrate the database schema.
|
2010-04-12 04:03:16 +02:00
|
|
|
if($this->extensionsToReapply || $this->extensionsToRemove || $this->extraDataObjects) {
|
2010-04-12 07:39:48 +02:00
|
|
|
if(!self::using_temp_db()) self::create_temp_db();
|
2010-04-12 04:03:16 +02:00
|
|
|
$this->resetDBSchema(true);
|
2010-04-12 03:52:39 +02:00
|
|
|
}
|
2009-10-22 02:30:21 +02:00
|
|
|
// clear singletons, they're caching old extension info
|
|
|
|
// which is used in DatabaseAdmin->doBuild()
|
|
|
|
global $_SINGLETONS;
|
|
|
|
$_SINGLETONS = array();
|
2012-03-11 17:59:09 +01:00
|
|
|
|
|
|
|
// Set default timezone consistently to avoid NZ-specific dependencies
|
|
|
|
date_default_timezone_set('UTC');
|
2009-04-22 05:22:09 +02:00
|
|
|
}
|
|
|
|
|
2010-04-12 03:52:39 +02:00
|
|
|
/**
|
|
|
|
* tearDown method that's called once per test class rather once per test method.
|
|
|
|
*/
|
|
|
|
function tearDownOnce() {
|
|
|
|
// If we have made changes to the extensions present, then migrate the database schema.
|
|
|
|
if($this->extensionsToReapply || $this->extensionsToRemove) {
|
|
|
|
// Remove extensions added for testing
|
|
|
|
foreach($this->extensionsToRemove as $class => $extensions) {
|
|
|
|
foreach($extensions as $extension) {
|
|
|
|
Object::remove_extension($class, $extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reapply ones removed
|
|
|
|
foreach($this->extensionsToReapply as $class => $extensions) {
|
|
|
|
foreach($extensions as $extension) {
|
|
|
|
Object::add_extension($class, $extension);
|
|
|
|
}
|
|
|
|
}
|
2010-04-12 04:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if($this->extensionsToReapply || $this->extensionsToRemove || $this->extraDataObjects) {
|
|
|
|
$this->resetDBSchema();
|
2010-04-12 03:52:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-22 05:22:09 +02:00
|
|
|
/**
|
|
|
|
* Array
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
|
|
|
protected $fixtureDictionary;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID of an object from the fixture.
|
|
|
|
* @param $className The data class, as specified in your fixture file. Parent classes won't work
|
|
|
|
* @param $identifier The identifier string, as provided in your fixture file
|
2009-07-08 02:06:16 +02:00
|
|
|
* @return int
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
|
|
|
protected function idFromFixture($className, $identifier) {
|
2009-07-08 02:06:16 +02:00
|
|
|
if(!$this->fixtures) {
|
2009-07-08 03:38:01 +02:00
|
|
|
user_error("You've called idFromFixture() but you haven't specified static \$fixture_file.\n", E_USER_WARNING);
|
2009-07-08 02:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($this->fixtures as $fixture) {
|
|
|
|
$match = $fixture->idFromFixture($className, $identifier);
|
|
|
|
if($match) return $match;
|
|
|
|
}
|
|
|
|
|
2009-07-08 04:54:01 +02:00
|
|
|
$fixtureFiles = Object::get_static(get_class($this), 'fixture_file');
|
2009-07-08 04:36:05 +02:00
|
|
|
user_error(sprintf(
|
|
|
|
"Couldn't find object '%s' (class: %s) in files %s",
|
|
|
|
$identifier,
|
|
|
|
$className,
|
2009-07-08 04:54:01 +02:00
|
|
|
(is_array($fixtureFiles)) ? implode(',', $fixtureFiles) : $fixtureFiles
|
2009-07-08 04:36:05 +02:00
|
|
|
), E_USER_ERROR);
|
|
|
|
|
2009-07-08 02:06:16 +02:00
|
|
|
return false;
|
2007-08-16 08:36:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the IDs in the fixture of a particular class name.
|
2009-07-08 02:06:16 +02:00
|
|
|
* Will collate all IDs form all fixtures if multiple fixtures are provided.
|
|
|
|
*
|
|
|
|
* @param string $className
|
2007-08-16 08:36:02 +02:00
|
|
|
* @return A map of fixture-identifier => object-id
|
|
|
|
*/
|
|
|
|
protected function allFixtureIDs($className) {
|
2009-07-08 02:06:16 +02:00
|
|
|
if(!$this->fixtures) {
|
2009-07-08 03:38:01 +02:00
|
|
|
user_error("You've called allFixtureIDs() but you haven't specified static \$fixture_file.\n", E_USER_WARNING);
|
2009-07-08 02:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ids = array();
|
|
|
|
foreach($this->fixtures as $fixture) {
|
|
|
|
$ids += $fixture->allFixtureIDs($className);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ids;
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an object from the fixture.
|
|
|
|
* @param $className The data class, as specified in your fixture file. Parent classes won't work
|
|
|
|
* @param $identifier The identifier string, as provided in your fixture file
|
|
|
|
*/
|
|
|
|
protected function objFromFixture($className, $identifier) {
|
2009-07-08 02:06:16 +02:00
|
|
|
if(!$this->fixtures) {
|
2009-07-08 03:38:01 +02:00
|
|
|
user_error("You've called objFromFixture() but you haven't specified static \$fixture_file.\n", E_USER_WARNING);
|
2009-07-08 02:06:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($this->fixtures as $fixture) {
|
|
|
|
$match = $fixture->objFromFixture($className, $identifier);
|
|
|
|
if($match) return $match;
|
|
|
|
}
|
2009-07-08 04:54:01 +02:00
|
|
|
|
|
|
|
$fixtureFiles = Object::get_static(get_class($this), 'fixture_file');
|
2009-07-08 04:36:05 +02:00
|
|
|
user_error(sprintf(
|
|
|
|
"Couldn't find object '%s' (class: %s) in files %s",
|
|
|
|
$identifier,
|
|
|
|
$className,
|
2009-07-08 04:54:01 +02:00
|
|
|
(is_array($fixtureFiles)) ? implode(',', $fixtureFiles) : $fixtureFiles
|
2009-07-08 04:36:05 +02:00
|
|
|
), E_USER_ERROR);
|
|
|
|
|
2009-07-08 02:06:16 +02:00
|
|
|
return false;
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a YAML fixture file into the database.
|
2009-07-08 02:06:16 +02:00
|
|
|
* Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture.
|
|
|
|
* Doesn't clear existing fixtures.
|
|
|
|
*
|
2007-08-15 08:38:41 +02:00
|
|
|
* @param $fixtureFile The location of the .yml fixture file, relative to the site base dir
|
|
|
|
*/
|
|
|
|
function loadFixture($fixtureFile) {
|
|
|
|
$parser = new Spyc();
|
|
|
|
$fixtureContent = $parser->load(Director::baseFolder().'/'.$fixtureFile);
|
|
|
|
|
2009-07-08 02:06:16 +02:00
|
|
|
$fixture = new YamlFixture($fixtureFile);
|
|
|
|
$fixture->saveIntoDatabase();
|
|
|
|
$this->fixtures[] = $fixture;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all fixtures which were previously loaded through
|
|
|
|
* {@link loadFixture()}.
|
|
|
|
*/
|
|
|
|
function clearFixtures() {
|
|
|
|
$this->fixtures = array();
|
2007-08-17 07:43:14 +02:00
|
|
|
}
|
|
|
|
|
2011-03-30 08:47:30 +02:00
|
|
|
/**
|
|
|
|
* Useful for writing unit tests without hardcoding folder structures.
|
|
|
|
*
|
|
|
|
* @return String Absolute path to current class.
|
|
|
|
*/
|
|
|
|
protected function getCurrentAbsolutePath() {
|
|
|
|
return dirname(SS_ClassLoader::instance()->getManifest()->getItemPath(get_class($this)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String File path relative to webroot
|
|
|
|
*/
|
|
|
|
protected function getCurrentRelativePath() {
|
|
|
|
$base = Director::baseFolder();
|
|
|
|
$path = $this->getCurrentAbsolutePath();
|
|
|
|
if(substr($path,0,strlen($base)) == $base) $path = preg_replace('/^\/*/', '', substr($path,strlen($base)));
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
function tearDown() {
|
2010-10-15 04:58:02 +02:00
|
|
|
// Preserve memory settings
|
2010-10-15 04:59:10 +02:00
|
|
|
ini_set('memory_limit', ($this->originalMemoryLimit) ? $this->originalMemoryLimit : -1);
|
2010-10-15 04:58:02 +02:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
// Restore email configuration
|
|
|
|
Email::set_mailer($this->originalMailer);
|
|
|
|
$this->originalMailer = null;
|
|
|
|
$this->mailer = null;
|
2008-09-18 05:59:00 +02:00
|
|
|
|
|
|
|
// Restore password validation
|
|
|
|
Member::set_password_validator($this->originalMemberPasswordValidator);
|
2008-11-10 02:13:42 +01:00
|
|
|
|
|
|
|
// Restore requirements
|
|
|
|
Requirements::set_backend($this->originalRequirements);
|
2008-11-22 04:33:00 +01:00
|
|
|
|
|
|
|
// Mark test as no longer being run - we use originalIsRunningTest to allow for nested SapphireTest calls
|
|
|
|
self::$is_running_test = $this->originalIsRunningTest;
|
|
|
|
$this->originalIsRunningTest = null;
|
2010-04-12 05:21:30 +02:00
|
|
|
|
2010-04-13 03:44:45 +02:00
|
|
|
// Reset theme setting
|
|
|
|
SSViewer::set_theme($this->originalTheme);
|
|
|
|
|
2010-04-12 05:21:30 +02:00
|
|
|
// Reset mocked datetime
|
2010-04-12 05:24:29 +02:00
|
|
|
SS_Datetime::clear_mock_now();
|
2010-04-14 05:49:06 +02:00
|
|
|
|
2011-03-22 23:49:44 +01:00
|
|
|
if(class_exists('SiteTree')) {
|
|
|
|
// Restore nested_urls state
|
|
|
|
if ( $this->originalNestedURLsState )
|
|
|
|
SiteTree::enable_nested_urls();
|
|
|
|
else
|
|
|
|
SiteTree::disable_nested_urls();
|
|
|
|
}
|
2010-04-14 05:49:06 +02:00
|
|
|
|
|
|
|
// Stop the redirection that might have been requested in the test.
|
|
|
|
// Note: Ideally a clean Controller should be created for each test.
|
|
|
|
// Now all tests executed in a batch share the same controller.
|
2010-10-19 05:36:12 +02:00
|
|
|
$controller = Controller::has_curr() ? Controller::curr() : null;
|
2010-04-14 05:49:06 +02:00
|
|
|
if ( $controller && $controller->response && $controller->response->getHeader('Location') ) {
|
|
|
|
$controller->response->setStatusCode(200);
|
|
|
|
$controller->response->removeHeader('Location');
|
|
|
|
}
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Clear the log of emails sent
|
|
|
|
*/
|
|
|
|
function clearEmails() {
|
|
|
|
return $this->mailer->clearEmails();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for an email that was sent.
|
|
|
|
* All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
|
|
|
|
* @param $to
|
|
|
|
* @param $from
|
|
|
|
* @param $subject
|
|
|
|
* @param $content
|
|
|
|
* @return An array containing the keys: 'type','to','from','subject','content', 'plainContent','attachedFiles','customHeaders','htmlContent',inlineImages'
|
|
|
|
*/
|
|
|
|
function findEmail($to, $from = null, $subject = null, $content = null) {
|
|
|
|
return $this->mailer->findEmail($to, $from, $subject, $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the matching email was sent since the last call to clearEmails()
|
|
|
|
* All of the parameters can either be a string, or, if they start with "/", a PREG-compatible regular expression.
|
|
|
|
* @param $to
|
|
|
|
* @param $from
|
|
|
|
* @param $subject
|
|
|
|
* @param $content
|
|
|
|
* @return An array containing the keys: 'type','to','from','subject','content', 'plainContent','attachedFiles','customHeaders','htmlContent',inlineImages'
|
|
|
|
*/
|
|
|
|
function assertEmailSent($to, $from = null, $subject = null, $content = null) {
|
|
|
|
// To do - this needs to be turned into a "real" PHPUnit ass
|
|
|
|
if(!$this->findEmail($to, $from, $subject, $content)) {
|
|
|
|
|
|
|
|
$infoParts = "";
|
|
|
|
$withParts = array();
|
|
|
|
if($to) $infoParts .= " to '$to'";
|
|
|
|
if($from) $infoParts .= " from '$from'";
|
|
|
|
if($subject) $withParts[] = "subject '$subject'";
|
|
|
|
if($content) $withParts[] = "content '$content'";
|
|
|
|
if($withParts) $infoParts .= " with " . implode(" and ", $withParts);
|
|
|
|
|
|
|
|
throw new PHPUnit_Framework_AssertionFailedError(
|
|
|
|
"Failed asserting that an email was sent$infoParts."
|
|
|
|
);
|
|
|
|
}
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
2009-08-24 08:14:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* Assert that the given {@link SS_List} includes DataObjects matching the given key-value
|
2009-08-24 08:14:54 +02:00
|
|
|
* pairs. Each match must correspond to 1 distinct record.
|
|
|
|
*
|
|
|
|
* @param $matches The patterns to match. Each pattern is a map of key-value pairs. You can
|
|
|
|
* either pass a single pattern or an array of patterns.
|
2011-10-26 08:09:04 +02:00
|
|
|
* @param $dataObjectSet The {@link SS_List} to test.
|
2009-08-24 08:14:54 +02:00
|
|
|
*
|
|
|
|
* Examples
|
|
|
|
* --------
|
|
|
|
* Check that $members includes an entry with Email = sam@example.com:
|
|
|
|
* $this->assertDOSContains(array('Email' => '...@example.com'), $members);
|
|
|
|
*
|
|
|
|
* Check that $members includes entries with Email = sam@example.com and with
|
|
|
|
* Email = ingo@example.com:
|
|
|
|
* $this->assertDOSContains(array(
|
|
|
|
* array('Email' => '...@example.com'),
|
|
|
|
* array('Email' => 'i...@example.com'),
|
|
|
|
* ), $members);
|
|
|
|
*/
|
|
|
|
function assertDOSContains($matches, $dataObjectSet) {
|
|
|
|
$extracted = array();
|
|
|
|
foreach($dataObjectSet as $item) $extracted[] = $item->toMap();
|
|
|
|
|
|
|
|
foreach($matches as $match) {
|
|
|
|
$matched = false;
|
|
|
|
foreach($extracted as $i => $item) {
|
|
|
|
if($this->dataObjectArrayMatch($item, $match)) {
|
|
|
|
// Remove it from $extracted so that we don't get duplicate mapping.
|
|
|
|
unset($extracted[$i]);
|
|
|
|
$matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We couldn't find a match - assertion failed
|
|
|
|
if(!$matched) {
|
|
|
|
throw new PHPUnit_Framework_AssertionFailedError(
|
2011-10-26 08:09:04 +02:00
|
|
|
"Failed asserting that the SS_List contains an item matching "
|
|
|
|
. var_export($match, true) . "\n\nIn the following SS_List:\n"
|
2009-08-24 08:14:54 +02:00
|
|
|
. $this->DOSSummaryForMatch($dataObjectSet, $match)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2008-08-13 04:47:14 +02:00
|
|
|
|
2009-08-24 08:14:54 +02:00
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* Assert that the given {@link SS_List} includes only DataObjects matching the given
|
2009-08-24 08:14:54 +02:00
|
|
|
* key-value pairs. Each match must correspond to 1 distinct record.
|
|
|
|
*
|
|
|
|
* @param $matches The patterns to match. Each pattern is a map of key-value pairs. You can
|
|
|
|
* either pass a single pattern or an array of patterns.
|
2011-10-26 08:09:04 +02:00
|
|
|
* @param $dataObjectSet The {@link SS_List} to test.
|
2009-08-24 08:14:54 +02:00
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* --------
|
|
|
|
* Check that *only* the entries Sam Minnee and Ingo Schommer exist in $members. Order doesn't
|
|
|
|
* matter:
|
|
|
|
* $this->assertDOSEquals(array(
|
|
|
|
* array('FirstName' =>'Sam', 'Surname' => 'Minnee'),
|
|
|
|
* array('FirstName' => 'Ingo', 'Surname' => 'Schommer'),
|
|
|
|
* ), $members);
|
|
|
|
*/
|
|
|
|
function assertDOSEquals($matches, $dataObjectSet) {
|
2009-09-14 07:16:15 +02:00
|
|
|
if(!$dataObjectSet) return false;
|
|
|
|
|
2009-08-24 08:14:54 +02:00
|
|
|
$extracted = array();
|
|
|
|
foreach($dataObjectSet as $item) $extracted[] = $item->toMap();
|
|
|
|
|
|
|
|
foreach($matches as $match) {
|
|
|
|
$matched = false;
|
|
|
|
foreach($extracted as $i => $item) {
|
|
|
|
if($this->dataObjectArrayMatch($item, $match)) {
|
|
|
|
// Remove it from $extracted so that we don't get duplicate mapping.
|
|
|
|
unset($extracted[$i]);
|
|
|
|
$matched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We couldn't find a match - assertion failed
|
|
|
|
if(!$matched) {
|
|
|
|
throw new PHPUnit_Framework_AssertionFailedError(
|
2011-10-26 08:09:04 +02:00
|
|
|
"Failed asserting that the SS_List contains an item matching "
|
|
|
|
. var_export($match, true) . "\n\nIn the following SS_List:\n"
|
2009-08-24 08:14:54 +02:00
|
|
|
. $this->DOSSummaryForMatch($dataObjectSet, $match)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have leftovers than the DOS has extra data that shouldn't be there
|
|
|
|
if($extracted) {
|
|
|
|
// If we didn't break by this point then we couldn't find a match
|
|
|
|
throw new PHPUnit_Framework_AssertionFailedError(
|
2011-10-26 08:09:04 +02:00
|
|
|
"Failed asserting that the SS_List contained only the given items, the "
|
2009-08-24 08:14:54 +02:00
|
|
|
. "following items were left over:\n" . var_export($extracted, true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-26 08:09:04 +02:00
|
|
|
* Assert that the every record in the given {@link SS_List} matches the given key-value
|
2009-08-24 08:14:54 +02:00
|
|
|
* pairs.
|
|
|
|
*
|
|
|
|
* @param $match The pattern to match. The pattern is a map of key-value pairs.
|
2011-10-26 08:09:04 +02:00
|
|
|
* @param $dataObjectSet The {@link SS_List} to test.
|
2009-08-24 08:14:54 +02:00
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* --------
|
|
|
|
* Check that every entry in $members has a Status of 'Active':
|
|
|
|
* $this->assertDOSAllMatch(array('Status' => 'Active'), $members);
|
|
|
|
*/
|
|
|
|
function assertDOSAllMatch($match, $dataObjectSet) {
|
|
|
|
$extracted = array();
|
|
|
|
foreach($dataObjectSet as $item) $extracted[] = $item->toMap();
|
|
|
|
|
|
|
|
foreach($extracted as $i => $item) {
|
|
|
|
if(!$this->dataObjectArrayMatch($item, $match)) {
|
|
|
|
throw new PHPUnit_Framework_AssertionFailedError(
|
|
|
|
"Failed asserting that the the following item matched "
|
|
|
|
. var_export($match, true) . ": " . var_export($item, true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-21 06:55:31 +01:00
|
|
|
/**
|
|
|
|
* Backported from PHPUnit 3.4 in order to maintain backwards
|
|
|
|
* compatibility: assertType() is deprecated in PHPUnit 3.5 (with PHP 5.2.7+),
|
|
|
|
* but as SilverStripe 2.3 and 2.4 support PHP 5.1 we can't require it.
|
|
|
|
*/
|
|
|
|
public static function assertType($expected, $actual, $message = '') {
|
|
|
|
// PHPUnit_Util_DeprecatedFeature_Logger::log(
|
|
|
|
// 'assertType() will be removed in PHPUnit 3.6 and should no longer ' .
|
|
|
|
// 'be used. assertInternalType() should be used for asserting ' .
|
|
|
|
// 'internal types such as "integer" or "string" whereas ' .
|
|
|
|
// 'assertInstanceOf() should be used for asserting that an object is ' .
|
|
|
|
// 'an instance of a specified class or interface.'
|
|
|
|
// );
|
|
|
|
|
|
|
|
if (is_string($expected)) {
|
|
|
|
if (PHPUnit_Util_Type::isType($expected)) {
|
|
|
|
$constraint = new PHPUnit_Framework_Constraint_IsType(
|
|
|
|
$expected
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (class_exists($expected) || interface_exists($expected)) {
|
|
|
|
$constraint = new PHPUnit_Framework_Constraint_IsInstanceOf(
|
|
|
|
$expected
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
throw PHPUnit_Util_InvalidArgumentHelper::factory(
|
|
|
|
1, 'class or interface name'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'string');
|
|
|
|
}
|
|
|
|
|
|
|
|
self::assertThat($actual, $constraint, $message);
|
|
|
|
}
|
2012-03-14 02:43:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide assertEmpty() in PHPUnit <3.5.
|
|
|
|
* We want to support PHPUnit 3.4, as this is the most recent release available
|
|
|
|
* to environments running PHP <=5.2.6, such as Debian Lenny.
|
|
|
|
*/
|
|
|
|
public static function assertEmpty($item, $message = '') {
|
|
|
|
if(class_exists('PHPUnit_Framework_Constraint_IsEmpty')) {
|
|
|
|
parent::assertEmpty($item, $message);
|
|
|
|
} else {
|
|
|
|
if(!empty($item)) {
|
|
|
|
$message = $message ? $message : "Failed asserting that " . var_export($item, true) . " is empty.";
|
|
|
|
throw new PHPUnit_Framework_AssertionFailedError($message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-21 06:55:31 +01:00
|
|
|
|
2009-08-24 08:14:54 +02:00
|
|
|
/**
|
|
|
|
* Helper function for the DOS matchers
|
|
|
|
*/
|
|
|
|
private function dataObjectArrayMatch($item, $match) {
|
|
|
|
foreach($match as $k => $v) {
|
2011-03-27 15:48:27 +02:00
|
|
|
if(!array_key_exists($k, $item) || $item[$k] != $v) return false;
|
2009-08-24 08:14:54 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for the DOS matchers
|
|
|
|
*/
|
|
|
|
private function DOSSummaryForMatch($dataObjectSet, $match) {
|
|
|
|
$extracted = array();
|
|
|
|
foreach($dataObjectSet as $item) $extracted[] = array_intersect_key($item->toMap(), $match);
|
|
|
|
return var_export($extracted, true);
|
|
|
|
}
|
|
|
|
|
2008-08-27 10:19:46 +02:00
|
|
|
/**
|
|
|
|
* Returns true if we are currently using a temporary database
|
|
|
|
*/
|
|
|
|
static function using_temp_db() {
|
|
|
|
$dbConn = DB::getConn();
|
2010-12-21 05:46:43 +01:00
|
|
|
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
|
|
|
|
return $dbConn && (substr($dbConn->currentDatabase(), 0, strlen($prefix) + 5) == strtolower(sprintf('%stmpdb', $prefix)));
|
2008-08-27 10:19:46 +02:00
|
|
|
}
|
|
|
|
|
2008-08-13 04:47:14 +02:00
|
|
|
static function kill_temp_db() {
|
|
|
|
// Delete our temporary database
|
2008-08-27 10:19:46 +02:00
|
|
|
if(self::using_temp_db()) {
|
|
|
|
$dbConn = DB::getConn();
|
2008-08-13 04:47:14 +02:00
|
|
|
$dbName = $dbConn->currentDatabase();
|
2009-05-07 05:49:15 +02:00
|
|
|
if($dbName && DB::getConn()->databaseExists($dbName)) {
|
2011-04-15 11:35:30 +02:00
|
|
|
// Some DataExtensions keep a static cache of information that needs to
|
2010-04-14 05:53:44 +02:00
|
|
|
// be reset whenever the database is killed
|
2011-04-15 11:35:30 +02:00
|
|
|
foreach(ClassInfo::subclassesFor('DataExtension') as $class) {
|
2010-04-14 05:53:44 +02:00
|
|
|
$toCall = array($class, 'on_db_reset');
|
|
|
|
if(is_callable($toCall)) call_user_func($toCall);
|
|
|
|
}
|
2010-04-12 05:24:29 +02:00
|
|
|
|
|
|
|
// echo "Deleted temp database " . $dbConn->currentDatabase() . "\n";
|
|
|
|
$dbConn->dropDatabase();
|
2008-08-13 04:47:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-12 05:21:30 +02:00
|
|
|
/**
|
|
|
|
* Remove all content from the temporary database.
|
|
|
|
*/
|
|
|
|
static function empty_temp_db() {
|
|
|
|
if(self::using_temp_db()) {
|
|
|
|
$dbadmin = new DatabaseAdmin();
|
|
|
|
$dbadmin->clearAllData();
|
2010-04-14 05:53:44 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// Some DataExtensions keep a static cache of information that needs to
|
2010-04-14 05:53:44 +02:00
|
|
|
// be reset whenever the database is cleaned out
|
2011-04-15 11:35:30 +02:00
|
|
|
foreach(array_merge(ClassInfo::subclassesFor('DataExtension'), ClassInfo::subclassesFor('DataObject')) as $class) {
|
2010-04-14 05:53:44 +02:00
|
|
|
$toCall = array($class, 'on_db_reset');
|
|
|
|
if(is_callable($toCall)) call_user_func($toCall);
|
|
|
|
}
|
2010-04-12 05:21:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-13 04:47:14 +02:00
|
|
|
static function create_temp_db() {
|
2010-10-19 02:42:53 +02:00
|
|
|
// Disable PHPUnit error handling
|
|
|
|
restore_error_handler();
|
2012-04-11 17:22:55 +02:00
|
|
|
|
2012-03-11 17:59:09 +01:00
|
|
|
// Create a temporary database, and force the connection to use UTC for time
|
|
|
|
global $databaseConfig;
|
|
|
|
$databaseConfig['timezone'] = '+0:00';
|
|
|
|
DB::connect($databaseConfig);
|
2008-08-13 04:47:14 +02:00
|
|
|
$dbConn = DB::getConn();
|
2010-12-21 05:46:43 +01:00
|
|
|
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
|
|
|
|
$dbname = strtolower(sprintf('%stmpdb', $prefix)) . rand(1000000,9999999);
|
2008-08-13 04:47:14 +02:00
|
|
|
while(!$dbname || $dbConn->databaseExists($dbname)) {
|
2010-12-21 05:46:43 +01:00
|
|
|
$dbname = strtolower(sprintf('%stmpdb', $prefix)) . rand(1000000,9999999);
|
2008-08-13 04:47:14 +02:00
|
|
|
}
|
2010-04-12 04:14:33 +02:00
|
|
|
|
2008-08-13 04:47:14 +02:00
|
|
|
$dbConn->selectDatabase($dbname);
|
|
|
|
$dbConn->createDatabase();
|
2010-04-12 04:14:33 +02:00
|
|
|
|
|
|
|
$st = new SapphireTest();
|
|
|
|
$st->resetDBSchema();
|
2008-08-27 10:19:46 +02:00
|
|
|
|
2010-10-19 02:42:53 +02:00
|
|
|
// Reinstate PHPUnit error handling
|
|
|
|
set_error_handler(array('PHPUnit_Util_ErrorHandler', 'handleError'));
|
|
|
|
|
2008-08-27 10:19:46 +02:00
|
|
|
return $dbname;
|
2008-08-13 04:47:14 +02:00
|
|
|
}
|
2009-03-04 08:31:23 +01:00
|
|
|
|
|
|
|
static function delete_all_temp_dbs() {
|
2010-12-21 05:46:43 +01:00
|
|
|
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
|
2009-03-04 08:31:23 +01:00
|
|
|
foreach(DB::getConn()->allDatabaseNames() as $dbName) {
|
2010-12-11 06:27:34 +01:00
|
|
|
if(preg_match(sprintf('/^%stmpdb[0-9]+$/', $prefix), $dbName)) {
|
2009-03-04 08:31:23 +01:00
|
|
|
DB::getConn()->dropDatabaseByName($dbName);
|
2010-12-05 09:35:48 +01:00
|
|
|
if(Director::is_cli()) {
|
|
|
|
echo "Dropped database \"$dbName\"" . PHP_EOL;
|
|
|
|
} else {
|
|
|
|
echo "<li>Dropped database \"$dbName\"</li>" . PHP_EOL;
|
|
|
|
}
|
2009-03-04 08:31:23 +01:00
|
|
|
flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-10-16 00:40:17 +02:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
/**
|
|
|
|
* Reset the testing database's schema.
|
|
|
|
* @param $includeExtraDataObjects If true, the extraDataObjects tables will also be included
|
|
|
|
*/
|
|
|
|
function resetDBSchema($includeExtraDataObjects = false) {
|
|
|
|
if(self::using_temp_db()) {
|
|
|
|
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
|
|
|
|
global $_SINGLETONS;
|
|
|
|
$_SINGLETONS = array();
|
|
|
|
|
|
|
|
$dataClasses = ClassInfo::subclassesFor('DataObject');
|
|
|
|
array_shift($dataClasses);
|
|
|
|
|
|
|
|
$conn = DB::getConn();
|
|
|
|
$conn->beginSchemaUpdate();
|
|
|
|
DB::quiet();
|
|
|
|
|
|
|
|
foreach($dataClasses as $dataClass) {
|
|
|
|
// Check if class exists before trying to instantiate - this sidesteps any manifest weirdness
|
|
|
|
if(class_exists($dataClass)) {
|
|
|
|
$SNG = singleton($dataClass);
|
|
|
|
if(!($SNG instanceof TestOnly)) $SNG->requireTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have additional dataobjects which need schema, do so here:
|
|
|
|
if($includeExtraDataObjects && $this->extraDataObjects) {
|
|
|
|
foreach($this->extraDataObjects as $dataClass) {
|
|
|
|
$SNG = singleton($dataClass);
|
|
|
|
if(singleton($dataClass) instanceof DataObject) $SNG->requireTable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$conn->endSchemaUpdate();
|
|
|
|
|
|
|
|
ClassInfo::reset_db_cache();
|
|
|
|
singleton('DataObject')->flushCache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-16 00:40:17 +02:00
|
|
|
/**
|
|
|
|
* Create a member and group with the given permission code, and log in with it.
|
|
|
|
* Returns the member ID.
|
|
|
|
*/
|
2010-04-14 06:41:30 +02:00
|
|
|
function logInWithPermission($permCode = "ADMIN") {
|
2009-10-16 00:40:17 +02:00
|
|
|
if(!isset($this->cache_generatedMembers[$permCode])) {
|
|
|
|
$group = new Group();
|
|
|
|
$group->Title = "$permCode group";
|
|
|
|
$group->write();
|
|
|
|
|
|
|
|
$permission = new Permission();
|
|
|
|
$permission->Code = $permCode;
|
|
|
|
$permission->write();
|
|
|
|
$group->Permissions()->add($permission);
|
|
|
|
|
2010-05-25 06:24:53 +02:00
|
|
|
$member = DataObject::get_one('Member', sprintf('"Email" = \'%s\'', "$permCode@example.org"));
|
|
|
|
if(!$member) $member = new Member();
|
|
|
|
|
2009-10-16 00:40:17 +02:00
|
|
|
$member->FirstName = $permCode;
|
|
|
|
$member->Surname = "User";
|
|
|
|
$member->Email = "$permCode@example.org";
|
|
|
|
$member->write();
|
|
|
|
$group->Members()->add($member);
|
|
|
|
|
|
|
|
$this->cache_generatedMembers[$permCode] = $member;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->cache_generatedMembers[$permCode]->logIn();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cache for logInWithPermission()
|
|
|
|
*/
|
|
|
|
protected $cache_generatedMembers = array();
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|