mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-12 21:00:21 +00:00
In get_adjacent_post(), return private post if the current user has the capacity to read it.
This mirrors the check that happens post-query in `WP_Query`. See #30911. Props bswatson. Fixes #30287. git-svn-id: https://develop.svn.wordpress.org/trunk@31302 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -279,10 +279,10 @@ class Tests_URL extends WP_UnitTestCase {
|
||||
force_ssl_login( $forced_login );
|
||||
}
|
||||
|
||||
function test_get_adjacent_post() {
|
||||
$post_id = $this->factory->post->create();
|
||||
sleep( 1 ); // get_adjacent_post() doesn't handle posts created in the same second.
|
||||
$post_id2 = $this->factory->post->create();
|
||||
public function test_get_adjacent_post() {
|
||||
$now = time();
|
||||
$post_id = $this->factory->post->create( array( 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
|
||||
$post_id2 = $this->factory->post->create( array( 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
|
||||
|
||||
if ( ! isset( $GLOBALS['post'] ) )
|
||||
$GLOBALS['post'] = null;
|
||||
@@ -306,9 +306,89 @@ class Tests_URL extends WP_UnitTestCase {
|
||||
$this->assertNull( get_adjacent_post() );
|
||||
|
||||
$GLOBALS['post'] = $orig_post;
|
||||
}
|
||||
|
||||
// Tests requiring creating more posts can't be run since the query
|
||||
// cache in get_adjacent_post() requires a fresh page load to invalidate.
|
||||
/**
|
||||
* Test get_adjacent_post returns the next private post when the author is the currently logged in user.
|
||||
*
|
||||
* @ticket 30287
|
||||
*/
|
||||
public function test_get_adjacent_post_should_return_private_posts_belonging_to_the_current_user() {
|
||||
$u = $this->factory->user->create( array( 'role' => 'author' ) );
|
||||
$old_uid = get_current_user_id();
|
||||
wp_set_current_user( $u );
|
||||
|
||||
$now = time();
|
||||
$p1 = $this->factory->post->create( array( 'post_author' => $u, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
|
||||
$p2 = $this->factory->post->create( array( 'post_author' => $u, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
|
||||
|
||||
if ( ! isset( $GLOBALS['post'] ) ) {
|
||||
$GLOBALS['post'] = null;
|
||||
}
|
||||
$orig_post = $GLOBALS['post'];
|
||||
|
||||
$GLOBALS['post'] = get_post( $p2 );
|
||||
|
||||
$p = get_adjacent_post();
|
||||
$this->assertEquals( $p1, $p->ID );
|
||||
|
||||
$GLOBALS['post'] = $orig_post;
|
||||
wp_set_current_user( $old_uid );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 30287
|
||||
*/
|
||||
public function test_get_adjacent_post_should_return_private_posts_belonging_to_other_users_if_the_current_user_can_read_private_posts() {
|
||||
$u1 = $this->factory->user->create( array( 'role' => 'author' ) );
|
||||
$u2 = $this->factory->user->create( array( 'role' => 'administrator' ) );
|
||||
$old_uid = get_current_user_id();
|
||||
wp_set_current_user( $u2 );
|
||||
|
||||
$now = time();
|
||||
$p1 = $this->factory->post->create( array( 'post_author' => $u1, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
|
||||
$p2 = $this->factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
|
||||
|
||||
if ( ! isset( $GLOBALS['post'] ) ) {
|
||||
$GLOBALS['post'] = null;
|
||||
}
|
||||
$orig_post = $GLOBALS['post'];
|
||||
|
||||
$GLOBALS['post'] = get_post( $p2 );
|
||||
|
||||
$p = get_adjacent_post();
|
||||
$this->assertEquals( $p1, $p->ID );
|
||||
|
||||
$GLOBALS['post'] = $orig_post;
|
||||
wp_set_current_user( $old_uid );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 30287
|
||||
*/
|
||||
public function test_get_adjacent_post_should_not_return_private_posts_belonging_to_other_users_if_the_current_user_cannot_read_private_posts() {
|
||||
$u1 = $this->factory->user->create( array( 'role' => 'author' ) );
|
||||
$u2 = $this->factory->user->create( array( 'role' => 'author' ) );
|
||||
$old_uid = get_current_user_id();
|
||||
wp_set_current_user( $u2 );
|
||||
|
||||
$now = time();
|
||||
$p1 = $this->factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now - 2 ) ) );
|
||||
$p2 = $this->factory->post->create( array( 'post_author' => $u1, 'post_status' => 'private', 'post_date' => date( 'Y-m-d H:i:s', $now - 1 ) ) );
|
||||
$p3 = $this->factory->post->create( array( 'post_author' => $u1, 'post_date' => date( 'Y-m-d H:i:s', $now ) ) );
|
||||
|
||||
if ( ! isset( $GLOBALS['post'] ) ) {
|
||||
$GLOBALS['post'] = null;
|
||||
}
|
||||
$orig_post = $GLOBALS['post'];
|
||||
|
||||
$GLOBALS['post'] = get_post( $p3 );
|
||||
|
||||
$p = get_adjacent_post();
|
||||
$this->assertEquals( $p1, $p->ID );
|
||||
|
||||
$GLOBALS['post'] = $orig_post;
|
||||
wp_set_current_user( $old_uid );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user