Merge pull request #223 from creative-commoners/pulls/4/review-behat-tests

ENH New steps to creat groups and members with permissions
This commit is contained in:
Guy Sartorelli 2022-09-06 12:34:03 +12:00 committed by GitHub
commit 5aec7db4c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@ namespace SilverStripe\BehatExtension\Context;
use Behat\Behat\Context\Context;
use Behat\Mink\Element\NodeElement;
use LogicException;
use PHPUnit\Framework\Assert;
use SilverStripe\Security\Authenticator;
use SilverStripe\Security\Group;
@ -53,6 +54,26 @@ class LoginContext implements Context
$this->stepILogInWith($email, $password);
}
/**
* Creates and login as a member of a group with the correct permissions.
* Example: Given I am logged in as a member of "ADMIN" group
*
* @Given /^I am logged in as a member of "([^"]*)" group$/
* @param string $groupName
*/
public function iAmLoggedInAsMemberOfGroup($groupName)
{
$group = Group::get()->filter('Title', "$groupName")->first();
if (!$group) {
throw new LogicException("Group $groupName does not exist");
}
$email = "{$groupName}@example.org";
$password = 'Secret!123';
$this->generateMember($email, $password, $group, $groupName);
$this->stepILogInWith($email, $password);
}
/**
* @Given /^I am not logged in$/
*/
@ -245,22 +266,48 @@ class LoginContext implements Context
* @return Member
*/
protected function generateMemberWithPermission($email, $password, $permCode)
{
$group = $this->generateGroupWithPermission($permCode);
return $this->generateMember($email, $password, $group, $permCode);
}
/**
* Get or generate a group with the given permission code
*
* @param string $permCode
* @return Member
*/
protected function generateGroupWithPermission($permCode)
{
// Get or create group
$group = Group::get()->filter('Title', "$permCode group")->first();
if (!$group) {
$group = Group::create();
$group->Title = "$permCode group";
$group->write();
}
$group->Title = "$permCode group";
$group->write();
// Get or create permission
$permission = Permission::create();
$permission->Code = $permCode;
$permission->write();
$group->Permissions()->add($permission);
return $group;
}
/**
* Get or generate a member with the given permission code and permission group
*
* @param string $email
* @param string $password
* @param object $group
* @param string $identifier
* @return Member
*/
protected function generateMember($email, $password, $group, $identifier)
{
// Get or create member
$member = Member::get()->filter('Email', $email)->first();
if (!$member) {
@ -270,7 +317,7 @@ class LoginContext implements Context
// make sure any validation for password is skipped, since we're not testing complexity here
$validator = Member::password_validator();
Member::set_password_validator(null);
$member->FirstName = $permCode;
$member->FirstName = $identifier;
$member->Surname = "User";
$member->Email = $email;
$member->PasswordEncryption = "none";