[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/t/ -> test_permissions.php (source)

   1  <?php
   2  require_once ('init.php');
   3  
   4  class GP_Test_Permissions extends GP_UnitTestCase {
   5  	function test_create_find() {
   6          $args = array( 'user_id' => 2, 'action' => 'write', 'object_type' => 'translation-set', 'object_id' => 5 );
   7          GP::$permission->create( $args );
   8          $from_db = GP::$permission->find_one( $args );
   9          $this->assertEqualPermissions( $args,  $from_db );
  10      }
  11  
  12  	function test_create_find_with_nulls() {
  13          $args = array( 'user_id' => 2, 'action' => 'write', 'object_type' => 'translation-set', );
  14          GP::$permission->create( array_merge( $args, array( 'object_id' => 11 ) ) );
  15          GP::$permission->create( $args );
  16          $args['object_id'] = null;
  17          $from_db = GP::$permission->find_one( $args );
  18          $this->assertEqualPermissions( $args, $from_db );
  19      }
  20  
  21  	function test_logged_out_permissions() {
  22          $this->assertFalse( (bool)GP::$user->current()->can( 'admin' ) );
  23          $this->assertFalse( (bool)GP::$user->current()->can( 'write', 'project', 1 ) );
  24      }
  25  
  26  	function test_recursive_project_permissions() {
  27          $user = GP::$user->create( array( 'user_login' => 'gugu', 'user_email' => 'gugu@gugu.net' ) );
  28          $other = GP::$project->create( array( 'name' => 'Other', 'slug' => 'other', 'path' => 'other') );
  29          $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root', 'path' => 'root') );
  30          $sub = GP::$project->create( array( 'name' => 'Sub', 'slug' => 'sub', 'parent_project_id' => $root->id, 'path' => 'root/sub' ) );
  31  
  32          GP::$permission->create( array( 'user_id' => $user->id, 'action' => 'write', 'object_type' => 'project', 'object_id' => $root->id ) );
  33          $this->assertTrue( (bool)$user->can( 'write', 'project', $root->id ) );
  34          $this->assertTrue( (bool)$user->can( 'write', 'project', $sub->id ) );
  35          $this->assertFalse( (bool)$user->can( 'write', 'project', $other->id ) );
  36      }
  37  
  38  	function test_recursive_validator_permissions() {
  39          $object_type = GP::$validator_permission->object_type;
  40          $action = 'whatever';
  41          $user = GP::$user->create( array( 'user_login' => 'gugu', 'user_email' => 'gugu@gugu.net' ) );
  42  
  43          $other = GP::$project->create( array( 'name' => 'Other', 'slug' => 'other', 'path' => 'other') );
  44          $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root', 'path' => 'root') );
  45          $sub = GP::$project->create( array( 'name' => 'Sub', 'slug' => 'sub', 'parent_project_id' => $root->id, 'path' => 'root/sub' ) );
  46  
  47          GP::$validator_permission->create( array( 'user_id' => $user->id, 'action' => 'whatever',
  48              'project_id' => $root->id, 'locale_slug' => 'bg', 'set_slug' => 'default' ) );
  49  
  50          $this->assertTrue( (bool)$user->can( 'whatever', $object_type, GP::$validator_permission->object_id( $root->id, 'bg', 'default' ) ) );
  51          $this->assertTrue( (bool)$user->can( 'whatever', $object_type, GP::$validator_permission->object_id( $sub->id, 'bg', 'default' ) ) );
  52          $this->assertTrue( (bool)$user->can( 'whatever', $object_type, GP::$validator_permission->object_id( $sub->id, 'bg', 'default' ) ) );
  53          $this->assertFalse( (bool)$user->can( 'other', $object_type, $sub->id.'|bg|default' ) );
  54          $this->assertFalse( (bool)$user->can( 'whatever', $object_type, $sub->id.'|en|default' ) );
  55          $this->assertFalse( (bool)$user->can( 'whatever', $object_type, $sub->id.'|bg|slug' ) );
  56      }
  57  
  58  
  59  	function test_approve_translation_set_permissions() {
  60          $user = GP::$user->create( array( 'user_login' => 'gugu', 'user_email' => 'gugu@gugu.net' ) );
  61  
  62          $other = GP::$project->create( array( 'name' => 'Other', 'slug' => 'other', 'path' => 'other') );
  63          $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root', 'path' => 'root') );
  64          $sub = GP::$project->create( array( 'name' => 'Sub', 'slug' => 'sub', 'parent_project_id' => $root->id, 'path' => 'root/sub' ) );
  65  
  66          GP::$validator_permission->create( array( 'user_id' => $user->id, 'action' => 'approve',
  67              'project_id' => $root->id, 'locale_slug' => 'bg', 'set_slug' => 'default' ) );
  68  
  69          $set_root_bg = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $root->id, 'locale' => 'bg') );
  70          $set_sub_bg = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $sub->id, 'locale' => 'bg') );
  71          $set_root_en = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $root->id, 'locale' => 'en') );
  72          $set_root_bg_slug = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'baba', 'project_id' => $root->id, 'locale' => 'bg') );
  73          $set_other_bg = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $other->id, 'locale' => 'bg') );
  74          $this->assertTrue( (bool)$user->can( 'approve', 'translation-set', $set_root_bg->id ) );
  75          $this->assertTrue( (bool)$user->can( 'approve', 'translation-set', $set_sub_bg->id ) );
  76          $this->assertTrue( (bool)$user->can( 'approve', 'translation-set', $set_root_bg->id, array('set' => $set_root_bg) ) );
  77          $this->assertFalse( (bool)$user->can( 'approve', 'translation-set', $set_root_en->id ) );
  78          $this->assertFalse( (bool)$user->can( 'approve', 'translation-set', $set_root_bg_slug->id ) );
  79          $this->assertFalse( (bool)$user->can( 'approve', 'translation-set', $set_other_bg->id ) );
  80      }
  81  
  82  	function assertEqualPermissions( $expected, $actual ) {
  83          $fields = $actual->fields();
  84          unset($fields['id']);
  85      }
  86  }


Generated: Sun May 19 03:59:52 2013 Hosted by follow the white rabbit.