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