| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | class test_meta_key_not_exists_query extends WP_UnitTestCase { |
|---|
| 4 | |
|---|
| 5 | function test_post() { |
|---|
| 6 | $post_ids = $this->factory()->post->create_many( 7 ); |
|---|
| 7 | |
|---|
| 8 | // Give first post nothing. |
|---|
| 9 | |
|---|
| 10 | // Give second post 1. |
|---|
| 11 | add_post_meta( $post_ids[1], 'test', 1 ); |
|---|
| 12 | |
|---|
| 13 | // Give third post 0. |
|---|
| 14 | add_post_meta( $post_ids[2], 'test', 0 ); |
|---|
| 15 | |
|---|
| 16 | // Give fourth post 200. |
|---|
| 17 | add_post_meta( $post_ids[3], 'test', 200 ); |
|---|
| 18 | |
|---|
| 19 | // Give fifth post 30. |
|---|
| 20 | add_post_meta( $post_ids[4], 'test', 30 ); |
|---|
| 21 | |
|---|
| 22 | // Give sixth post -5. |
|---|
| 23 | add_post_meta( $post_ids[5], 'test', -5 ); |
|---|
| 24 | |
|---|
| 25 | // Give the seventh post nothing. |
|---|
| 26 | |
|---|
| 27 | $query = new WP_Query( |
|---|
| 28 | array( |
|---|
| 29 | 'fields' => 'ids', |
|---|
| 30 | 'orderby' => array( 'meta_value_num' => 'DESC', 'date' => 'DESC', 'ID' => 'ASC' ), |
|---|
| 31 | 'meta_query' => array( |
|---|
| 32 | array( |
|---|
| 33 | 'key' => 'test', |
|---|
| 34 | 'default' => 0, |
|---|
| 35 | 'compare' => 'NONE', |
|---|
| 36 | ), |
|---|
| 37 | ), |
|---|
| 38 | ) |
|---|
| 39 | ); |
|---|
| 40 | |
|---|
| 41 | $this->assertSame( |
|---|
| 42 | array( |
|---|
| 43 | $post_ids[3], // 200 |
|---|
| 44 | $post_ids[4], // 30 |
|---|
| 45 | $post_ids[1], // 1 |
|---|
| 46 | $post_ids[0], // empty |
|---|
| 47 | $post_ids[2], // 0 |
|---|
| 48 | $post_ids[6], // empty |
|---|
| 49 | $post_ids[5], // -5 |
|---|
| 50 | ) |
|---|
| 51 | , $query->get_posts() |
|---|
| 52 | ); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | function test_post_2() { |
|---|
| 56 | $post_ids = $this->factory()->post->create_many( 7 ); |
|---|
| 57 | |
|---|
| 58 | // Give first post nothing. |
|---|
| 59 | add_post_meta( $post_ids[0], 'other', 'a' ); |
|---|
| 60 | |
|---|
| 61 | // Give second post 1. |
|---|
| 62 | add_post_meta( $post_ids[1], 'test', 1 ); |
|---|
| 63 | add_post_meta( $post_ids[1], 'other', 'b' ); |
|---|
| 64 | |
|---|
| 65 | // Give third post 0. |
|---|
| 66 | add_post_meta( $post_ids[2], 'test', 0 ); |
|---|
| 67 | add_post_meta( $post_ids[2], 'other', 'a' ); |
|---|
| 68 | |
|---|
| 69 | // Give fourth post 200. |
|---|
| 70 | add_post_meta( $post_ids[3], 'test', 200 ); |
|---|
| 71 | add_post_meta( $post_ids[3], 'other', 'a' ); |
|---|
| 72 | |
|---|
| 73 | // Give fifth post 30. |
|---|
| 74 | add_post_meta( $post_ids[4], 'test', 30 ); |
|---|
| 75 | |
|---|
| 76 | // Give sixth post -5. |
|---|
| 77 | add_post_meta( $post_ids[5], 'test', -5 ); |
|---|
| 78 | add_post_meta( $post_ids[5], 'other', 'a' ); |
|---|
| 79 | |
|---|
| 80 | // Give the seventh post nothing. |
|---|
| 81 | |
|---|
| 82 | $query = new WP_Query( |
|---|
| 83 | array( |
|---|
| 84 | 'fields' => 'ids', |
|---|
| 85 | 'orderby' => array( 'meta_value_num' => 'DESC', 'date' => 'DESC' ), |
|---|
| 86 | 'meta_query' => array( |
|---|
| 87 | array( |
|---|
| 88 | 'key' => 'test', |
|---|
| 89 | 'default' => 0, |
|---|
| 90 | 'compare' => 'NONE', |
|---|
| 91 | ), |
|---|
| 92 | array( |
|---|
| 93 | 'key' => 'other', |
|---|
| 94 | 'value' => 'a', |
|---|
| 95 | ), |
|---|
| 96 | ), |
|---|
| 97 | ) |
|---|
| 98 | ); |
|---|
| 99 | |
|---|
| 100 | $this->assertSame( |
|---|
| 101 | array( |
|---|
| 102 | $post_ids[3], // 200 |
|---|
| 103 | // $post_ids[4], // 30 (empty) |
|---|
| 104 | // $post_ids[1], // 1 (b) |
|---|
| 105 | $post_ids[0], // empty |
|---|
| 106 | $post_ids[2], // 0 |
|---|
| 107 | // $post_ids[6], // empty (empty) |
|---|
| 108 | $post_ids[5], // -5 |
|---|
| 109 | ) |
|---|
| 110 | , $query->get_posts() |
|---|
| 111 | ); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|