|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @group query |
| 4 | + * |
| 5 | + * @covers WP_Query::get_posts |
| 6 | + */ |
| 7 | +class Tests_Query_FieldsClause extends WP_UnitTestCase { |
| 8 | + |
| 9 | + /** |
| 10 | + * Post IDs. |
| 11 | + * |
| 12 | + * @var int[] |
| 13 | + */ |
| 14 | + static $post_ids = array(); |
| 15 | + |
| 16 | + /** |
| 17 | + * Page IDs. |
| 18 | + * |
| 19 | + * @var int[] |
| 20 | + */ |
| 21 | + static $page_ids = array(); |
| 22 | + |
| 23 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 24 | + // Register CPT for use with shared fixtures. |
| 25 | + register_post_type( 'wptests_pt' ); |
| 26 | + |
| 27 | + self::$post_ids = $factory->post->create_many( 5, array( 'post_type' => 'wptests_pt' ) ); |
| 28 | + } |
| 29 | + |
| 30 | + public function set_up() { |
| 31 | + parent::set_up(); |
| 32 | + /* |
| 33 | + * Re-register the CPT for use within each test. |
| 34 | + * |
| 35 | + * Custom post types are deregistered by the default tear_down method |
| 36 | + * so need to be re-registered for each test as WP_Query calls |
| 37 | + * get_post_types(). |
| 38 | + */ |
| 39 | + register_post_type( 'wptests_pt' ); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Tests limiting the WP_Query fields to the ID and parent sub-set. |
| 44 | + * |
| 45 | + * @ticket 57012 |
| 46 | + */ |
| 47 | + public function test_should_limit_fields_to_id_and_parent_subset() { |
| 48 | + $query_args = array( |
| 49 | + 'post_type' => 'wptests_pt', |
| 50 | + 'fields' => 'id=>parent', |
| 51 | + ); |
| 52 | + |
| 53 | + $q = new WP_Query( $query_args ); |
| 54 | + |
| 55 | + $expected = array(); |
| 56 | + foreach ( self::$post_ids as $post_id ) { |
| 57 | + // Use array_shift to populate in the reverse order. |
| 58 | + array_unshift( |
| 59 | + $expected, |
| 60 | + (object) array( |
| 61 | + 'ID' => $post_id, |
| 62 | + 'post_parent' => 0, |
| 63 | + ) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' ); |
| 68 | + $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' ); |
| 69 | + $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' ); |
| 70 | + |
| 71 | + // Test the second query's results match. |
| 72 | + $q2 = new WP_Query( $query_args ); |
| 73 | + $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Tests limiting the WP_Query fields to the IDs only. |
| 78 | + * |
| 79 | + * @ticket 57012 |
| 80 | + */ |
| 81 | + public function test_should_limit_fields_to_ids() { |
| 82 | + $query_args = array( |
| 83 | + 'post_type' => 'wptests_pt', |
| 84 | + 'fields' => 'ids', |
| 85 | + ); |
| 86 | + |
| 87 | + $q = new WP_Query( $query_args ); |
| 88 | + |
| 89 | + $expected = array_reverse( self::$post_ids ); |
| 90 | + |
| 91 | + $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' ); |
| 92 | + $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' ); |
| 93 | + $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' ); |
| 94 | + |
| 95 | + // Test the second query's results match. |
| 96 | + $q2 = new WP_Query( $query_args ); |
| 97 | + $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Tests querying all fields via WP_Query. |
| 102 | + * |
| 103 | + * @ticket 57012 |
| 104 | + */ |
| 105 | + public function test_should_query_all_fields() { |
| 106 | + $query_args = array( |
| 107 | + 'post_type' => 'wptests_pt', |
| 108 | + 'fields' => 'all', |
| 109 | + ); |
| 110 | + |
| 111 | + $q = new WP_Query( $query_args ); |
| 112 | + |
| 113 | + $expected = array_map( 'get_post', array_reverse( self::$post_ids ) ); |
| 114 | + |
| 115 | + $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' ); |
| 116 | + $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' ); |
| 117 | + $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' ); |
| 118 | + |
| 119 | + // Test the second query's results match. |
| 120 | + $q2 = new WP_Query( $query_args ); |
| 121 | + $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' ); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Tests adding fields to WP_Query via filters when requesting the ID and parent sub-set. |
| 126 | + * |
| 127 | + * @ticket 57012 |
| 128 | + */ |
| 129 | + public function test_should_include_filtered_values_in_addition_to_id_and_parent_subset() { |
| 130 | + add_filter( 'posts_fields', array( $this, 'filter_posts_fields' ) ); |
| 131 | + add_filter( 'posts_clauses', array( $this, 'filter_posts_clauses' ) ); |
| 132 | + |
| 133 | + $query_args = array( |
| 134 | + 'post_type' => 'wptests_pt', |
| 135 | + 'fields' => 'id=>parent', |
| 136 | + ); |
| 137 | + |
| 138 | + $q = new WP_Query( $query_args ); |
| 139 | + |
| 140 | + $expected = array(); |
| 141 | + foreach ( self::$post_ids as $post_id ) { |
| 142 | + // Use array_shift to populate in the reverse order. |
| 143 | + array_unshift( |
| 144 | + $expected, |
| 145 | + (object) array( |
| 146 | + 'ID' => $post_id, |
| 147 | + 'post_parent' => 0, |
| 148 | + 'test_post_fields' => 1, |
| 149 | + 'test_post_clauses' => 2, |
| 150 | + ) |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' ); |
| 155 | + $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' ); |
| 156 | + $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' ); |
| 157 | + |
| 158 | + // Test the second query's results match. |
| 159 | + $q2 = new WP_Query( $query_args ); |
| 160 | + $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' ); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Tests adding fields to WP_Query via filters when requesting the ID field. |
| 165 | + * |
| 166 | + * @ticket 57012 |
| 167 | + */ |
| 168 | + public function test_should_include_filtered_values_in_addition_to_id() { |
| 169 | + add_filter( 'posts_fields', array( $this, 'filter_posts_fields' ) ); |
| 170 | + add_filter( 'posts_clauses', array( $this, 'filter_posts_clauses' ) ); |
| 171 | + |
| 172 | + $query_args = array( |
| 173 | + 'post_type' => 'wptests_pt', |
| 174 | + 'fields' => 'ids', |
| 175 | + ); |
| 176 | + |
| 177 | + $q = new WP_Query( $query_args ); |
| 178 | + |
| 179 | + // Fields => ID does not include the additional fields. |
| 180 | + $expected = array_reverse( self::$post_ids ); |
| 181 | + |
| 182 | + $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' ); |
| 183 | + $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' ); |
| 184 | + $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' ); |
| 185 | + |
| 186 | + // Test the second query's results match. |
| 187 | + $q2 = new WP_Query( $query_args ); |
| 188 | + $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' ); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Tests adding fields to WP_Query via filters when requesting all fields. |
| 193 | + * |
| 194 | + * @ticket 57012 |
| 195 | + */ |
| 196 | + public function test_should_include_filtered_values() { |
| 197 | + add_filter( 'posts_fields', array( $this, 'filter_posts_fields' ) ); |
| 198 | + add_filter( 'posts_clauses', array( $this, 'filter_posts_clauses' ) ); |
| 199 | + |
| 200 | + $query_args = array( |
| 201 | + 'post_type' => 'wptests_pt', |
| 202 | + 'fields' => 'all', |
| 203 | + ); |
| 204 | + |
| 205 | + $q = new WP_Query( $query_args ); |
| 206 | + |
| 207 | + $expected = array_map( 'get_post', array_reverse( self::$post_ids ) ); |
| 208 | + foreach ( $expected as $post ) { |
| 209 | + $post->test_post_fields = 1; |
| 210 | + $post->test_post_clauses = 2; |
| 211 | + } |
| 212 | + |
| 213 | + $this->assertEquals( $expected, $q->posts, 'Posts property for first query is not of expected form.' ); |
| 214 | + $this->assertSame( 5, $q->found_posts, 'Number of found posts is not five.' ); |
| 215 | + $this->assertEquals( 1, $q->max_num_pages, 'Number of found pages is not one.' ); |
| 216 | + |
| 217 | + // Test the second query's results match. |
| 218 | + $q2 = new WP_Query( $query_args ); |
| 219 | + $this->assertEquals( $expected, $q2->posts, 'Posts property for second query is not in the expected form.' ); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Filters the posts fields. |
| 224 | + * |
| 225 | + * @param string $fields The fields to SELECT. |
| 226 | + * @return string The filtered fields. |
| 227 | + */ |
| 228 | + function filter_posts_fields( $fields ) { |
| 229 | + return "$fields, 1 as test_post_fields"; |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Filters the posts clauses. |
| 234 | + * |
| 235 | + * @param array $clauses The WP_Query database clauses. |
| 236 | + * @return array The filtered database clauses. |
| 237 | + */ |
| 238 | + function filter_posts_clauses( $clauses ) { |
| 239 | + $clauses['fields'] .= ', 2 as test_post_clauses'; |
| 240 | + return $clauses; |
| 241 | + } |
| 242 | +} |
0 commit comments