From 59abfc6fb3a9cd5ef355a88cf65a1cadb09eb47c Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Mon, 4 Jun 2018 15:53:03 -0300 Subject: [PATCH] simple dependencies capabilities test --- tests/test-default-capabilities.php | 76 +++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/tests/test-default-capabilities.php b/tests/test-default-capabilities.php index cb7ad4ec5..be644eed6 100644 --- a/tests/test-default-capabilities.php +++ b/tests/test-default-capabilities.php @@ -49,10 +49,80 @@ class DefaultCapabilities extends TAINACAN_UnitTestCase { $this->assertTrue(current_user_can($entity_cap->$cap), "tainacan-$role_name does not have capability {$entity_cap->$cap}"); } } - - } } - + } + + /** + * @group capabilities_denpendecies + */ + function test_capabilities_denpendecies() { + + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'test capabilities denpendecies', + ), + true + ); + $item = $this->tainacan_entity_factory->create_entity( + 'item', + array( + 'title' => 'test capabilities denpendecies Item', + 'collection' => $collection, + ), + true + ); + + $Tainacan_Capabilities = \Tainacan\Capabilities::get_instance(); + $deps = $Tainacan_Capabilities::$dependencies; + $defaults_caps = $Tainacan_Capabilities->defaults; + $tainacan_roles = $Tainacan_Capabilities->get_tainacan_roles(); + + foreach ($defaults_caps as $post_type => $wp_append_roles) { + if(array_key_exists($post_type, $deps)) { + $entity = false; + $entity_cap = false; + + if($post_type != 'tainacan-items') { + $entity = Repository::get_entity_by_post_type($post_type); + $entity_cap = $entity->get_capabilities(); + } + + foreach ($wp_append_roles as $role_name => $caps) { + $role = get_role($role_name); + + $new_user = $this->factory()->user->create(array( 'role' => $role_name )); + wp_set_current_user($new_user); + + foreach ($caps as $cap) { + if(array_key_exists($cap, $deps[$post_type])) { + $dep_cap = $deps[$post_type][$cap]; + if($post_type == 'tainacan-items') { + $this->assertTrue(current_user_can($dep_cap), "$role_name does not have a dependency capability {$dep_cap} for tainacan-items" ); + } else { + $this->assertTrue(current_user_can($dep_cap), "$role_name does not have a dependency capability {$dep_cap} for {$entity_cap->$cap}" ); + } + } + } + + $new_user = $this->factory()->user->create(array( 'role' => 'tainacan-' . $role_name )); + wp_set_current_user($new_user); + + if(in_array($role_name, $tainacan_roles) ) { + foreach ($caps as $cap) { + if(array_key_exists($cap, $deps[$post_type])) { + $dep_cap = $deps[$post_type][$cap]; + if($post_type == 'tainacan-items') { + $this->assertTrue(current_user_can($dep_cap), "tainaca-$role_name does not have a dependency capability {$dep_cap} for tainacan-items" ); + } else { + $this->assertTrue(current_user_can($dep_cap), "tainaca-$role_name does not have a dependency capability {$dep_cap} for {$entity_cap->$cap}" ); + } + } + } + } + } + } + } } } \ No newline at end of file