silverstripe-framework/tests/security/PermissionCheckboxSetFieldTest.php

104 lines
3.5 KiB
PHP
Raw Normal View History

<?php
use SilverStripe\ORM\DataObject;
2016-06-23 01:37:22 +02:00
use SilverStripe\Security\PermissionCheckboxSetField;
/**
* @package framework
* @subpackage tests
*/
class PermissionCheckboxSetFieldTest extends SapphireTest {
protected static $fixture_file = 'PermissionCheckboxSetFieldTest.yml';
2014-08-15 08:53:05 +02:00
public function testHiddenPermissions() {
$f = new PermissionCheckboxSetField(
'Permissions',
'Permissions',
2016-06-23 01:37:22 +02:00
'SilverStripe\\Security\\Permission',
'GroupID'
);
$f->setHiddenPermissions(
array('NON-ADMIN')
);
$this->assertEquals(
$f->getHiddenPermissions(),
array('NON-ADMIN')
);
$this->assertContains('ADMIN', $f->Field());
$this->assertNotContains('NON-ADMIN', $f->Field());
}
2014-08-15 08:53:05 +02:00
public function testSaveInto() {
2016-06-23 01:37:22 +02:00
$group = $this->objFromFixture('SilverStripe\\Security\\Group', 'group'); // tested group
$untouchable = $this->objFromFixture('SilverStripe\\Security\\Group', 'untouchable'); // group that should not change
2014-08-15 08:53:05 +02:00
$field = new PermissionCheckboxSetField(
'Permissions',
'Permissions',
2016-06-23 01:37:22 +02:00
'SilverStripe\\Security\\Permission',
'GroupID',
$group
);
// get the number of permissions before we start
2016-06-23 01:37:22 +02:00
$baseCount = DataObject::get('SilverStripe\\Security\\Permission')->Count();
2014-08-15 08:53:05 +02:00
// 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');
2016-06-23 01:37:22 +02:00
$this->assertEquals(DataObject::get('SilverStripe\\Security\\Permission')->Count(), $baseCount, 'There are no orphaned permissions');
2014-08-15 08:53:05 +02:00
// 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');
2016-06-23 01:37:22 +02:00
$this->assertEquals(DataObject::get('SilverStripe\\Security\\Permission')->Count(), $baseCount+2,
'There are no orphaned permissions');
2014-08-15 08:53:05 +02:00
// 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');
2016-06-23 01:37:22 +02:00
$this->assertEquals(DataObject::get('SilverStripe\\Security\\Permission')->Count(), $baseCount+1,
'There are no orphaned permissions');
}
}