From ab10e5fe71b6b3767ec2beba0ef14ed58d5a21f5 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 13 Feb 2015 02:24:57 +0000 Subject: [PATCH] Add tests for some of `WP_Query`'s sticky post logic. See #27282. git-svn-id: https://develop.svn.wordpress.org/trunk@31439 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/query/stickies.php | 115 +++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 tests/phpunit/tests/query/stickies.php diff --git a/tests/phpunit/tests/query/stickies.php b/tests/phpunit/tests/query/stickies.php new file mode 100644 index 0000000000..73ff74623b --- /dev/null +++ b/tests/phpunit/tests/query/stickies.php @@ -0,0 +1,115 @@ +post->create( array( + 'post_date' => $post_date, + ) ); + } + + stick_post( self::$posts[2] ); + stick_post( self::$posts[14] ); + stick_post( self::$posts[8] ); + + self::commit_transaction(); + } + + public static function tearDownAfterClass() { + foreach ( self::$posts as $p ) { + wp_delete_post( $p, true ); + } + + self::commit_transaction(); + } + + public function test_stickies_should_be_ignored_when_is_home_is_false() { + $q = new WP_Query( array( + 'year' => date( 'Y' ), + 'fields' => 'ids', + 'posts_per_page' => 3, + ) ); + + $expected = array( + self::$posts[0], + self::$posts[1], + self::$posts[2], + ); + + $this->assertEquals( $expected, $q->posts ); + } + + public function test_stickies_should_be_included_when_is_home_is_true() { + $this->go_to( '/' ); + + $q = $GLOBALS['wp_query']; + + $this->assertEquals( self::$posts[2], $q->posts[0]->ID ); + $this->assertEquals( self::$posts[8], $q->posts[1]->ID ); + $this->assertEquals( self::$posts[14], $q->posts[2]->ID ); + } + + public function test_stickies_should_not_be_included_on_pages_other_than_1() { + $this->go_to( '/?paged=2' ); + + $q = $GLOBALS['wp_query']; + + $found = wp_list_pluck( $q->posts, 'ID' ); + $this->assertNotContains( self::$posts[2], $found ); + } + + public function test_stickies_should_not_be_included_when_ignore_sticky_posts_is_true() { + add_action( 'parse_query', array( $this, 'set_ignore_sticky_posts' ) ); + $this->go_to( '/' ); + remove_action( 'parse_query', array( $this, 'set_ignore_sticky_posts' ) ); + + $q = $GLOBALS['wp_query']; + + $expected = array( + self::$posts[0], + self::$posts[1], + self::$posts[2], + self::$posts[3], + self::$posts[4], + self::$posts[5], + self::$posts[6], + self::$posts[7], + self::$posts[8], + self::$posts[9], + ); + + $this->assertEquals( $expected, wp_list_pluck( $q->posts, 'ID' ) ); + } + + public function test_stickies_should_obey_post__not_in() { + add_action( 'parse_query', array( $this, 'set_post__not_in' ) ); + $this->go_to( '/' ); + remove_action( 'parse_query', array( $this, 'set_post__not_in' ) ); + + $q = $GLOBALS['wp_query']; + + $this->assertEquals( self::$posts[2], $q->posts[0]->ID ); + $this->assertEquals( self::$posts[14], $q->posts[1]->ID ); + $this->assertNotContains( self::$posts[8], wp_list_pluck( $q->posts, 'ID' ) ); + } + + public function set_ignore_sticky_posts( $q ) { + $q->set( 'ignore_sticky_posts', true ); + } + + public function set_post__not_in( $q ) { + $q->set( 'post__not_in', array( self::$posts[8] ) ); + } +}