silverstripe-framework/tests/security/PermissionTest.php
Sam Minnee de1494e3a8 ENHANCEMENT: Implemented DataList as the successor of DataObjectSet. DataList doesn't execute the query until it's actually needed, allowing for a more flexible ORM.
API CHANGE: augmentSQL is now passed a DataQuery object from which query parameters can be extracted.
API CHANGE: DataObjectDecorators that manipulate the query can now define augmentDataQueryCreation().
API CHANGE: The container class argument for DataObject::get() is deprecated.
API CHANGE: DataObject::buildSQL() and DataObject::extendedSQL() are deprecated; just use DataObject::get() now.
API CHANGE: DataObject::instance_get() and DataObject::instance_get_one() are deprecated, and can no longer be overloaded.
API CHANGE: DataObject::buildDataObjectSet() is deprecated.
API CHANGE: Cant't call manual manipulation methods on DataList such as insertFirst()
2011-05-01 15:25:45 +12:00

82 lines
3.5 KiB
PHP

<?php
class PermissionTest extends SapphireTest {
static $fixture_file = 'PermissionTest.yml';
function testGetCodesGrouped() {
$codes = Permission::get_codes();
$this->assertArrayNotHasKey('SITETREE_VIEW_ALL', $codes);
}
function testGetCodesUngrouped() {
$codes = Permission::get_codes(null, false);
$this->assertArrayHasKey('SITETREE_VIEW_ALL', $codes);
}
function testDirectlyAppliedPermissions() {
$member = $this->objFromFixture('Member', 'author');
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL"));
}
function testPermissionAreInheritedFromOneRole() {
$member = $this->objFromFixture('Member', 'author');
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin"));
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin"));
$this->assertFalse(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin"));
}
function testPermissionAreInheritedFromMultipleRoles() {
$member = $this->objFromFixture('Member', 'access');
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin"));
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin"));
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin"));
$this->assertTrue(Permission::checkMember($member, "EDIT_PERMISSIONS"));
$this->assertFalse(Permission::checkMember($member, "SITETREE_VIEW_ALL"));
}
function testRolesAndPermissionsFromParentGroupsAreInherited() {
$member = $this->objFromFixture('Member', 'globalauthor');
// Check that permissions applied to the group are there
$this->assertTrue(Permission::checkMember($member, "SITETREE_EDIT_ALL"));
// Check that roles from parent groups are there
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_MyAdmin"));
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin"));
// Check that permissions from parent groups are there
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL"));
// Check that a random permission that shouldn't be there isn't
$this->assertFalse(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin"));
}
/**
* Ensure the the get_*_by_permission functions are permission role aware
*/
function testGettingMembersByPermission() {
$accessMember = $this->objFromFixture('Member', 'access');
$accessAuthor = $this->objFromFixture('Member', 'author');
$result = Permission::get_members_by_permission(array('CMS_ACCESS_SecurityAdmin'));
$resultIDs = $result ? $result->column() : array();
$this->assertContains($accessMember->ID, $resultIDs,
'Member is found via a permission attached to a role');
$this->assertNotContains($accessAuthor->ID, $resultIDs);
}
function testHiddenPermissions(){
$permissionCheckboxSet = new PermissionCheckboxSetField('Permissions','Permissions','Permission','GroupID');
$this->assertContains('CMS_ACCESS_CMSMain', $permissionCheckboxSet->Field());
$this->assertContains('CMS_ACCESS_AssetAdmin', $permissionCheckboxSet->Field());
Permission::add_to_hidden_permissions('CMS_ACCESS_CMSMain');
Permission::add_to_hidden_permissions('CMS_ACCESS_AssetAdmin');
$this->assertNotContains('CMS_ACCESS_CMSMain', $permissionCheckboxSet->Field());
$this->assertNotContains('CMS_ACCESS_AssetAdmin', $permissionCheckboxSet->Field());
Permission::remove_from_hidden_permissions('CMS_ACCESS_AssetAdmin');
$this->assertContains('CMS_ACCESS_AssetAdmin', $permissionCheckboxSet->Field());
}
}