| | 320 | * Test get_adjacent_post returns the next private post when the author is the currently logged in user. |
| | 321 | * |
| | 322 | * @ticket 30287 |
| | 323 | */ |
| | 324 | function test_get_author_private_adjacent_posts() { |
| | 325 | $user_id = $this->factory->user->create( array( 'user_login' => 'user-a' ) ); |
| | 326 | $old_uid = get_current_user_id(); |
| | 327 | wp_set_current_user( $user_id ); |
| | 328 | |
| | 329 | $post_id = $this->factory->post->create( array( 'post_author' => $user_id, 'post_status' => 'private' ) ); |
| | 330 | sleep( 1 ); // get_adjacent_post() doesn't handle posts created in the same second. |
| | 331 | $post_id2 = $this->factory->post->create( array( 'post_author' => $user_id ) ); |
| | 332 | |
| | 333 | if ( ! isset( $GLOBALS['post'] ) ) |
| | 334 | $GLOBALS['post'] = null; |
| | 335 | $orig_post = $GLOBALS['post']; |
| | 336 | $GLOBALS['post'] = get_post( $post_id2 ); |
| | 337 | |
| | 338 | $p = get_adjacent_post(); |
| | 339 | $this->assertInstanceOf( 'WP_Post', $p ); |
| | 340 | $this->assertEquals( $post_id, $p->ID ); |
| | 341 | |
| | 342 | // The same again to make sure a cached query returns the same result |
| | 343 | $p = get_adjacent_post(); |
| | 344 | $this->assertInstanceOf( 'WP_Post', $p ); |
| | 345 | $this->assertEquals( $post_id, $p->ID ); |
| | 346 | |
| | 347 | // Test next |
| | 348 | $p = get_adjacent_post( false, '', false ); |
| | 349 | $this->assertEquals( '', $p ); |
| | 350 | |
| | 351 | unset( $GLOBALS['post'] ); |
| | 352 | $this->assertNull( get_adjacent_post() ); |
| | 353 | |
| | 354 | $GLOBALS['post'] = $orig_post; |
| | 355 | wp_set_current_user( $old_uid ); |
| | 356 | |
| | 357 | // Tests requiring creating more posts can't be run since the query |
| | 358 | // cache in get_adjacent_post() requires a fresh page load to invalidate. |
| | 359 | } |
| | 360 | |
| | 361 | /** |
| | 362 | * Test get_adjacent_post excludes the next private post when the author is not the currently logged in user. |
| | 363 | * |
| | 364 | * @ticket 30287 |
| | 365 | */ |
| | 366 | function test_get_non_author_private_adjacent_posts() { |
| | 367 | $user_id = $this->factory->user->create( array( 'user_login' => 'user-a' ) ); |
| | 368 | $user_id2 = $this->factory->user->create( array( 'user_login' => 'user-b' ) ); |
| | 369 | $old_uid = get_current_user_id(); |
| | 370 | wp_set_current_user( $user_id2 ); |
| | 371 | |
| | 372 | $post_id = $this->factory->post->create( array( 'post_author' => $user_id, 'post_status' => 'private' ) ); |
| | 373 | sleep( 1 ); // get_adjacent_post() doesn't handle posts created in the same second. |
| | 374 | $post_id2 = $this->factory->post->create( array( 'post_author' => $user_id2 ) ); |
| | 375 | |
| | 376 | if ( ! isset( $GLOBALS['post'] ) ) |
| | 377 | $GLOBALS['post'] = null; |
| | 378 | $orig_post = $GLOBALS['post']; |
| | 379 | $GLOBALS['post'] = get_post( $post_id2 ); |
| | 380 | |
| | 381 | $this->assertEmpty( get_adjacent_post() ); |
| | 382 | |
| | 383 | // The same again to make sure a cached query returns the same result |
| | 384 | $this->assertEmpty( get_adjacent_post() ); |
| | 385 | |
| | 386 | // Test next |
| | 387 | $this->assertEmpty( get_adjacent_post( false, '', false ) ); |
| | 388 | |
| | 389 | unset( $GLOBALS['post'] ); |
| | 390 | $this->assertEmpty( get_adjacent_post() ); |
| | 391 | |
| | 392 | $GLOBALS['post'] = $orig_post; |
| | 393 | wp_set_current_user( $old_uid ); |
| | 394 | |
| | 395 | // Tests requiring creating more posts can't be run since the query |
| | 396 | // cache in get_adjacent_post() requires a fresh page load to invalidate. |
| | 397 | } |
| | 398 | |
| | 399 | /** |