-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathTestFacetTypeMeta.php
More file actions
473 lines (397 loc) · 13.3 KB
/
Copy pathTestFacetTypeMeta.php
File metadata and controls
473 lines (397 loc) · 13.3 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<?php
/**
* Test meta facet type feature
*
* @package elasticpress
*/
namespace ElasticPressTest;
use ElasticPress\Features;
/**
* Facets\Types\Taxonomy\FacetType test class
*/
class TestFacetTypeMeta extends BaseTestCase {
/**
* Setup each test.
*/
public function set_up() {
/**
* It is too late to use the `ep_facet_types` filter.
*
* NOTE: This can be removed after the meta facet type is made available.
*/
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
if ( ! isset( $facet_feature->types['meta'] ) && class_exists( '\ElasticPress\Feature\Facets\Types\Meta\FacetType' ) ) {
$facet_feature->types['meta'] = new \ElasticPress\Feature\Facets\Types\Meta\FacetType();
$facet_feature->types['meta']->setup();
}
parent::set_up();
}
/**
* Test get_filter_name
*
* @since 4.3.0
* @group facets
*/
public function testGetFilterName() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['meta'];
/**
* Test default behavior
*/
$this->assertEquals( 'ep_meta_filter_', $facet_type->get_filter_name() );
/**
* Test the `ep_facet_meta_filter_name` filter
*/
$change_filter_name = function ( $filter_name ) {
return $filter_name . '_';
};
add_filter( 'ep_facet_meta_filter_name', $change_filter_name );
$this->assertEquals( 'ep_meta_filter__', $facet_type->get_filter_name() );
}
/**
* Test get_filter_type
*
* @since 4.3.0
* @group facets
*/
public function testGetFilterType() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['meta'];
/**
* Test default behavior
*/
$this->assertEquals( 'meta', $facet_type->get_filter_type() );
/**
* Test the `ep_facet_filter_type` filter
*/
$change_filter_type = function ( $filter_type ) {
return $filter_type . '_';
};
add_filter( 'ep_facet_meta_filter_type', $change_filter_type );
$this->assertEquals( 'meta_', $facet_type->get_filter_type() );
}
/**
* Test set_wp_query_aggs
*
* @since 4.3.0
* @group facets
*/
public function testSetWpQueryAggs() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['meta'];
$set_facet_meta_field = function () {
return [ 'new_meta_key_1', 'new_meta_key_2' ];
};
add_filter( 'ep_facet_meta_fields', $set_facet_meta_field );
$with_aggs = $facet_type->set_wp_query_aggs( [] );
/**
* Test default behavior
*/
$default_cat_agg = [
'terms' => [
'size' => 10000,
'field' => 'meta.new_meta_key_1.raw',
],
];
$this->assertSame( $with_aggs['ep_meta_filter_new_meta_key_1'], $default_cat_agg );
/**
* Test the `ep_facet_meta_use_field` filter
*/
$change_meta_facet_field = function ( $es_field, $meta_field ) {
return ( 'new_meta_key_1' === $meta_field ) ? 'boolean' : $es_field;
};
add_filter( 'ep_facet_meta_use_field', $change_meta_facet_field, 10, 2 );
$with_aggs = $facet_type->set_wp_query_aggs( [] );
$this->assertSame( 'meta.new_meta_key_1.boolean', $with_aggs['ep_meta_filter_new_meta_key_1']['terms']['field'] );
$this->assertSame( 'meta.new_meta_key_2.raw', $with_aggs['ep_meta_filter_new_meta_key_2']['terms']['field'] );
remove_filter( 'ep_facet_meta_use_field', $change_meta_facet_field );
/**
* Test the `ep_facet_meta_size` filter
*/
$change_meta_bucket_size = function ( $size, $meta_field ) {
return ( 'new_meta_key_1' === $meta_field ) ? 5 : $size;
};
add_filter( 'ep_facet_meta_size', $change_meta_bucket_size, 10, 2 );
$with_aggs = $facet_type->set_wp_query_aggs( [] );
$this->assertSame( 5, $with_aggs['ep_meta_filter_new_meta_key_1']['terms']['size'] );
$this->assertSame( 10000, $with_aggs['ep_meta_filter_new_meta_key_2']['terms']['size'] );
}
/**
* Test get_meta_values
*
* @since 4.3.0
* @group facets
*/
public function testGetMetaValues() {
add_filter(
'ep_prepare_meta_allowed_keys',
function ( $allowed_metakeys ) {
return array_merge( $allowed_metakeys, [ 'new_meta_key_1', 'new_meta_key_2' ] );
}
);
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['meta'];
$this->ep_factory->post->create( array( 'meta_input' => array( 'new_meta_key_1' => 'foo' ) ) );
$this->ep_factory->post->create( array( 'meta_input' => array( 'new_meta_key_1' => 'bar' ) ) );
$this->ep_factory->post->create( array( 'meta_input' => array( 'new_meta_key_1' => 'foobar' ) ) );
$this->ep_factory->post->create( array( 'meta_input' => array( 'new_meta_key_2' => 'lorem' ) ) );
$this->ep_factory->post->create( array( 'meta_input' => array( 'new_meta_key_2' => 'ipsum' ) ) );
\ElasticPress\Elasticsearch::factory()->refresh_indices();
/**
* Test default behavior
*/
$meta_values = $facet_type->get_meta_values( 'new_meta_key_1' );
$this->assertEqualsCanonicalizing( [ 'foo', 'bar', 'foobar' ], $meta_values );
// Make sure it is using the cached value and is not going to Elasticsearch.
$ep_remote_request_count = did_action( 'ep_remote_request' );
$meta_values = $facet_type->get_meta_values( 'new_meta_key_1' );
$this->assertSame( $ep_remote_request_count, did_action( 'ep_remote_request' ) );
/**
* Test the `ep_facet_meta_custom_meta_values` filter
*/
$change_meta_values = function ( $meta_values, $meta_key ) {
return ( 'new_meta_key_1' === $meta_key ) ? [ '123' ] : $meta_values;
};
add_filter( 'ep_facet_meta_custom_meta_values', $change_meta_values, 10, 2 );
$meta_values_1 = $facet_type->get_meta_values( 'new_meta_key_1' );
$meta_values_2 = $facet_type->get_meta_values( 'new_meta_key_2' );
$this->assertSame( [ '123' ], $meta_values_1 );
$this->assertEqualsCanonicalizing( [ 'lorem', 'ipsum' ], $meta_values_2 );
remove_filter( 'ep_facet_meta_custom_meta_values', $change_meta_values );
delete_transient( get_class( $facet_type )::TRANSIENT_PREFIX . 'new_meta_key_1' );
delete_transient( get_class( $facet_type )::TRANSIENT_PREFIX . 'new_meta_key_2' );
/**
* Test the `ep_facet_meta_value_max_strlen` filter
*/
$change_max_str_len = function ( $length, $meta_key ) {
return ( 'new_meta_key_2' === $meta_key ) ? 4 : $length;
};
add_filter( 'ep_facet_meta_value_max_strlen', $change_max_str_len, 10, 2 );
$meta_values_1 = $facet_type->get_meta_values( 'new_meta_key_1' );
$meta_values_2 = $facet_type->get_meta_values( 'new_meta_key_2' );
$this->assertEqualsCanonicalizing( [ 'foo', 'bar', 'foobar' ], $meta_values_1 );
$this->assertEqualsCanonicalizing( [ 'lore', 'ipsu' ], $meta_values_2 );
}
/**
* Test add_query_filters
*
* @since 4.4.0
* @group facets
*/
public function testAddQueryFilters() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['meta'];
$allow_field = function ( $fields ) {
$fields[] = 'my_custom_field';
return $fields;
};
add_filter( 'ep_facet_meta_fields', $allow_field );
parse_str( 'ep_meta_filter_my_custom_field=dolor,amet', $_GET );
$new_filters = $facet_type->add_query_filters( [] );
$expected = [
[
'term' => [
'meta.my_custom_field.raw' => 'dolor',
],
],
[
'term' => [
'meta.my_custom_field.raw' => 'amet',
],
],
];
$this->assertSame( $expected, $new_filters );
/**
* Changing the match type should change from `term` to `terms`
*/
$change_match_type = function () {
return 'any';
};
add_filter( 'ep_facet_match_type', $change_match_type );
$new_filters = $facet_type->add_query_filters( [] );
$expected = [
[
'terms' => [
'meta.my_custom_field.raw' => [ 'dolor', 'amet' ],
],
],
];
$this->assertSame( $expected, $new_filters );
}
/**
* Test add_query_filters with not allowed parameters
*
* @since 4.5.1
* @group facets
*/
public function testAddQueryFiltersWithNotAllowedParameters() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['meta'];
$allow_field = function ( $fields ) {
$fields[] = 'my_custom_field';
return $fields;
};
add_filter( 'ep_facet_meta_fields', $allow_field );
parse_str( 'ep_meta_filter_my_custom_field=dolor,amet&ep_meta_filter_not_allowed=1', $_GET );
$new_filters = $facet_type->add_query_filters( [] );
$expected = [
[
'term' => [
'meta.my_custom_field.raw' => 'dolor',
],
],
[
'term' => [
'meta.my_custom_field.raw' => 'amet',
],
],
];
$this->assertSame( $expected, $new_filters );
add_filter( 'ep_facet_should_check_if_allowed', '__return_false' );
$new_filters = $facet_type->add_query_filters( [] );
$expected = [
[
'term' => [
'meta.my_custom_field.raw' => 'dolor',
],
],
[
'term' => [
'meta.my_custom_field.raw' => 'amet',
],
],
[
'term' => [
'meta.not_allowed.raw' => 1,
],
],
];
$this->assertSame( $expected, $new_filters );
}
/**
* Test that meta facet aggregations can be recovered at runtime when the
* main facetable query did not include that meta field aggregation.
*
* This verifies the fallback path used by the renderer to avoid empty
* (zero-count) facet items when aggregation data is missing.
*
* @since 5.3.1
* @group facets
*/
public function testGetFacetAggregationForFieldRuntimeFallback() {
$meta_field = 'runtime_meta_' . wp_generate_password( 8, false, false );
$allow_meta_key = function ( $allowed_meta_keys ) use ( $meta_field ) {
return array_merge( $allowed_meta_keys, [ $meta_field ] );
};
add_filter( 'ep_prepare_meta_allowed_keys', $allow_meta_key );
try {
$this->ep_factory->post->create_many(
2,
[
'meta_input' => [
$meta_field => 'alpha',
],
]
);
$this->ep_factory->post->create(
[
'meta_input' => [
$meta_field => 'beta',
],
]
);
\ElasticPress\Elasticsearch::factory()->refresh_indices();
$query = new \WP_Query(
[
'ep_is_facetable' => true,
'post_type' => 'post',
]
);
// Confirm this query was served by Elasticsearch and can hold facet aggs.
$this->assertTrue( $query->elasticsearch_success );
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['meta'];
$facet_name = $facet_type->get_filter_name() . $meta_field;
// The requested meta field is intentionally missing before fallback runs.
$this->assertFalse( $facet_feature->get_facet_aggregation( $query, $facet_name ) );
$aggregation = $facet_type->get_facet_aggregation_for_field( $query, $meta_field );
// Fallback returns real counts for the requested field.
$this->assertIsArray( $aggregation );
$this->assertArrayHasKey( 'alpha', $aggregation );
$this->assertArrayHasKey( 'beta', $aggregation );
$this->assertSame( 2, $aggregation['alpha'] );
$this->assertSame( 1, $aggregation['beta'] );
// Fallback result is merged back into the original query aggregation cache.
$this->assertSame( $aggregation, $facet_feature->get_facet_aggregation( $query, $facet_name ) );
} finally {
remove_filter( 'ep_prepare_meta_allowed_keys', $allow_meta_key );
}
}
/**
* Test get_facets_meta_fields
*
* @since 4.3.0
* @group facets
*/
public function testGetFacetsMetaFields() {
$this->markTestIncomplete();
}
/**
* Test invalidate_meta_values_cache
*
* @since 4.3.0
* @group facets
*/
public function testInvalidateMetaValuesCache() {
$this->markTestIncomplete();
}
/**
* Test invalidate_meta_values_cache_after_bulk
*
* @since 4.3.0
* @group facets
*/
public function testInvalidateMetaValuesCacheAfterBulk() {
$this->markTestIncomplete();
}
/**
* Test get_sanitize_callback method.
*
* @since 4.4.0
* @group facets
*/
public function testGetSanitizeCallback() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$test_meta = 'This is a test meta';
parse_str( "ep_meta_filter_new_meta_key_1={$test_meta}", $_GET );
$selected = $facet_feature->get_selected();
// test sanitize_text_field runs by default on taxonomy facets
$expected_result = sanitize_text_field( $test_meta );
$this->assertArrayHasKey( $expected_result, $selected['meta']['new_meta_key_1']['terms'] );
$sanitize_function = function ( $callback ) {
$this->assertSame( 'sanitize_text_field', $callback );
return 'sanitize_title';
};
// modify the sanitize callback.
add_filter( 'ep_facet_default_sanitize_callback', $sanitize_function );
$selected = $facet_feature->get_selected();
// test sanitize_text_field runs when filter is applied.
$expected_result = sanitize_title( $test_meta );
$this->assertArrayHasKey( $expected_result, $selected['meta']['new_meta_key_1']['terms'] );
}
/**
* Test the block does not register in the editor
*
* @since 5.3.0
* @group facets
*/
public function test_block_does_not_register_in_editor() {
$GLOBALS['pagenow'] = 'post-new.php';
set_current_screen( 'post-new.php' );
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_feature->tear_down();
$facet_feature->setup();
$facet_type = $facet_feature->types['meta'];
$this->assertFalse( has_action( 'init', [ $facet_type->block, 'register_block' ] ) );
}
}