|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Tests for author_can(). |
| 5 | + * |
| 6 | + * @group user |
| 7 | + * @group capabilities |
| 8 | + * |
| 9 | + * @covers ::author_can |
| 10 | + */ |
| 11 | +class Tests_User_AuthorCan extends WP_UnitTestCase { |
| 12 | + |
| 13 | + /** |
| 14 | + * Editor user ID. |
| 15 | + * |
| 16 | + * @var int |
| 17 | + */ |
| 18 | + protected static $editor_id; |
| 19 | + |
| 20 | + /** |
| 21 | + * Contributor user ID. |
| 22 | + * |
| 23 | + * @var int |
| 24 | + */ |
| 25 | + protected static $contributor_id; |
| 26 | + |
| 27 | + /** |
| 28 | + * Published post by the editor. |
| 29 | + * |
| 30 | + * @var WP_Post |
| 31 | + */ |
| 32 | + protected static $editor_post; |
| 33 | + |
| 34 | + /** |
| 35 | + * Draft post by the contributor. |
| 36 | + * |
| 37 | + * @var WP_Post |
| 38 | + */ |
| 39 | + protected static $contributor_draft; |
| 40 | + |
| 41 | + /** |
| 42 | + * Published post by the contributor. |
| 43 | + * |
| 44 | + * @var WP_Post |
| 45 | + */ |
| 46 | + protected static $contributor_published; |
| 47 | + |
| 48 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 49 | + self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) ); |
| 50 | + self::$contributor_id = $factory->user->create( array( 'role' => 'contributor' ) ); |
| 51 | + |
| 52 | + self::$editor_post = $factory->post->create_and_get( array( 'post_author' => self::$editor_id ) ); |
| 53 | + self::$contributor_draft = $factory->post->create_and_get( |
| 54 | + array( |
| 55 | + 'post_author' => self::$contributor_id, |
| 56 | + 'post_status' => 'draft', |
| 57 | + ) |
| 58 | + ); |
| 59 | + self::$contributor_published = $factory->post->create_and_get( array( 'post_author' => self::$contributor_id ) ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Tests that the capability is checked against the post's author. |
| 64 | + * |
| 65 | + * @ticket 65819 |
| 66 | + */ |
| 67 | + public function test_checks_capability_of_post_author() { |
| 68 | + $this->assertTrue( author_can( self::$editor_post, 'edit_others_posts' ), 'An editor should be able to edit others\' posts.' ); |
| 69 | + $this->assertFalse( author_can( self::$contributor_draft, 'edit_others_posts' ), 'A contributor should not be able to edit others\' posts.' ); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Tests that a post ID is accepted as well as a post object. |
| 74 | + * |
| 75 | + * @ticket 65819 |
| 76 | + */ |
| 77 | + public function test_accepts_post_id() { |
| 78 | + $this->assertTrue( author_can( self::$editor_post->ID, 'publish_posts' ), 'The editor\'s post ID should resolve to the editor.' ); |
| 79 | + $this->assertFalse( author_can( self::$contributor_draft->ID, 'publish_posts' ), 'The contributor\'s post ID should resolve to the contributor.' ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Tests that the result does not depend on the current user. |
| 84 | + * |
| 85 | + * @ticket 65819 |
| 86 | + */ |
| 87 | + public function test_ignores_current_user() { |
| 88 | + wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); |
| 89 | + |
| 90 | + $this->assertFalse( author_can( self::$contributor_draft, 'edit_others_posts' ) ); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Tests that extra arguments are passed on, so meta capabilities are checked for the given object. |
| 95 | + * |
| 96 | + * @ticket 65819 |
| 97 | + */ |
| 98 | + public function test_passes_extra_arguments_for_meta_capabilities() { |
| 99 | + $this->assertTrue( |
| 100 | + author_can( self::$contributor_draft, 'edit_post', self::$contributor_draft->ID ), |
| 101 | + 'A contributor should be able to edit their own draft.' |
| 102 | + ); |
| 103 | + $this->assertFalse( |
| 104 | + author_can( self::$contributor_draft, 'edit_post', self::$contributor_published->ID ), |
| 105 | + 'A contributor should not be able to edit their own published post.' |
| 106 | + ); |
| 107 | + $this->assertFalse( |
| 108 | + author_can( self::$contributor_draft, 'edit_post', self::$editor_post->ID ), |
| 109 | + 'A contributor should not be able to edit another user\'s post.' |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Tests that false is returned for a post that does not exist. |
| 115 | + * |
| 116 | + * @ticket 65819 |
| 117 | + */ |
| 118 | + public function test_returns_false_for_missing_post() { |
| 119 | + $this->assertFalse( author_can( PHP_INT_MAX, 'read' ) ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Tests that false is returned when the post's author does not exist. |
| 124 | + * |
| 125 | + * @ticket 65819 |
| 126 | + */ |
| 127 | + public function test_returns_false_for_missing_author() { |
| 128 | + $post = self::factory()->post->create_and_get( array( 'post_author' => PHP_INT_MAX ) ); |
| 129 | + |
| 130 | + $this->assertFalse( author_can( $post, 'read' ) ); |
| 131 | + } |
| 132 | +} |
0 commit comments