Skip to content

Commit 6466247

Browse files
committed
fix: capabilities for feedzy categories Codeinwp/feedzy-rss-feeds-pro#674
1 parent 63bcdfb commit 6466247

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

includes/admin/feedzy-rss-feeds-admin.php

+10
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ public function register_post_type() {
266266
$supports = array(
267267
'title',
268268
);
269+
$capability = feedzy_current_user_can();
269270
$args = array(
270271
'labels' => $labels,
271272
'supports' => $supports,
@@ -280,6 +281,15 @@ public function register_post_type() {
280281
'show_in_rest' => true,
281282
'rest_base' => 'feedzy_categories',
282283
'rest_controller_class' => 'WP_REST_Posts_Controller',
284+
'map_meta_cap' => true,
285+
'capabilities' => array(
286+
'publish_posts' => $capability,
287+
'edit_posts' => $capability,
288+
'edit_others_posts' => $capability,
289+
'delete_posts' => $capability,
290+
'delete_others_posts' => $capability,
291+
'read_private_posts' => $capability,
292+
),
283293
);
284294
$args = apply_filters( 'feedzy_post_type_args', $args );
285295
register_post_type( 'feedzy_categories', $args );

phpunit.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
convertWarningsToExceptions="true"
88
>
99
<testsuites>
10-
<testsuite>
10+
<testsuite name="unit-tests">
1111
<directory prefix="test-" suffix=".php">./tests/</directory>
1212
</testsuite>
1313
</testsuites>

tests/test-post-access.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* WordPress unit test plugin.
4+
*
5+
* @package feedzy-rss-feeds-pro
6+
* @subpackage Tests
7+
* @copyright Copyright (c) 2017, Bogdan Preda
8+
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9+
* @since 1.2.0
10+
*/
11+
class Test_Post_Access extends WP_UnitTestCase {
12+
13+
/**
14+
* Utility method to generate a random 5 char string.
15+
*
16+
* @since 3.0.12
17+
* @access private
18+
* @return string
19+
*/
20+
private function get_rand_name() {
21+
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
22+
$result = '';
23+
for ( $i = 0; $i < 5; $i ++ ) {
24+
$result .= $characters[ mt_rand( 0, 61 ) ];
25+
}
26+
27+
return $result;
28+
}
29+
30+
public function test_post_access() {
31+
$random_name = $this->get_rand_name();
32+
$admin_id = $this->factory->user->create(
33+
array(
34+
'role' => 'administrator',
35+
)
36+
);
37+
wp_set_current_user( $admin_id );
38+
$p = $this->factory->post->create_and_get(
39+
array(
40+
'post_title' => $random_name,
41+
'post_type' => 'feedzy_categories',
42+
'post_author' => $admin_id,
43+
)
44+
);
45+
do_action( 'save_post', $p->ID, $p );
46+
$this->assertEquals( $p->post_title, $random_name );
47+
$this->assertEquals( $p->post_type, 'feedzy_categories' );
48+
49+
$this->assertTrue( feedzy_current_user_can() );
50+
$this->assertTrue( current_user_can( 'edit_post', $p->ID ) );
51+
52+
53+
$contributor_id = $this->factory->user->create(
54+
array(
55+
'role' => 'contributor',
56+
)
57+
);
58+
wp_set_current_user( $contributor_id );
59+
60+
$this->assertFalse( feedzy_current_user_can() );
61+
$this->assertFalse( current_user_can( 'edit_post', $p->ID ) );
62+
63+
}
64+
65+
}

0 commit comments

Comments
 (0)