| 110 | |
| 111 | /** |
| 112 | * Tests if you don't hit the database if you update a meta right after you added it |
| 113 | * @ticket 22192 |
| 114 | */ |
| 115 | function test_updating_after_saving() { |
| 116 | global $wpdb; |
| 117 | |
| 118 | $this->query_count = 0; |
| 119 | $post_id = wp_insert_post( array( 'post_type' => 'page' ) ); |
| 120 | |
| 121 | add_filter( 'query', array( $this, 'query_counter' ) ); |
| 122 | |
| 123 | $values = array( 'somevalue', 1, 0, true, false, 60, array() ); |
| 124 | $update_result = false; |
| 125 | |
| 126 | foreach ( $values as $key => $value ) { |
| 127 | add_post_meta( $post_id, 'unique-' . $key, $value ); |
| 128 | $update_result = $update_result || update_post_meta( $post_id, 'unique-' . $key, $value ); |
| 129 | } |
| 130 | |
| 131 | // There should be only 1 query each because we save with "add_post_meta" |
| 132 | // and update_post_meta should see it in the cache |
| 133 | // |
| 134 | // And 1 query extra for filling the cache all the meta values ( filling the cache ) |
| 135 | $this->assertEquals( count( $values ) + 1, $this->query_count ); |
| 136 | $this->assertFalse( $update_result ); |
| 137 | |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Helper function to count queries, meant to be called by 'query' action |
| 142 | */ |
| 143 | public function query_counter( $query ) { |
| 144 | var_dump( $query ); |
| 145 | $this->query_count++; |
| 146 | return $query; |
| 147 | } |