2007-08-15 08:38:41 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Dev;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\CMS\Controllers\RootURLController;
|
|
|
|
use SilverStripe\CMS\Model\SiteTree;
|
|
|
|
use SilverStripe\Control\Cookie;
|
|
|
|
use SilverStripe\Control\Email\Email;
|
|
|
|
use SilverStripe\Control\Session;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\Core\Manifest\SS_ClassManifest;
|
|
|
|
use SilverStripe\Core\Manifest\SS_ClassLoader;
|
|
|
|
use SilverStripe\Core\Manifest\SS_ConfigStaticManifest;
|
|
|
|
use SilverStripe\i18n\i18n;
|
|
|
|
use SilverStripe\ORM\SS_List;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\Hierarchy\Hierarchy;
|
|
|
|
use SilverStripe\ORM\DataModel;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
|
|
|
use SilverStripe\ORM\DB;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Security;
|
|
|
|
use SilverStripe\Security\Group;
|
|
|
|
use SilverStripe\Security\Permission;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
use SilverStripe\View\SSViewer;
|
2016-07-19 04:09:15 +02:00
|
|
|
use SilverStripe\View\ThemeResourceLoader;
|
2016-07-13 14:36:52 +02:00
|
|
|
use SilverStripe\View\ThemeManifest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use PHPUnit_Framework_TestCase;
|
|
|
|
use Translatable;
|
|
|
|
use LogicException;
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-08-05 00:32:13 +02:00
|
|
|
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
/**
|
|
|
|
* Test case class for the Sapphire framework.
|
2012-09-26 23:34:00 +02:00
|
|
|
* Sapphire unit testing is based on PHPUnit, but provides a number of hooks into our data model that make it easier
|
|
|
|
* to work with.
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
|
|
|
class SapphireTest extends PHPUnit_Framework_TestCase {
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
/** @config */
|
|
|
|
private static $dependencies = array(
|
2012-12-07 18:44:00 +01:00
|
|
|
'fixtureFactory' => '%$FixtureFactory',
|
|
|
|
);
|
|
|
|
|
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.
|
2015-03-06 23:37:06 +01:00
|
|
|
*
|
2009-07-08 02:06:16 +02:00
|
|
|
* @var string|array
|
2008-08-11 00:49:59 +02:00
|
|
|
*/
|
2013-03-21 19:48:54 +01:00
|
|
|
protected static $fixture_file = null;
|
2012-12-07 18:44:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FixtureFactory
|
|
|
|
*/
|
|
|
|
protected $fixtureFactory;
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2009-11-30 02:48:26 +01:00
|
|
|
/**
|
|
|
|
* @var Boolean If set to TRUE, this will force a test database to be generated
|
2015-03-06 23:37:06 +01:00
|
|
|
* in {@link setUp()}. Note that this flag is overruled by the presence of a
|
2009-11-30 02:48:26 +01:00
|
|
|
* {@link $fixture_file}, which always forces a database build.
|
|
|
|
*/
|
|
|
|
protected $usesDatabase = null;
|
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-14 05:49:06 +02:00
|
|
|
protected $originalNestedURLsState;
|
2010-10-15 04:58:02 +02:00
|
|
|
protected $originalMemoryLimit;
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @var TestMailer
|
|
|
|
*/
|
2008-04-26 08:32:31 +02:00
|
|
|
protected $mailer;
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2012-05-28 04:25:34 +02:00
|
|
|
/**
|
|
|
|
* Pointer to the manifest that isn't a test manifest
|
|
|
|
*/
|
|
|
|
protected static $regular_manifest;
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2010-10-15 05:43:30 +02:00
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2008-11-22 04:33:00 +01:00
|
|
|
protected static $is_running_test = false;
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @var SS_ClassManifest
|
|
|
|
*/
|
2012-05-28 04:25:34 +02:00
|
|
|
protected static $test_class_manifest;
|
2015-03-06 23:37:06 +01:00
|
|
|
|
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();
|
2015-03-06 23:37:06 +01:00
|
|
|
|
|
|
|
|
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.
|
2015-03-06 23:37:06 +01:00
|
|
|
*
|
2010-04-12 03:52:39 +02:00
|
|
|
* 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.
|
2015-03-06 23:37:06 +01:00
|
|
|
*
|
2010-04-12 03:52:39 +02:00
|
|
|
* 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.
|
2015-03-06 23:37:06 +01:00
|
|
|
*
|
2010-10-19 04:46:26 +02:00
|
|
|
* Example:
|
|
|
|
* <code>
|
|
|
|
* array("MyTreeDataObject" => array("Versioned", "Hierarchy"))
|
|
|
|
* </code>
|
2010-04-12 03:52:39 +02:00
|
|
|
*/
|
|
|
|
protected $requiredExtensions = array(
|
|
|
|
);
|
2015-03-06 23:37:06 +01:00
|
|
|
|
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();
|
2015-03-06 23:37:06 +01:00
|
|
|
|
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.
|
2015-03-06 23:37:06 +01:00
|
|
|
*
|
2009-08-27 06:45:58 +02:00
|
|
|
* @see http://sebastian-bergmann.de/archives/797-Global-Variables-and-PHPUnit.html
|
|
|
|
*/
|
|
|
|
protected $backupGlobals = FALSE;
|
2010-04-12 03:52:39 +02:00
|
|
|
|
2015-03-06 23:37:06 +01:00
|
|
|
/**
|
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
|
|
|
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2010-10-15 05:43:30 +02:00
|
|
|
/**
|
2015-07-16 11:32:42 +02:00
|
|
|
* Determines if unit tests are currently run, flag set during test bootstrap.
|
2010-10-15 05:43:30 +02:00
|
|
|
* 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.
|
2015-03-06 23:37:06 +01:00
|
|
|
*
|
2010-10-15 05:43:30 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2008-11-22 04:33:00 +01:00
|
|
|
public static function is_running_test() {
|
|
|
|
return self::$is_running_test;
|
|
|
|
}
|
2012-05-28 04:25:34 +02:00
|
|
|
|
2013-01-09 23:31:10 +01:00
|
|
|
public static function set_is_running_test($bool) {
|
2015-03-06 23:37:06 +01:00
|
|
|
self::$is_running_test = $bool;
|
2013-01-09 23:31:10 +01:00
|
|
|
}
|
2012-05-28 04:25:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the manifest to be used to look up test classes by helper functions
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param SS_ClassManifest $manifest
|
2012-05-28 04:25:34 +02:00
|
|
|
*/
|
|
|
|
public static function set_test_class_manifest($manifest) {
|
|
|
|
self::$test_class_manifest = $manifest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the manifest being used to look up test classes by helper functions
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @return SS_ClassManifest
|
2012-05-28 04:25:34 +02:00
|
|
|
*/
|
|
|
|
public static function get_test_class_manifest() {
|
|
|
|
return self::$test_class_manifest;
|
|
|
|
}
|
2013-03-21 19:48:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public static function get_fixture_file() {
|
|
|
|
return static::$fixture_file;
|
|
|
|
}
|
|
|
|
|
2011-05-01 07:33:02 +02:00
|
|
|
protected $model;
|
2014-08-18 05:49:32 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
/**
|
|
|
|
* State of Versioned before this test is run
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $originalReadingMode = null;
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUp() {
|
2015-03-06 23:37:06 +01:00
|
|
|
|
|
|
|
//nest config and injector for each test so they are effectively sandboxed per test
|
|
|
|
Config::nest();
|
|
|
|
Injector::nest();
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->originalReadingMode = Versioned::get_reading_mode();
|
2016-01-26 04:56:07 +01:00
|
|
|
|
2011-09-26 05:08:41 +02:00
|
|
|
// We cannot run the tests on this abstract class.
|
2016-08-19 00:51:35 +02:00
|
|
|
if(get_class($this) == __CLASS__) {
|
2015-07-16 12:12:15 +02:00
|
|
|
$this->markTestSkipped(sprintf('Skipping %s ', get_class($this)));
|
2011-09-26 05:08:41 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2008-11-22 04:33:00 +01:00
|
|
|
// Mark test as being run
|
|
|
|
$this->originalIsRunningTest = self::$is_running_test;
|
|
|
|
self::$is_running_test = true;
|
2013-03-21 19:48:54 +01:00
|
|
|
|
2010-10-19 02:37:55 +02:00
|
|
|
// i18n needs to be set to the defaults or tests fail
|
2016-08-19 00:51:35 +02:00
|
|
|
i18n::set_locale(i18n::config()->get('default_locale'));
|
2013-03-21 19:48:54 +01:00
|
|
|
i18n::config()->date_format = null;
|
|
|
|
i18n::config()->time_format = null;
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2012-03-11 17:59:09 +01:00
|
|
|
// Set default timezone consistently to avoid NZ-specific dependencies
|
|
|
|
date_default_timezone_set('UTC');
|
2015-03-06 23:37:06 +01:00
|
|
|
|
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);
|
2016-08-19 00:51:35 +02:00
|
|
|
Cookie::config()->update('report_errors', false);
|
|
|
|
if(class_exists('SilverStripe\\CMS\\Controllers\\RootURLController')) {
|
|
|
|
RootURLController::reset();
|
|
|
|
}
|
|
|
|
if(class_exists('Translatable')) {
|
|
|
|
Translatable::reset();
|
|
|
|
}
|
2009-08-11 06:45:54 +02:00
|
|
|
Versioned::reset();
|
|
|
|
DataObject::reset();
|
2016-08-19 00:51:35 +02:00
|
|
|
if(class_exists('SilverStripe\\CMS\\Model\\SiteTree')) {
|
|
|
|
SiteTree::reset();
|
|
|
|
}
|
2009-11-30 03:24:46 +01:00
|
|
|
Hierarchy::reset();
|
2016-08-19 00:51:35 +02:00
|
|
|
if(Controller::has_curr()) {
|
|
|
|
Controller::curr()->setSession(Session::create(array()));
|
|
|
|
}
|
2012-04-11 17:22:55 +02:00
|
|
|
Security::$database_is_ready = null;
|
2013-01-29 14:14:47 +01:00
|
|
|
|
|
|
|
// Add controller-name auto-routing
|
2016-08-19 00:51:35 +02:00
|
|
|
// @todo Fix to work with namespaced controllers
|
|
|
|
Director::config()->update('rules', array(
|
2013-01-29 14:14:47 +01:00
|
|
|
'$Controller//$Action/$ID/$OtherID' => '*'
|
|
|
|
));
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
$fixtureFile = static::get_fixture_file();
|
2011-05-21 04:42:26 +02:00
|
|
|
|
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
|
2016-05-20 16:37:52 +02:00
|
|
|
if($fixtureFile || $this->usesDatabase) {
|
|
|
|
if (!self::using_temp_db()) {
|
2008-08-13 04:47:14 +02:00
|
|
|
self::create_temp_db();
|
2008-03-17 03:04:58 +01:00
|
|
|
}
|
2008-08-13 04:47:14 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
DataObject::singleton()->flushCache();
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2010-04-12 05:21:30 +02:00
|
|
|
self::empty_temp_db();
|
2015-03-06 23:37:06 +01:00
|
|
|
|
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.
|
2015-03-06 23:37:06 +01:00
|
|
|
$isRelativeToFile = (strpos('/', $fixtureFilePath) === false
|
2012-09-26 23:34:00 +02:00
|
|
|
|| preg_match('/^\.\./', $fixtureFilePath));
|
|
|
|
|
2011-03-30 08:47:30 +02:00
|
|
|
if($isRelativeToFile) {
|
|
|
|
$resolvedPath = realpath($pathForClass . '/' . $fixtureFilePath);
|
2016-08-19 00:51:35 +02:00
|
|
|
if($resolvedPath) {
|
|
|
|
$fixtureFilePath = $resolvedPath;
|
|
|
|
}
|
2011-03-30 08:47:30 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
$fixture = YamlFixture::create($fixtureFilePath);
|
2012-12-07 18:44:00 +01:00
|
|
|
$fixture->writeInto($this->getFixtureFactory());
|
2010-05-25 06:17:21 +02:00
|
|
|
$i++;
|
|
|
|
}
|
2009-07-08 02:06:16 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2010-04-14 06:41:30 +02:00
|
|
|
$this->logInWithPermission("ADMIN");
|
2008-03-17 03:04:58 +01:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2010-10-15 04:58:02 +02:00
|
|
|
// Preserve memory settings
|
|
|
|
$this->originalMemoryLimit = ini_get('memory_limit');
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2012-04-13 23:47:17 +02:00
|
|
|
// turn off template debugging
|
2016-08-19 00:51:35 +02:00
|
|
|
SSViewer::config()->update('source_file_comments', false);
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2012-01-31 22:36:53 +01:00
|
|
|
// Clear requirements
|
|
|
|
Requirements::clear();
|
2016-04-04 11:48:56 +02:00
|
|
|
|
|
|
|
// Set up email
|
|
|
|
$this->mailer = new TestMailer();
|
2016-08-19 00:51:35 +02:00
|
|
|
Injector::inst()->registerService($this->mailer, 'SilverStripe\\Control\\Email\\Mailer');
|
|
|
|
Email::config()->remove('send_all_emails_to');
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
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.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setUpOnce() {
|
2015-03-06 23:37:06 +01:00
|
|
|
//nest config and injector for each suite so they are effectively sandboxed
|
|
|
|
Config::nest();
|
|
|
|
Injector::nest();
|
2012-11-23 11:12:03 +01:00
|
|
|
$isAltered = false;
|
|
|
|
|
2013-06-19 00:13:02 +02:00
|
|
|
if(!Director::isDev()) {
|
|
|
|
user_error('Tests can only run in "dev" mode', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2010-04-12 03:52:39 +02:00
|
|
|
// Remove any illegal extensions that are present
|
|
|
|
foreach($this->illegalExtensions as $class => $extensions) {
|
|
|
|
foreach($extensions as $extension) {
|
2012-10-09 23:59:23 +02:00
|
|
|
if ($class::has_extension($extension)) {
|
2010-04-12 03:52:39 +02:00
|
|
|
if(!isset($this->extensionsToReapply[$class])) $this->extensionsToReapply[$class] = array();
|
|
|
|
$this->extensionsToReapply[$class][] = $extension;
|
2012-10-09 23:30:40 +02:00
|
|
|
$class::remove_extension($extension);
|
2010-04-12 03:52:39 +02:00
|
|
|
$isAltered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add any required extensions that aren't present
|
|
|
|
foreach($this->requiredExtensions as $class => $extensions) {
|
|
|
|
$this->extensionsToRemove[$class] = array();
|
|
|
|
foreach($extensions as $extension) {
|
2012-10-09 23:59:23 +02:00
|
|
|
if(!$class::has_extension($extension)) {
|
2010-04-12 03:52:39 +02:00
|
|
|
if(!isset($this->extensionsToRemove[$class])) $this->extensionsToReapply[$class] = array();
|
|
|
|
$this->extensionsToRemove[$class][] = $extension;
|
2012-10-09 05:55:37 +02:00
|
|
|
$class::add_extension($extension);
|
2010-04-12 03:52:39 +02:00
|
|
|
$isAltered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2010-04-12 03:52:39 +02:00
|
|
|
// If we have made changes to the extensions present, then migrate the database schema.
|
2012-11-23 11:12:03 +01:00
|
|
|
if($isAltered || $this->extensionsToReapply || $this->extensionsToRemove || $this->extraDataObjects) {
|
2016-08-19 00:51:35 +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
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
// clear singletons, they're caching old extension info
|
2009-10-22 02:30:21 +02:00
|
|
|
// which is used in DatabaseAdmin->doBuild()
|
2012-06-11 12:40:47 +02:00
|
|
|
Injector::inst()->unregisterAllObjects();
|
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
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2010-04-12 03:52:39 +02:00
|
|
|
/**
|
|
|
|
* tearDown method that's called once per test class rather once per test method.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function tearDownOnce() {
|
2015-08-21 01:18:58 +02:00
|
|
|
// If we have made changes to the extensions present, then migrate the database schema.
|
|
|
|
if($this->extensionsToReapply || $this->extensionsToRemove) {
|
|
|
|
// @todo: This isn't strictly necessary to restore extensions, but only to ensure that
|
|
|
|
// Object::$extra_methods is properly flushed. This should be replaced with a simple
|
|
|
|
// flush mechanism for each $class.
|
|
|
|
//
|
|
|
|
// Remove extensions added for testing
|
|
|
|
foreach($this->extensionsToRemove as $class => $extensions) {
|
|
|
|
foreach($extensions as $extension) {
|
|
|
|
$class::remove_extension($extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reapply ones removed
|
|
|
|
foreach($this->extensionsToReapply as $class => $extensions) {
|
|
|
|
foreach($extensions as $extension) {
|
|
|
|
$class::add_extension($extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-03-06 23:37:06 +01:00
|
|
|
//unnest injector / config now that the test suite is over
|
|
|
|
// this will reset all the extensions on the object too (see setUpOnce)
|
|
|
|
Injector::unnest();
|
|
|
|
Config::unnest();
|
2013-03-21 19:48:54 +01:00
|
|
|
|
2015-03-07 11:32:17 +01:00
|
|
|
if(!empty($this->extensionsToReapply) || !empty($this->extensionsToRemove) || !empty($this->extraDataObjects)) {
|
2010-04-12 04:03:16 +02:00
|
|
|
$this->resetDBSchema();
|
2010-04-12 03:52:39 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2009-04-22 05:22:09 +02:00
|
|
|
/**
|
2012-12-07 18:44:00 +01:00
|
|
|
* @return FixtureFactory
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
2012-12-07 18:44:00 +01:00
|
|
|
public function getFixtureFactory() {
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->fixtureFactory) $this->fixtureFactory = Injector::inst()->create('SilverStripe\\Dev\\FixtureFactory');
|
2012-12-07 18:44:00 +01:00
|
|
|
return $this->fixtureFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFixtureFactory(FixtureFactory $factory) {
|
|
|
|
$this->fixtureFactory = $factory;
|
|
|
|
return $this;
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
/**
|
|
|
|
* Get the ID of an object from the fixture.
|
2015-03-06 23:37:06 +01:00
|
|
|
*
|
2016-02-25 05:32:41 +01:00
|
|
|
* @param string $className The data class, as specified in your fixture file. Parent classes won't work
|
|
|
|
* @param string $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) {
|
2012-12-07 18:44:00 +01:00
|
|
|
$id = $this->getFixtureFactory()->getId($className, $identifier);
|
|
|
|
|
|
|
|
if(!$id) {
|
|
|
|
user_error(sprintf(
|
|
|
|
"Couldn't find object '%s' (class: %s)",
|
|
|
|
$identifier,
|
|
|
|
$className
|
|
|
|
), E_USER_ERROR);
|
2009-07-08 02:06:16 +02:00
|
|
|
}
|
2012-04-18 13:10:57 +02:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
return $id;
|
2007-08-16 08:36:02 +02:00
|
|
|
}
|
2014-02-20 14:19:53 +01:00
|
|
|
|
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.
|
2014-02-20 14:19:53 +01:00
|
|
|
*
|
2009-07-08 02:06:16 +02:00
|
|
|
* @param string $className
|
2015-08-26 16:46:52 +02:00
|
|
|
* @return array A map of fixture-identifier => object-id
|
2007-08-16 08:36:02 +02:00
|
|
|
*/
|
|
|
|
protected function allFixtureIDs($className) {
|
2012-12-07 18:44:00 +01:00
|
|
|
return $this->getFixtureFactory()->getIds($className);
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an object from the fixture.
|
2014-02-20 14:19:53 +01:00
|
|
|
*
|
|
|
|
* @param string $className The data class, as specified in your fixture file. Parent classes won't work
|
|
|
|
* @param string $identifier The identifier string, as provided in your fixture file
|
|
|
|
*
|
|
|
|
* @return DataObject
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
|
|
|
protected function objFromFixture($className, $identifier) {
|
2012-12-07 18:44:00 +01:00
|
|
|
$obj = $this->getFixtureFactory()->get($className, $identifier);
|
|
|
|
|
|
|
|
if(!$obj) {
|
|
|
|
user_error(sprintf(
|
|
|
|
"Couldn't find object '%s' (class: %s)",
|
|
|
|
$identifier,
|
|
|
|
$className
|
2015-03-06 23:37:06 +01:00
|
|
|
), E_USER_ERROR);
|
2009-07-08 02:06:16 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2012-12-07 18:44:00 +01:00
|
|
|
return $obj;
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
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.
|
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $fixtureFile The location of the .yml fixture file, relative to the site base dir
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function loadFixture($fixtureFile) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$fixture = Injector::inst()->create('SilverStripe\\Dev\\YamlFixture', $fixtureFile);
|
2012-12-07 18:44:00 +01:00
|
|
|
$fixture->writeInto($this->getFixtureFactory());
|
2009-07-08 02:06:16 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2009-07-08 02:06:16 +02:00
|
|
|
/**
|
|
|
|
* Clear all fixtures which were previously loaded through
|
2015-03-06 23:37:06 +01:00
|
|
|
* {@link loadFixture()}
|
2009-07-08 02:06:16 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function clearFixtures() {
|
2012-12-07 18:44:00 +01:00
|
|
|
$this->getFixtureFactory()->clear();
|
2007-08-17 07:43:14 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2011-03-30 08:47:30 +02:00
|
|
|
/**
|
|
|
|
* Useful for writing unit tests without hardcoding folder structures.
|
2015-03-06 23:37:06 +01:00
|
|
|
*
|
2011-03-30 08:47:30 +02:00
|
|
|
* @return String Absolute path to current class.
|
|
|
|
*/
|
|
|
|
protected function getCurrentAbsolutePath() {
|
2012-05-28 04:25:34 +02:00
|
|
|
$filename = self::$test_class_manifest->getItemPath(get_class($this));
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$filename) {
|
|
|
|
throw new LogicException("getItemPath returned null for " . get_class($this));
|
|
|
|
}
|
2012-05-28 04:25:34 +02:00
|
|
|
return dirname($filename);
|
2011-03-30 08:47:30 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2011-03-30 08:47:30 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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
|
2015-03-06 23:37:06 +01:00
|
|
|
$this->mailer = null;
|
2008-09-18 05:59:00 +02:00
|
|
|
|
|
|
|
// Restore password validation
|
2012-12-14 11:45:26 +01:00
|
|
|
if($this->originalMemberPasswordValidator) {
|
2015-03-06 23:37:06 +01:00
|
|
|
Member::set_password_validator($this->originalMemberPasswordValidator);
|
2012-12-14 11:45:26 +01:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2008-11-10 02:13:42 +01:00
|
|
|
// Restore requirements
|
2012-12-14 11:45:26 +01:00
|
|
|
if($this->originalRequirements) {
|
2015-03-06 23:37:06 +01:00
|
|
|
Requirements::set_backend($this->originalRequirements);
|
2012-12-14 11:45:26 +01:00
|
|
|
}
|
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
|
|
|
|
|
|
|
// Reset mocked datetime
|
2015-08-30 07:02:55 +02:00
|
|
|
DBDatetime::clear_mock_now();
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2010-04-14 05:49:06 +02:00
|
|
|
// Stop the redirection that might have been requested in the test.
|
2015-03-06 23:37:06 +01:00
|
|
|
// Note: Ideally a clean Controller should be created for each test.
|
2010-04-14 05:49:06 +02:00
|
|
|
// 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;
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($controller && ($response = $controller->getResponse()) && $response->getHeader('Location')) {
|
|
|
|
$response->setStatusCode(200);
|
|
|
|
$response->removeHeader('Location');
|
2010-04-14 05:49:06 +02:00
|
|
|
}
|
2016-01-26 04:56:07 +01:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
Versioned::set_reading_mode($this->originalReadingMode);
|
2016-01-26 04:56:07 +01:00
|
|
|
|
2015-03-06 23:37:06 +01:00
|
|
|
//unnest injector / config now that tests are over
|
|
|
|
Injector::unnest();
|
|
|
|
Config::unnest();
|
2008-04-26 08:32:31 +02:00
|
|
|
}
|
2013-03-12 00:17:20 +01:00
|
|
|
|
2013-08-21 08:54:05 +02:00
|
|
|
public static function assertContains(
|
|
|
|
$needle,
|
|
|
|
$haystack,
|
|
|
|
$message = '',
|
|
|
|
$ignoreCase = FALSE,
|
2014-06-27 06:05:40 +02:00
|
|
|
$checkForObjectIdentity = TRUE,
|
|
|
|
$checkForNonObjectIdentity = false
|
2013-08-21 08:54:05 +02:00
|
|
|
) {
|
2013-03-12 00:17:20 +01:00
|
|
|
if ($haystack instanceof DBField) $haystack = (string)$haystack;
|
2014-06-27 06:05:40 +02:00
|
|
|
parent::assertContains($needle, $haystack, $message, $ignoreCase, $checkForObjectIdentity, $checkForNonObjectIdentity);
|
2013-03-12 00:17:20 +01:00
|
|
|
}
|
|
|
|
|
2013-08-21 08:54:05 +02:00
|
|
|
public static function assertNotContains(
|
|
|
|
$needle,
|
|
|
|
$haystack,
|
|
|
|
$message = '',
|
|
|
|
$ignoreCase = FALSE,
|
2014-06-27 06:05:40 +02:00
|
|
|
$checkForObjectIdentity = TRUE,
|
|
|
|
$checkForNonObjectIdentity = false
|
2013-08-21 08:54:05 +02:00
|
|
|
) {
|
2013-03-12 00:17:20 +01:00
|
|
|
if ($haystack instanceof DBField) $haystack = (string)$haystack;
|
2014-06-27 06:05:40 +02:00
|
|
|
parent::assertNotContains($needle, $haystack, $message, $ignoreCase, $checkForObjectIdentity, $checkForNonObjectIdentity);
|
2013-03-12 00:17:20 +01:00
|
|
|
}
|
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
/**
|
|
|
|
* Clear the log of emails sent
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function clearEmails() {
|
2008-04-26 08:32:31 +02:00
|
|
|
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
|
2012-09-26 23:34:00 +02:00
|
|
|
* @return array Contains keys: 'type', 'to', 'from', 'subject','content', 'plainContent', 'attachedFiles',
|
|
|
|
* 'customHeaders', 'htmlContent', 'inlineImages'
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function findEmail($to, $from = null, $subject = null, $content = null) {
|
2008-04-26 08:32:31 +02:00
|
|
|
return $this->mailer->findEmail($to, $from, $subject, $content);
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2008-04-26 08:32:31 +02:00
|
|
|
/**
|
|
|
|
* 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
|
2012-09-26 23:34:00 +02:00
|
|
|
* @return array Contains the keys: 'type', 'to', 'from', 'subject', 'content', 'plainContent', 'attachedFiles',
|
|
|
|
* 'customHeaders', 'htmlContent', inlineImages'
|
2008-04-26 08:32:31 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function assertEmailSent($to, $from = null, $subject = null, $content = null) {
|
2012-11-23 14:55:19 +01:00
|
|
|
$found = (bool)$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);
|
|
|
|
|
|
|
|
$this->assertTrue(
|
|
|
|
$found,
|
|
|
|
"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.
|
2015-03-06 23:37:06 +01:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param SS_List|array $matches The patterns to match. Each pattern is a map of key-value pairs. You can
|
2009-08-24 08:14:54 +02:00
|
|
|
* either pass a single pattern or an array of patterns.
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param SS_List $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:
|
2015-03-06 23:37:06 +01:00
|
|
|
* $this->assertDOSContains(array('Email' => '...@example.com'), $members);
|
|
|
|
*
|
|
|
|
* Check that $members includes entries with Email = sam@example.com and with
|
2009-08-24 08:14:54 +02:00
|
|
|
* Email = ingo@example.com:
|
2015-03-06 23:37:06 +01:00
|
|
|
* $this->assertDOSContains(array(
|
|
|
|
* array('Email' => '...@example.com'),
|
|
|
|
* array('Email' => 'i...@example.com'),
|
|
|
|
* ), $members);
|
2009-08-24 08:14:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function assertDOSContains($matches, $dataObjectSet) {
|
2009-08-24 08:14:54 +02:00
|
|
|
$extracted = array();
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach($dataObjectSet as $object) {
|
|
|
|
/** @var DataObject $object */
|
|
|
|
$extracted[] = $object->toMap();
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2009-08-24 08:14:54 +02:00
|
|
|
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
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$matched,
|
|
|
|
"Failed asserting that the SS_List contains an item matching "
|
2015-03-06 23:37:06 +01:00
|
|
|
. var_export($match, true) . "\n\nIn the following SS_List:\n"
|
2012-11-23 14:55:19 +01:00
|
|
|
. $this->DOSSummaryForMatch($dataObjectSet, $match)
|
|
|
|
);
|
2009-08-24 08:14:54 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
}
|
|
|
|
|
2009-08-24 08:14:54 +02:00
|
|
|
/**
|
2015-03-06 23:37:06 +01: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.
|
2015-03-06 23:37:06 +01:00
|
|
|
*
|
2009-08-24 08:14:54 +02:00
|
|
|
* Example
|
|
|
|
* --------
|
2015-03-06 23:37:06 +01:00
|
|
|
* Check that *only* the entries Sam Minnee and Ingo Schommer exist in $members. Order doesn't
|
2009-08-24 08:14:54 +02:00
|
|
|
* matter:
|
2015-03-06 23:37:06 +01:00
|
|
|
* $this->assertDOSEquals(array(
|
|
|
|
* array('FirstName' =>'Sam', 'Surname' => 'Minnee'),
|
|
|
|
* array('FirstName' => 'Ingo', 'Surname' => 'Schommer'),
|
|
|
|
* ), $members);
|
2016-02-25 05:32:41 +01:00
|
|
|
*
|
|
|
|
* @param mixed $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.
|
|
|
|
* @param mixed $dataObjectSet The {@link SS_List} to test.
|
2009-08-24 08:14:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function assertDOSEquals($matches, $dataObjectSet) {
|
2016-03-30 23:35:46 +02:00
|
|
|
// Extract dataobjects
|
2009-08-24 08:14:54 +02:00
|
|
|
$extracted = array();
|
2016-03-30 23:35:46 +02:00
|
|
|
if($dataObjectSet) {
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach ($dataObjectSet as $object) {
|
|
|
|
/** @var DataObject $object */
|
|
|
|
$extracted[] = $object->toMap();
|
2016-03-30 23:35:46 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2016-03-30 23:35:46 +02:00
|
|
|
// Check all matches
|
|
|
|
if($matches) {
|
|
|
|
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;
|
|
|
|
}
|
2009-08-24 08:14:54 +02:00
|
|
|
}
|
|
|
|
|
2016-03-30 23:35:46 +02:00
|
|
|
// We couldn't find a match - assertion failed
|
|
|
|
$this->assertTrue(
|
|
|
|
$matched,
|
|
|
|
"Failed asserting that the SS_List contains an item matching "
|
|
|
|
. var_export($match, true) . "\n\nIn the following SS_List:\n"
|
|
|
|
. $this->DOSSummaryForMatch($dataObjectSet, $match)
|
|
|
|
);
|
|
|
|
}
|
2009-08-24 08:14:54 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2009-08-24 08:14:54 +02:00
|
|
|
// If we have leftovers than the DOS has extra data that shouldn't be there
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
(count($extracted) == 0),
|
2009-08-24 08:14:54 +02:00
|
|
|
// If we didn't break by this point then we couldn't find a match
|
2012-11-23 14:55:19 +01:00
|
|
|
"Failed asserting that the SS_List contained only the given items, the "
|
|
|
|
. "following items were left over:\n" . var_export($extracted, true)
|
|
|
|
);
|
2015-03-06 23:37:06 +01:00
|
|
|
}
|
2009-08-24 08:14:54 +02:00
|
|
|
|
|
|
|
/**
|
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.
|
2015-03-06 23:37:06 +01:00
|
|
|
*
|
2009-08-24 08:14:54 +02:00
|
|
|
* Example
|
|
|
|
* --------
|
|
|
|
* Check that every entry in $members has a Status of 'Active':
|
2015-03-06 23:37:06 +01:00
|
|
|
* $this->assertDOSAllMatch(array('Status' => 'Active'), $members);
|
2016-02-25 05:32:41 +01:00
|
|
|
*
|
|
|
|
* @param mixed $match The pattern to match. The pattern is a map of key-value pairs.
|
|
|
|
* @param mixed $dataObjectSet The {@link SS_List} to test.
|
2009-08-24 08:14:54 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function assertDOSAllMatch($match, $dataObjectSet) {
|
2009-08-24 08:14:54 +02:00
|
|
|
$extracted = array();
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach($dataObjectSet as $object) {
|
|
|
|
/** @var DataObject $object */
|
|
|
|
$extracted[] = $object->toMap();
|
|
|
|
}
|
2009-08-24 08:14:54 +02:00
|
|
|
|
|
|
|
foreach($extracted as $i => $item) {
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
$this->dataObjectArrayMatch($item, $match),
|
2015-03-06 23:37:06 +01:00
|
|
|
"Failed asserting that the the following item matched "
|
2012-11-23 14:55:19 +01:00
|
|
|
. var_export($match, true) . ": " . var_export($item, true)
|
|
|
|
);
|
2009-08-24 08:14:54 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes sequences of repeated whitespace characters from SQL queries
|
|
|
|
* making them suitable for string comparison
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $sql
|
|
|
|
* @return string The cleaned and normalised SQL string
|
|
|
|
*/
|
|
|
|
protected function normaliseSQL($sql) {
|
|
|
|
return trim(preg_replace('/\s+/m', ' ', $sql));
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Asserts that two SQL queries are equivalent
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $expectedSQL
|
|
|
|
* @param string $actualSQL
|
|
|
|
* @param string $message
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param float|int $delta
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param integer $maxDepth
|
|
|
|
* @param boolean $canonicalize
|
|
|
|
* @param boolean $ignoreCase
|
|
|
|
*/
|
|
|
|
public function assertSQLEquals($expectedSQL, $actualSQL, $message = '', $delta = 0, $maxDepth = 10,
|
|
|
|
$canonicalize = false, $ignoreCase = false
|
|
|
|
) {
|
|
|
|
// Normalise SQL queries to remove patterns of repeating whitespace
|
|
|
|
$expectedSQL = $this->normaliseSQL($expectedSQL);
|
|
|
|
$actualSQL = $this->normaliseSQL($actualSQL);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->assertEquals($expectedSQL, $actualSQL, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that a SQL query contains a SQL fragment
|
|
|
|
*
|
|
|
|
* @param string $needleSQL
|
|
|
|
* @param string $haystackSQL
|
|
|
|
* @param string $message
|
|
|
|
* @param boolean $ignoreCase
|
|
|
|
* @param boolean $checkForObjectIdentity
|
|
|
|
*/
|
|
|
|
public function assertSQLContains($needleSQL, $haystackSQL, $message = '', $ignoreCase = false,
|
|
|
|
$checkForObjectIdentity = true
|
|
|
|
) {
|
|
|
|
$needleSQL = $this->normaliseSQL($needleSQL);
|
|
|
|
$haystackSQL = $this->normaliseSQL($haystackSQL);
|
|
|
|
|
|
|
|
$this->assertContains($needleSQL, $haystackSQL, $message, $ignoreCase, $checkForObjectIdentity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that a SQL query contains a SQL fragment
|
|
|
|
*
|
|
|
|
* @param string $needleSQL
|
|
|
|
* @param string $haystackSQL
|
|
|
|
* @param string $message
|
|
|
|
* @param boolean $ignoreCase
|
|
|
|
* @param boolean $checkForObjectIdentity
|
|
|
|
*/
|
|
|
|
public function assertSQLNotContains($needleSQL, $haystackSQL, $message = '', $ignoreCase = false,
|
|
|
|
$checkForObjectIdentity = true
|
|
|
|
) {
|
|
|
|
$needleSQL = $this->normaliseSQL($needleSQL);
|
|
|
|
$haystackSQL = $this->normaliseSQL($haystackSQL);
|
|
|
|
|
|
|
|
$this->assertNotContains($needleSQL, $haystackSQL, $message, $ignoreCase, $checkForObjectIdentity);
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2009-08-24 08:14:54 +02:00
|
|
|
/**
|
|
|
|
* Helper function for the DOS matchers
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
* @param array $match
|
|
|
|
* @return bool
|
2009-08-24 08:14:54 +02:00
|
|
|
*/
|
|
|
|
private function dataObjectArrayMatch($item, $match) {
|
|
|
|
foreach($match as $k => $v) {
|
2016-08-19 00:51:35 +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
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param SS_List|array $dataObjectSet
|
|
|
|
* @param array $match
|
|
|
|
* @return string
|
2009-08-24 08:14:54 +02:00
|
|
|
*/
|
|
|
|
private function DOSSummaryForMatch($dataObjectSet, $match) {
|
|
|
|
$extracted = array();
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach($dataObjectSet as $item) {
|
|
|
|
$extracted[] = array_intersect_key($item->toMap(), $match);
|
|
|
|
}
|
2009-08-24 08:14:54 +02:00
|
|
|
return var_export($extracted, true);
|
|
|
|
}
|
|
|
|
|
2015-07-16 11:32:42 +02:00
|
|
|
/**
|
|
|
|
* Pushes a class and template manifest instance that include tests onto the
|
|
|
|
* top of the loader stacks.
|
|
|
|
*/
|
|
|
|
public static function use_test_manifest() {
|
|
|
|
$flush = true;
|
|
|
|
if(isset($_GET['flush']) && $_GET['flush'] === '0') {
|
|
|
|
$flush = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$classManifest = new SS_ClassManifest(
|
|
|
|
BASE_PATH, true, $flush
|
|
|
|
);
|
|
|
|
|
|
|
|
SS_ClassLoader::instance()->pushManifest($classManifest, false);
|
|
|
|
SapphireTest::set_test_class_manifest($classManifest);
|
|
|
|
|
2016-07-19 04:09:15 +02:00
|
|
|
ThemeResourceLoader::instance()->addSet('$default', new ThemeManifest(
|
2015-07-16 11:32:42 +02:00
|
|
|
BASE_PATH, project(), true, $flush
|
|
|
|
));
|
|
|
|
|
|
|
|
Config::inst()->pushConfigStaticManifest(new SS_ConfigStaticManifest(
|
|
|
|
BASE_PATH, true, $flush
|
|
|
|
));
|
|
|
|
|
|
|
|
// Invalidate classname spec since the test manifest will now pull out new subclasses for each internal class
|
|
|
|
// (e.g. Member will now have various subclasses of DataObjects that implement TestOnly)
|
|
|
|
DataObject::reset();
|
|
|
|
}
|
|
|
|
|
2008-08-27 10:19:46 +02:00
|
|
|
/**
|
|
|
|
* Returns true if we are currently using a temporary database
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function using_temp_db() {
|
2013-06-21 00:32:08 +02:00
|
|
|
$dbConn = DB::get_conn();
|
2010-12-21 05:46:43 +01:00
|
|
|
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
|
2016-01-06 00:34:58 +01:00
|
|
|
return $dbConn && (substr($dbConn->getSelectedDatabase(), 0, strlen($prefix) + 5)
|
2012-09-26 23:34:00 +02:00
|
|
|
== strtolower(sprintf('%stmpdb', $prefix)));
|
2008-08-27 10:19:46 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function kill_temp_db() {
|
2008-08-13 04:47:14 +02:00
|
|
|
// Delete our temporary database
|
2008-08-27 10:19:46 +02:00
|
|
|
if(self::using_temp_db()) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$dbConn = DB::get_conn();
|
|
|
|
$dbName = $dbConn->getSelectedDatabase();
|
|
|
|
if($dbName && DB::get_conn()->databaseExists($dbName)) {
|
2015-03-06 23:37:06 +01: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
|
2016-06-15 06:03:16 +02:00
|
|
|
foreach(ClassInfo::subclassesFor('SilverStripe\\ORM\\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";
|
2013-06-21 00:32:08 +02:00
|
|
|
$dbConn->dropSelectedDatabase();
|
2008-08-13 04:47:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2010-04-12 05:21:30 +02:00
|
|
|
/**
|
|
|
|
* Remove all content from the temporary database.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function empty_temp_db() {
|
2010-04-12 05:21:30 +02:00
|
|
|
if(self::using_temp_db()) {
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::get_conn()->clearAllData();
|
2015-03-06 23:37:06 +01: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
|
2016-06-15 06:03:16 +02:00
|
|
|
$classes = array_merge(ClassInfo::subclassesFor('SilverStripe\\ORM\\DataExtension'), ClassInfo::subclassesFor('SilverStripe\\ORM\\DataObject'));
|
2012-09-26 23:34:00 +02:00
|
|
|
foreach($classes 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
|
|
|
}
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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);
|
2013-06-21 00:32:08 +02:00
|
|
|
$dbConn = DB::get_conn();
|
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
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$dbConn->selectDatabase($dbname, true);
|
2010-04-12 04:14:33 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var static $st */
|
|
|
|
$st = Injector::inst()->create(__CLASS__);
|
2010-04-12 04:14:33 +02:00
|
|
|
$st->resetDBSchema();
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2010-10-19 02:42:53 +02:00
|
|
|
// Reinstate PHPUnit error handling
|
|
|
|
set_error_handler(array('PHPUnit_Util_ErrorHandler', 'handleError'));
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2008-08-27 10:19:46 +02:00
|
|
|
return $dbname;
|
2008-08-13 04:47:14 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function delete_all_temp_dbs() {
|
2010-12-21 05:46:43 +01:00
|
|
|
$prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_';
|
2013-06-21 00:32:08 +02:00
|
|
|
foreach(DB::get_schema()->databaseList() as $dbName) {
|
2010-12-11 06:27:34 +01:00
|
|
|
if(preg_match(sprintf('/^%stmpdb[0-9]+$/', $prefix), $dbName)) {
|
2013-06-21 00:32:08 +02:00
|
|
|
DB::get_schema()->dropDatabase($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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
/**
|
|
|
|
* Reset the testing database's schema.
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param bool $includeExtraDataObjects If true, the extraDataObjects tables will also be included
|
2010-04-12 04:03:16 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function resetDBSchema($includeExtraDataObjects = false) {
|
2010-04-12 04:03:16 +02:00
|
|
|
if(self::using_temp_db()) {
|
2012-11-23 11:14:02 +01:00
|
|
|
DataObject::reset();
|
|
|
|
|
2010-04-12 04:03:16 +02:00
|
|
|
// clear singletons, they're caching old extension info which is used in DatabaseAdmin->doBuild()
|
BUGFIX Versioned's constructor doesn't provide suitable defaults. Previously a bug/feature in singleton, where it would pass null,true as params to strong_create, which would then get passed through as params to Versioned's constructor, meant that the code still executed fine (as was set to something that wasn't an array, so the null and true were instead taken as args). The fact that the usage of singleton(Versioned) never really used the classes code, purely for value lookup, meant that this never propagated errors. I've now switched singleton() to use the injector for retrieving values, which means these dud values are no longer passed through
CHANGE Given that Config::inst is an implementation of the singleton pattern itself, I've removed the extra call to singleton(). A side effect of this is that it gets around a possibly nasty circular reference with the dependency injector (which relies on the config object); in future, this dependency structure should really be structured from the DI directly.
MINOR Change singleton and strong_create to use dependency injector
BUGFIX: Provide default constructor values for classes (fixes issues when used in 'singleton' scenario during dev/build in particular)
MINOR Clear out injector state when resetting db schema during tests (a follow on from changing singleton() calls to use the injector underneath)
2012-05-22 02:34:08 +02:00
|
|
|
Injector::inst()->unregisterAllObjects();
|
2010-04-12 04:03:16 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
$dataClasses = ClassInfo::subclassesFor('SilverStripe\\ORM\\DataObject');
|
2010-04-12 04:03:16 +02:00
|
|
|
array_shift($dataClasses);
|
|
|
|
|
|
|
|
DB::quiet();
|
2013-06-21 00:32:08 +02:00
|
|
|
$schema = DB::get_schema();
|
|
|
|
$extraDataObjects = $includeExtraDataObjects ? $this->extraDataObjects : null;
|
|
|
|
$schema->schemaUpdate(function() use($dataClasses, $extraDataObjects){
|
|
|
|
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();
|
|
|
|
}
|
2010-04-12 04:03:16 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// If we have additional dataobjects which need schema, do so here:
|
|
|
|
if($extraDataObjects) {
|
|
|
|
foreach($extraDataObjects as $dataClass) {
|
|
|
|
$SNG = singleton($dataClass);
|
|
|
|
if(singleton($dataClass) instanceof DataObject) $SNG->requireTable();
|
|
|
|
}
|
2010-04-12 04:03:16 +02:00
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
});
|
2010-04-12 04:03:16 +02:00
|
|
|
|
|
|
|
ClassInfo::reset_db_cache();
|
2016-06-15 06:03:16 +02:00
|
|
|
singleton('SilverStripe\\ORM\\DataObject')->flushCache();
|
2010-04-12 04:03:16 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
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.
|
2016-03-30 23:36:09 +02:00
|
|
|
*
|
|
|
|
* @param string|array $permCode Either a permission, or list of permissions
|
|
|
|
* @return int Member ID
|
2009-10-16 00:40:17 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function logInWithPermission($permCode = "ADMIN") {
|
2016-03-30 23:36:09 +02:00
|
|
|
if(is_array($permCode)) {
|
|
|
|
$permArray = $permCode;
|
|
|
|
$permCode = implode('.', $permCode);
|
|
|
|
} else {
|
|
|
|
$permArray = array($permCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check cached member
|
|
|
|
if(isset($this->cache_generatedMembers[$permCode])) {
|
|
|
|
$member = $this->cache_generatedMembers[$permCode];
|
|
|
|
} else {
|
|
|
|
// Generate group with these permissions
|
|
|
|
$group = Group::create();
|
2009-10-16 00:40:17 +02:00
|
|
|
$group->Title = "$permCode group";
|
|
|
|
$group->write();
|
|
|
|
|
2016-03-30 23:36:09 +02:00
|
|
|
// Create each individual permission
|
|
|
|
foreach($permArray as $permArrayItem) {
|
|
|
|
$permission = Permission::create();
|
|
|
|
$permission->Code = $permArrayItem;
|
|
|
|
$permission->write();
|
|
|
|
$group->Permissions()->add($permission);
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2016-06-23 01:37:22 +02:00
|
|
|
$member = DataObject::get_one('SilverStripe\\Security\\Member', array(
|
2013-06-21 00:32:08 +02:00
|
|
|
'"Member"."Email"' => "$permCode@example.org"
|
|
|
|
));
|
2016-03-30 23:36:09 +02:00
|
|
|
if (!$member) {
|
|
|
|
$member = Member::create();
|
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
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);
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2009-10-16 00:40:17 +02:00
|
|
|
$this->cache_generatedMembers[$permCode] = $member;
|
|
|
|
}
|
2016-03-30 23:36:09 +02:00
|
|
|
$member->logIn();
|
|
|
|
return $member->ID;
|
2009-10-16 00:40:17 +02:00
|
|
|
}
|
2015-03-06 23:37:06 +01:00
|
|
|
|
2009-10-16 00:40:17 +02:00
|
|
|
/**
|
|
|
|
* Cache for logInWithPermission()
|
|
|
|
*/
|
|
|
|
protected $cache_generatedMembers = array();
|
2013-12-23 22:54:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test against a theme.
|
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $themeBaseDir themes directory
|
|
|
|
* @param string $theme Theme name
|
|
|
|
* @param callable $callback
|
|
|
|
* @throws Exception
|
2013-12-23 22:54:20 +01:00
|
|
|
*/
|
|
|
|
protected function useTestTheme($themeBaseDir, $theme, $callback) {
|
2014-05-02 16:25:31 +02:00
|
|
|
Config::nest();
|
2013-12-23 22:54:20 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if (strpos($themeBaseDir, BASE_PATH) === 0) {
|
|
|
|
$themeBaseDir = substr($themeBaseDir, strlen(BASE_PATH));
|
|
|
|
}
|
|
|
|
SSViewer::config()->update('theme_enabled', true);
|
2016-07-13 14:36:52 +02:00
|
|
|
SSViewer::set_themes([$themeBaseDir.'/themes/'.$theme, '$default']);
|
2013-12-23 22:54:20 +01:00
|
|
|
|
|
|
|
$e = null;
|
|
|
|
|
|
|
|
try { $callback(); }
|
|
|
|
catch (Exception $e) { /* NOP for now, just save $e */ }
|
|
|
|
|
2014-05-02 16:25:31 +02:00
|
|
|
Config::unnest();
|
2013-12-23 22:54:20 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($e) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2013-12-23 22:54:20 +01:00
|
|
|
}
|
|
|
|
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|
|
|
|
|
2012-02-12 21:22:11 +01:00
|
|
|
|