Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeelia committed Feb 1, 2023
1 parent 8d7c0e6 commit db873e9
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 5 deletions.
6 changes: 2 additions & 4 deletions includes/classes/Feature/Facets/Types/MetaRange/FacetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function add_query_filters( $filters ) {
foreach ( $values as $min_or_max => $value ) {
$operator = '_min' === $min_or_max ? 'gte' : 'lte';

$range_filters[ $field_name ][ $operator ] = $value;
$range_filters[ $field_name ][ $operator ] = floatval( $value );
}
}

Expand Down Expand Up @@ -162,9 +162,7 @@ public function format_selected( string $facet, $value, array $filters ) {
$min_or_max = substr( $facet, -4 );
$field_name = substr( $facet, 0, -4 );

$filters[ $this->get_filter_type() ][ $field_name ] = [
$min_or_max => $value,
];
$filters[ $this->get_filter_type() ][ $field_name ][ $min_or_max ] = $value;

return $filters;
}
Expand Down
228 changes: 228 additions & 0 deletions tests/php/features/TestFacetTypeMetaRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<?php
/**
* Test meta range facet type feature
*
* @since 4.5.0
* @package elasticpress
*/

namespace ElasticPressTest;

use ElasticPress\Features as Features;

/**
* Facets\Types\Taxonomy\FacetType test class
*/
class TestFacetTypeMetaRange extends BaseTestCase {

protected $facet_type = null;

/**
* 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 range facet type is made available.
*/
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
if ( ! isset( $facet_feature->types['meta-range'] ) && class_exists( '\ElasticPress\Feature\Facets\Types\MetaRange\FacetType' ) ) {
$facet_feature->types['meta-range'] = new \ElasticPress\Feature\Facets\Types\MetaRange\FacetType();
$facet_feature->types['meta-range']->setup();
}

$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$this->facet_type = $facet_feature->types['meta-range'];

parent::set_up();
}

/**
* Test get_filter_name
*
* @group facets
*/
public function testGetFilterName() {
/**
* Test default behavior
*/
$this->assertEquals( 'ep_meta_range_filter_', $this->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_range_filter_name', $change_filter_name );
$this->assertEquals( 'ep_meta_range_filter__', $this->facet_type->get_filter_name() );
}

/**
* Test get_filter_type
*
* @group facets
*/
public function testGetFilterType() {
/**
* Test default behavior
*/
$this->assertEquals( 'meta-range', $this->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_range_filter_type', $change_filter_type );
$this->assertEquals( 'meta-range_', $this->facet_type->get_filter_type() );
}

/**
* Test add_query_filters
*
* @group facets
*/
public function testAddQueryFilters() {
parse_str( 'ep_meta_range_filter_my_custom_field_min=5&ep_meta_range_filter_my_custom_field_max=25', $_GET );

$new_filters = $this->facet_type->add_query_filters( [] );
$expected = [
[
'range' => [
'meta.my_custom_field.double' => [
'gte' => floatval( 5 ),
'lte' => floatval( 25 ),
],
],
],
];
$this->assertSame( $expected, $new_filters );
}

/**
* Test set_wp_query_aggs
*
* @group facets
*/
public function testSetWpQueryAggs() {
$set_facet_meta_field = function() {
return [ 'new_meta_key_1', 'new_meta_key_2' ];
};
add_filter( 'ep_facet_meta_range_fields', $set_facet_meta_field );

$with_aggs = $this->facet_type->set_wp_query_aggs( [] );

/**
* Test default behavior
*/
$default_min_agg = [
'min' => [
'field' => 'meta.new_meta_key_1.double',
],
];
$this->assertSame( $with_aggs['ep_meta_range_filter_new_meta_key_1_min'], $default_min_agg );
$default_max_agg = [
'max' => [
'field' => 'meta.new_meta_key_1.double',
],
];
$this->assertSame( $with_aggs['ep_meta_range_filter_new_meta_key_1_max'], $default_max_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 ) ? 'long' : $es_field;
};

add_filter( 'ep_facet_meta_use_field', $change_meta_facet_field, 10, 2 );

$with_aggs = $this->facet_type->set_wp_query_aggs( [] );
$this->assertSame( 'meta.new_meta_key_1.double', $with_aggs['ep_meta_range_filter_new_meta_key_1_min']['min']['field'] );
$this->assertSame( 'meta.new_meta_key_1.double', $with_aggs['ep_meta_range_filter_new_meta_key_1_max']['max']['field'] );
$this->assertSame( 'meta.new_meta_key_2.double', $with_aggs['ep_meta_range_filter_new_meta_key_2_min']['min']['field'] );
$this->assertSame( 'meta.new_meta_key_2.double', $with_aggs['ep_meta_range_filter_new_meta_key_2_max']['max']['field'] );

remove_filter( 'ep_facet_meta_use_field', $change_meta_facet_field );
}

/**
* Test the format_selected method.
*/
public function testFormatSelected() {
$original_filters = [ 'custom_type' => [ 'facet' => [ 1, 2, 3 ] ] ];
$new_filters = $this->facet_type->format_selected( 'my_meta_min', '5', $original_filters );
$expected_filters = array_merge(
$original_filters,
[
$this->facet_type->get_filter_type() => [
'my_meta' => [
'_min' => '5',
],
],
]
);

$this->assertSame( $new_filters, $expected_filters );

/**
* Analyzing tags=slug3,slug4 should ADD tags, keeping the category index.
*/
$original_filters = $expected_filters;
$new_filters = $this->facet_type->format_selected( 'my_meta_max', '25', $original_filters );

$expected_filters[ $this->facet_type->get_filter_type() ]['my_meta'] = [
'_min' => '5',
'_max' => '25',
];

$this->assertSame( $new_filters, $expected_filters );
}

/**
* Test the add_query_params method.
*/
public function testAddQueryParams() {
$original_query_params = [ 'custom_name' => 'custom_value' ];
$selected_filters = [
[
'custom_type' => [ 'facet' => [ 1, 2, 3 ] ]
],
$this->facet_type->get_filter_type() => [
'my_meta_1' => [
'_min' => '5',
'_max' => '25',
],
'my_meta_2' => [
'_min' => '10',
'_max' => '50',
],
],
];

$new_query_params = $this->facet_type->add_query_params( $original_query_params, $selected_filters );
$expected_query_params = array_merge(
$original_query_params,
[
$this->facet_type->get_filter_name() . 'my_meta_1_min' => '5',
$this->facet_type->get_filter_name() . 'my_meta_1_max' => '25',
$this->facet_type->get_filter_name() . 'my_meta_2_min' => '10',
$this->facet_type->get_filter_name() . 'my_meta_2_max' => '50',
]
);

$this->assertSame( $new_query_params, $expected_query_params );
}

/**
* Test get_facets_meta_fields
*
* @group facets
*/
public function testGetFacetsMetaFields() {
$this->markTestIncomplete();
}
}
88 changes: 87 additions & 1 deletion tests/php/features/TestFacetTypeTaxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ public function testAddQueryFilters() {
* @group facets
*/
public function testGetSanitizeCallback() {

$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$test_taxonomy = 'This is a test taxonomy';

Expand All @@ -228,4 +227,91 @@ public function testGetSanitizeCallback() {
$expected_result = sanitize_text_field( $test_taxonomy );
$this->assertArrayHasKey( $expected_result, $selected['taxonomies']['taxonomy']['terms'] );
}

/**
* Test the format_selected method.
*
* @todo Move this to a mock, as it is just inherited now
* @since 4.5.0
*/
public function testFormatSelected() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['taxonomy'];

$original_filters = [ 'custom_type' => [ 'facet' => [ 1, 2, 3 ] ] ];
$new_filters = $facet_type->format_selected( 'category', 'slug1,slug2', $original_filters );
$expected_filters = array_merge(
$original_filters,
[
$facet_type->get_filter_type() => [
'category' => [
'terms' => [
'slug1' => true,
'slug2' => true,
],
],
],
]
);

$this->assertSame( $new_filters, $expected_filters );

/**
* Analyzing tags=slug3,slug4 should ADD tags, keeping the category index.
*/
$original_filters = $expected_filters;
$new_filters = $facet_type->format_selected( 'tags', 'slug3,slug4', $original_filters );

$expected_filters[ $facet_type->get_filter_type() ]['tags'] = [
'terms' => [
'slug3' => true,
'slug4' => true,
],
];

$this->assertSame( $new_filters, $expected_filters );
}

/**
* Test the add_query_params method.
*
* @todo Move this to a mock, as it is just inherited now
* @since 4.5.0
*/
public function testAddQueryParams() {
$facet_feature = Features::factory()->get_registered_feature( 'facets' );
$facet_type = $facet_feature->types['taxonomy'];

$original_query_params = [ 'custom_name' => 'custom_value' ];
$selected_filters = [
[
'custom_type' => [ 'facet' => [ 1, 2, 3 ] ]
],
$facet_type->get_filter_type() => [
'category' => [
'terms' => [
'slug1' => true,
'slug2' => true,
],
],
'tags' => [
'terms' => [
'slug3' => true,
'slug4' => true,
],
],
],
];

$new_query_params = $facet_type->add_query_params( $original_query_params, $selected_filters );
$expected_query_params = array_merge(
$original_query_params,
[
$facet_type->get_filter_name() . 'category' => 'slug1,slug2',
$facet_type->get_filter_name() . 'tags' => 'slug3,slug4',
]
);

$this->assertSame( $new_query_params, $expected_query_params );
}
}

0 comments on commit db873e9

Please sign in to comment.