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
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
use Exception;
|
|
|
|
use LogicException;
|
|
|
|
use PHPUnit_Framework_TestCase;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\CMS\Controllers\RootURLController;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\CLIRequestBuilder;
|
|
|
|
use SilverStripe\Control\Controller;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Cookie;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Email\Email;
|
2017-01-13 02:48:46 +01:00
|
|
|
use SilverStripe\Control\Email\Mailer;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\HTTPApplication;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Core\Injector\InjectorLoader;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Core\Manifest\ClassLoader;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Dev\State\SapphireTestState;
|
|
|
|
use SilverStripe\Dev\State\TestState;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\i18n\i18n;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\ORM\Connect\TempDatabase;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\ORM\SS_List;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Group;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Security\IdentityStore;
|
|
|
|
use SilverStripe\Security\Member;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Permission;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\SSViewer;
|
2017-06-22 12:50:45 +02:00
|
|
|
|
|
|
|
if (!class_exists(PHPUnit_Framework_TestCase::class)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-19 00:51:35 +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.
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* This class should not be used anywhere outside of unit tests, as phpunit may not be installed
|
|
|
|
* in production sites.
|
2007-08-15 08:38:41 +02:00
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Path to fixture data for this test run.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @var string|array
|
|
|
|
*/
|
|
|
|
protected static $fixture_file = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FixtureFactory
|
|
|
|
*/
|
|
|
|
protected $fixtureFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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.
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* @var bool
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
protected $usesDatabase = null;
|
|
|
|
|
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* @var bool
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
protected static $is_running_test = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
protected $requireDefaultRecordsFrom = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2017-05-17 07:40:13 +02:00
|
|
|
*
|
|
|
|
* Set a class to `*` to remove all extensions (unadvised)
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $illegal_extensions = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* of required extensions on that class.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* <code>
|
|
|
|
* array("MyTreeDataObject" => array("Versioned", "Hierarchy"))
|
|
|
|
* </code>
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $required_extensions = [];
|
2016-11-29 00:31:16 +01: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.
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_dataobjects = [];
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of class names of {@see Controller} objects to register routes for
|
|
|
|
* Controllers must implement Link() method
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_controllers = [];
|
2016-11-29 00:31:16 +01: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;
|
|
|
|
|
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* State management container for SapphireTest
|
|
|
|
*
|
|
|
|
* @var TestState
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
protected static $state = null;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-01-25 23:34:39 +01:00
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* Temp database helper
|
2017-01-25 23:34:39 +01:00
|
|
|
*
|
2017-06-22 12:50:45 +02:00
|
|
|
* @var TempDatabase
|
2017-01-25 23:34:39 +01:00
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
protected static $tempDB = null;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* Gets illegal extensions for this class
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2017-06-22 12:50:45 +02:00
|
|
|
* @return array
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
public static function getIllegalExtensions()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
return static::$illegal_extensions;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Gets required extensions for this class
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getRequiredExtensions()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
return static::$required_extensions;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* Check if test bootstrapping has been performed. Must not be relied on
|
|
|
|
* outside of unit tests.
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2017-06-22 12:50:45 +02:00
|
|
|
* @return bool
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
protected static function is_running_test()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
return self::$is_running_test;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* Set test running state
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2017-06-22 12:50:45 +02:00
|
|
|
* @param bool $bool
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
protected static function set_is_running_test($bool)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
self::$is_running_test = $bool;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public static function get_fixture_file()
|
|
|
|
{
|
|
|
|
return static::$fixture_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* Setup the test.
|
|
|
|
* Always sets up in order:
|
|
|
|
* - Reset php state
|
|
|
|
* - Nest
|
|
|
|
* - Custom state helpers
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
2017-06-22 12:50:45 +02:00
|
|
|
* User code should call parent::setUp() before custom setup code
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
// Call state helpers
|
|
|
|
static::$state->setUp($this);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
// We cannot run the tests on this abstract class.
|
2017-05-17 07:40:13 +02:00
|
|
|
if (static::class == __CLASS__) {
|
2017-05-23 01:36:15 +02:00
|
|
|
$this->markTestSkipped(sprintf('Skipping %s ', static::class));
|
2016-11-29 00:31:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// i18n needs to be set to the defaults or tests fail
|
2017-02-22 04:14:53 +01:00
|
|
|
i18n::set_locale(i18n::config()->uninherited('default_locale'));
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
// Set default timezone consistently to avoid NZ-specific dependencies
|
|
|
|
date_default_timezone_set('UTC');
|
|
|
|
|
|
|
|
Member::set_password_validator(null);
|
|
|
|
Cookie::config()->update('report_errors', false);
|
2017-03-21 04:22:23 +01:00
|
|
|
if (class_exists(RootURLController::class)) {
|
2016-11-29 00:31:16 +01:00
|
|
|
RootURLController::reset();
|
|
|
|
}
|
2017-01-26 05:20:08 +01:00
|
|
|
|
2017-04-23 05:30:33 +02:00
|
|
|
Security::clear_database_is_ready();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
// Set up test routes
|
|
|
|
$this->setUpRoutes();
|
|
|
|
|
|
|
|
$fixtureFiles = $this->getFixturePaths();
|
|
|
|
|
|
|
|
// Set up fixture
|
|
|
|
if ($fixtureFiles || $this->usesDatabase) {
|
2017-06-22 12:50:45 +02:00
|
|
|
if (!static::$tempDB->isUsed()) {
|
|
|
|
static::$tempDB->build();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DataObject::singleton()->flushCache();
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
static::$tempDB->clearAllData();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
foreach ($this->requireDefaultRecordsFrom as $className) {
|
|
|
|
$instance = singleton($className);
|
|
|
|
if (method_exists($instance, 'requireDefaultRecords')) {
|
|
|
|
$instance->requireDefaultRecords();
|
|
|
|
}
|
|
|
|
if (method_exists($instance, 'augmentDefaultRecords')) {
|
|
|
|
$instance->augmentDefaultRecords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($fixtureFiles as $fixtureFilePath) {
|
|
|
|
$fixture = YamlFixture::create($fixtureFilePath);
|
|
|
|
$fixture->writeInto($this->getFixtureFactory());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->logInWithPermission("ADMIN");
|
|
|
|
}
|
|
|
|
|
|
|
|
// turn off template debugging
|
|
|
|
SSViewer::config()->update('source_file_comments', false);
|
|
|
|
|
2017-01-13 02:48:46 +01:00
|
|
|
// Set up the test mailer
|
2017-06-22 12:50:45 +02:00
|
|
|
Injector::inst()->registerService(new TestMailer(), Mailer::class);
|
2016-11-29 00:31:16 +01:00
|
|
|
Email::config()->remove('send_all_emails_to');
|
2017-01-13 02:48:46 +01:00
|
|
|
Email::config()->remove('send_all_emails_from');
|
|
|
|
Email::config()->remove('cc_all_emails_to');
|
|
|
|
Email::config()->remove('bcc_all_emails_to');
|
2016-11-29 00:31:16 +01: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,
|
2017-03-24 12:17:26 +01:00
|
|
|
* e.g. dynamically adding an extension. See {@link teardownAfterClass()}
|
2016-11-29 00:31:16 +01:00
|
|
|
* for tearing down the state again.
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* Always sets up in order:
|
|
|
|
* - Reset php state
|
|
|
|
* - Nest
|
|
|
|
* - Custom state helpers
|
|
|
|
*
|
|
|
|
* User code should call parent::setUpBeforeClass() before custom setup code
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-03-24 12:17:26 +01:00
|
|
|
public static function setUpBeforeClass()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
// Start tests
|
2017-02-22 04:14:53 +01:00
|
|
|
static::start();
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Call state helpers
|
|
|
|
static::$state->setUpOnce(static::class);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Build DB if we have objects
|
|
|
|
if (static::getExtraDataObjects()) {
|
2016-11-29 00:31:16 +01:00
|
|
|
DataObject::reset();
|
2017-06-22 12:50:45 +02:00
|
|
|
static::resetDBSchema(true, true);
|
2017-01-25 23:34:39 +01:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tearDown method that's called once per test class rather once per test method.
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* Always sets up in order:
|
|
|
|
* - Custom state helpers
|
|
|
|
* - Unnest
|
|
|
|
* - Reset php state
|
|
|
|
*
|
|
|
|
* User code should call parent::tearDownAfterClass() after custom tear down code
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-03-24 12:17:26 +01:00
|
|
|
public static function tearDownAfterClass()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
// Call state helpers
|
|
|
|
static::$state->tearDownOnce(static::class);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Reset DB schema
|
|
|
|
static::resetDBSchema();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FixtureFactory
|
|
|
|
*/
|
|
|
|
public function getFixtureFactory()
|
|
|
|
{
|
|
|
|
if (!$this->fixtureFactory) {
|
2017-05-18 12:01:55 +02:00
|
|
|
$this->fixtureFactory = Injector::inst()->create(FixtureFactory::class);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
return $this->fixtureFactory;
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Sets a new fixture factory
|
|
|
|
*
|
|
|
|
* @param FixtureFactory $factory
|
|
|
|
* @return $this
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function setFixtureFactory(FixtureFactory $factory)
|
|
|
|
{
|
|
|
|
$this->fixtureFactory = $factory;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID of an object from the fixture.
|
|
|
|
*
|
2017-05-04 07:48:10 +02:00
|
|
|
* @param string $className The data class or table name, as specified in your fixture file. Parent classes won't work
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param string $identifier The identifier string, as provided in your fixture file
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function idFromFixture($className, $identifier)
|
|
|
|
{
|
|
|
|
$id = $this->getFixtureFactory()->getId($className, $identifier);
|
|
|
|
|
|
|
|
if (!$id) {
|
|
|
|
user_error(sprintf(
|
|
|
|
"Couldn't find object '%s' (class: %s)",
|
|
|
|
$identifier,
|
|
|
|
$className
|
|
|
|
), E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all of the IDs in the fixture of a particular class name.
|
|
|
|
* Will collate all IDs form all fixtures if multiple fixtures are provided.
|
|
|
|
*
|
2017-05-04 07:48:10 +02:00
|
|
|
* @param string $className The data class or table name, as specified in your fixture file
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return array A map of fixture-identifier => object-id
|
|
|
|
*/
|
|
|
|
protected function allFixtureIDs($className)
|
|
|
|
{
|
|
|
|
return $this->getFixtureFactory()->getIds($className);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an object from the fixture.
|
|
|
|
*
|
2017-05-04 07:48:10 +02:00
|
|
|
* @param string $className The data class or table name, as specified in your fixture file. Parent classes won't work
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param string $identifier The identifier string, as provided in your fixture file
|
|
|
|
*
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
protected function objFromFixture($className, $identifier)
|
|
|
|
{
|
|
|
|
$obj = $this->getFixtureFactory()->get($className, $identifier);
|
|
|
|
|
|
|
|
if (!$obj) {
|
|
|
|
user_error(sprintf(
|
|
|
|
"Couldn't find object '%s' (class: %s)",
|
|
|
|
$identifier,
|
|
|
|
$className
|
|
|
|
), E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load a YAML fixture file into the database.
|
|
|
|
* Once loaded, you can use idFromFixture() and objFromFixture() to get items from the fixture.
|
|
|
|
* Doesn't clear existing fixtures.
|
|
|
|
*
|
|
|
|
* @param string $fixtureFile The location of the .yml fixture file, relative to the site base dir
|
|
|
|
*/
|
|
|
|
public function loadFixture($fixtureFile)
|
|
|
|
{
|
2017-05-18 12:01:55 +02:00
|
|
|
$fixture = Injector::inst()->create(YamlFixture::class, $fixtureFile);
|
2016-11-29 00:31:16 +01:00
|
|
|
$fixture->writeInto($this->getFixtureFactory());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all fixtures which were previously loaded through
|
|
|
|
* {@link loadFixture()}
|
|
|
|
*/
|
|
|
|
public function clearFixtures()
|
|
|
|
{
|
|
|
|
$this->getFixtureFactory()->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Useful for writing unit tests without hardcoding folder structures.
|
|
|
|
*
|
2017-06-22 12:50:45 +02:00
|
|
|
* @return string Absolute path to current class.
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
protected function getCurrentAbsolutePath()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$filename = ClassLoader::inst()->getItemPath(static::class);
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!$filename) {
|
2017-05-23 01:36:15 +02:00
|
|
|
throw new LogicException("getItemPath returned null for " . static::class);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
return dirname($filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* @return string File path relative to webroot
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* Setup the test.
|
|
|
|
* Always sets up in order:
|
|
|
|
* - Custom state helpers
|
|
|
|
* - Unnest
|
|
|
|
* - Reset php state
|
|
|
|
*
|
|
|
|
* User code should call parent::tearDown() after custom tear down code
|
|
|
|
*/
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function tearDown()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
// Reset mocked datetime
|
|
|
|
DBDatetime::clear_mock_now();
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
$controller = Controller::has_curr() ? Controller::curr() : null;
|
|
|
|
if ($controller && ($response = $controller->getResponse()) && $response->getHeader('Location')) {
|
|
|
|
$response->setStatusCode(200);
|
|
|
|
$response->removeHeader('Location');
|
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Call state helpers
|
|
|
|
static::$state->tearDown($this);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function assertContains(
|
|
|
|
$needle,
|
|
|
|
$haystack,
|
|
|
|
$message = '',
|
|
|
|
$ignoreCase = false,
|
|
|
|
$checkForObjectIdentity = true,
|
|
|
|
$checkForNonObjectIdentity = false
|
|
|
|
) {
|
|
|
|
if ($haystack instanceof DBField) {
|
|
|
|
$haystack = (string)$haystack;
|
|
|
|
}
|
|
|
|
parent::assertContains($needle, $haystack, $message, $ignoreCase, $checkForObjectIdentity, $checkForNonObjectIdentity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function assertNotContains(
|
|
|
|
$needle,
|
|
|
|
$haystack,
|
|
|
|
$message = '',
|
|
|
|
$ignoreCase = false,
|
|
|
|
$checkForObjectIdentity = true,
|
|
|
|
$checkForNonObjectIdentity = false
|
|
|
|
) {
|
|
|
|
if ($haystack instanceof DBField) {
|
|
|
|
$haystack = (string)$haystack;
|
|
|
|
}
|
|
|
|
parent::assertNotContains($needle, $haystack, $message, $ignoreCase, $checkForObjectIdentity, $checkForNonObjectIdentity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear the log of emails sent
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* @return bool True if emails cleared
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function clearEmails()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var Mailer $mailer */
|
|
|
|
$mailer = Injector::inst()->get(Mailer::class);
|
|
|
|
if ($mailer instanceof TestMailer) {
|
|
|
|
$mailer->clearEmails();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2017-06-22 12:50:45 +02:00
|
|
|
* @param string $to
|
|
|
|
* @param string $from
|
|
|
|
* @param string $subject
|
|
|
|
* @param string $content
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return array Contains keys: 'type', 'to', 'from', 'subject','content', 'plainContent', 'attachedFiles',
|
|
|
|
* 'customHeaders', 'htmlContent', 'inlineImages'
|
|
|
|
*/
|
|
|
|
public function findEmail($to, $from = null, $subject = null, $content = null)
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var Mailer $mailer */
|
|
|
|
$mailer = Injector::inst()->get(Mailer::class);
|
|
|
|
if ($mailer instanceof TestMailer) {
|
|
|
|
return $mailer->findEmail($to, $from, $subject, $content);
|
|
|
|
}
|
|
|
|
return null;
|
2016-11-29 00:31:16 +01: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.
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* @param string $to
|
|
|
|
* @param string $from
|
|
|
|
* @param string $subject
|
|
|
|
* @param string $content
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function assertEmailSent($to, $from = null, $subject = null, $content = null)
|
|
|
|
{
|
|
|
|
$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."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the given {@link SS_List} includes DataObjects matching the given key-value
|
|
|
|
* pairs. Each match must correspond to 1 distinct record.
|
|
|
|
*
|
|
|
|
* @param SS_List|array $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 SS_List $dataObjectSet The {@link SS_List} to test.
|
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
*/
|
|
|
|
public function assertDOSContains($matches, $dataObjectSet)
|
|
|
|
{
|
|
|
|
$extracted = array();
|
|
|
|
foreach ($dataObjectSet as $object) {
|
|
|
|
/** @var DataObject $object */
|
|
|
|
$extracted[] = $object->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
|
|
|
|
$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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Asserts that no items in a given list appear in the given dataobject list
|
|
|
|
*
|
|
|
|
* @param SS_List|array $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 SS_List $dataObjectSet The {@link SS_List} to test.
|
|
|
|
*
|
|
|
|
* Examples
|
|
|
|
* --------
|
|
|
|
* Check that $members doesn't have an entry with Email = sam@example.com:
|
|
|
|
* $this->assertNotDOSContains(array('Email' => '...@example.com'), $members);
|
|
|
|
*
|
|
|
|
* Check that $members doesn't have entries with Email = sam@example.com and with
|
|
|
|
* Email = ingo@example.com:
|
|
|
|
* $this->assertNotDOSContains(array(
|
|
|
|
* array('Email' => '...@example.com'),
|
|
|
|
* array('Email' => 'i...@example.com'),
|
|
|
|
* ), $members);
|
|
|
|
*/
|
|
|
|
public function assertNotDOSContains($matches, $dataObjectSet)
|
|
|
|
{
|
|
|
|
$extracted = array();
|
|
|
|
foreach ($dataObjectSet as $object) {
|
|
|
|
/** @var DataObject $object */
|
|
|
|
$extracted[] = $object->toMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
$matched = [];
|
|
|
|
foreach ($matches as $match) {
|
|
|
|
foreach ($extracted as $i => $item) {
|
|
|
|
if ($this->dataObjectArrayMatch($item, $match)) {
|
|
|
|
$matched[] = $extracted[$i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We couldn't find a match - assertion failed
|
|
|
|
$this->assertEmpty(
|
|
|
|
$matched,
|
|
|
|
"Failed asserting that the SS_List dosn't contain a set of objects. "
|
|
|
|
. "Found objects were: " . var_export($matched, true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the given {@link SS_List} includes only DataObjects matching the given
|
|
|
|
* key-value pairs. Each match must correspond to 1 distinct record.
|
|
|
|
*
|
|
|
|
* 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);
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
public function assertDOSEquals($matches, $dataObjectSet)
|
|
|
|
{
|
|
|
|
// Extract dataobjects
|
|
|
|
$extracted = array();
|
|
|
|
if ($dataObjectSet) {
|
|
|
|
foreach ($dataObjectSet as $object) {
|
|
|
|
/** @var DataObject $object */
|
|
|
|
$extracted[] = $object->toMap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have leftovers than the DOS has extra data that shouldn't be there
|
|
|
|
$this->assertTrue(
|
|
|
|
(count($extracted) == 0),
|
|
|
|
// If we didn't break by this point then we couldn't find a match
|
|
|
|
"Failed asserting that the SS_List contained only the given items, the "
|
|
|
|
. "following items were left over:\n" . var_export($extracted, true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that the every record in the given {@link SS_List} matches the given key-value
|
|
|
|
* pairs.
|
|
|
|
*
|
|
|
|
* Example
|
|
|
|
* --------
|
|
|
|
* Check that every entry in $members has a Status of 'Active':
|
|
|
|
* $this->assertDOSAllMatch(array('Status' => 'Active'), $members);
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
public function assertDOSAllMatch($match, $dataObjectSet)
|
|
|
|
{
|
|
|
|
$extracted = array();
|
|
|
|
foreach ($dataObjectSet as $object) {
|
|
|
|
/** @var DataObject $object */
|
|
|
|
$extracted[] = $object->toMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($extracted as $i => $item) {
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->dataObjectArrayMatch($item, $match),
|
|
|
|
"Failed asserting that the the following item matched "
|
|
|
|
. var_export($match, true) . ": " . var_export($item, true)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes sequences of repeated whitespace characters from SQL queries
|
|
|
|
* making them suitable for string comparison
|
|
|
|
*
|
|
|
|
* @param string $sql
|
|
|
|
* @return string The cleaned and normalised SQL string
|
|
|
|
*/
|
|
|
|
protected function normaliseSQL($sql)
|
|
|
|
{
|
|
|
|
return trim(preg_replace('/\s+/m', ' ', $sql));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that two SQL queries are equivalent
|
|
|
|
*
|
|
|
|
* @param string $expectedSQL
|
|
|
|
* @param string $actualSQL
|
|
|
|
* @param string $message
|
|
|
|
* @param float|int $delta
|
|
|
|
* @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);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for the DOS matchers
|
|
|
|
*
|
|
|
|
* @param array $item
|
|
|
|
* @param array $match
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function dataObjectArrayMatch($item, $match)
|
|
|
|
{
|
|
|
|
foreach ($match as $k => $v) {
|
|
|
|
if (!array_key_exists($k, $item) || $item[$k] != $v) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for the DOS matchers
|
|
|
|
*
|
|
|
|
* @param SS_List|array $dataObjectSet
|
|
|
|
* @param array $match
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function DOSSummaryForMatch($dataObjectSet, $match)
|
|
|
|
{
|
|
|
|
$extracted = array();
|
|
|
|
foreach ($dataObjectSet as $item) {
|
|
|
|
$extracted[] = array_intersect_key($item->toMap(), $match);
|
|
|
|
}
|
|
|
|
return var_export($extracted, true);
|
|
|
|
}
|
|
|
|
|
2017-02-22 04:14:53 +01:00
|
|
|
/**
|
|
|
|
* Start test environment
|
|
|
|
*/
|
|
|
|
public static function start()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
if (static::is_running_test()) {
|
|
|
|
return;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Health check
|
|
|
|
if (InjectorLoader::inst()->countManifests()) {
|
|
|
|
throw new LogicException("SapphireTest::start() cannot be called within another application");
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
static::set_is_running_test(true);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Mock request
|
|
|
|
$_SERVER['argv'] = ['vendor/bin/phpunit', '/'];
|
|
|
|
$request = CLIRequestBuilder::createFromEnvironment();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Test application
|
|
|
|
$kernel = new TestKernel(BASE_PATH);
|
|
|
|
$app = new HTTPApplication($kernel);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Custom application
|
|
|
|
$app->execute($request, function (HTTPRequest $request) {
|
|
|
|
// Start session and execute
|
|
|
|
$request->getSession()->init();
|
2016-11-29 22:59:37 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// 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();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
// Set dummy controller;
|
|
|
|
$controller = Controller::create();
|
|
|
|
$controller->setRequest($request);
|
|
|
|
$controller->pushCurrent();
|
|
|
|
$controller->doInit();
|
|
|
|
}, true);
|
|
|
|
|
|
|
|
// Register state
|
|
|
|
static::$state = SapphireTestState::singleton();
|
|
|
|
// Register temp DB holder
|
|
|
|
static::$tempDB = TempDatabase::create();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-06-22 12:50:45 +02:00
|
|
|
* Reset the testing database's schema, but only if it is active
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param bool $includeExtraDataObjects If true, the extraDataObjects tables will also be included
|
2017-06-22 12:50:45 +02:00
|
|
|
* @param bool $forceCreate Force DB to be created if it doesn't exist
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
public static function resetDBSchema($includeExtraDataObjects = false, $forceCreate = false)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
// Check if DB is active before reset
|
|
|
|
if (!static::$tempDB->isUsed()) {
|
|
|
|
if (!$forceCreate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
static::$tempDB->build();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
$extraDataObjects = $includeExtraDataObjects ? static::getExtraDataObjects() : [];
|
|
|
|
static::$tempDB->resetDBSchema((array)$extraDataObjects);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a member and group with the given permission code, and log in with it.
|
|
|
|
* Returns the member ID.
|
|
|
|
*
|
|
|
|
* @param string|array $permCode Either a permission, or list of permissions
|
|
|
|
* @return int Member ID
|
|
|
|
*/
|
|
|
|
public function logInWithPermission($permCode = "ADMIN")
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
$group->Title = "$permCode group";
|
|
|
|
$group->write();
|
|
|
|
|
|
|
|
// Create each individual permission
|
|
|
|
foreach ($permArray as $permArrayItem) {
|
|
|
|
$permission = Permission::create();
|
|
|
|
$permission->Code = $permArrayItem;
|
|
|
|
$permission->write();
|
|
|
|
$group->Permissions()->add($permission);
|
|
|
|
}
|
|
|
|
|
2017-05-18 12:01:55 +02:00
|
|
|
$member = Member::get()->filter([
|
|
|
|
'Email' => "$permCode@example.org",
|
|
|
|
])->first();
|
2016-11-29 00:31:16 +01:00
|
|
|
if (!$member) {
|
|
|
|
$member = Member::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
$member->FirstName = $permCode;
|
|
|
|
$member->Surname = "User";
|
|
|
|
$member->Email = "$permCode@example.org";
|
|
|
|
$member->write();
|
|
|
|
$group->Members()->add($member);
|
|
|
|
|
|
|
|
$this->cache_generatedMembers[$permCode] = $member;
|
|
|
|
}
|
2017-06-09 06:25:40 +02:00
|
|
|
$this->logInAs($member);
|
2016-11-29 00:31:16 +01:00
|
|
|
return $member->ID;
|
|
|
|
}
|
|
|
|
|
2017-06-09 06:25:40 +02:00
|
|
|
/**
|
|
|
|
* Log in as the given member
|
|
|
|
*
|
|
|
|
* @param Member|int|string $member The ID, fixture codename, or Member object of the member that you want to log in
|
|
|
|
*/
|
|
|
|
public function logInAs($member)
|
|
|
|
{
|
|
|
|
if (is_numeric($member)) {
|
|
|
|
$member = DataObject::get_by_id(Member::class, $member);
|
|
|
|
} elseif (!is_object($member)) {
|
|
|
|
$member = $this->objFromFixture(Member::class, $member);
|
|
|
|
}
|
|
|
|
Injector::inst()->get(IdentityStore::class)->logIn($member);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log out the current user
|
|
|
|
*/
|
|
|
|
public function logOut()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var IdentityStore $store */
|
|
|
|
$store = Injector::inst()->get(IdentityStore::class);
|
|
|
|
$store->logOut();
|
2017-06-09 06:25:40 +02:00
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Cache for logInWithPermission()
|
|
|
|
*/
|
|
|
|
protected $cache_generatedMembers = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test against a theme.
|
|
|
|
*
|
|
|
|
* @param string $themeBaseDir themes directory
|
|
|
|
* @param string $theme Theme name
|
|
|
|
* @param callable $callback
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function useTestTheme($themeBaseDir, $theme, $callback)
|
|
|
|
{
|
|
|
|
Config::nest();
|
|
|
|
if (strpos($themeBaseDir, BASE_PATH) === 0) {
|
|
|
|
$themeBaseDir = substr($themeBaseDir, strlen(BASE_PATH));
|
|
|
|
}
|
|
|
|
SSViewer::config()->update('theme_enabled', true);
|
|
|
|
SSViewer::set_themes([$themeBaseDir.'/themes/'.$theme, '$default']);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$callback();
|
2017-06-22 12:50:45 +02:00
|
|
|
} finally {
|
|
|
|
Config::unnest();
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get fixture paths for this test
|
|
|
|
*
|
|
|
|
* @return array List of paths
|
|
|
|
*/
|
|
|
|
protected function getFixturePaths()
|
|
|
|
{
|
|
|
|
$fixtureFile = static::get_fixture_file();
|
|
|
|
if (empty($fixtureFile)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$fixtureFiles = (is_array($fixtureFile)) ? $fixtureFile : [$fixtureFile];
|
|
|
|
|
|
|
|
return array_map(function ($fixtureFilePath) {
|
|
|
|
return $this->resolveFixturePath($fixtureFilePath);
|
|
|
|
}, $fixtureFiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all extra objects to scaffold for this test
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
public static function getExtraDataObjects()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-03-24 12:17:26 +01:00
|
|
|
return static::$extra_dataobjects;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get additional controller classes to register routes for
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
public static function getExtraControllers()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-03-24 12:17:26 +01:00
|
|
|
return static::$extra_controllers;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map a fixture path to a physical file
|
|
|
|
*
|
|
|
|
* @param string $fixtureFilePath
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function resolveFixturePath($fixtureFilePath)
|
|
|
|
{
|
|
|
|
// 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('/^(\.){1,2}/', $fixtureFilePath);
|
|
|
|
|
|
|
|
if ($isRelativeToFile) {
|
|
|
|
$resolvedPath = realpath($this->getCurrentAbsolutePath() . '/' . $fixtureFilePath);
|
|
|
|
if ($resolvedPath) {
|
|
|
|
return $resolvedPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if file exists relative to base dir
|
|
|
|
$resolvedPath = realpath(Director::baseFolder() . '/' . $fixtureFilePath);
|
|
|
|
if ($resolvedPath) {
|
|
|
|
return $resolvedPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fixtureFilePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function setUpRoutes()
|
2017-02-22 04:14:53 +01:00
|
|
|
{
|
|
|
|
// Get overridden routes
|
|
|
|
$rules = $this->getExtraRoutes();
|
|
|
|
|
|
|
|
// Add all other routes
|
|
|
|
foreach (Director::config()->uninherited('rules') as $route => $rule) {
|
|
|
|
if (!isset($rules[$route])) {
|
|
|
|
$rules[$route] = $rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add default catch-all rule
|
|
|
|
$rules['$Controller//$Action/$ID/$OtherID'] = '*';
|
|
|
|
|
|
|
|
// Add controller-name auto-routing
|
|
|
|
Director::config()->set('rules', $rules);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get extra routes to merge into Director.rules
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getExtraRoutes()
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
$rules = [];
|
|
|
|
foreach ($this->getExtraControllers() as $class) {
|
|
|
|
$controllerInst = Controller::singleton($class);
|
|
|
|
$link = Director::makeRelative($controllerInst->Link());
|
|
|
|
$route = rtrim($link, '/') . '//$Action/$ID/$OtherID';
|
|
|
|
$rules[$route] = $class;
|
|
|
|
}
|
2017-02-22 04:14:53 +01:00
|
|
|
return $rules;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2007-08-15 08:38:41 +02:00
|
|
|
}
|