Skip to content

Commit

Permalink
Merge pull request #122 from creative-commoners/pulls/4/review-behat-…
Browse files Browse the repository at this point in the history
…tests

ENH Extracting logic for canX from Company and Employee
  • Loading branch information
GuySartorelli authored Sep 14, 2022
2 parents dbf6dfe + 8b9a80e commit da63f28
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 39 deletions.
7 changes: 7 additions & 0 deletions _config/extensions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ SilverStripe\ORM\DataObject:
SilverStripe\Assets\File:
extensions:
- BasicFieldsTestFileExtension

SilverStripe\FrameworkTest\Model\Company:
extensions:
- SilverStripe\FrameworkTest\Extension\TestDataObjectExtension
SilverStripe\FrameworkTest\Model\Employee:
extensions:
- SilverStripe\FrameworkTest\Extension\TestDataObjectExtension

---
Only:
Expand Down
40 changes: 1 addition & 39 deletions code/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
use SilverStripe\Versioned\Versioned;
use RelationFieldsTestPage;
use GridFieldTestPage;
use SilverStripe\Security\Permission;
use SilverStripe\Security\PermissionProvider;

/**
*
Expand All @@ -33,7 +31,7 @@
* @mixin Versioned
* @mixin RecursivePublishable
*/
class Company extends DataObject implements PermissionProvider
class Company extends DataObject
{
private static $table_name = 'Company';

Expand Down Expand Up @@ -373,40 +371,4 @@ public function scaffoldSearchField()
return DropdownField::create('CompanyID', 'Company', self::get()->map())->setEmptyString('');
}

public function providePermissions()
{
return [
'COMPANY_EDIT' => [
'name' => _t(
__CLASS__ . '.EditPermissionLabel',
'Edit a company'
),
'category' => _t(
__CLASS__ . '.Category',
'Company'
),
],
];
}

public function canView($member = null)
{
return Permission::check('COMPANY_EDIT', 'any', $member);
}

public function canEdit($member = null)
{
return Permission::check('COMPANY_EDIT', 'any', $member);
}

public function canDelete($member = null)
{
return Permission::check('COMPANY_EDIT', 'any', $member);
}

public function canCreate($member = null, $context = [])
{
return Permission::check('COMPANY_EDIT', 'any', $member);
}

}
46 changes: 46 additions & 0 deletions code/TestDataObjectExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace SilverStripe\FrameworkTest\Extension;

use SilverStripe\ORM\DataExtension;
use SilverStripe\Security\Permission;
use SilverStripe\Security\PermissionProvider;

class TestDataObjectExtension extends DataExtension implements PermissionProvider
{
public function providePermissions()
{
return [
'TEST_DATAOBJECT_EDIT' => [
'name' => _t(
__CLASS__ . '.EditPermissionLabel',
'Manage a test object'
),
'category' => _t(
__CLASS__ . '.Category',
'Test Data Object'
),
],
];
}

public function canView($member = null)
{
return Permission::check('TEST_DATAOBJECT_EDIT', 'any', $member);
}

public function canEdit($member = null)
{
return Permission::check('TEST_DATAOBJECT_EDIT', 'any', $member);
}

public function canDelete($member = null)
{
return Permission::check('TEST_DATAOBJECT_EDIT', 'any', $member);
}

public function canCreate($member = null, $context = [])
{
return Permission::check('TEST_DATAOBJECT_EDIT', 'any', $member);
}
}

0 comments on commit da63f28

Please sign in to comment.