2009-11-21 06:23:42 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-11-21 06:23:42 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class PermissionCheckboxSetFieldTest extends SapphireTest {
|
2011-03-30 08:49:11 +02:00
|
|
|
static $fixture_file = 'PermissionCheckboxSetFieldTest.yml';
|
2009-12-16 06:43:59 +01: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',
|
|
|
|
'Permission',
|
|
|
|
'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
|
|
|
}
|
2009-12-16 06:43:59 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testSaveInto() {
|
2009-12-16 06:43:59 +01:00
|
|
|
$group = $this->objFromFixture('Group', 'group'); // tested group
|
|
|
|
$untouchable = $this->objFromFixture('Group', 'untouchable'); // group that should not change
|
|
|
|
|
|
|
|
$field = new PermissionCheckboxSetField(
|
|
|
|
'Permissions',
|
|
|
|
'Permissions',
|
|
|
|
'Permission',
|
|
|
|
'GroupID',
|
|
|
|
$group
|
|
|
|
);
|
|
|
|
|
|
|
|
// get the number of permissions before we start
|
|
|
|
$baseCount = DataObject::get('Permission')->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');
|
2010-05-25 06:23:44 +02:00
|
|
|
$this->assertEquals($untouchable->Permissions("\"Code\"='ADMIN'")->Count(), 1, 'The other group has ADMIN permission');
|
2009-12-16 06:43:59 +01:00
|
|
|
|
|
|
|
$this->assertEquals(DataObject::get('Permission')->Count(), $baseCount, 'There are no orphaned permissions');
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
$this->assertEquals($group->Permissions()->Count(), 2, 'The tested group has two permissions permission');
|
2010-05-25 06:23:44 +02:00
|
|
|
$this->assertEquals($group->Permissions("\"Code\"='ADMIN'")->Count(), 1, 'The tested group has ADMIN permission');
|
2011-03-18 03:01:09 +01:00
|
|
|
$this->assertEquals($group->Permissions("\"Code\"='NON-ADMIN'")->Count(), 1, 'The tested group has CMS_ACCESS_AssetAdmin permission');
|
2009-12-16 06:43:59 +01:00
|
|
|
|
|
|
|
$this->assertEquals($untouchable->Permissions()->Count(), 1, 'The other group has one permission');
|
2010-05-25 06:23:44 +02:00
|
|
|
$this->assertEquals($untouchable->Permissions("\"Code\"='ADMIN'")->Count(), 1, 'The other group has ADMIN permission');
|
2009-12-16 06:43:59 +01:00
|
|
|
|
|
|
|
$this->assertEquals(DataObject::get('Permission')->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');
|
2010-05-25 06:23:44 +02:00
|
|
|
$this->assertEquals($group->Permissions("\"Code\"='ADMIN'")->Count(), 1, 'The tested group has ADMIN permission');
|
2009-12-16 06:43:59 +01:00
|
|
|
|
|
|
|
$this->assertEquals($untouchable->Permissions()->Count(), 1, 'The other group has one permission');
|
2010-05-25 06:23:44 +02:00
|
|
|
$this->assertEquals($untouchable->Permissions("\"Code\"='ADMIN'")->Count(), 1, 'The other group has ADMIN permission');
|
2009-12-16 06:43:59 +01:00
|
|
|
|
|
|
|
$this->assertEquals(DataObject::get('Permission')->Count(), $baseCount+1, 'There are no orphaned permissions');
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|