From 6466247c6a9c69bdf9a993c2558331e02fa784d6 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Thu, 1 Feb 2024 14:50:23 +0200 Subject: [PATCH 1/3] fix: capabilities for feedzy categories Codeinwp/feedzy-rss-feeds-pro#674 --- includes/admin/feedzy-rss-feeds-admin.php | 10 ++++ phpunit.xml | 2 +- tests/test-post-access.php | 65 +++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 tests/test-post-access.php diff --git a/includes/admin/feedzy-rss-feeds-admin.php b/includes/admin/feedzy-rss-feeds-admin.php index 91c6d77a..c2082715 100644 --- a/includes/admin/feedzy-rss-feeds-admin.php +++ b/includes/admin/feedzy-rss-feeds-admin.php @@ -266,6 +266,7 @@ public function register_post_type() { $supports = array( 'title', ); + $capability = feedzy_current_user_can(); $args = array( 'labels' => $labels, 'supports' => $supports, @@ -280,6 +281,15 @@ public function register_post_type() { 'show_in_rest' => true, 'rest_base' => 'feedzy_categories', 'rest_controller_class' => 'WP_REST_Posts_Controller', + 'map_meta_cap' => true, + 'capabilities' => array( + 'publish_posts' => $capability, + 'edit_posts' => $capability, + 'edit_others_posts' => $capability, + 'delete_posts' => $capability, + 'delete_others_posts' => $capability, + 'read_private_posts' => $capability, + ), ); $args = apply_filters( 'feedzy_post_type_args', $args ); register_post_type( 'feedzy_categories', $args ); diff --git a/phpunit.xml b/phpunit.xml index 44f0fdb6..f177514f 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertWarningsToExceptions="true" > - + ./tests/ diff --git a/tests/test-post-access.php b/tests/test-post-access.php new file mode 100644 index 00000000..347dc24f --- /dev/null +++ b/tests/test-post-access.php @@ -0,0 +1,65 @@ +get_rand_name(); + $admin_id = $this->factory->user->create( + array( + 'role' => 'administrator', + ) + ); + wp_set_current_user( $admin_id ); + $p = $this->factory->post->create_and_get( + array( + 'post_title' => $random_name, + 'post_type' => 'feedzy_categories', + 'post_author' => $admin_id, + ) + ); + do_action( 'save_post', $p->ID, $p ); + $this->assertEquals( $p->post_title, $random_name ); + $this->assertEquals( $p->post_type, 'feedzy_categories' ); + + $this->assertTrue( feedzy_current_user_can() ); + $this->assertTrue( current_user_can( 'edit_post', $p->ID ) ); + + + $contributor_id = $this->factory->user->create( + array( + 'role' => 'contributor', + ) + ); + wp_set_current_user( $contributor_id ); + + $this->assertFalse( feedzy_current_user_can() ); + $this->assertFalse( current_user_can( 'edit_post', $p->ID ) ); + + } + +} From 269623da7980e8fffaa8d28ca9f8a4a273a72b88 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Thu, 1 Feb 2024 14:53:31 +0200 Subject: [PATCH 2/3] chore: code style --- includes/admin/feedzy-rss-feeds-admin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/admin/feedzy-rss-feeds-admin.php b/includes/admin/feedzy-rss-feeds-admin.php index c2082715..ce55b9fa 100644 --- a/includes/admin/feedzy-rss-feeds-admin.php +++ b/includes/admin/feedzy-rss-feeds-admin.php @@ -266,7 +266,7 @@ public function register_post_type() { $supports = array( 'title', ); - $capability = feedzy_current_user_can(); + $capability = feedzy_current_user_can(); $args = array( 'labels' => $labels, 'supports' => $supports, @@ -281,7 +281,7 @@ public function register_post_type() { 'show_in_rest' => true, 'rest_base' => 'feedzy_categories', 'rest_controller_class' => 'WP_REST_Posts_Controller', - 'map_meta_cap' => true, + 'map_meta_cap' => true, 'capabilities' => array( 'publish_posts' => $capability, 'edit_posts' => $capability, From 87aec729f16f173ebf1f1bdc3b0c3392ca3bba02 Mon Sep 17 00:00:00 2001 From: Bogdan Preda Date: Thu, 1 Feb 2024 14:57:07 +0200 Subject: [PATCH 3/3] chore: fix naming for phpunit test --- tests/test-post-access.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test-post-access.php b/tests/test-post-access.php index 347dc24f..15fc1532 100644 --- a/tests/test-post-access.php +++ b/tests/test-post-access.php @@ -27,7 +27,7 @@ private function get_rand_name() { return $result; } - public function test_post_access() { + public function test_custom_post_access() { $random_name = $this->get_rand_name(); $admin_id = $this->factory->user->create( array(