Show
Ignore:
Timestamp:
09/06/08 21:47:14 (4 months ago)
Author:
bjohnson
Message:

Adding new unit tests of the ACL class.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/tests/test_acl.php

    r1784 r2452  
    1010class ACLTest extends UnitTestCase 
    1111{ 
     12    private $acl_group; 
     13    private $acl_user_alice; 
     14    private $acl_user_bob; 
     15     
     16    function setup() 
     17    { 
     18        // create test group and user 
     19        $this->acl_group = UserGroup::create( array( 'name' => 'acltest-group' ) ); 
     20        $this->acl_user_alice = User::create( array( 'username' => 'acl-alice' ) ); 
     21        $this->acl_user_bob = User::create( array( 'username' => 'acl-bob' ) ); 
     22        $this->acl_group->add( 'acl-alice' ); 
     23        $this->acl_group->add( 'acl-bob' ); 
     24    } 
     25     
     26    function test_group_permissions() 
     27    { 
     28        ACL::create_permission( 'acltest', 'A test ACL permission' ); 
     29 
     30        $this->assert_true( 
     31            ACL::token_exists( 'acltest' ), 
     32            'Could not create acltest permission.' 
     33        ); 
     34         
     35        $token_id = ACL::token_id( 'acltest' ); 
     36 
     37        ACL::grant_group( $this->acl_group->id, $token_id, 'full' ); 
     38        $this->assert_true( 
     39            $this->acl_group->can( 'acltest', 'full' ), 
     40            'Could not grant acltest permission to acltest-group.' 
     41        ); 
     42         
     43        ACL::revoke_group_permission( $this->acl_group->id, $token_id ); 
     44        $this->assert_false( 
     45            ACL::group_can( $this->acl_group->id, $token_id, 'full' ), 
     46            'Could not revoke acltest permission from acltest-group.' 
     47        ); 
     48 
     49        // check alternate means of granting a permission 
     50        $this->acl_group->grant( 'acltest', 'full' ); 
     51        $this->assert_true( 
     52            $this->acl_group->can( 'acltest', 'full' ), 
     53            'Could not grant acltest permission to acltest-group through UserGroup call.' 
     54        ); 
     55         
     56        // full > read/write 
     57        $this->assert_true( 
     58            $this->acl_group->can( 'acltest', 'read' ), 
     59            "Group with 'full' acltest permission cannot not 'read'." 
     60        ); 
     61        $this->assert_true( 
     62            $this->acl_group->can( 'acltest', 'write' ), 
     63            "Group with 'full' acltest permission cannot 'write'." 
     64        ); 
     65    } 
     66     
     67    function test_user_permissions() 
     68    { 
     69        $this->acl_user_alice->grant( 'acltest', 'full' ); 
     70        $this->assert_true( 
     71            $this->acl_user_alice->can( 'acltest', 'full' ), 
     72            'Could not grant acltest permission to user.' 
     73        ); 
     74         
     75        $this->acl_user_alice->revoke( 'acltest' ); 
     76         
     77        // check that members of a group inherit that group's permissions 
     78        $this->acl_group->grant( 'acltest', 'full' ); 
     79        $this->assert_true( 
     80            $this->acl_user_alice->can( 'acltest', 'full' ), 
     81            'Users do not inherit group permissions.' 
     82        ); 
     83    } 
     84     
     85    /** TODO write test_post_permissions() to verify that sensible default 
     86     * permissions are attached to new posts 
     87     */ 
     88    function test_post_permissions() 
     89    { 
     90 
     91    } 
    1292 
    1393    /** 
     
    1898    function test_admin_access() 
    1999    { 
    20         // Create an a user and add them to the admin group 
    21     //(which has been granted admin priviliges in installhandler). 
    22         $admin= User::create('username=adminuser&email=test@example.com&password=test'); 
    23         $admin->add_to_group( 'admin' ); 
     100        // Add acl-alice to the admin group 
     101        //(which has been granted admin priviliges in installhandler). 
     102        $this->acl_user_alice->add_to_group( 'admin' ); 
    24103 
    25104        $this->assert_true( 
    26             $admin->can( 'admin' ), 
     105            $this->acl_user_alice->can( 'admin' ), 
    27106            'Admin user does not have admin permission.' 
    28107        ); 
    29108 
    30         $user= User::create('username=normaluser&email=test@example.com&password=test'); 
    31109        $this->assert_false( 
    32             $user->can( 'admin' ), 
     110            $this->acl_user_bob->can( 'admin' ), 
    33111            'Unpriviliged user has admin permission.' 
    34112        ); 
    35113 
     114    } 
     115     
     116    function teardown() 
     117    { 
     118        $this->acl_group->delete(); 
     119        $this->acl_user_alice->delete(); 
     120        $this->acl_user_bob->delete(); 
    36121    } 
    37122