Jump to:

5541 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Testing Permission

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 295 Views
  • juergr
    Avatar
    Community Member
    17 Posts

    Testing Permission Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Testing Permission Link to this post

    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.

  • juergr
    Avatar
    Community Member
    17 Posts

    Re: Testing Permission Link to this post

    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?

  • Willr
    Avatar
    Forum Moderator
    5508 Posts

    Re: Testing Permission Link to this post

    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.

    295 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.