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   386 Views

Avatar
juergr

Community Member, 17 Posts

4 May 2013 at 9:31am

Edited: 04/05/2013 9:32am

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

Forum Moderator, 5513 Posts

5 May 2013 at 4:19pm

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

Community Member, 17 Posts

6 May 2013 at 7:27am

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

Forum Moderator, 5513 Posts

6 May 2013 at 6:43pm

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.