-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathsearchColumns.php
More file actions
413 lines (372 loc) · 11.6 KB
/
searchColumns.php
File metadata and controls
413 lines (372 loc) · 11.6 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
/**
* Testing the search columns support in `WP_Query`.
*
* @package WordPress\UnitTests
* @since 6.2.0
*/
/**
* Test cases for the search columns feature.
*
* @group query
* @group search
*
* @covers WP_Query::parse_search
*
* @since 6.2.0
*/
class Tests_Query_SearchColumns extends WP_UnitTestCase {
/**
* The post ID of the first fixture post.
*
* @since 6.2.0
* @var int $pid1
*/
protected static $pid1;
/**
* The post ID of the second fixture post.
*
* @since 6.2.0
* @var int $pid2
*/
protected static $pid2;
/**
* The post ID of the third fixture post.
*
* @since 6.2.0
* @var int $pid3
*/
protected static $pid3;
/**
* Create posts fixtures.
*
* @param WP_UnitTest_Factory $factory The factory instance.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$pid1 = $factory->post->create(
array(
'post_status' => 'publish',
'post_title' => 'foo title',
'post_excerpt' => 'foo excerpt',
'post_content' => 'foo content',
)
);
self::$pid2 = $factory->post->create(
array(
'post_status' => 'publish',
'post_title' => 'bar title',
'post_excerpt' => 'foo bar excerpt',
'post_content' => 'foo bar content',
)
);
self::$pid3 = $factory->post->create(
array(
'post_status' => 'publish',
'post_title' => 'baz title',
'post_excerpt' => 'baz bar excerpt',
'post_content' => 'baz bar foo content',
)
);
}
/**
* Tests that search uses default search columns when search columns are empty.
*
* @ticket 43867
*/
public function test_s_should_use_default_search_columns_when_empty_search_columns() {
$q = new WP_Query(
array(
's' => 'foo',
'search_columns' => array(),
'fields' => 'ids',
)
);
$this->assertStringContainsString( 'post_title', $q->request, 'SQL request should contain post_title string.' );
$this->assertStringContainsString( 'post_excerpt', $q->request, 'SQL request should contain post_excerpt string.' );
$this->assertStringContainsString( 'post_content', $q->request, 'SQL request should contain post_content string.' );
$this->assertSameSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts, 'Query results should be equal to the set.' );
}
/**
* Tests that search supports the `post_title` search column.
*
* @ticket 43867
*/
public function test_s_should_support_post_title_search_column() {
$q = new WP_Query(
array(
's' => 'foo',
'search_columns' => array( 'post_title' ),
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid1 ), $q->posts );
}
/**
* Tests that search supports the `post_excerpt` search column.
*
* @ticket 43867
*/
public function test_s_should_support_post_excerpt_search_column() {
$q = new WP_Query(
array(
's' => 'foo',
'search_columns' => array( 'post_excerpt' ),
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid1, self::$pid2 ), $q->posts );
}
/**
* Tests that search supports the `post_content` search column.
*
* @ticket 43867
*/
public function test_s_should_support_post_content_search_column() {
$q = new WP_Query(
array(
's' => 'foo',
'search_columns' => array( 'post_content' ),
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts );
}
/**
* Tests that search supports the `post_title` and `post_excerpt` search columns together.
*
* @ticket 43867
*/
public function test_s_should_support_post_title_and_post_excerpt_search_columns() {
$q = new WP_Query(
array(
's' => 'foo',
'search_columns' => array( 'post_title', 'post_excerpt' ),
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid1, self::$pid2 ), $q->posts );
}
/**
* Tests that search supports the `post_title` and `post_content` search columns together.
*
* @ticket 43867
*/
public function test_s_should_support_post_title_and_post_content_search_columns() {
$q = new WP_Query(
array(
's' => 'foo',
'search_columns' => array( 'post_title', 'post_content' ),
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts );
}
/**
* Tests that search supports the `post_excerpt` and `post_content` search columns together.
*
* @ticket 43867
*/
public function test_s_should_support_post_excerpt_and_post_content_search_columns() {
$q = new WP_Query(
array(
's' => 'foo',
'search_columns' => array( 'post_excerpt', 'post_content' ),
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts );
}
/**
* Tests that search supports the `post_title`, `post_excerpt` and `post_content` search columns together.
*
* @ticket 43867
*/
public function test_s_should_support_post_title_and_post_excerpt_and_post_content_search_columns() {
$q = new WP_Query(
array(
's' => 'foo',
'search_columns' => array( 'post_title', 'post_excerpt', 'post_content' ),
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts );
}
/**
* Tests that search uses default search columns when using a non-existing search column.
*
* @ticket 43867
*/
public function test_s_should_use_default_search_columns_when_using_non_existing_search_column() {
$q = new WP_Query(
array(
's' => 'foo',
'search_columns' => array( 'post_non_existing_column' ),
'fields' => 'ids',
)
);
$this->assertStringContainsString( 'post_title', $q->request, 'SQL request should contain post_title string.' );
$this->assertStringContainsString( 'post_excerpt', $q->request, 'SQL request should contain post_excerpt string.' );
$this->assertStringContainsString( 'post_content', $q->request, 'SQL request should contain post_content string.' );
$this->assertSameSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts, 'Query results should be equal to the set.' );
}
/**
* Tests that search ignores a non-existing search column when used together with a supported one.
*
* @ticket 43867
*/
public function test_s_should_ignore_non_existing_search_column_when_used_with_supported_one() {
$q = new WP_Query(
array(
's' => 'foo',
'search_columns' => array( 'post_title', 'post_non_existing_column' ),
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid1 ), $q->posts );
}
/**
* Tests that search supports search columns when searching multiple terms.
*
* @ticket 43867
*/
public function test_s_should_support_search_columns_when_searching_multiple_terms() {
$q = new WP_Query(
array(
's' => 'foo bar',
'search_columns' => array( 'post_content' ),
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid2, self::$pid3 ), $q->posts );
}
/**
* Tests that search supports search columns when searching for a sentence.
*
* @ticket 43867
*/
public function test_s_should_support_search_columns_when_sentence_true() {
$q = new WP_Query(
array(
's' => 'bar foo',
'search_columns' => array( 'post_content' ),
'sentence' => true,
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid3 ), $q->posts );
}
/**
* Tests that search supports search columns when searching for a sentence.
*
* @ticket 43867
*/
public function test_s_should_support_search_columns_when_sentence_false() {
$q = new WP_Query(
array(
's' => 'bar foo',
'search_columns' => array( 'post_content' ),
'sentence' => false,
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid2, self::$pid3 ), $q->posts );
}
/**
* Tests that search supports search columns when using term exclusion.
*
* @ticket 43867
*/
public function test_s_should_support_search_columns_when_searching_with_term_exclusion() {
$q = new WP_Query(
array(
's' => 'bar -baz',
'search_columns' => array( 'post_excerpt', 'post_content' ),
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid2 ), $q->posts );
}
/**
* Tests that search columns is filterable with the `post_search_columns` filter.
*
* @ticket 43867
*/
public function test_search_columns_should_be_filterable() {
add_filter( 'post_search_columns', array( $this, 'post_supported_search_column' ), 10, 3 );
$q = new WP_Query(
array(
's' => 'foo',
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$pid1 ), $q->posts );
}
/**
* Filter callback that sets a supported search column.
*
* @param string[] $search_columns Array of column names to be searched.
* @param string $search Text being searched.
* @param WP_Query $wp_query The current WP_Query instance.
* @return string[] $search_columns Array of column names to be searched.
*/
public function post_supported_search_column( $search_columns, $search, $wp_query ) {
$search_columns = array( 'post_title' );
return $search_columns;
}
/**
* Tests that search columns ignores non-supported search columns from the `post_search_columns` filter.
*
* @ticket 43867
*/
public function test_search_columns_should_not_be_filterable_with_non_supported_search_columns() {
add_filter( 'post_search_columns', array( $this, 'post_non_supported_search_column' ), 10, 3 );
$q = new WP_Query(
array(
's' => 'foo',
'fields' => 'ids',
)
);
$this->assertStringNotContainsString( 'post_author', $q->request, "SQL request shouldn't contain post_author string." );
$this->assertSameSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts, 'Query results should be equal to the set.' );
}
/**
* Filter callback that sets an existing but non-supported search column.
*
* @param string[] $search_columns Array of column names to be searched.
* @param string $search Text being searched.
* @param WP_Query $wp_query The current WP_Query instance.
* @return string[] $search_columns Array of column names to be searched.
*/
public function post_non_supported_search_column( $search_columns, $search, $wp_query ) {
$search_columns = array( 'post_author' );
return $search_columns;
}
/**
* Tests that search columns ignores non-existing search columns from the `post_search_columns` filter.
*
* @ticket 43867
*/
public function test_search_columns_should_not_be_filterable_with_non_existing_search_column() {
add_filter( 'post_search_columns', array( $this, 'post_non_existing_search_column' ), 10, 3 );
$q = new WP_Query(
array(
's' => 'foo',
'fields' => 'ids',
)
);
$this->assertStringNotContainsString( 'post_non_existing_column', $q->request, "SQL request shouldn't contain post_non_existing_column string." );
$this->assertSameSets( array( self::$pid1, self::$pid2, self::$pid3 ), $q->posts, 'Query results should be equal to the set.' );
}
/**
* Filter callback that sets a non-existing search column.
*
* @param string[] $search_columns Array of column names to be searched.
* @param string $search Text being searched.
* @param WP_Query $wp_query The current WP_Query instance.
* @return string[] $search_columns Array of column names to be searched.
*/
public function post_non_existing_search_column( $search_columns, $search, $wp_query ) {
$search_columns = array( 'post_non_existing_column' );
return $search_columns;
}
}