2009-11-21 05:23:42 +00:00
|
|
|
<?php
|
2016-06-15 16:03:16 +12:00
|
|
|
|
2016-10-14 14:30:05 +13:00
|
|
|
namespace SilverStripe\Security\Tests;
|
|
|
|
|
2016-06-15 16:03:16 +12:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-10-14 14:30:05 +13:00
|
|
|
use SilverStripe\Security\Group;
|
|
|
|
use SilverStripe\Security\Permission;
|
2016-06-23 11:37:22 +12:00
|
|
|
use SilverStripe\Security\PermissionCheckboxSetField;
|
2016-08-19 10:51:35 +12:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2017-07-03 12:21:27 +12:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2016-12-16 17:34:21 +13:00
|
|
|
class PermissionCheckboxSetFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'PermissionCheckboxSetFieldTest.yml';
|
|
|
|
|
|
|
|
public function testHiddenPermissions()
|
|
|
|
{
|
|
|
|
$f = new PermissionCheckboxSetField(
|
|
|
|
'Permissions',
|
|
|
|
'Permissions',
|
|
|
|
Permission::class,
|
|
|
|
'GroupID'
|
|
|
|
);
|
|
|
|
$f->setHiddenPermissions(
|
|
|
|
array('NON-ADMIN')
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->getHiddenPermissions(),
|
|
|
|
array('NON-ADMIN')
|
|
|
|
);
|
|
|
|
$this->assertContains('ADMIN', $f->Field());
|
|
|
|
$this->assertNotContains('NON-ADMIN', $f->Field());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSaveInto()
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Group $group
|
|
|
|
*/
|
|
|
|
$group = $this->objFromFixture(Group::class, 'group'); // tested group
|
|
|
|
/**
|
|
|
|
* @var Group $untouchable
|
|
|
|
*/
|
|
|
|
$untouchable = $this->objFromFixture(Group::class, 'untouchable'); // group that should not change
|
|
|
|
|
|
|
|
$field = new PermissionCheckboxSetField(
|
|
|
|
'Permissions',
|
|
|
|
'Permissions',
|
|
|
|
Permission::class,
|
|
|
|
'GroupID',
|
|
|
|
$group
|
|
|
|
);
|
|
|
|
|
|
|
|
// get the number of permissions before we start
|
|
|
|
$baseCount = DataObject::get(Permission::class)->count();
|
|
|
|
|
|
|
|
// there are currently no permissions, save empty checkbox
|
|
|
|
$field->saveInto($group);
|
|
|
|
$group->flushCache();
|
|
|
|
$untouchable->flushCache();
|
|
|
|
$this->assertEquals($group->Permissions()->count(), 0, 'The tested group has no permissions');
|
|
|
|
|
|
|
|
$this->assertEquals($untouchable->Permissions()->count(), 1, 'The other group has one permission');
|
|
|
|
$this->assertEquals(
|
|
|
|
$untouchable->Permissions()->where("\"Code\"='ADMIN'")->count(),
|
|
|
|
1,
|
|
|
|
'The other group has ADMIN permission'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(DataObject::get(Permission::class)->count(), $baseCount, 'There are no orphaned permissions');
|
|
|
|
|
|
|
|
// add some permissions
|
|
|
|
$field->setValue(
|
|
|
|
array(
|
|
|
|
'ADMIN'=>true,
|
|
|
|
'NON-ADMIN'=>true
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$field->saveInto($group);
|
|
|
|
$group->flushCache();
|
|
|
|
$untouchable->flushCache();
|
|
|
|
$this->assertEquals(
|
|
|
|
$group->Permissions()->count(),
|
|
|
|
2,
|
|
|
|
'The tested group has two permissions permission'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$group->Permissions()->where("\"Code\"='ADMIN'")->count(),
|
|
|
|
1,
|
|
|
|
'The tested group has ADMIN permission'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$group->Permissions()->where("\"Code\"='NON-ADMIN'")->count(),
|
|
|
|
1,
|
|
|
|
'The tested group has CMS_ACCESS_AssetAdmin permission'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$untouchable->Permissions()->count(),
|
|
|
|
1,
|
|
|
|
'The other group has one permission'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$untouchable->Permissions()->where("\"Code\"='ADMIN'")->count(),
|
|
|
|
1,
|
|
|
|
'The other group has ADMIN permission'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
DataObject::get(Permission::class)->count(),
|
|
|
|
$baseCount+2,
|
|
|
|
'There are no orphaned permissions'
|
|
|
|
);
|
|
|
|
|
|
|
|
// remove permission
|
|
|
|
$field->setValue(
|
|
|
|
array(
|
|
|
|
'ADMIN'=>true,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$field->saveInto($group);
|
|
|
|
$group->flushCache();
|
|
|
|
$untouchable->flushCache();
|
|
|
|
$this->assertEquals(
|
|
|
|
$group->Permissions()->count(),
|
|
|
|
1,
|
|
|
|
'The tested group has 1 permission'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$group->Permissions()->where("\"Code\"='ADMIN'")->count(),
|
|
|
|
1,
|
|
|
|
'The tested group has ADMIN permission'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
$untouchable->Permissions()->count(),
|
|
|
|
1,
|
|
|
|
'The other group has one permission'
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$untouchable->Permissions()->where("\"Code\"='ADMIN'")->count(),
|
|
|
|
1,
|
|
|
|
'The other group has ADMIN permission'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
DataObject::get(Permission::class)->count(),
|
|
|
|
$baseCount+1,
|
|
|
|
'There are no orphaned permissions'
|
|
|
|
);
|
|
|
|
}
|
2012-03-24 16:04:52 +13:00
|
|
|
}
|