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;
|
|
|
|
|
2017-07-03 02:21:27 +02:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class PermissionCheckboxSetFieldTest extends SapphireTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'PermissionCheckboxSetFieldTest.yml';
|
|
|
|
|
|
|
|
public function testHiddenPermissions()
|
|
|
|
{
|
|
|
|
$f = new PermissionCheckboxSetField(
|
|
|
|
'Permissions',
|
|
|
|
'Permissions',
|
|
|
|
Permission::class,
|
|
|
|
'GroupID'
|
|
|
|
);
|
|
|
|
$f->setHiddenPermissions(
|
2020-04-20 19:58:09 +02:00
|
|
|
['NON-ADMIN']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$f->getHiddenPermissions(),
|
2020-04-20 19:58:09 +02:00
|
|
|
['NON-ADMIN']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('ADMIN', $f->Field());
|
|
|
|
$this->assertStringNotContainsString('NON-ADMIN', $f->Field());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'ADMIN'=>true,
|
|
|
|
'NON-ADMIN'=>true
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$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(
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'ADMIN'=>true,
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$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 04:04:52 +01:00
|
|
|
}
|