Skip to content

Commit

Permalink
Merge pull request #102 from muskie9/refactor/permissionsTest
Browse files Browse the repository at this point in the history
REFACTOR permission checks and tests
  • Loading branch information
muskie9 authored Jul 26, 2016
2 parents 12009e0 + e93c2bc commit fb88e94
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
6 changes: 3 additions & 3 deletions code/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public function canView($member = null)
*/
public function canEdit($member = null)
{
return Permission::check('Location_EDIT');
return Permission::check('Location_EDIT', 'any', $member);
}

/**
Expand All @@ -227,7 +227,7 @@ public function canEdit($member = null)
*/
public function canDelete($member = null)
{
return Permission::check('Location_DELETE');
return Permission::check('Location_DELETE', 'any', $member);
}

/**
Expand All @@ -236,7 +236,7 @@ public function canDelete($member = null)
*/
public function canCreate($member = null)
{
return Permission::check('Location_CREATE');
return Permission::check('Location_CREATE', 'any', $member);
}

/**
Expand Down
54 changes: 43 additions & 11 deletions tests/LocationTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

class LocationTest extends Locator_Test
class LocationTest extends SapphireTest
{

protected static $fixture_file = 'locator/tests/Locator_Test.yml';

public function testGetCoords()
{
$location = $this->objFromFixture('Location', 'dynamic');
Expand Down Expand Up @@ -60,51 +63,80 @@ public function testCanView()
{
$object = $this->objFromFixture('Location', 'dynamic');
$object->write();
$this->logInWithPermission('ADMIN');

$this->assertTrue($object->canView());
$this->logOut();

$nullMember = Member::create();
$nullMember->write();

$this->assertTrue($object->canView($nullMember));

$nullMember->delete();
}

public function testCanEdit()
{
$object = $this->objFromFixture('Location', 'dynamic');
$object->write();

$objectID = $object->ID;
$this->logInWithPermission('Location_EDIT');

//test permissions per permission setting
$create = $this->objFromFixture('Member', 'locationcreate');
$edit = $this->objFromFixture('Member', 'locationedit');
$delete = $this->objFromFixture('Member', 'locationdelete');

$originalTitle = $object->Title;
$this->assertEquals($originalTitle, 'Dynamic, Inc.');
$this->assertTrue($object->canEdit());

$this->assertTrue($object->canEdit($edit));
$this->assertFalse($object->canEdit($create));
$this->assertFalse($object->canEdit($delete));

$object->Title = 'Changed Title';
$object->write();

$testEdit = Location::get()->byID($objectID);
$this->assertEquals($testEdit->Title, 'Changed Title');
$this->logOut();
}

public function testCanDelete()
{
$object = $this->objFromFixture('Location', 'dynamic');
$object->write();
$this->logInWithPermission('Location_DELETE');
$this->assertTrue($object->canDelete());

//test permissions per permission setting
$create = $this->objFromFixture('Member', 'locationcreate');
$edit = $this->objFromFixture('Member', 'locationedit');
$delete = $this->objFromFixture('Member', 'locationdelete');

$this->assertTrue($object->canDelete($delete));
$this->assertFalse($object->canDelete($create));
$this->assertFalse($object->canDelete($edit));

$checkObject = $object;
$object->delete();

$this->assertEquals($checkObject->ID, 0);
}

public function testCanCreate()
{
$object = singleton('Location');
$this->logInWithPermission('Location_CREATE');
$this->assertTrue($object->canCreate());
$this->logOut();

//test permissions per permission setting
$create = $this->objFromFixture('Member', 'locationcreate');
$edit = $this->objFromFixture('Member', 'locationedit');
$delete = $this->objFromFixture('Member', 'locationdelete');

$this->assertTrue($object->canCreate($create));
$this->assertFalse($object->canCreate($edit));
$this->assertFalse($object->canCreate($delete));

$nullMember = Member::create();
$nullMember->write();
$this->assertFalse($object->canCreate($nullMember));

$nullMember->delete();
}

Expand Down
36 changes: 36 additions & 0 deletions tests/Locator_Test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,39 @@
Group:
locationcreate:
Title: Create Locations
locationedit:
Title: Edit Locations
locationdelete:
Title: Location Delete
Permission:
locationcreate:
Code: Location_CREATE
Type: 1
Group: =>Group.locationcreate
locationedit:
Code: Location_EDIT
Type: 1
Group: =>Permission.locationedit
locationdelete:
Code: Location_DELETE
Type: 1
Group: =>Permission.locationdelete
Member:
locationcreate:
FirstName: Create
Surname: Location
Email: [email protected]
Groups: =>Group.locationcreate
locationedit:
FirstName: Edit
Surname: Location
Email: [email protected]
Groups: =>Group.locationedit
locationdelete:
FirstName: Delete
Surname: Location
Email: [email protected]
Groups: =>Group.locationdelete
Locator:
locator1:
Title: Locator
Expand Down

0 comments on commit fb88e94

Please sign in to comment.