mapMetaCap.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * @group user
  4. * @group capabilities
  5. */
  6. class Tests_User_MapMetaCap extends WP_UnitTestCase {
  7. var $super_admins = null;
  8. function setUp() {
  9. parent::setUp();
  10. $this->user_ids = array();
  11. $this->user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  12. $this->author_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
  13. if ( isset( $GLOBALS['super_admins'] ) )
  14. $this->super_admins = $GLOBALS['super_admins'];
  15. $user = new WP_User( $this->user_id );
  16. $GLOBALS['super_admins'] = array( $user->user_login );
  17. $this->post_type = rand_str( 20 );
  18. register_post_type( $this->post_type );
  19. $this->post_id = wp_insert_post( array(
  20. 'post_title' => rand_str(),
  21. 'post_type' => $this->post_type,
  22. 'post_status' => 'private',
  23. 'post_author' => $this->author_id,
  24. ) );
  25. }
  26. function tearDown() {
  27. parent::tearDown();
  28. $GLOBALS['super_admins'] = $this->super_admins;
  29. unset( $GLOBALS['wp_post_types'][ $this->post_type ] );
  30. }
  31. function test_capability_type_post_with_no_extra_caps() {
  32. register_post_type( $this->post_type, array(
  33. 'capability_type' => 'post',
  34. ) );
  35. $post_type_object = get_post_type_object( $this->post_type );
  36. $this->assertTrue( $post_type_object->map_meta_cap );
  37. $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
  38. map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
  39. $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
  40. map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
  41. $this->assertEquals( array( 'read_private_posts' ),
  42. map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
  43. $this->assertEquals( array( 'read_private_posts' ),
  44. map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
  45. $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
  46. map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
  47. $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
  48. map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
  49. }
  50. function test_custom_capability_type_with_map_meta_cap() {
  51. register_post_type( $this->post_type, array(
  52. 'capability_type' => 'book',
  53. 'map_meta_cap' => true,
  54. ) );
  55. $post_type_object = get_post_type_object( $this->post_type );
  56. $this->assertEquals( array( 'edit_others_books', 'edit_private_books' ),
  57. map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
  58. $this->assertEquals( array( 'edit_others_books', 'edit_private_books' ),
  59. map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
  60. $this->assertEquals( array( 'read_private_books' ),
  61. map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
  62. $this->assertEquals( array( 'read_private_books' ),
  63. map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
  64. $this->assertEquals( array( 'delete_others_books', 'delete_private_books' ),
  65. map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
  66. $this->assertEquals( array( 'delete_others_books', 'delete_private_books' ),
  67. map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
  68. }
  69. function test_capability_type_post_with_one_renamed_cap() {
  70. register_post_type( $this->post_type, array(
  71. 'capability_type' => 'post',
  72. 'capabilities' => array( 'edit_posts' => 'edit_books' ),
  73. ) );
  74. $post_type_object = get_post_type_object( $this->post_type );
  75. $this->assertFalse( $post_type_object->map_meta_cap );
  76. $this->assertEquals( array( 'edit_post' ),
  77. map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
  78. $this->assertEquals( array( 'edit_post' ),
  79. map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
  80. $this->assertEquals( array( 'read_post' ),
  81. map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
  82. $this->assertEquals( array( 'read_post' ),
  83. map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
  84. $this->assertEquals( array( 'delete_post' ),
  85. map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
  86. $this->assertEquals( array( 'delete_post' ),
  87. map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
  88. }
  89. function test_capability_type_post_map_meta_cap_true_with_renamed_cap() {
  90. register_post_type( $this->post_type, array(
  91. 'capability_type' => 'post',
  92. 'map_meta_cap' => true,
  93. 'capabilities' => array(
  94. 'edit_post' => 'edit_book', // maps back to itself.
  95. 'edit_others_posts' => 'edit_others_books',
  96. ),
  97. ) );
  98. $post_type_object = get_post_type_object( $this->post_type );
  99. $this->assertTrue( $post_type_object->map_meta_cap );
  100. $this->assertEquals( array( 'edit_others_books', 'edit_private_posts' ),
  101. map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
  102. $this->assertEquals( array( 'edit_others_books', 'edit_private_posts' ),
  103. map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
  104. $this->assertEquals( array( 'read_private_posts' ),
  105. map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
  106. $this->assertEquals( array( 'read_private_posts' ),
  107. map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
  108. $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
  109. map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
  110. $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
  111. map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
  112. }
  113. function test_capability_type_post_with_all_meta_caps_renamed() {
  114. register_post_type( $this->post_type, array(
  115. 'capability_type' => 'post',
  116. 'capabilities' => array(
  117. 'edit_post' => 'edit_book',
  118. 'read_post' => 'read_book',
  119. 'delete_post' => 'delete_book',
  120. ),
  121. ) );
  122. $post_type_object = get_post_type_object( $this->post_type );
  123. $this->assertFalse( $post_type_object->map_meta_cap );
  124. $this->assertEquals( array( 'edit_book' ),
  125. map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
  126. $this->assertEquals( array( 'edit_book' ),
  127. map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
  128. $this->assertEquals( array( 'read_book' ),
  129. map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
  130. $this->assertEquals( array( 'read_book' ),
  131. map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
  132. $this->assertEquals( array( 'delete_book' ),
  133. map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
  134. $this->assertEquals( array( 'delete_book' ),
  135. map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
  136. }
  137. function test_capability_type_post_with_all_meta_caps_renamed_mapped() {
  138. register_post_type( $this->post_type, array(
  139. 'capability_type' => 'post',
  140. 'map_meta_cap' => true,
  141. 'capabilities' => array(
  142. 'edit_post' => 'edit_book',
  143. 'read_post' => 'read_book',
  144. 'delete_post' => 'delete_book',
  145. ),
  146. ) );
  147. $post_type_object = get_post_type_object( $this->post_type );
  148. $this->assertTrue( $post_type_object->map_meta_cap );
  149. $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
  150. map_meta_cap( 'edit_post', $this->user_id, $this->post_id ) );
  151. $this->assertEquals( array( 'edit_others_posts', 'edit_private_posts' ),
  152. map_meta_cap( $post_type_object->cap->edit_post, $this->user_id, $this->post_id ) );
  153. $this->assertEquals( array( 'read_private_posts' ),
  154. map_meta_cap( 'read_post', $this->user_id, $this->post_id ) );
  155. $this->assertEquals( array( 'read_private_posts' ),
  156. map_meta_cap( $post_type_object->cap->read_post, $this->user_id, $this->post_id ) );
  157. $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
  158. map_meta_cap( 'delete_post', $this->user_id, $this->post_id ) );
  159. $this->assertEquals( array( 'delete_others_posts', 'delete_private_posts' ),
  160. map_meta_cap( $post_type_object->cap->delete_post, $this->user_id, $this->post_id ) );
  161. }
  162. function test_unfiltered_html_cap() {
  163. if ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML )
  164. $this->markTestSkipped( 'DISALLOW_UNFILTERED_HTML is defined.' );
  165. if ( is_multisite() ) {
  166. $this->assertEquals( array( 'do_not_allow' ), map_meta_cap( 'unfiltered_html', 0 ) );
  167. $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', $this->user_id ) );
  168. } else {
  169. $this->assertEquals( array( 'unfiltered_html' ), map_meta_cap( 'unfiltered_html', $this->user_id ) );
  170. }
  171. }
  172. /**
  173. * @ticket 20488
  174. */
  175. function test_file_edit_caps_not_reliant_on_unfiltered_html_constant() {
  176. if ( defined( 'DISALLOW_FILE_MODS' ) || defined( 'DISALLOW_FILE_EDIT' ) )
  177. $this->markTestSkipped('DISALLOW_FILE_MODS or DISALLOW_FILE_EDIT is defined.');
  178. if ( defined( 'DISALLOW_UNFILTERED_HTML' ) ) {
  179. if ( ! DISALLOW_UNFILTERED_HTML )
  180. $this->markTestSkipped( 'DISALLOW_UNFILTERED_HTML is defined.' );
  181. } else {
  182. define( 'DISALLOW_UNFILTERED_HTML', true );
  183. }
  184. $this->assertEquals( array( 'update_core' ), map_meta_cap( 'update_core', $this->user_id ) );
  185. $this->assertEquals( array( 'edit_plugins' ), map_meta_cap( 'edit_plugins', $this->user_id ) );
  186. }
  187. }