getPostsByAuthorSql.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @group post
  4. */
  5. class Tests_Post_GetPostsByAuthorSql extends WP_UnitTestCase {
  6. public function test_post_type_post() {
  7. $maybe_string = get_posts_by_author_sql( 'post' );
  8. $this->assertContains( "post_type = 'post'", $maybe_string );
  9. }
  10. public function test_post_type_page() {
  11. $maybe_string = get_posts_by_author_sql( 'page' );
  12. $this->assertContains( "post_type = 'page'", $maybe_string );
  13. }
  14. public function test_non_existent_post_type() {
  15. $maybe_string = get_posts_by_author_sql( 'non_existent_post_type' );
  16. $this->assertContains( '1 = 0', $maybe_string );
  17. }
  18. public function test_multiple_post_types() {
  19. register_post_type( 'foo' );
  20. register_post_type( 'bar' );
  21. $maybe_string = get_posts_by_author_sql( array( 'foo', 'bar' ) );
  22. $this->assertContains( "post_type = 'foo'", $maybe_string );
  23. $this->assertContains( "post_type = 'bar'", $maybe_string );
  24. _unregister_post_type( 'foo' );
  25. _unregister_post_type( 'bar' );
  26. }
  27. public function test_full_true() {
  28. $maybe_string = get_posts_by_author_sql( 'post', true );
  29. $this->assertRegExp( '/^WHERE /', $maybe_string );
  30. }
  31. public function test_full_false() {
  32. $maybe_string = get_posts_by_author_sql( 'post', false );
  33. $this->assertNotRegExp( '/^WHERE /', $maybe_string );
  34. }
  35. public function test_post_type_clause_should_be_included_when_full_is_true() {
  36. $maybe_string = get_posts_by_author_sql( 'post', true );
  37. $this->assertContains( "post_type = 'post'", $maybe_string );
  38. }
  39. public function test_post_type_clause_should_be_included_when_full_is_false() {
  40. $maybe_string = get_posts_by_author_sql( 'post', false );
  41. $this->assertContains( "post_type = 'post'", $maybe_string );
  42. }
  43. public function test_post_author_should_create_post_author_clause() {
  44. $maybe_string = get_posts_by_author_sql( 'post', true, 1 );
  45. $this->assertContains( 'post_author = 1', $maybe_string );
  46. }
  47. public function test_public_only_true_should_not_allow_any_private_posts_for_loggedin_user() {
  48. $current_user = get_current_user_id();
  49. $u = self::factory()->user->create();
  50. wp_set_current_user( $u );
  51. $maybe_string = get_posts_by_author_sql( 'post', true, $u, true );
  52. $this->assertNotContains( "post_status = 'private'", $maybe_string );
  53. wp_set_current_user( $current_user );
  54. }
  55. public function test_public_only_should_default_to_false() {
  56. $current_user = get_current_user_id();
  57. $u = self::factory()->user->create();
  58. wp_set_current_user( $u );
  59. $this->assertSame( get_posts_by_author_sql( 'post', true, $u, false ), get_posts_by_author_sql( 'post', true, $u ) );
  60. wp_set_current_user( $current_user );
  61. }
  62. public function test_public_only_false_should_allow_current_user_access_to_own_private_posts_when_current_user_matches_post_author() {
  63. $current_user = get_current_user_id();
  64. $u = self::factory()->user->create();
  65. wp_set_current_user( $u );
  66. $maybe_string = get_posts_by_author_sql( 'post', true, $u, false );
  67. $this->assertContains( "post_status = 'private'", $maybe_string );
  68. wp_set_current_user( $current_user );
  69. }
  70. public function test_public_only_false_should_not_allow_access_to_private_posts_if_current_user_is_not_post_author() {
  71. $current_user = get_current_user_id();
  72. $u1 = self::factory()->user->create();
  73. $u2 = self::factory()->user->create();
  74. wp_set_current_user( $u1 );
  75. $maybe_string = get_posts_by_author_sql( 'post', true, $u2, false );
  76. $this->assertNotContains( "post_status = 'private'", $maybe_string );
  77. wp_set_current_user( $current_user );
  78. }
  79. public function test_public_only_false_should_allow_current_user_access_to_own_private_posts_when_post_author_is_not_provided() {
  80. $current_user = get_current_user_id();
  81. $u = self::factory()->user->create();
  82. wp_set_current_user( $u );
  83. $maybe_string = get_posts_by_author_sql( 'post', true, $u, false );
  84. $this->assertContains( "post_status = 'private'", $maybe_string );
  85. $this->assertContains( "post_author = $u", $maybe_string );
  86. wp_set_current_user( $current_user );
  87. }
  88. public function test_administrator_should_have_access_to_private_posts_when_public_only_is_false() {
  89. $current_user = get_current_user_id();
  90. $u = self::factory()->user->create( array( 'role' => 'administrator' ) );
  91. wp_set_current_user( $u );
  92. $maybe_string = get_posts_by_author_sql( 'post', true, null, false );
  93. $this->assertContains( "post_status = 'private'", $maybe_string );
  94. $this->assertNotContains( 'post_author', $maybe_string );
  95. wp_set_current_user( $current_user );
  96. }
  97. public function test_user_has_access_only_to_private_posts_for_certain_post_types() {
  98. register_post_type( 'foo', array( 'capabilities' => array( 'read_private_posts' => 'read_private_foo' ) ) );
  99. register_post_type( 'bar', array( 'capabilities' => array( 'read_private_posts' => 'read_private_bar' ) ) );
  100. register_post_type( 'baz', array( 'capabilities' => array( 'read_private_posts' => 'read_private_baz' ) ) );
  101. $current_user = get_current_user_id();
  102. $u = self::factory()->user->create( array( 'role' => 'editor' ) );
  103. $editor_role = get_role( 'editor' );
  104. $editor_role->add_cap( 'read_private_baz' );
  105. wp_set_current_user( $u );
  106. $maybe_string = get_posts_by_author_sql( array( 'foo', 'bar', 'baz' ) );
  107. $editor_role->remove_cap( 'read_private_baz' );
  108. $this->assertNotContains( "post_type = 'foo' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
  109. $this->assertNotContains( "post_type = 'bar' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
  110. $this->assertContains( "post_type = 'baz' AND ( post_status = 'publish' OR post_status = 'private' )", $maybe_string );
  111. _unregister_post_type( 'foo' );
  112. _unregister_post_type( 'bar' );
  113. _unregister_post_type( 'baz' );
  114. wp_set_current_user( $current_user );
  115. }
  116. }