[ 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          $fields = $actual->fields();
 104          unset($fields['id']);
 105      }
 106  
 107      /**
 108       * @ticket gh194
 109       */
 110  	function test_permissions_delete_on_user_delete() {
 111          $user = $this->factory->user->create();
 112          $project = $this->factory->project->create();
 113  
 114          GP::$validator_permission->create( array( 'user_id' => $user, 'action' => 'approve',
 115              'project_id' => $project->id, 'locale_slug' => 'bg', 'set_slug' => 'default' ) );
 116  
 117          $permissions = GP::$permission->find_many( array( 'user_id' => $user ) );
 118          $this->assertSame( 1, count( $permissions ) );
 119  
 120          wp_delete_user( $user );
 121  
 122          $permissions = GP::$permission->find_many( array( 'user_id' => $user ) );
 123          $this->assertSame( 0, count( $permissions ) );
 124      }
 125  
 126      /**
 127       * @ticket gh-233
 128       */
 129  	function test_administrator_permissions_create() {
 130          $user = $this->factory->user->create();
 131  
 132          GP::$administrator_permission->create( array( 'user_id' => $user ) );
 133  
 134          $permissions = GP::$administrator_permission->find( array( 'user_id' => $user ) );
 135          $this->assertSame( 1, count( $permissions ) );
 136      }
 137  
 138      /**
 139       * @ticket gh-377
 140       */
 141  	function test_import_permissions() {
 142          $user = $this->factory->user->create();
 143  
 144          $project = $this->factory->project->create();
 145          $set = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $project->id, 'locale' => 'bg' ) );
 146          $set2 = GP::$translation_set->create( array( 'name' => 'Set', 'slug' => 'default', 'project_id' => $project->id, 'locale' => 'de' ) );
 147  
 148          $this->assertFalse( (bool) GP::$permission->user_can( $user, 'import-waiting', 'translation-set', $set->id ) );
 149          $this->assertFalse( (bool) GP::$permission->user_can( $user, 'import-waiting', 'translation-set', $set2->id ) );
 150  
 151          GP::$permission->create( array( 'user_id' => $user, 'action' => 'import-waiting', 'object_type' => 'translation-set', 'object_id' => $set2->id ) );
 152  
 153          $this->assertTrue( (bool) GP::$permission->user_can( $user, 'import-waiting', 'translation-set', $set2->id ) );
 154          $this->assertFalse( (bool) GP::$permission->user_can( $user, 'import-waiting', 'translation-set', $set->id ) );
 155      }
 156  }


Generated: Mon Nov 18 01:01:56 2019 Cross-referenced by PHPXref 0.7.1