Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS

Testing Permission


Reply

4 Posts   305 Views

Avatar
juergr

4 May 2013 at 9:31am (Last edited: 4 May 2013 9:32am), Community Member, 17 Posts

While writing some test for my site, i found something which confuses me

On my model "AnnualProgramm", I have a Function called "canFrontendEdit":

public function canFrontendEdit($member=null){
return Permission::check('ADMIN');
}

And I have a very simple test for this function:

public function testCanFrontendEdit() {
$obj = $this->objFromFixture('AnnualProgramm', '2013');
$this->assertFalse($obj->canFrontendEdit());
}

This test always fail, but if i call the page on wich this function is called, i can clearly see that the call to Permission::check() returns false. Has this something to do with the testing framework? And how can i test this function?

Avatar
Willr

5 May 2013 at 4:19pm Forum Moderator, 5511 Posts

Are you running your tests as a logged in admin user? You could do Permission::check('ADMIN' ,$member); then pass a non admin member into the test's function call.

Avatar
juergr

6 May 2013 at 7:27am Community Member, 17 Posts

If done both: runn the tests from the console and via the web-backend. I think the call to Permission::check() should have three arguments, (code, arg, member, ...) but i don't know what to pass as second argument ($arg). And i also want to check the behaviour if no user is logged in, so how to check this? Can I pass null as member or should I create a new mermber and pass this to the check() function?

Avatar
Willr

6 May 2013 at 6:43pm Forum Moderator, 5511 Posts

A good way to pick up a few tricks is to checkout the tests already in core. For example checkout https://github.com/silverstripe/sapphire/blob/3.0/tests/security/PermissionTest.php for a couple ideas on testing with members.