mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #7403 from dhensby/pulls/4/act-as-for-tests
NEW Add actWithPermission to SapphireTest
This commit is contained in:
commit
1ba18461f5
@ -950,13 +950,24 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a member and group with the given permission code, and log in with it.
|
||||
* Returns the member ID.
|
||||
* A wrapper for automatically performing callbacks as a user with a specific permission
|
||||
*
|
||||
* @param string|array $permCode Either a permission, or list of permissions
|
||||
* @return int Member ID
|
||||
* @param string|array $permCode
|
||||
* @param callable $callback
|
||||
* @return mixed
|
||||
*/
|
||||
public function logInWithPermission($permCode = "ADMIN")
|
||||
public function actWithPermission($permCode, $callback)
|
||||
{
|
||||
return Member::actAs($this->createMemberWithPermission($permCode), $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Member and Group objects on demand with specific permission code
|
||||
*
|
||||
* @param string|array $permCode
|
||||
* @return Member
|
||||
*/
|
||||
protected function createMemberWithPermission($permCode)
|
||||
{
|
||||
if (is_array($permCode)) {
|
||||
$permArray = $permCode;
|
||||
@ -997,6 +1008,19 @@ class SapphireTest extends PHPUnit_Framework_TestCase implements TestOnly
|
||||
|
||||
$this->cache_generatedMembers[$permCode] = $member;
|
||||
}
|
||||
return $member;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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")
|
||||
{
|
||||
$member = $this->createMemberWithPermission($permCode);
|
||||
$this->logInAs($member);
|
||||
return $member->ID;
|
||||
}
|
||||
|
@ -3,6 +3,8 @@
|
||||
namespace SilverStripe\Dev\Tests;
|
||||
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\Security\Member;
|
||||
use SilverStripe\Security\Permission;
|
||||
|
||||
class SapphireTestTest extends SapphireTest
|
||||
{
|
||||
@ -29,4 +31,24 @@ class SapphireTestTest extends SapphireTest
|
||||
$this->resolveFixturePath(dirname(__DIR__) .'/ORM/DataObjectTest.yml')
|
||||
);
|
||||
}
|
||||
|
||||
public function testActWithPermission()
|
||||
{
|
||||
$this->logOut();
|
||||
$this->assertFalse(Permission::check('ADMIN'));
|
||||
$this->actWithPermission('ADMIN', function () {
|
||||
$this->assertTrue(Permission::check('ADMIN'));
|
||||
// check nested actAs calls work as expected
|
||||
Member::actAs(null, function () {
|
||||
$this->assertFalse(Permission::check('ADMIN'));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public function testCreateMemberWithPermission()
|
||||
{
|
||||
$this->assertCount(0, Member::get()->filter([ 'Email' => 'TESTPERM@example.org' ]));
|
||||
$this->createMemberWithPermission('TESTPERM');
|
||||
$this->assertCount(1, Member::get()->filter([ 'Email' => 'TESTPERM@example.org' ]));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user