From 1eed512b7dfcc5043025ae522057246696617334 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 5 Jun 2013 13:27:32 +0200 Subject: [PATCH] Member and Group fixture support --- .../BehatExtension/Context/FixtureContext.php | 83 ++++++++++++++++++- 1 file changed, 82 insertions(+), 1 deletion(-) diff --git a/src/SilverStripe/BehatExtension/Context/FixtureContext.php b/src/SilverStripe/BehatExtension/Context/FixtureContext.php index c622bf1..3ddf253 100644 --- a/src/SilverStripe/BehatExtension/Context/FixtureContext.php +++ b/src/SilverStripe/BehatExtension/Context/FixtureContext.php @@ -249,6 +249,14 @@ class FixtureContext extends BehatContext } /** + * Accepts YAML fixture definitions similar to the ones used in SilverStripe unit testing. + * + * Example: Given there are the following member records: + * member1: + * Email: member1@test.com + * member2: + * Email: member2@test.com + * * @Given /^there are the following ([^\s]*) records$/ */ public function stepThereAreTheFollowingRecords($dataObject, PyStringNode $string) @@ -262,6 +270,79 @@ class FixtureContext extends BehatContext $yamlFixture->writeInto($this->getFixtureFactory()); } + /** + * Example: Given a member "Admin" belonging to "Admin Group" + * + * @Given /^(?:(an|a|the) )member "(?[^"]+)" belonging to "(?[^"]+)"$/ + */ + public function stepCreateMemberWithGroup($id, $groupId) + { + $group = $this->fixtureFactory->get('Group', $groupId); + if(!$group) $group = $this->fixtureFactory->createObject('Group', $groupId); + + $member = $this->fixtureFactory->createObject('Member', $id); + $member->Groups()->add($group); + } + + /** + * Example: Given a member "Admin" belonging to "Admin Group" with "Email"="test@test.com" + * + * @Given /^(?:(an|a|the) )member "(?[^"]+)" belonging to "(?[^"]+)" with (?.*)$/ + */ + public function stepCreateMemberWithGroupAndData($id, $groupId, $data) + { + $class = 'Member'; + preg_match_all( + '/"(?[^"]+)"\s*=\s*"(?[^"]+)"/', + $data, + $matches + ); + $fields = $this->convertFields( + $class, + array_combine($matches['key'], $matches['value']) + ); + + $group = $this->fixtureFactory->get('Group', $groupId); + if(!$group) $group = $this->fixtureFactory->createObject('Group', $groupId); + + $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" + * + * @Given /^(?:(an|a|the) )group "(?[^"]+)" (?:(with|has)) permissions (?.*)$/ + */ + public function stepCreateGroupWithPermissions($id, $permissionStr) + { + // Convert natural language permissions to codes + preg_match_all('/"([^"]+)"/', $permissionStr, $matches); + $permissions = $matches[1]; + $codes = \Permission::get_codes(false); + + $group = $this->fixtureFactory->get('Group', $id); + if(!$group) $group = $this->fixtureFactory->createObject('Group', $id); + + foreach($permissions as $permission) { + $found = false; + foreach($codes as $code => $details) { + if( + $permission == $code + || $permission == $details['name'] + ) { + \Permission::grant($group->ID, $code); + $found = true; + } + } + if(!$found) { + throw new \InvalidArgumentException(sprintf( + 'No permission found for "%s"', $permission + )); + } + } + } + /** * Replaces fixture references in values with their respective database IDs, * with the notation "=>.". Example: "=>Page.My Page". @@ -373,4 +454,4 @@ class FixtureContext extends BehatContext return join('/', $paths); } -} +} \ No newline at end of file