| 82 | * @ticket 41131 |
| 83 | */ |
| 84 | public function test_get_adjacent_post_cache() { |
| 85 | global $wpdb; |
| 86 | // Need some sample posts to test adjacency |
| 87 | $post_one = self::factory()->post->create_and_get( array( |
| 88 | 'post_title' => 'First', |
| 89 | 'post_date' => '2012-01-01 12:00:00' |
| 90 | ) ); |
| 91 | |
| 92 | $post_two = self::factory()->post->create_and_get( array( |
| 93 | 'post_title' => 'Second', |
| 94 | 'post_date' => '2012-02-01 12:00:00' |
| 95 | ) ); |
| 96 | |
| 97 | $post_three = self::factory()->post->create_and_get( array( |
| 98 | 'post_title' => 'Third', |
| 99 | 'post_date' => '2012-03-01 12:00:00' |
| 100 | ) ); |
| 101 | |
| 102 | $post_four = self::factory()->post->create_and_get( array( |
| 103 | 'post_title' => 'Fourth', |
| 104 | 'post_date' => '2012-04-01 12:00:00' |
| 105 | ) ); |
| 106 | |
| 107 | |
| 108 | // Assign some terms |
| 109 | wp_set_object_terms( $post_one->ID, 'wordpress', 'category', false ); |
| 110 | wp_set_object_terms( $post_three->ID, 'wordpress', 'category', false ); |
| 111 | |
| 112 | wp_set_object_terms( $post_two->ID, 'plugins', 'post_tag', false ); |
| 113 | wp_set_object_terms( $post_four->ID, 'plugins', 'post_tag', false ); |
| 114 | |
| 115 | // Test normal post adjacency |
| 116 | $this->go_to( get_permalink( $post_two->ID ) ); |
| 117 | |
| 118 | // Test getting the right result |
| 119 | $this->assertEquals( $post_one, get_adjacent_post( false, '', true ) ); |
| 120 | $this->assertNotEquals( $post_two, get_adjacent_post( false, '', true ) ); |
| 121 | |
| 122 | // Query count to test cachcing. |
| 123 | $num_queries = $wpdb->num_queries; |
| 124 | $this->assertNotEquals( $post_two, get_adjacent_post( false, '', true ) ); |
| 125 | $this->assertEquals( $post_one, get_adjacent_post( false, '', true ) ); |
| 126 | $this->assertSame( $num_queries, $wpdb->num_queries ); |
| 127 | |
| 128 | // Test creating new post busts cache |
| 129 | $post_five = self::factory()->post->create_and_get( array( |
| 130 | 'post_title' => 'Five', |
| 131 | 'post_date' => '2012-04-01 12:00:00' |
| 132 | ) ); |
| 133 | $num_queries = $wpdb->num_queries; |
| 134 | |
| 135 | $this->assertEquals( $post_one, get_adjacent_post( false, '', true ) ); |
| 136 | $this->assertSame( $num_queries + 1, $wpdb->num_queries ); |
| 137 | |
| 138 | $this->assertEquals( $post_four, get_adjacent_post( true, '', false ) ); |
| 139 | $num_queries = $wpdb->num_queries; |
| 140 | $this->assertEquals( $post_four, get_adjacent_post( true, '', false ) ); |
| 141 | $this->assertSame( $num_queries, $wpdb->num_queries ); |
| 142 | |
| 143 | wp_set_object_terms( $post_four->ID, 'themes', 'post_tag', false ); |
| 144 | |
| 145 | $num_queries = $wpdb->num_queries; |
| 146 | $this->assertEquals( $post_four, get_adjacent_post( true, '', false ) ); |
| 147 | $this->assertSame( $num_queries + 2, $wpdb->num_queries ); |
| 148 | } |
| 149 | /** |