|
| 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