[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/tests/phpunit/testcases/ -> test_permissions.php (source)

   1  <?php
   2  
   3  class GP_Test_Permissions extends GP_UnitTestCase {
   4  
   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_user_can() {
  22          $user = $this->factory->user->create();
  23          $set_1_permission = array( 'user_id' => $user, 'action' => 'write', 'object_type' => 'translation-set', 'object_id' => 1 );
  24          GP::$permission->create( $set_1_permission );
  25          $this->assertTrue( GP::$permission->user_can( $user, 'write', 'translation-set', 1 ) );
  26          $this->assertFalse( GP::$permission->user_can( $user, 'write', 'translation-set', 2 ) );
  27          $this->assertFalse( GP::$permission->user_can( $user, 'write', 'translation-set' ) );
  28          $this->assertFalse( GP::$permission->user_can( $user, 'write' ) );
  29      }
  30  
  31      function test_admin_should_be_able_to_do_random_actions() {
  32          $admin_user = $this->factory->user->create_admin();
  33          $this->assertTrue( GP::$permission->user_can( $admin_user, 'milk', 'a cow' ) );
  34          $this->assertTrue( GP::$permission->user_can( $admin_user, 'milk', 'a cow', 5 ) );
  35      }
  36  
  37      function test_non_admin_should_not_be_able_to_do_random_actions() {
  38          $nonadmin_user = $this->factory->user->create();
  39          $this->assertFalse( GP::$permission->user_can( $nonadmin_user, 'milk', 'a cow' ) );
  40          $this->assertFalse( GP::$permission->user_can( $nonadmin_user, 'milk', 'a cow', 5 ) );
  41      }
  42  
  43  	function test_logged_out_permissions() {
  44          $project = $this->factory->project->create();
  45          $this->assertFalse( (bool)GP::$permission->current_user_can( 'admin' ) );
  46          $this->assertFalse( (bool)GP::$permission->current_user_can( 'write', 'project', $project->id ) );
  47      }
  48  
  49  	function test_recursive_project_permissions() {
  50          $user = $this->factory->user->create();
  51          $other = GP::$project->create( array( 'name' => 'Other', 'slug' => 'other', 'path' => 'other') );
  52          $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root', 'path' => 'root') );
  53          $sub = GP::$project->create( array( 'name' => 'Sub', 'slug' => 'sub', 'parent_project_id' => $root->id, 'path' => 'root/sub' ) );
  54  
  55          GP::$permission->create( array( 'user_id' => $user, 'action' => 'write', 'object_type' => 'project', 'object_id' => $root->id ) );
  56          $this->assertTrue( (bool)GP::$permission->user_can( $user, 'write', 'project', $root->id ) );
  57          $this->assertTrue( (bool)GP::$permission->user_can( $user, 'write', 'project', $sub->id ) );
  58          $this->assertFalse( (bool)GP::$permission->user_can( $user, 'write', 'project', $other->id ) );
  59      }
  60  
  61  	function test_recursive_validator_permissions() {
  62          $object_type = GP::$validator_permission->object_type;
  63          $user = $this->factory->user->create();
  64  
  65          $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root', 'path' => 'root') );
  66          $sub = GP::$project->create( array( 'name' => 'Sub', 'slug' => 'sub', 'parent_project_id' => $root->id, 'path' => 'root/sub' ) );
  67  
  68          GP::$validator_permission->create( array( 'user_id' => $user, 'action' => 'whatever',
  69              'project_id' => $root->id, 'locale_slug' => 'bg', 'set_slug' => 'default' ) );
  70  
  71          $this->assertTrue( (bool)GP::$permission->user_can( $user, 'whatever', $object_type, GP::$validator_permission->object_id( $root->id, 'bg', 'default' ) ) );
  72          $this->assertTrue( (bool)GP::$permission->user_can( $user, 'whatever', $object_type, GP::$validator_permission->object_id( $sub->id, 'bg', 'default' ) ) );
  73          $this->assertTrue( (bool)GP::$permission->user_can( $user, 'whatever', $object_type, GP::$validator_permission->object_id( $sub->id, 'bg', 'default' ) ) );
  74          $this->assertFalse( (bool)GP::$permission->user_can( $user, 'other', $object_type, $sub->id.'|bg|default' ) );
  75          $this->assertFalse( (bool)GP::$permission->user_can( $user, 'whatever', $object_type, $sub->id.'|en|default' ) );
  76          $this->assertFalse( (bool)GP::$permission->user_can( $user, 'whatever', $object_type, $sub->id.'|bg|slug' ) );
  77      }
  78  
  79  	function test_approve_translation_set_permissions() {
  80          $user = $this->factory->user->create();
  81  
  82          $other = GP::$project->create( array( 'name' => 'Other', 'slug' => 'other', 'path' => 'other') );
  83          $root = GP::$project->create( array( 'name' => 'Root', 'slug' => 'root', 'path' => 'root') );
  84          $sub = GP::$project->create( array( 'name' => 'Sub', 'slug' => 'sub', 'parent_project_id' => $root->id, 'path' => 'root/sub' ) );
  85  
  86          GP::$validator_permission->create( array( 'user_id' => $user, 'action' => 'approve',
  87              'project_id' => $root->id, 'locale_slug' => 'bg', 'set_slug' => 'default' ) );
  88  
  89          $set_root_bg = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $root->id, 'locale' => 'bg') );
  90          $set_sub_bg = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $sub->id, 'locale' => 'bg') );
  91          $set_root_en = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $root->id, 'locale' => 'en') );
  92          $set_root_bg_slug = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'baba', 'project_id' => $root->id, 'locale' => 'bg') );
  93          $set_other_bg = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $other->id, 'locale' => 'bg') );
  94          $this->assertTrue( (bool)GP::$permission->user_can( $user, 'approve', 'translation-set', $set_root_bg->id ) );
  95          $this->assertTrue( (bool)GP::$permission->user_can( $user, 'approve', 'translation-set', $set_sub_bg->id ) );
  96          $this->assertTrue( (bool)GP::$permission->user_can( $user, 'approve', 'translation-set', $set_root_bg->id, array('set' => $set_root_bg) ) );
  97          $this->assertFalse( (bool)GP::$permission->user_can( $user, 'approve', 'translation-set', $set_root_en->id ) );
  98          $this->assertFalse( (bool)GP::$permission->user_can( $user, 'approve', 'translation-set', $set_root_bg_slug->id ) );
  99          $this->assertFalse( (bool)GP::$permission->user_can( $user, 'approve', 'translation-set', $set_other_bg->id ) );
 100      }
 101  
 102  	function assertEqualPermissions( $expected, $actual ) {
 103          foreach(  $expected as $field => $value ) {
 104              $this->assertSame( $value, $actual->$field, "Field: $field" );
 105          }
 106      }
 107  
 108      /**
 109       * @ticket gh194
 110       */
 111  	function test_permissions_delete_on_user_delete() {
 112          $user = $this->factory->user->create();
 113          $project = $this->factory->project->create();
 114  
 115          GP::$validator_permission->create( array( 'user_id' => $user, 'action' => 'approve',
 116              'project_id' => $project->id, 'locale_slug' => 'bg', 'set_slug' => 'default' ) );
 117  
 118          $permissions = GP::$permission->find_many( array( 'user_id' => $user ) );
 119          $this->assertSame( 1, count( $permissions ) );
 120  
 121          wp_delete_user( $user );
 122  
 123          $permissions = GP::$permission->find_many( array( 'user_id' => $user ) );
 124          $this->assertSame( 0, count( $permissions ) );
 125      }
 126  
 127      /**
 128       * @ticket gh-233
 129       */
 130  	function test_administrator_permissions_create() {
 131          $user = $this->factory->user->create();
 132  
 133          GP::$administrator_permission->create( array( 'user_id' => $user ) );
 134  
 135          $permissions = GP::$administrator_permission->find( array( 'user_id' => $user ) );
 136          $this->assertSame( 1, count( $permissions ) );
 137      }
 138  
 139      /**
 140       * @ticket gh-377
 141       */
 142  	function test_import_permissions() {
 143          $user = $this->factory->user->create();
 144  
 145          $project = $this->factory->project->create();
 146          $set = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $project->id, 'locale' => 'bg' ) );
 147          $set2 = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $project->id, 'locale' => 'de' ) );
 148  
 149          $this->assertFalse( (bool) GP::$permission->user_can( $user, 'import-waiting', 'translation-set', $set->id ) );
 150          $this->assertFalse( (bool) GP::$permission->user_can( $user, 'import-waiting', 'translation-set', $set2->id ) );
 151  
 152          GP::$permission->create( array( 'user_id' => $user, 'action' => 'import-waiting', 'object_type' => 'translation-set', 'object_id' => $set2->id ) );
 153  
 154          $this->assertTrue( (bool) GP::$permission->user_can( $user, 'import-waiting', 'translation-set', $set2->id ) );
 155          $this->assertFalse( (bool) GP::$permission->user_can( $user, 'import-waiting', 'translation-set', $set->id ) );
 156      }
 157  }


Generated: Sun Dec 22 01:01:04 2024 Cross-referenced by PHPXref 0.7.1