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