From 6ba5122dfeb01399dc2a2744399ba40c8749eefa Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Wed, 10 Jan 2018 21:09:41 -0200 Subject: [PATCH] simple perm test using map --- tests/test-collections.php | 18 +++++++++++++++--- tests/test-permissions.php | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/tests/test-collections.php b/tests/test-collections.php index d052d8a3a..2b904a1b1 100644 --- a/tests/test-collections.php +++ b/tests/test-collections.php @@ -31,8 +31,9 @@ class Collections extends TAINACAN_UnitTestCase { $user_id = get_current_user_id(); $this->assertEquals($new_user, $user_id); - global $Tainacan_Collections; - $this->assertTrue($Tainacan_Collections->can_read($x)); + //global $Tainacan_Collections; + //$this->assertTrue($Tainacan_Collections->can_read($x)); + $this->assertFalse(current_user_can('edit_collection')); $autor1 = $this->factory()->user->create(array( 'role' => 'author' )); wp_set_current_user($autor1); @@ -49,9 +50,20 @@ class Collections extends TAINACAN_UnitTestCase { $this->assertEquals($autor1_id, $x->WP_Post->post_author); $autor2 = $this->factory()->user->create(array( 'role' => 'author' )); wp_set_current_user($autor2); + $x2 = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'testeCapsOwner2', + 'description' => 'adasdasdsa', + 'default_order' => 'DESC' + ), + true + ); $current_user_id = get_current_user_id(); $this->assertEquals($autor2, $current_user_id); - $this->assertFalse($Tainacan_Collections->can_edit($x, $current_user_id)); + $this->assertFalse(current_user_can($x->cap->edit_post, $x->WP_Post->ID)); + $this->assertTrue(current_user_can($x2->cap->edit_post, $x2->WP_Post->ID)); + $this->assertFalse(user_can($autor2, $x->cap->edit_post, $x->WP_Post->ID)); } /** diff --git a/tests/test-permissions.php b/tests/test-permissions.php index b0ba27c25..95ab848bd 100644 --- a/tests/test-permissions.php +++ b/tests/test-permissions.php @@ -2,6 +2,8 @@ namespace Tainacan\Tests; +use Tainacan\Entities\Collection; + /** * Class TestCollections * @@ -17,19 +19,29 @@ class Permissions extends TAINACAN_UnitTestCase { * */ function test_roles () { + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'testePerms', + 'description' => 'adasdasdsa', + ), + true + ); $new_user = $this->factory()->user->create(array( 'role' => 'subscriber' )); wp_set_current_user($new_user); $user_id = get_current_user_id(); $this->assertEquals($new_user, $user_id); - $this->assertTrue(user_can($user_id, 'read_'.\Tainacan\Entities\Collection::get_post_type()), 'User cannot read Collections'); + //var_dump($collection->cap); + $this->assertTrue(user_can($user_id, $collection->cap->read, $collection->get_id()), 'A subscriber user cannot read Collections'); $this->assertTrue(user_can($user_id, 'subscriber')); - $this->assertFalse(user_can($user_id, 'edit_'.\Tainacan\Entities\Collection::get_post_type()), 'A subscriber user can edit a Collections?'); + $this->assertFalse(user_can($user_id, $collection->cap->edit_post, $collection->get_id()), 'A subscriber user can edit a Collections?'); + $new_admin_user = $this->factory()->user->create(array( 'role' => 'administrator' )); wp_set_current_user($new_admin_user); $user_id = get_current_user_id(); $this->assertTrue(user_can($user_id, 'administrator')); - $this->assertTrue(user_can($user_id, 'edit_'.\Tainacan\Entities\Collection::get_post_type()), 'A administrator user cannot edit a Collections?'); + $this->assertTrue(user_can($user_id, $collection->cap->edit_post, $collection->get_id()), 'A administrator user cannot edit a Collections?'); //TODO test all roles and check the capabilities }