Ticket #25193: 25193.diff
File 25193.diff, 30.1 KB (added by , 11 years ago) |
---|
-
src
-
src/wp-includes/query.php
Property changes on: src ___________________________________________________________________ Added: svn:ignore ## -0,0 +1 ## +.wp-tests-version
2051 2051 $fields = "$wpdb->posts.*"; 2052 2052 } 2053 2053 2054 if ( ! empty( $q['scope'] ) ) { 2055 $_scopes = array( 2056 'has_password' => "$wpdb->posts.post_password != ''", 2057 'no_password' => "$wpdb->posts.post_password = ''", 2058 'has_comments' => "$wpdb->posts.comment_count > 0", 2059 'no_comments' => "$wpdb->posts.comment_count = 0", 2060 ); 2061 2062 if ( ! is_array( $q['scope'] ) ) 2063 $q['scope'] = explode( ',', $q['scope'] ); 2064 2065 $scopes = apply_filters( 'query_scopes', $_scopes, $q['scope'] ); 2066 2067 foreach ( $q['scope'] as $scope ) { 2068 if ( ! empty( $scopes[$scope] ) ) 2069 $where .= sprintf( ' AND %s', $scopes[$scope] ); 2070 } 2071 } 2072 2054 2073 if ( '' !== $q['menu_order'] ) 2055 2074 $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order']; 2056 2075 -
tests/phpunit/tests/post/query.php
4 4 * @group meta 5 5 */ 6 6 class Tests_Post_Query extends WP_UnitTestCase { 7 8 parent::setUp();9 7 function setUp() { 8 parent::setUp(); 9 } 10 10 11 12 $post_id = $this->factory->post->create();13 add_post_meta( $post_id, 'foo', rand_str() );14 add_post_meta( $post_id, 'foo', rand_str() );15 $post_id2 = $this->factory->post->create();16 add_post_meta( $post_id2, 'bar', 'val2' );17 $post_id3 = $this->factory->post->create();18 add_post_meta( $post_id3, 'baz', rand_str() );19 $post_id4 = $this->factory->post->create();20 add_post_meta( $post_id4, 'froo', rand_str() );21 $post_id5 = $this->factory->post->create();22 add_post_meta( $post_id5, 'tango', 'val2' );23 $post_id6 = $this->factory->post->create();24 add_post_meta( $post_id6, 'bar', 'val1' );11 function test_meta_key_or_query() { 12 $post_id = $this->factory->post->create(); 13 add_post_meta( $post_id, 'foo', rand_str() ); 14 add_post_meta( $post_id, 'foo', rand_str() ); 15 $post_id2 = $this->factory->post->create(); 16 add_post_meta( $post_id2, 'bar', 'val2' ); 17 $post_id3 = $this->factory->post->create(); 18 add_post_meta( $post_id3, 'baz', rand_str() ); 19 $post_id4 = $this->factory->post->create(); 20 add_post_meta( $post_id4, 'froo', rand_str() ); 21 $post_id5 = $this->factory->post->create(); 22 add_post_meta( $post_id5, 'tango', 'val2' ); 23 $post_id6 = $this->factory->post->create(); 24 add_post_meta( $post_id6, 'bar', 'val1' ); 25 25 26 $query = new WP_Query( array(27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 ) );26 $query = new WP_Query( array( 27 'meta_query' => array( 28 array( 29 'key' => 'foo' 30 ), 31 array( 32 'key' => 'bar', 33 'value' => 'val2' 34 ), 35 array( 36 'key' => 'baz' 37 ), 38 array( 39 'key' => 'froo' 40 ), 41 'relation' => 'OR', 42 ), 43 ) ); 44 44 45 $posts = $query->get_posts(); 46 $this->assertEquals( 4, count( $posts ) ); 47 foreach ( $posts as $post ) { 48 $this->assertInstanceOf( 'WP_Post', $post ); 49 $this->assertEquals( 'raw', $post->filter ); 45 $posts = $query->get_posts(); 46 $this->assertEquals( 4, count( $posts ) ); 47 foreach ( $posts as $post ) { 48 $this->assertInstanceOf( 'WP_Post', $post ); 49 $this->assertEquals( 'raw', $post->filter ); 50 } 51 52 $post_ids = wp_list_pluck( $posts, 'ID' ); 53 $this->assertEqualSets( array( $post_id, $post_id2, $post_id3, $post_id4 ), $post_ids ); 50 54 } 51 55 52 $post_ids = wp_list_pluck( $posts, 'ID' ); 53 $this->assertEqualSets( array( $post_id, $post_id2, $post_id3, $post_id4 ), $post_ids ); 54 } 56 function test_meta_key_and_query() { 57 $post_id = $this->factory->post->create(); 58 add_post_meta( $post_id, 'foo', rand_str() ); 59 add_post_meta( $post_id, 'foo', rand_str() ); 60 $post_id2 = $this->factory->post->create(); 61 add_post_meta( $post_id2, 'bar', 'val2' ); 62 add_post_meta( $post_id2, 'foo', rand_str() ); 63 $post_id3 = $this->factory->post->create(); 64 add_post_meta( $post_id3, 'baz', rand_str() ); 65 $post_id4 = $this->factory->post->create(); 66 add_post_meta( $post_id4, 'froo', rand_str() ); 67 $post_id5 = $this->factory->post->create(); 68 add_post_meta( $post_id5, 'tango', 'val2' ); 69 $post_id6 = $this->factory->post->create(); 70 add_post_meta( $post_id6, 'bar', 'val1' ); 71 add_post_meta( $post_id6, 'foo', rand_str() ); 72 $post_id7 = $this->factory->post->create(); 73 add_post_meta( $post_id7, 'foo', rand_str() ); 74 add_post_meta( $post_id7, 'froo', rand_str() ); 75 add_post_meta( $post_id7, 'baz', rand_str() ); 76 add_post_meta( $post_id7, 'bar', 'val2' ); 55 77 56 function test_meta_key_and_query() { 57 $post_id = $this->factory->post->create(); 58 add_post_meta( $post_id, 'foo', rand_str() ); 59 add_post_meta( $post_id, 'foo', rand_str() ); 60 $post_id2 = $this->factory->post->create(); 61 add_post_meta( $post_id2, 'bar', 'val2' ); 62 add_post_meta( $post_id2, 'foo', rand_str() ); 63 $post_id3 = $this->factory->post->create(); 64 add_post_meta( $post_id3, 'baz', rand_str() ); 65 $post_id4 = $this->factory->post->create(); 66 add_post_meta( $post_id4, 'froo', rand_str() ); 67 $post_id5 = $this->factory->post->create(); 68 add_post_meta( $post_id5, 'tango', 'val2' ); 69 $post_id6 = $this->factory->post->create(); 70 add_post_meta( $post_id6, 'bar', 'val1' ); 71 add_post_meta( $post_id6, 'foo', rand_str() ); 72 $post_id7 = $this->factory->post->create(); 73 add_post_meta( $post_id7, 'foo', rand_str() ); 74 add_post_meta( $post_id7, 'froo', rand_str() ); 75 add_post_meta( $post_id7, 'baz', rand_str() ); 76 add_post_meta( $post_id7, 'bar', 'val2' ); 78 $query = new WP_Query( array( 79 'meta_query' => array( 80 array( 81 'key' => 'foo' 82 ), 83 array( 84 'key' => 'bar', 85 'value' => 'val2' 86 ), 87 array( 88 'key' => 'baz' 89 ), 90 array( 91 'key' => 'froo' 92 ), 93 'relation' => 'AND', 94 ), 95 ) ); 77 96 78 $query = new WP_Query( array( 79 'meta_query' => array( 80 array( 81 'key' => 'foo' 82 ), 83 array( 84 'key' => 'bar', 85 'value' => 'val2' 86 ), 87 array( 88 'key' => 'baz' 89 ), 90 array( 91 'key' => 'froo' 92 ), 93 'relation' => 'AND', 94 ), 95 ) ); 97 $posts = $query->get_posts(); 98 $this->assertEquals( 1, count( $posts ) ); 99 foreach ( $posts as $post ) { 100 $this->assertInstanceOf( 'WP_Post', $post ); 101 $this->assertEquals( 'raw', $post->filter ); 102 } 96 103 97 $posts = $query->get_posts(); 98 $this->assertEquals( 1, count( $posts ) ); 99 foreach ( $posts as $post ) { 100 $this->assertInstanceOf( 'WP_Post', $post ); 101 $this->assertEquals( 'raw', $post->filter ); 102 } 104 $post_ids = wp_list_pluck( $posts, 'ID' ); 105 $this->assertEquals( array( $post_id7 ), $post_ids ); 103 106 104 $post_ids = wp_list_pluck( $posts, 'ID' ); 105 $this->assertEquals( array( $post_id7 ), $post_ids ); 107 $query = new WP_Query( array( 108 'meta_query' => array( 109 array( 110 'key' => 'foo' 111 ), 112 array( 113 'key' => 'bar', 114 ), 115 'relation' => 'AND', 116 ), 117 ) ); 106 118 107 $query = new WP_Query( array( 108 'meta_query' => array( 109 array( 110 'key' => 'foo' 111 ), 112 array( 113 'key' => 'bar', 114 ), 115 'relation' => 'AND', 116 ), 117 ) ); 119 $posts = $query->get_posts(); 120 $this->assertEquals( 3, count( $posts ) ); 121 foreach ( $posts as $post ) { 122 $this->assertInstanceOf( 'WP_Post', $post ); 123 $this->assertEquals( 'raw', $post->filter ); 124 } 118 125 119 $posts = $query->get_posts(); 120 $this->assertEquals( 3, count( $posts ) ); 121 foreach ( $posts as $post ) { 122 $this->assertInstanceOf( 'WP_Post', $post ); 123 $this->assertEquals( 'raw', $post->filter ); 126 $post_ids = wp_list_pluck( $posts, 'ID' ); 127 $this->assertEqualSets( array( $post_id2, $post_id6, $post_id7 ), $post_ids ); 124 128 } 125 129 126 $post_ids = wp_list_pluck( $posts, 'ID' ); 127 $this->assertEqualSets( array( $post_id2, $post_id6, $post_id7 ), $post_ids ); 128 } 130 /** 131 * @ticket 18158 132 */ 133 function test_meta_key_not_exists() { 134 $post_id = $this->factory->post->create(); 135 add_post_meta( $post_id, 'foo', rand_str() ); 136 $post_id2 = $this->factory->post->create(); 137 add_post_meta( $post_id2, 'bar', rand_str() ); 138 $post_id3 = $this->factory->post->create(); 139 add_post_meta( $post_id3, 'bar', rand_str() ); 140 $post_id4 = $this->factory->post->create(); 141 add_post_meta( $post_id4, 'baz', rand_str() ); 142 $post_id5 = $this->factory->post->create(); 143 add_post_meta( $post_id5, 'foo', rand_str() ); 129 144 130 /** 131 * @ticket 18158 132 */ 133 function test_meta_key_not_exists() { 134 $post_id = $this->factory->post->create(); 135 add_post_meta( $post_id, 'foo', rand_str() ); 136 $post_id2 = $this->factory->post->create(); 137 add_post_meta( $post_id2, 'bar', rand_str() ); 138 $post_id3 = $this->factory->post->create(); 139 add_post_meta( $post_id3, 'bar', rand_str() ); 140 $post_id4 = $this->factory->post->create(); 141 add_post_meta( $post_id4, 'baz', rand_str() ); 142 $post_id5 = $this->factory->post->create(); 143 add_post_meta( $post_id5, 'foo', rand_str() ); 145 $query = new WP_Query( array( 146 'meta_query' => array( 147 array( 148 'key' => 'foo', 149 'compare' => 'NOT EXISTS', 150 ), 151 ), 152 ) ); 144 153 145 $query = new WP_Query( array( 146 'meta_query' => array( 147 array( 148 'key' => 'foo', 149 'compare' => 'NOT EXISTS', 150 ), 151 ), 152 ) ); 154 $posts = $query->get_posts(); 155 $this->assertEquals( 3, count( $posts ) ); 156 foreach ( $posts as $post ) { 157 $this->assertInstanceOf( 'WP_Post', $post ); 158 $this->assertEquals( 'raw', $post->filter ); 159 } 153 160 154 $posts = $query->get_posts(); 155 $this->assertEquals( 3, count( $posts ) ); 156 foreach ( $posts as $post ) { 157 $this->assertInstanceOf( 'WP_Post', $post ); 158 $this->assertEquals( 'raw', $post->filter ); 159 } 161 $query = new WP_Query( array( 162 'meta_query' => array( 163 array( 164 'key' => 'foo', 165 'compare' => 'NOT EXISTS', 166 ), 167 array( 168 'key' => 'bar', 169 'compare' => 'NOT EXISTS', 170 ), 171 ), 172 ) ); 160 173 161 $query = new WP_Query( array( 162 'meta_query' => array( 163 array( 164 'key' => 'foo', 165 'compare' => 'NOT EXISTS', 166 ), 167 array( 168 'key' => 'bar', 169 'compare' => 'NOT EXISTS', 170 ), 171 ), 172 ) ); 174 $posts = $query->get_posts(); 175 $this->assertEquals( 1, count( $posts ) ); 176 foreach ( $posts as $post ) { 177 $this->assertInstanceOf( 'WP_Post', $post ); 178 $this->assertEquals( 'raw', $post->filter ); 179 } 173 180 174 $posts = $query->get_posts(); 175 $this->assertEquals( 1, count( $posts ) ); 176 foreach ( $posts as $post ) { 177 $this->assertInstanceOf( 'WP_Post', $post ); 178 $this->assertEquals( 'raw', $post->filter ); 181 $query = new WP_Query( array( 182 'meta_query' => array( 183 array( 184 'key' => 'foo', 185 'compare' => 'NOT EXISTS', 186 ), 187 array( 188 'key' => 'bar', 189 'compare' => 'NOT EXISTS', 190 ), 191 array( 192 'key' => 'baz', 193 'compare' => 'NOT EXISTS', 194 ), 195 ) 196 ) ); 197 198 $posts = $query->get_posts(); 199 $this->assertEquals( 0, count( $posts ) ); 179 200 } 180 201 181 $query = new WP_Query( array( 182 'meta_query' => array( 183 array( 184 'key' => 'foo', 185 'compare' => 'NOT EXISTS', 186 ), 187 array( 188 'key' => 'bar', 189 'compare' => 'NOT EXISTS', 190 ), 191 array( 192 'key' => 'baz', 193 'compare' => 'NOT EXISTS', 194 ), 195 ) 196 ) ); 202 /** 203 * @ticket 20604 204 */ 205 function test_taxonomy_empty_or() { 206 // An empty tax query should return an empty array, not all posts. 197 207 198 $posts = $query->get_posts(); 199 $this->assertEquals( 0, count( $posts ) ); 200 } 208 $this->factory->post->create_many( 10 ); 201 209 202 /** 203 * @ticket 20604 204 */ 205 function test_taxonomy_empty_or() { 206 // An empty tax query should return an empty array, not all posts. 210 $query = new WP_Query( array( 211 'fields' => 'ids', 212 'tax_query' => array( 213 'relation' => 'OR', 214 array( 215 'taxonomy' => 'post_tag', 216 'field' => 'id', 217 'terms' => false, 218 'operator' => 'IN' 219 ), 220 array( 221 'taxonomy' => 'category', 222 'field' => 'id', 223 'terms' => false, 224 'operator' => 'IN' 225 ) 226 ) 227 ) ); 207 228 208 $this->factory->post->create_many( 10 ); 229 $posts = $query->get_posts(); 230 $this->assertEquals( 0 , count( $posts ) ); 231 } 209 232 210 $query = new WP_Query( array( 211 'fields' => 'ids', 212 'tax_query' => array( 213 'relation' => 'OR', 214 array( 215 'taxonomy' => 'post_tag', 216 'field' => 'id', 217 'terms' => false, 218 'operator' => 'IN' 219 ), 220 array( 221 'taxonomy' => 'category', 222 'field' => 'id', 223 'terms' => false, 224 'operator' => 'IN' 225 ) 226 ) 227 ) ); 233 function test_meta_between_not_between() { 234 $post_id = $this->factory->post->create(); 235 add_post_meta( $post_id, 'time', 500 ); 236 $post_id2 = $this->factory->post->create(); 237 add_post_meta( $post_id2, 'time', 1001 ); 238 $post_id3 = $this->factory->post->create(); 239 add_post_meta( $post_id3, 'time', 0 ); 240 $post_id4 = $this->factory->post->create(); 241 add_post_meta( $post_id4, 'time', 1 ); 242 $post_id5 = $this->factory->post->create(); 243 add_post_meta( $post_id5, 'time', 1000 ); 228 244 229 $posts = $query->get_posts(); 230 $this->assertEquals( 0 , count( $posts ) ); 231 } 245 $args = array( 246 'meta_key' => 'time', 247 'meta_value' => array( 1, 1000 ), 248 'meta_type' => 'numeric', 249 'meta_compare' => 'NOT BETWEEN' 250 ); 232 251 233 function test_meta_between_not_between() { 234 $post_id = $this->factory->post->create(); 235 add_post_meta( $post_id, 'time', 500 ); 236 $post_id2 = $this->factory->post->create(); 237 add_post_meta( $post_id2, 'time', 1001 ); 238 $post_id3 = $this->factory->post->create(); 239 add_post_meta( $post_id3, 'time', 0 ); 240 $post_id4 = $this->factory->post->create(); 241 add_post_meta( $post_id4, 'time', 1 ); 242 $post_id5 = $this->factory->post->create(); 243 add_post_meta( $post_id5, 'time', 1000 ); 252 $query = new WP_Query( $args ); 253 $this->assertEquals( 2, count ( $query->posts ) ); 254 foreach ( $query->posts as $post ) { 255 $this->assertInstanceOf( 'WP_Post', $post ); 256 $this->assertEquals( 'raw', $post->filter ); 257 } 258 $posts = wp_list_pluck( $query->posts, 'ID' ); 259 $this->assertEqualSets( array( $post_id2, $post_id3 ), $posts ); 244 260 245 $args = array(246 'meta_key' => 'time',247 'meta_value' => array( 1, 1000 ),248 'meta_type' => 'numeric',249 'meta_compare' => 'NOTBETWEEN'250 261 $args = array( 262 'meta_key' => 'time', 263 'meta_value' => array( 1, 1000 ), 264 'meta_type' => 'numeric', 265 'meta_compare' => 'BETWEEN' 266 ); 251 267 252 $query = new WP_Query( $args ); 253 $this->assertEquals( 2, count ( $query->posts ) ); 254 foreach ( $query->posts as $post ) { 255 $this->assertInstanceOf( 'WP_Post', $post ); 256 $this->assertEquals( 'raw', $post->filter ); 268 $query = new WP_Query( $args ); 269 $this->assertEquals( 3, count ( $query->posts ) ); 270 foreach ( $query->posts as $post ) { 271 $this->assertInstanceOf( 'WP_Post', $post ); 272 $this->assertEquals( 'raw', $post->filter ); 273 } 274 $posts = wp_list_pluck( $query->posts, 'ID' ); 275 $this->assertEqualSets( array( $post_id, $post_id4, $post_id5 ), $posts ); 257 276 } 258 $posts = wp_list_pluck( $query->posts, 'ID' );259 $this->assertEqualSets( array( $post_id2, $post_id3 ), $posts );260 277 261 $args = array( 262 'meta_key' => 'time', 263 'meta_value' => array( 1, 1000 ), 264 'meta_type' => 'numeric', 265 'meta_compare' => 'BETWEEN' 266 ); 278 /** 279 * @ticket 16829 280 */ 281 function test_meta_default_compare() { 282 // compare should default to IN when meta_value is an array 283 $post_id = $this->factory->post->create(); 284 add_post_meta( $post_id, 'foo', 'bar' ); 285 $post_id2 = $this->factory->post->create(); 286 add_post_meta( $post_id2, 'bar', 'baz' ); 287 $post_id3 = $this->factory->post->create(); 288 add_post_meta( $post_id3, 'foo', 'baz' ); 289 $post_id4 = $this->factory->post->create(); 290 add_post_meta( $post_id4, 'baz', 'bar' ); 291 $post_id5 = $this->factory->post->create(); 292 add_post_meta( $post_id5, 'foo', rand_str() ); 267 293 268 $query = new WP_Query( $args ); 269 $this->assertEquals( 3, count ( $query->posts ) ); 270 foreach ( $query->posts as $post ) { 271 $this->assertInstanceOf( 'WP_Post', $post ); 272 $this->assertEquals( 'raw', $post->filter ); 273 } 274 $posts = wp_list_pluck( $query->posts, 'ID' ); 275 $this->assertEqualSets( array( $post_id, $post_id4, $post_id5 ), $posts ); 276 } 294 $posts = get_posts( array( 295 'meta_key' => 'foo', 296 'meta_value' => array( 'bar', 'baz' ) 297 ) ); 277 298 278 /** 279 * @ticket 16829 280 */ 281 function test_meta_default_compare() { 282 // compare should default to IN when meta_value is an array 283 $post_id = $this->factory->post->create(); 284 add_post_meta( $post_id, 'foo', 'bar' ); 285 $post_id2 = $this->factory->post->create(); 286 add_post_meta( $post_id2, 'bar', 'baz' ); 287 $post_id3 = $this->factory->post->create(); 288 add_post_meta( $post_id3, 'foo', 'baz' ); 289 $post_id4 = $this->factory->post->create(); 290 add_post_meta( $post_id4, 'baz', 'bar' ); 291 $post_id5 = $this->factory->post->create(); 292 add_post_meta( $post_id5, 'foo', rand_str() ); 299 $this->assertEquals( 2, count( $posts ) ); 300 $posts = wp_list_pluck( $posts, 'ID' ); 301 $this->assertEqualSets( array( $post_id, $post_id3 ), $posts ); 293 302 294 $posts = get_posts( array( 295 'meta_key' => 'foo', 296 'meta_value' => array( 'bar', 'baz' ) 297 ) ); 303 $posts = get_posts( array( 304 'meta_key' => 'foo', 305 'meta_value' => array( 'bar', 'baz' ), 306 'meta_compare' => 'IN' 307 ) ); 298 308 299 $this->assertEquals( 2, count( $posts ) ); 300 $posts = wp_list_pluck( $posts, 'ID' ); 301 $this->assertEqualSets( array( $post_id, $post_id3 ), $posts ); 302 303 $posts = get_posts( array( 304 'meta_key' => 'foo', 305 'meta_value' => array( 'bar', 'baz' ), 306 'meta_compare' => 'IN' 307 ) ); 308 309 $this->assertEquals( 2, count( $posts ) ); 310 foreach ( $posts as $post ) { 311 $this->assertInstanceOf( 'WP_Post', $post ); 312 $this->assertEquals( 'raw', $post->filter ); 309 $this->assertEquals( 2, count( $posts ) ); 310 foreach ( $posts as $post ) { 311 $this->assertInstanceOf( 'WP_Post', $post ); 312 $this->assertEquals( 'raw', $post->filter ); 313 } 314 $posts = wp_list_pluck( $posts, 'ID' ); 315 $this->assertEqualSets( array( $post_id, $post_id3 ), $posts ); 313 316 } 314 $posts = wp_list_pluck( $posts, 'ID' );315 $this->assertEqualSets( array( $post_id, $post_id3 ), $posts );316 }317 317 318 319 320 321 322 $post_id = $this->factory->post->create();323 add_post_meta( $post_id, 'city', 'Lorem' );324 add_post_meta( $post_id, 'address', '123 Lorem St.' );325 $post_id2 = $this->factory->post->create();326 add_post_meta( $post_id2, 'city', 'Lorem' );327 $post_id3 = $this->factory->post->create();328 add_post_meta( $post_id3, 'city', 'Loren' );318 /** 319 * @ticket 17264 320 */ 321 function test_duplicate_posts_when_no_key() { 322 $post_id = $this->factory->post->create(); 323 add_post_meta( $post_id, 'city', 'Lorem' ); 324 add_post_meta( $post_id, 'address', '123 Lorem St.' ); 325 $post_id2 = $this->factory->post->create(); 326 add_post_meta( $post_id2, 'city', 'Lorem' ); 327 $post_id3 = $this->factory->post->create(); 328 add_post_meta( $post_id3, 'city', 'Loren' ); 329 329 330 $args = array(331 332 array(333 334 335 )336 337 );330 $args = array( 331 'meta_query' => array( 332 array( 333 'value' => 'lorem', 334 'compare' => 'LIKE' 335 ) 336 ) 337 ); 338 338 339 $posts = get_posts( $args ); 340 $this->assertEquals( 2, count( $posts ) ); 341 foreach ( $posts as $post ) { 342 $this->assertInstanceOf( 'WP_Post', $post ); 343 $this->assertEquals( 'raw', $post->filter ); 339 $posts = get_posts( $args ); 340 $this->assertEquals( 2, count( $posts ) ); 341 foreach ( $posts as $post ) { 342 $this->assertInstanceOf( 'WP_Post', $post ); 343 $this->assertEquals( 'raw', $post->filter ); 344 } 345 $posts = wp_list_pluck( $posts, 'ID' ); 346 $this->assertEqualSets( array( $post_id, $post_id2 ), $posts ); 344 347 } 345 $posts = wp_list_pluck( $posts, 'ID' );346 $this->assertEqualSets( array( $post_id, $post_id2 ), $posts );347 }348 348 349 350 351 352 353 $post_id = $this->factory->post->create();354 add_post_meta( $post_id, 'foo', '0' );355 add_post_meta( $post_id, 'bar', 0 );356 $post_id2 = $this->factory->post->create();357 add_post_meta( $post_id2, 'foo', 1 );358 $post_id3 = $this->factory->post->create();359 add_post_meta( $post_id3, 'baz', 0 );360 $post_id4 = $this->factory->post->create();361 add_post_meta( $post_id4, 'baz', 0 );362 $post_id5 = $this->factory->post->create();363 add_post_meta( $post_id5, 'baz', 0 );364 add_post_meta( $post_id5, 'bar', '0' );365 $post_id6 = $this->factory->post->create();366 add_post_meta( $post_id6, 'baz', 0 );349 /** 350 * @ticket 15292 351 */ 352 function test_empty_meta_value() { 353 $post_id = $this->factory->post->create(); 354 add_post_meta( $post_id, 'foo', '0' ); 355 add_post_meta( $post_id, 'bar', 0 ); 356 $post_id2 = $this->factory->post->create(); 357 add_post_meta( $post_id2, 'foo', 1 ); 358 $post_id3 = $this->factory->post->create(); 359 add_post_meta( $post_id3, 'baz', 0 ); 360 $post_id4 = $this->factory->post->create(); 361 add_post_meta( $post_id4, 'baz', 0 ); 362 $post_id5 = $this->factory->post->create(); 363 add_post_meta( $post_id5, 'baz', 0 ); 364 add_post_meta( $post_id5, 'bar', '0' ); 365 $post_id6 = $this->factory->post->create(); 366 add_post_meta( $post_id6, 'baz', 0 ); 367 367 368 $posts = get_posts( array( 'meta_key' => 'foo', 'meta_value' => '0' ) );369 $this->assertEquals( 1, count ( $posts ) );370 foreach ( $posts as $post ) {371 $this->assertInstanceOf( 'WP_Post', $post );372 $this->assertEquals( 'raw', $post->filter );373 }374 $this->assertEquals( $post_id, $posts[0]->ID );368 $posts = get_posts( array( 'meta_key' => 'foo', 'meta_value' => '0' ) ); 369 $this->assertEquals( 1, count ( $posts ) ); 370 foreach ( $posts as $post ) { 371 $this->assertInstanceOf( 'WP_Post', $post ); 372 $this->assertEquals( 'raw', $post->filter ); 373 } 374 $this->assertEquals( $post_id, $posts[0]->ID ); 375 375 376 $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => '0' ) );377 $this->assertEquals( 2, count ( $posts ) );378 foreach ( $posts as $post ) {379 $this->assertInstanceOf( 'WP_Post', $post );380 $this->assertEquals( 'raw', $post->filter );381 }382 $posts = wp_list_pluck( $posts, 'ID' );383 $this->assertEqualSets( array( $post_id, $post_id5 ), $posts );376 $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => '0' ) ); 377 $this->assertEquals( 2, count ( $posts ) ); 378 foreach ( $posts as $post ) { 379 $this->assertInstanceOf( 'WP_Post', $post ); 380 $this->assertEquals( 'raw', $post->filter ); 381 } 382 $posts = wp_list_pluck( $posts, 'ID' ); 383 $this->assertEqualSets( array( $post_id, $post_id5 ), $posts ); 384 384 385 386 $this->assertEquals( 2, count ( $posts ) );387 foreach ( $posts as $post ) {388 $this->assertInstanceOf( 'WP_Post', $post );389 $this->assertEquals( 'raw', $post->filter );390 }391 $posts = wp_list_pluck( $posts, 'ID' );392 $this->assertEqualSets( array( $post_id, $post_id5 ), $posts );385 $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => 0 ) ); 386 $this->assertEquals( 2, count ( $posts ) ); 387 foreach ( $posts as $post ) { 388 $this->assertInstanceOf( 'WP_Post', $post ); 389 $this->assertEquals( 'raw', $post->filter ); 390 } 391 $posts = wp_list_pluck( $posts, 'ID' ); 392 $this->assertEqualSets( array( $post_id, $post_id5 ), $posts ); 393 393 394 395 $this->assertEquals( 5, count ( $posts ) );396 foreach ( $posts as $post ) {397 $this->assertInstanceOf( 'WP_Post', $post );398 $this->assertEquals( 'raw', $post->filter );399 }400 $posts = wp_list_pluck( $posts, 'ID' );401 $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );394 $posts = get_posts( array( 'meta_value' => 0 ) ); 395 $this->assertEquals( 5, count ( $posts ) ); 396 foreach ( $posts as $post ) { 397 $this->assertInstanceOf( 'WP_Post', $post ); 398 $this->assertEquals( 'raw', $post->filter ); 399 } 400 $posts = wp_list_pluck( $posts, 'ID' ); 401 $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts ); 402 402 403 $posts = get_posts( array( 'meta_value' => '0' ) ); 404 $this->assertEquals( 5, count ( $posts ) ); 405 foreach ( $posts as $post ) { 406 $this->assertInstanceOf( 'WP_Post', $post ); 407 $this->assertEquals( 'raw', $post->filter ); 403 $posts = get_posts( array( 'meta_value' => '0' ) ); 404 $this->assertEquals( 5, count ( $posts ) ); 405 foreach ( $posts as $post ) { 406 $this->assertInstanceOf( 'WP_Post', $post ); 407 $this->assertEquals( 'raw', $post->filter ); 408 } 409 $posts = wp_list_pluck( $posts, 'ID' ); 410 $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts ); 408 411 } 409 $posts = wp_list_pluck( $posts, 'ID' );410 $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );411 }412 412 413 413 function test_taxonomy_include_children() { 414 414 $cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Australia' ) ); … … 422 422 $post_d = $this->factory->post->create( array( 'post_category' => array( $cat_d ) ) ); 423 423 424 424 $posts = get_posts( array( 425 425 'tax_query' => array( 426 426 array( 427 427 'taxonomy' => 'category', 428 428 'field' => 'id', 429 429 'terms' => array( $cat_a ), 430 430 ) 431 431 ) 432 432 ) ); 433 433 434 434 $this->assertEquals( 4 , count( $posts ) ); 435 435 436 436 $posts = get_posts( array( 437 437 'tax_query' => array( 438 438 array( 439 439 'taxonomy' => 'category', 440 440 'field' => 'id', 441 441 'terms' => array( $cat_a ), 442 442 'include_children' => false 443 443 ) 444 444 ) 445 445 ) ); 446 446 447 447 $this->assertEquals( 1 , count( $posts ) ); 448 448 449 449 $posts = get_posts( array( 450 450 'tax_query' => array( 451 451 array( 452 452 'taxonomy' => 'category', 453 453 'field' => 'id', 454 454 'terms' => array( $cat_b ), 455 455 ) 456 456 ) 457 457 ) ); 458 458 459 459 $this->assertEquals( 3 , count( $posts ) ); 460 460 461 461 $posts = get_posts( array( 462 462 'tax_query' => array( 463 463 array( 464 464 'taxonomy' => 'category', 465 465 'field' => 'id', 466 466 'terms' => array( $cat_b ), 467 467 'include_children' => false 468 468 ) 469 469 ) 470 470 ) ); 471 471 472 472 $this->assertEquals( 1 , count( $posts ) ); 473 473 474 474 $posts = get_posts( array( 475 475 'tax_query' => array( 476 476 array( 477 477 'taxonomy' => 'category', 478 478 'field' => 'id', 479 479 'terms' => array( $cat_c ), 480 480 ) 481 481 ) 482 482 ) ); 483 483 484 484 $this->assertEquals( 1 , count( $posts ) ); 485 485 486 486 $posts = get_posts( array( 487 487 'tax_query' => array( 488 488 array( 489 489 'taxonomy' => 'category', 490 490 'field' => 'id', 491 491 'terms' => array( $cat_c ), 492 492 'include_children' => false 493 493 ) 494 494 ) 495 495 ) ); 496 496 497 497 $this->assertEquals( 1 , count( $posts ) ); -
tests/phpunit/tests/query/scope.php
1 <?php 2 /** 3 * Runs query scope tests without pre-loading posts 4 * 5 * @group query 6 * @group scope 7 */ 8 class Test_Query_Scope extends WP_UnitTestCase { 9 protected $q; 10 11 function setUp() { 12 parent::setUp(); 13 14 unset( $this->q ); 15 $this->q = new WP_Query(); 16 } 17 18 function test_query_scope() { 19 $this->factory->post->create( array( 'post_title' => 'password-protected', 'post_password' => 'burrito' ) ); 20 $this->factory->post->create( array( 'post_title' => 'not-password-protected', 'post_password' => '' ) ); 21 $p3 = $this->factory->post->create( array( 'post_title' => 'comments', 'post_password' => 'taco' ) ); 22 $this->factory->comment->create_post_comments( $p3, 3 ); 23 $this->factory->post->create( array( 'post_title' => 'no-comments' ) ); 24 25 $password = $this->q->query( array( 'scope' => 'has_password' ) ); 26 $this->assertCount( 2, $password ); 27 $this->assertEqualSets( array( 28 'password-protected', 29 'comments' 30 ), wp_list_pluck( $password, 'post_title' ) ); 31 32 $no_password = $this->q->query( array( 'scope' => 'no_password' ) ); 33 $this->assertCount( 2, $no_password ); 34 $this->assertEqualSets( array( 35 'not-password-protected', 36 'no-comments' 37 ), wp_list_pluck( $no_password, 'post_title' ) ); 38 39 $comments = $this->q->query( array( 'scope' => 'has_comments' ) ); 40 $this->assertCount( 1, $comments ); 41 $this->assertEqualSets( array( 'comments' ), wp_list_pluck( $comments, 'post_title' ) ); 42 43 $no_comments = $this->q->query( array( 'scope' => 'no_comments' ) ); 44 $this->assertCount( 3, $no_comments ); 45 $this->assertEqualSets( array( 46 'password-protected', 47 'not-password-protected', 48 'no-comments' 49 ), wp_list_pluck( $no_comments, 'post_title' ) ); 50 51 $multi_comma = $this->q->query( array( 'scope' => 'has_comments,has_password' ) ); 52 $this->assertCount( 1, $multi_comma ); 53 $this->assertEqualSets( array( 'comments' ), wp_list_pluck( $multi_comma, 'post_title' ) ); 54 55 $multi_array = $this->q->query( array( 'scope' => array( 'has_comments', 'has_password' ) ) ); 56 $this->assertCount( 1, $multi_array ); 57 $this->assertEqualSets( array( 'comments' ), wp_list_pluck( $multi_array, 'post_title' ) ); 58 59 $multi_comma_none = $this->q->query( array( 'scope' => 'has_comments,no_password' ) ); 60 $this->assertCount( 0, $multi_comma_none ); 61 62 $multi_array_none = $this->q->query( array( 'scope' => array( 'has_comments', 'no_password' ) ) ); 63 $this->assertCount( 0, $multi_array_none ); 64 65 add_filter( 'query_scopes', array( $this, 'scope_filter' ) ); 66 $filtered = $this->q->query( array( 'scope' => 'password_in_title' ) ); 67 $this->assertCount( 2, $filtered ); 68 $this->assertEqualSets( array( 69 'password-protected', 70 'not-password-protected', 71 ), wp_list_pluck( $filtered, 'post_title' ) ); 72 } 73 74 function scope_filter( $scopes ) { 75 global $wpdb; 76 $scopes['password_in_title'] = "$wpdb->posts.post_title LIKE '%password%'"; 77 return $scopes; 78 } 79 } 80 No newline at end of file