2013-06-04 01:11:07 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\BehatExtension\Context;
|
|
|
|
|
2015-10-15 23:53:37 +02:00
|
|
|
use Behat\Behat\Context\BehatContext,
|
2013-11-15 17:38:37 +01:00
|
|
|
Behat\Behat\Event\ScenarioEvent,
|
|
|
|
Behat\Gherkin\Node\PyStringNode,
|
2015-10-15 23:53:37 +02:00
|
|
|
Behat\Gherkin\Node\TableNode,
|
|
|
|
SilverStripe\Filesystem\Storage\AssetStore;
|
2016-06-27 06:34:00 +02:00
|
|
|
use SilverStripe\ORM\DB;
|
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
2016-06-29 00:31:00 +02:00
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
|
2016-06-27 06:34:00 +02:00
|
|
|
|
2013-06-04 01:11:07 +02:00
|
|
|
|
|
|
|
// PHPUnit
|
2016-02-23 10:51:56 +01:00
|
|
|
require_once BASE_PATH . '/vendor/phpunit/phpunit/src/Framework/Assert/Functions.php';
|
2013-06-04 01:11:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Context used to create fixtures in the SilverStripe ORM.
|
|
|
|
*/
|
|
|
|
class FixtureContext extends BehatContext
|
|
|
|
{
|
2013-11-15 17:38:37 +01:00
|
|
|
protected $context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \FixtureFactory
|
|
|
|
*/
|
|
|
|
protected $fixtureFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String Absolute path where file fixtures are located.
|
|
|
|
* These will automatically get copied to their location
|
|
|
|
* declare through the 'Given a file "..."' step defition.
|
|
|
|
*/
|
|
|
|
protected $filesPath;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String Tracks all files and folders created from fixtures, for later cleanup.
|
|
|
|
*/
|
|
|
|
protected $createdFilesPaths = array();
|
|
|
|
|
2016-02-23 12:03:54 +01:00
|
|
|
/**
|
|
|
|
* @var array Stores the asset tuples.
|
|
|
|
*/
|
|
|
|
protected $createdAssets = array();
|
|
|
|
|
2013-11-15 17:38:37 +01:00
|
|
|
public function __construct(array $parameters) {
|
|
|
|
$this->context = $parameters;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSession($name = null) {
|
|
|
|
return $this->getMainContext()->getSession($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \FixtureFactory
|
|
|
|
*/
|
|
|
|
public function getFixtureFactory() {
|
|
|
|
if(!$this->fixtureFactory) {
|
|
|
|
$this->fixtureFactory = \Injector::inst()->create('FixtureFactory', 'FixtureContextFactory');
|
|
|
|
}
|
|
|
|
return $this->fixtureFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \FixtureFactory $factory
|
|
|
|
*/
|
|
|
|
public function setFixtureFactory(\FixtureFactory $factory) {
|
|
|
|
$this->fixtureFactory = $factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
*/
|
|
|
|
public function setFilesPath($path) {
|
|
|
|
$this->filesPath = $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function getFilesPath() {
|
|
|
|
return $this->filesPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @BeforeScenario @database-defaults
|
|
|
|
*/
|
|
|
|
public function beforeDatabaseDefaults(ScenarioEvent $event) {
|
|
|
|
\SapphireTest::empty_temp_db();
|
2016-06-27 06:34:00 +02:00
|
|
|
DB::get_conn()->quiet();
|
|
|
|
$dataClasses = \ClassInfo::subclassesFor('SilverStripe\\ORM\\DataObject');
|
2013-11-15 17:38:37 +01:00
|
|
|
array_shift($dataClasses);
|
|
|
|
foreach ($dataClasses as $dataClass) {
|
|
|
|
\singleton($dataClass)->requireDefaultRecords();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-27 06:34:00 +02:00
|
|
|
/**
|
|
|
|
* @AfterScenario
|
|
|
|
*/
|
2013-11-15 17:38:37 +01:00
|
|
|
public function afterResetDatabase(ScenarioEvent $event) {
|
|
|
|
\SapphireTest::empty_temp_db();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @AfterScenario
|
|
|
|
*/
|
|
|
|
public function afterResetAssets(ScenarioEvent $event) {
|
2016-02-23 12:03:54 +01:00
|
|
|
$store = $this->getAssetStore();
|
|
|
|
if (is_array($this->createdAssets)) {
|
|
|
|
foreach ($this->createdAssets as $asset) {
|
2016-02-24 02:27:46 +01:00
|
|
|
$store->delete($asset['FileFilename'], $asset['FileHash']);
|
2013-11-15 17:38:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Example: Given a "page" "Page 1"
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)"$/
|
|
|
|
*/
|
|
|
|
public function stepCreateRecord($type, $id) {
|
|
|
|
$class = $this->convertTypeToClass($type);
|
2014-04-14 23:25:32 +02:00
|
|
|
$fields = $this->prepareFixture($class, $id);
|
2013-11-15 17:38:37 +01:00
|
|
|
$this->fixtureFactory->createObject($class, $id, $fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-27 06:34:00 +02:00
|
|
|
* Example: Given a "page" "Page 1" has the "content" "My content"
|
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" has (?:(an|a|the) )"(?<field>.*)" "(?<value>.*)"$/
|
|
|
|
*/
|
|
|
|
public function stepCreateRecordHasField($type, $id, $field, $value) {
|
|
|
|
$class = $this->convertTypeToClass($type);
|
|
|
|
$fields = $this->convertFields(
|
|
|
|
$class,
|
|
|
|
array($field => $value)
|
|
|
|
);
|
2015-02-15 00:08:05 +01:00
|
|
|
// We should check if this fixture object already exists - if it does, we update it. If not, we create it
|
|
|
|
if($existingFixture = $this->fixtureFactory->get($class, $id)) {
|
|
|
|
// Merge existing data with new data, and create new object to replace existing object
|
|
|
|
foreach($fields as $k => $v) {
|
|
|
|
$existingFixture->$k = $v;
|
|
|
|
}
|
|
|
|
$existingFixture->write();
|
|
|
|
} else {
|
|
|
|
$this->fixtureFactory->createObject($class, $id, $fields);
|
|
|
|
}
|
2013-11-15 17:38:37 +01:00
|
|
|
}
|
2016-06-27 06:34:00 +02:00
|
|
|
|
2013-11-15 17:38:37 +01:00
|
|
|
/**
|
2016-06-27 06:34:00 +02:00
|
|
|
* Example: Given a "page" "Page 1" with "URL"="page-1" and "Content"="my page 1"
|
|
|
|
* Example: Given the "page" "Page 1" has "URL"="page-1" and "Content"="my page 1"
|
|
|
|
*
|
2015-02-15 00:07:06 +01:00
|
|
|
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" (?:(with|has)) (?<data>".*)$/
|
2013-11-15 17:38:37 +01:00
|
|
|
*/
|
|
|
|
public function stepCreateRecordWithData($type, $id, $data) {
|
|
|
|
$class = $this->convertTypeToClass($type);
|
|
|
|
preg_match_all(
|
2016-06-27 06:34:00 +02:00
|
|
|
'/"(?<key>[^"]+)"\s*=\s*"(?<value>[^"]+)"/',
|
2013-11-15 17:38:37 +01:00
|
|
|
$data,
|
|
|
|
$matches
|
|
|
|
);
|
|
|
|
$fields = $this->convertFields(
|
|
|
|
$class,
|
|
|
|
array_combine($matches['key'], $matches['value'])
|
|
|
|
);
|
2014-04-14 23:25:32 +02:00
|
|
|
$fields = $this->prepareFixture($class, $id, $fields);
|
2015-02-15 00:08:05 +01:00
|
|
|
// We should check if this fixture object already exists - if it does, we update it. If not, we create it
|
|
|
|
if($existingFixture = $this->fixtureFactory->get($class, $id)) {
|
|
|
|
// Merge existing data with new data, and create new object to replace existing object
|
|
|
|
foreach($fields as $k => $v) {
|
|
|
|
$existingFixture->$k = $v;
|
|
|
|
}
|
|
|
|
$existingFixture->write();
|
|
|
|
} else {
|
|
|
|
$this->fixtureFactory->createObject($class, $id, $fields);
|
|
|
|
}
|
2013-11-15 17:38:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-27 06:34:00 +02:00
|
|
|
* Example: And the "page" "Page 2" has the following data
|
2013-11-15 17:38:37 +01:00
|
|
|
* | Content | <blink> |
|
|
|
|
* | My Property | foo |
|
|
|
|
* | My Boolean | bar |
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" has the following data$/
|
|
|
|
*/
|
|
|
|
public function stepCreateRecordWithTable($type, $id, $null, TableNode $fieldsTable) {
|
|
|
|
$class = $this->convertTypeToClass($type);
|
|
|
|
// TODO Support more than one record
|
|
|
|
$fields = $this->convertFields($class, $fieldsTable->getRowsHash());
|
2014-04-14 23:25:32 +02:00
|
|
|
$fields = $this->prepareFixture($class, $id, $fields);
|
2014-01-30 22:01:31 +01:00
|
|
|
|
|
|
|
// We should check if this fixture object already exists - if it does, we update it. If not, we create it
|
|
|
|
if($existingFixture = $this->fixtureFactory->get($class, $id)) {
|
|
|
|
// Merge existing data with new data, and create new object to replace existing object
|
|
|
|
foreach($fields as $k => $v) {
|
|
|
|
$existingFixture->$k = $v;
|
|
|
|
}
|
|
|
|
$existingFixture->write();
|
|
|
|
} else {
|
|
|
|
$this->fixtureFactory->createObject($class, $id, $fields);
|
|
|
|
}
|
2013-11-15 17:38:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-11 03:59:24 +01:00
|
|
|
* Example: Given the "page" "Page 1.1" is a child of the "page" "Page1".
|
|
|
|
* Note that this change is not published by default
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" is a (?<relation>[^\s]*) of (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"/
|
|
|
|
*/
|
|
|
|
public function stepUpdateRecordRelation($type, $id, $relation, $relationType, $relationId) {
|
|
|
|
$class = $this->convertTypeToClass($type);
|
2015-02-15 00:07:41 +01:00
|
|
|
|
2013-11-15 17:38:37 +01:00
|
|
|
$relationClass = $this->convertTypeToClass($relationType);
|
|
|
|
$relationObj = $this->fixtureFactory->get($relationClass, $relationId);
|
|
|
|
if(!$relationObj) $relationObj = $this->fixtureFactory->createObject($relationClass, $relationId);
|
2015-02-15 00:07:41 +01:00
|
|
|
|
|
|
|
$data = array();
|
|
|
|
if($relation == 'child') {
|
|
|
|
$data['ParentID'] = $relationObj->ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
$obj = $this->fixtureFactory->get($class, $id);
|
|
|
|
if($obj) {
|
|
|
|
$obj->update($data);
|
|
|
|
$obj->write();
|
|
|
|
} else {
|
|
|
|
$obj = $this->fixtureFactory->createObject($class, $id, $data);
|
|
|
|
}
|
|
|
|
|
2013-11-15 17:38:37 +01:00
|
|
|
switch($relation) {
|
|
|
|
case 'parent':
|
|
|
|
$relationObj->ParentID = $obj->ID;
|
|
|
|
$relationObj->write();
|
|
|
|
break;
|
|
|
|
case 'child':
|
2015-02-15 00:07:41 +01:00
|
|
|
// already written through $data above
|
2013-11-15 17:38:37 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException(sprintf(
|
|
|
|
'Invalid relation "%s"', $relation
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
2013-06-04 01:11:07 +02:00
|
|
|
|
2015-03-13 13:41:02 +01:00
|
|
|
/**
|
|
|
|
* Assign a type of object to another type of object. The base object will be created if it does not exist already.
|
|
|
|
* If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used.
|
|
|
|
*
|
2014-08-17 11:33:11 +02:00
|
|
|
* @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1"
|
|
|
|
* @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)"$/
|
|
|
|
*/
|
|
|
|
public function stepIAssignObjToObj($type, $value, $relationType, $relationId) {
|
2015-03-13 13:41:02 +01:00
|
|
|
self::stepIAssignObjToObjInTheRelation($type, $value, $relationType, $relationId, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign a type of object to another type of object. The base object will be created if it does not exist already.
|
|
|
|
* If the last part of the string (in the "X" relation) is omitted, then the first matching relation will be used.
|
|
|
|
* Assumption: one object has relationship (has_one, has_many or many_many ) with the other object
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2015-03-13 13:41:02 +01:00
|
|
|
* @example I assign the "TaxonomyTerm" "For customers" to the "Page" "Page1" in the "Terms" relation
|
|
|
|
* @Given /^I assign (?:(an|a|the) )"(?<type>[^"]+)" "(?<value>[^"]+)" to (?:(an|a|the) )"(?<relationType>[^"]+)" "(?<relationId>[^"]+)" in the "(?<relationName>[^"]+)" relation$/
|
|
|
|
*/
|
|
|
|
public function stepIAssignObjToObjInTheRelation($type, $value, $relationType, $relationId, $relationName) {
|
2014-08-17 11:33:11 +02:00
|
|
|
$class = $this->convertTypeToClass($type);
|
|
|
|
$relationClass = $this->convertTypeToClass($relationType);
|
|
|
|
|
|
|
|
// Check if this fixture object already exists - if not, we create it
|
|
|
|
$relationObj = $this->fixtureFactory->get($relationClass, $relationId);
|
|
|
|
if(!$relationObj) $relationObj = $this->fixtureFactory->createObject($relationClass, $relationId);
|
|
|
|
|
|
|
|
// Check if there is relationship defined in many_many (includes belongs_many_many)
|
|
|
|
$manyField = null;
|
|
|
|
$oneField = null;
|
|
|
|
if ($relationObj->many_many()) {
|
|
|
|
$manyField = array_search($class, $relationObj->many_many());
|
2015-03-13 13:41:02 +01:00
|
|
|
if($manyField && strlen($relationName) > 0) $manyField = $relationName;
|
2014-08-17 11:33:11 +02:00
|
|
|
}
|
|
|
|
if(empty($manyField) && $relationObj->has_many()) {
|
|
|
|
$manyField = array_search($class, $relationObj->has_many());
|
2015-03-13 13:41:02 +01:00
|
|
|
if($manyField && strlen($relationName) > 0) $manyField = $relationName;
|
2014-08-17 11:33:11 +02:00
|
|
|
}
|
|
|
|
if(empty($manyField) && $relationObj->has_one()) {
|
|
|
|
$oneField = array_search($class, $relationObj->has_one());
|
2015-03-13 13:41:02 +01:00
|
|
|
if($oneField && strlen($relationName) > 0) $oneField = $relationName;
|
2014-08-17 11:33:11 +02:00
|
|
|
}
|
|
|
|
if(empty($manyField) && empty($oneField)) {
|
|
|
|
throw new \Exception("'$relationClass' has no relationship (has_one, has_many and many_many) with '$class'!");
|
|
|
|
}
|
|
|
|
|
2016-06-27 06:34:00 +02:00
|
|
|
// Get the searchable field to check if the fixture object already exists
|
2014-08-17 11:33:11 +02:00
|
|
|
$temObj = new $class;
|
|
|
|
if(isset($temObj->Name)) $field = "Name";
|
|
|
|
else if(isset($temObj->Title)) $field = "Title";
|
|
|
|
else $field = "ID";
|
|
|
|
|
|
|
|
// Check if the fixture object exists - if not, we create it
|
2016-06-27 06:34:00 +02:00
|
|
|
$obj = DataObject::get($class)->filter($field, $value)->first();
|
2014-08-17 11:33:11 +02:00
|
|
|
if(!$obj) $obj = $this->fixtureFactory->createObject($class, $value);
|
|
|
|
// If has_many or many_many, add this fixture object to the relation object
|
|
|
|
// If has_one, set value to the joint field with this fixture object's ID
|
|
|
|
if($manyField) {
|
|
|
|
$relationObj->$manyField()->add($obj);
|
|
|
|
} else if($oneField) {
|
|
|
|
// E.g. $has_one = array('PanelOffer' => 'Offer');
|
|
|
|
// then the join field is PanelOfferID. This is the common rule in the CMS
|
|
|
|
$relationObj->{$oneField . 'ID'} = $obj->ID;
|
2016-06-27 06:34:00 +02:00
|
|
|
}
|
|
|
|
|
2014-08-17 11:33:11 +02:00
|
|
|
$relationObj->write();
|
|
|
|
}
|
2016-06-27 06:34:00 +02:00
|
|
|
|
2013-11-15 17:38:37 +01:00
|
|
|
/**
|
2016-06-27 06:34:00 +02:00
|
|
|
* Example: Given the "page" "Page 1" is not published
|
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Given /^(?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)" is (?<state>[^"]*)$/
|
|
|
|
*/
|
|
|
|
public function stepUpdateRecordState($type, $id, $state) {
|
|
|
|
$class = $this->convertTypeToClass($type);
|
|
|
|
$obj = $this->fixtureFactory->get($class, $id);
|
|
|
|
if(!$obj) {
|
|
|
|
throw new \InvalidArgumentException(sprintf(
|
|
|
|
'Can not find record "%s" with identifier "%s"',
|
|
|
|
$type,
|
|
|
|
$id
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
switch($state) {
|
|
|
|
case 'published':
|
|
|
|
$obj->publish('Stage', 'Live');
|
|
|
|
break;
|
|
|
|
case 'not published':
|
|
|
|
case 'unpublished':
|
2016-06-27 06:34:00 +02:00
|
|
|
$oldMode = Versioned::get_reading_mode();
|
|
|
|
Versioned::set_stage(Versioned::LIVE);
|
2013-11-15 17:38:37 +01:00
|
|
|
$clone = clone $obj;
|
|
|
|
$clone->delete();
|
2016-06-27 06:34:00 +02:00
|
|
|
Versioned::set_reading_mode($oldMode);
|
2013-11-15 17:38:37 +01:00
|
|
|
break;
|
|
|
|
case 'deleted':
|
|
|
|
$obj->delete();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \InvalidArgumentException(sprintf(
|
|
|
|
'Invalid state: "%s"', $state
|
2016-06-27 06:34:00 +02:00
|
|
|
));
|
2013-11-15 17:38:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts YAML fixture definitions similar to the ones used in SilverStripe unit testing.
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* Example: Given there are the following member records:
|
|
|
|
* member1:
|
|
|
|
* Email: member1@test.com
|
|
|
|
* member2:
|
|
|
|
* Email: member2@test.com
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Given /^there are the following ([^\s]*) records$/
|
|
|
|
*/
|
|
|
|
public function stepThereAreTheFollowingRecords($dataObject, PyStringNode $string) {
|
|
|
|
$yaml = array_merge(array($dataObject . ':'), $string->getLines());
|
|
|
|
$yaml = implode("\n ", $yaml);
|
|
|
|
|
|
|
|
// Save fixtures into database
|
|
|
|
// TODO Run prepareAsset() for each File and Folder record
|
|
|
|
$yamlFixture = new \YamlFixture($yaml);
|
|
|
|
$yamlFixture->writeInto($this->getFixtureFactory());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Example: Given a "member" "Admin" belonging to "Admin Group"
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)"$/
|
|
|
|
*/
|
|
|
|
public function stepCreateMemberWithGroup($id, $groupId) {
|
2016-06-29 00:31:00 +02:00
|
|
|
$group = $this->fixtureFactory->get('SilverStripe\\Security\\Group', $groupId);
|
|
|
|
if(!$group) $group = $this->fixtureFactory->createObject('SilverStripe\\Security\\Group', $groupId);
|
2016-06-27 06:34:00 +02:00
|
|
|
|
2016-06-29 00:31:00 +02:00
|
|
|
$member = $this->fixtureFactory->createObject('SilverStripe\\Security\\Member', $id);
|
2013-11-15 17:38:37 +01:00
|
|
|
$member->Groups()->add($group);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Example: Given a "member" "Admin" belonging to "Admin Group" with "Email"="test@test.com"
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Given /^(?:(an|a|the) )"member" "(?<id>[^"]+)" belonging to "(?<groupId>[^"]+)" with (?<data>.*)$/
|
|
|
|
*/
|
|
|
|
public function stepCreateMemberWithGroupAndData($id, $groupId, $data) {
|
2016-06-29 00:31:00 +02:00
|
|
|
$class = 'SilverStripe\\Security\\Member';
|
2013-11-15 17:38:37 +01:00
|
|
|
preg_match_all(
|
2016-06-27 06:34:00 +02:00
|
|
|
'/"(?<key>[^"]+)"\s*=\s*"(?<value>[^"]+)"/',
|
2013-11-15 17:38:37 +01:00
|
|
|
$data,
|
|
|
|
$matches
|
|
|
|
);
|
|
|
|
$fields = $this->convertFields(
|
|
|
|
$class,
|
|
|
|
array_combine($matches['key'], $matches['value'])
|
|
|
|
);
|
2016-06-27 06:34:00 +02:00
|
|
|
|
2016-06-29 00:31:00 +02:00
|
|
|
$group = $this->fixtureFactory->get('SilverStripe\\Security\\Group', $groupId);
|
|
|
|
if(!$group) $group = $this->fixtureFactory->createObject('SilverStripe\\Security\\Group', $groupId);
|
2013-11-15 17:38:37 +01:00
|
|
|
|
|
|
|
$member = $this->fixtureFactory->createObject($class, $id, $fields);
|
|
|
|
$member->Groups()->add($group);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Example: Given a "group" "Admin" with permissions "Access to 'Pages' section" and "Access to 'Files' section"
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Given /^(?:(an|a|the) )"group" "(?<id>[^"]+)" (?:(with|has)) permissions (?<permissionStr>.*)$/
|
|
|
|
*/
|
|
|
|
public function stepCreateGroupWithPermissions($id, $permissionStr) {
|
|
|
|
// Convert natural language permissions to codes
|
|
|
|
preg_match_all('/"([^"]+)"/', $permissionStr, $matches);
|
|
|
|
$permissions = $matches[1];
|
2016-06-29 00:31:00 +02:00
|
|
|
$codes = Permission::get_codes(false);
|
2013-11-15 17:38:37 +01:00
|
|
|
|
2016-06-29 00:31:00 +02:00
|
|
|
$group = $this->fixtureFactory->get('SilverStripe\\Security\\Group', $id);
|
|
|
|
if(!$group) $group = $this->fixtureFactory->createObject('SilverStripe\\Security\\Group', $id);
|
2016-06-27 06:34:00 +02:00
|
|
|
|
2013-11-15 17:38:37 +01:00
|
|
|
foreach($permissions as $permission) {
|
|
|
|
$found = false;
|
|
|
|
foreach($codes as $code => $details) {
|
|
|
|
if(
|
|
|
|
$permission == $code
|
|
|
|
|| $permission == $details['name']
|
|
|
|
) {
|
2016-06-29 00:31:00 +02:00
|
|
|
Permission::grant($group->ID, $code);
|
2013-11-15 17:38:37 +01:00
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!$found) {
|
|
|
|
throw new \InvalidArgumentException(sprintf(
|
|
|
|
'No permission found for "%s"', $permission
|
2016-06-27 06:34:00 +02:00
|
|
|
));
|
2013-11-15 17:38:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigates to a record based on its identifier set during fixture creation,
|
|
|
|
* using its RelativeLink() method to map the record to a URL.
|
|
|
|
* Example: Given I go to the "page" "My Page"
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Given /^I go to (?:(an|a|the) )"(?<type>[^"]+)" "(?<id>[^"]+)"/
|
|
|
|
*/
|
|
|
|
public function stepGoToNamedRecord($type, $id) {
|
|
|
|
$class = $this->convertTypeToClass($type);
|
|
|
|
$record = $this->fixtureFactory->get($class, $id);
|
|
|
|
if(!$record) {
|
|
|
|
throw new \InvalidArgumentException(sprintf(
|
|
|
|
'Cannot resolve reference "%s", no matching fixture found',
|
|
|
|
$id
|
|
|
|
));
|
|
|
|
}
|
|
|
|
if(!$record->hasMethod('RelativeLink')) {
|
|
|
|
throw new \InvalidArgumentException('URL for record cannot be determined, missing RelativeLink() method');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->getSession()->visit($this->getMainContext()->locatePath($record->RelativeLink()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that a file or folder exists in the webroot.
|
|
|
|
* Example: There should be a file "assets/Uploads/test.jpg"
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Then /^there should be a (?<type>(file|folder) )"(?<path>[^"]*)"/
|
|
|
|
*/
|
|
|
|
public function stepThereShouldBeAFileOrFolder($type, $path) {
|
|
|
|
assertFileExists($this->joinPaths(BASE_PATH, $path));
|
|
|
|
}
|
|
|
|
|
2016-02-24 02:27:46 +01:00
|
|
|
/**
|
|
|
|
* Checks that a file exists in the asset store with a given filename and hash
|
|
|
|
*
|
|
|
|
* Example: there should be a filename "Uploads/test.jpg" with hash "59de0c841f"
|
|
|
|
*
|
|
|
|
* @Then /^there should be a filename "(?<filename>[^"]*)" with hash "(?<hash>[a-fA-Z0-9]+)"/
|
|
|
|
*/
|
|
|
|
public function stepThereShouldBeAFileWithTuple($filename, $hash) {
|
|
|
|
$exists = $this->getAssetStore()->exists($filename, $hash);
|
|
|
|
assertTrue((bool)$exists, "A file exists with filename $filename and hash $hash");
|
|
|
|
}
|
|
|
|
|
2013-11-15 17:38:37 +01:00
|
|
|
/**
|
2016-06-27 06:34:00 +02:00
|
|
|
* Replaces fixture references in values with their respective database IDs,
|
2013-11-15 17:38:37 +01:00
|
|
|
* with the notation "=><class>.<identifier>". Example: "=>Page.My Page".
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2013-11-15 17:38:37 +01:00
|
|
|
* @Transform /^([^"]+)$/
|
|
|
|
*/
|
|
|
|
public function lookupFixtureReference($string) {
|
|
|
|
if(preg_match('/^=>/', $string)) {
|
|
|
|
list($className, $identifier) = explode('.', preg_replace('/^=>/', '', $string), 2);
|
|
|
|
$id = $this->fixtureFactory->getId($className, $identifier);
|
|
|
|
if(!$id) {
|
|
|
|
throw new \InvalidArgumentException(sprintf(
|
|
|
|
'Cannot resolve reference "%s", no matching fixture found',
|
|
|
|
$string
|
|
|
|
));
|
|
|
|
}
|
|
|
|
return $id;
|
|
|
|
} else {
|
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^(?:(an|a|the) )"(?<type>[^"]*)" "(?<id>[^"]*)" was (?<mod>(created|last edited)) "(?<time>[^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function aRecordWasLastEditedRelative($type, $id, $mod, $time) {
|
|
|
|
$class = $this->convertTypeToClass($type);
|
2014-04-14 23:25:32 +02:00
|
|
|
$fields = $this->prepareFixture($class, $id);
|
2013-11-15 17:38:37 +01:00
|
|
|
$record = $this->fixtureFactory->createObject($class, $id, $fields);
|
|
|
|
$date = date("Y-m-d H:i:s",strtotime($time));
|
|
|
|
$table = \ClassInfo::baseDataClass(get_class($record));
|
|
|
|
$field = ($mod == 'created') ? 'Created' : 'LastEdited';
|
2016-06-27 06:34:00 +02:00
|
|
|
DB::query(sprintf(
|
2013-11-15 17:38:37 +01:00
|
|
|
'UPDATE "%s" SET "%s" = \'%s\' WHERE "ID" = \'%d\'',
|
|
|
|
$table,
|
|
|
|
$field,
|
|
|
|
$date,
|
|
|
|
$record->ID
|
2016-06-27 06:34:00 +02:00
|
|
|
));
|
2013-11-15 17:38:37 +01:00
|
|
|
// Support for Versioned extension, by checking for a "Live" stage
|
2016-06-27 06:34:00 +02:00
|
|
|
if(DB::getConn()->hasTable($table . '_Live')) {
|
|
|
|
DB::query(sprintf(
|
2013-11-15 17:38:37 +01:00
|
|
|
'UPDATE "%s_Live" SET "%s" = \'%s\' WHERE "ID" = \'%d\'',
|
|
|
|
$table,
|
|
|
|
$field,
|
|
|
|
$date,
|
|
|
|
$record->ID
|
2016-06-27 06:34:00 +02:00
|
|
|
));
|
2013-11-15 17:38:37 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-27 06:34:00 +02:00
|
|
|
|
2014-04-14 23:25:32 +02:00
|
|
|
/**
|
|
|
|
* Prepares a fixture for use
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
2014-04-14 23:25:32 +02:00
|
|
|
* @param string $class
|
|
|
|
* @param string $identifier
|
|
|
|
* @param array $data
|
|
|
|
* @return array Prepared $data with additional injected fields
|
|
|
|
*/
|
|
|
|
protected function prepareFixture($class, $identifier, $data = array()) {
|
|
|
|
if($class == 'File' || is_subclass_of($class, 'File')) {
|
|
|
|
$data = $this->prepareAsset($class, $identifier, $data);
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2013-11-15 17:38:37 +01:00
|
|
|
|
|
|
|
protected function prepareAsset($class, $identifier, $data = null) {
|
|
|
|
if(!$data) $data = array();
|
|
|
|
$relativeTargetPath = (isset($data['Filename'])) ? $data['Filename'] : $identifier;
|
2015-10-15 23:53:37 +02:00
|
|
|
$relativeTargetPath = preg_replace('/^' . ASSETS_DIR . '\/?/', '', $relativeTargetPath);
|
2013-11-15 17:38:37 +01:00
|
|
|
$sourcePath = $this->joinPaths($this->getFilesPath(), basename($relativeTargetPath));
|
2016-06-27 06:34:00 +02:00
|
|
|
|
2013-11-15 17:38:37 +01:00
|
|
|
// Create file or folder on filesystem
|
|
|
|
if($class == 'Folder' || is_subclass_of($class, 'Folder')) {
|
2015-10-18 23:55:56 +02:00
|
|
|
$parent = \Folder::find_or_make($relativeTargetPath);
|
2015-10-22 02:27:09 +02:00
|
|
|
$data['ID'] = $parent->ID;
|
2013-11-15 17:38:37 +01:00
|
|
|
} else {
|
2015-10-18 23:55:56 +02:00
|
|
|
$parent = \Folder::find_or_make(dirname($relativeTargetPath));
|
2013-11-15 17:38:37 +01:00
|
|
|
if(!file_exists($sourcePath)) {
|
|
|
|
throw new \InvalidArgumentException(sprintf(
|
|
|
|
'Source file for "%s" cannot be found in "%s"',
|
2016-02-24 02:27:46 +01:00
|
|
|
$relativeTargetPath,
|
2013-11-15 17:38:37 +01:00
|
|
|
$sourcePath
|
|
|
|
));
|
|
|
|
}
|
2015-10-22 02:27:09 +02:00
|
|
|
$data['ParentID'] = $parent->ID;
|
2016-06-27 06:34:00 +02:00
|
|
|
|
2015-10-15 23:53:37 +02:00
|
|
|
// Load file into APL and retrieve tuple
|
|
|
|
$asset = $this->getAssetStore()->setFromLocalFile(
|
|
|
|
$sourcePath,
|
|
|
|
$relativeTargetPath,
|
|
|
|
null,
|
|
|
|
null,
|
2016-02-23 12:03:54 +01:00
|
|
|
array(
|
|
|
|
'conflict' => AssetStore::CONFLICT_OVERWRITE,
|
|
|
|
'visibility' => AssetStore::VISIBILITY_PUBLIC
|
|
|
|
)
|
2015-10-15 23:53:37 +02:00
|
|
|
);
|
|
|
|
$data['FileFilename'] = $asset['Filename'];
|
|
|
|
$data['FileHash'] = $asset['Hash'];
|
|
|
|
$data['FileVariant'] = $asset['Variant'];
|
|
|
|
}
|
|
|
|
if(!isset($data['Name'])) {
|
|
|
|
$data['Name'] = basename($relativeTargetPath);
|
|
|
|
}
|
2013-11-15 17:38:37 +01:00
|
|
|
|
2016-02-24 02:27:46 +01:00
|
|
|
// Save assets
|
|
|
|
if(isset($data['FileFilename'])) {
|
|
|
|
$this->createdAssets[] = $data;
|
|
|
|
}
|
2013-11-15 17:38:37 +01:00
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2015-10-15 23:53:37 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return AssetStore
|
|
|
|
*/
|
|
|
|
protected function getAssetStore() {
|
|
|
|
return singleton('AssetStore');
|
|
|
|
}
|
|
|
|
|
2013-11-15 17:38:37 +01:00
|
|
|
/**
|
|
|
|
* Converts a natural language class description to an actual class name.
|
|
|
|
* Respects {@link DataObject::$singular_name} variations.
|
|
|
|
* Example: "redirector page" -> "RedirectorPage"
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
|
|
|
* @param String
|
2013-11-15 17:38:37 +01:00
|
|
|
* @return String Class name
|
|
|
|
*/
|
|
|
|
protected function convertTypeToClass($type) {
|
|
|
|
$type = trim($type);
|
|
|
|
|
|
|
|
// Try direct mapping
|
|
|
|
$class = str_replace(' ', '', ucwords($type));
|
2016-06-27 06:34:00 +02:00
|
|
|
if(class_exists($class) && is_subclass_of($class, 'SilverStripe\\ORM\\DataObject')) {
|
|
|
|
return \ClassInfo::class_name($class);
|
2013-11-15 17:38:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fall back to singular names
|
2016-06-27 06:34:00 +02:00
|
|
|
foreach(array_values(\ClassInfo::subclassesFor('SilverStripe\\ORM\\DataObject')) as $candidate) {
|
|
|
|
if(strcasecmp(singleton($candidate)->singular_name(), $type) === 0) {
|
|
|
|
return $candidate;
|
|
|
|
}
|
2013-11-15 17:38:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException(sprintf(
|
|
|
|
'Class "%s" does not exist, or is not a subclass of DataObjet',
|
|
|
|
$class
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates an object with values, resolving aliases set through
|
|
|
|
* {@link DataObject->fieldLabels()}.
|
2016-06-27 06:34:00 +02:00
|
|
|
*
|
|
|
|
* @param string $class Class name
|
|
|
|
* @param array $fields Map of field names or aliases to their values.
|
|
|
|
* @return array Map of actual object properties to their values.
|
2013-11-15 17:38:37 +01:00
|
|
|
*/
|
|
|
|
protected function convertFields($class, $fields) {
|
|
|
|
$labels = singleton($class)->fieldLabels();
|
|
|
|
foreach($fields as $fieldName => $fieldVal) {
|
2014-03-30 12:02:57 +02:00
|
|
|
if($fieldLabelKey = array_search($fieldName, $labels)) {
|
2013-11-15 17:38:37 +01:00
|
|
|
unset($fields[$fieldName]);
|
2014-03-30 12:02:57 +02:00
|
|
|
$fields[$labels[$fieldLabelKey]] = $fieldVal;
|
2016-06-27 06:34:00 +02:00
|
|
|
|
2013-11-15 17:38:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function joinPaths() {
|
|
|
|
$args = func_get_args();
|
|
|
|
$paths = array();
|
|
|
|
foreach($args as $arg) $paths = array_merge($paths, (array)$arg);
|
|
|
|
foreach($paths as &$path) $path = trim($path, '/');
|
|
|
|
if (substr($args[0], 0, 1) == '/') $paths[0] = '/' . $paths[0];
|
|
|
|
return join('/', $paths);
|
|
|
|
}
|
2016-06-27 06:34:00 +02:00
|
|
|
|
|
|
|
}
|