Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: accented values for facets #9483

Merged
merged 3 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ use boolean;
use Excel::Writer::XLSX;
use Template;
use Devel::Size qw(size total_size);
use Data::DeepAccess qw(deep_get);
use Data::DeepAccess qw(deep_get deep_set);
use Log::Log4perl;
use LWP::UserAgent;

Expand Down Expand Up @@ -529,7 +529,13 @@ A scalar value for the parameter, or undef if the parameter is not defined.
=cut

sub request_param ($request_ref, $param_name) {
return (scalar param($param_name)) || deep_get($request_ref, "body_json", $param_name);
my $cgi_param = scalar param($param_name);
if (defined $cgi_param) {
return decode utf8 => $cgi_param;
}
else {
return deep_get($request_ref, "body_json", $param_name);
}
}

=head2 init_request ()
Expand Down Expand Up @@ -4202,16 +4208,16 @@ HTML

foreach my $tag_ref (@{$request_ref->{tags}}) {
if ($tagtype eq 'users') {
param('creator', $tagid);
deep_set($request_ref, "body_json", "creator", $tagid);
}
else {
my $field_name = $tag_ref->{tagtype} . "_tags";
my $current_value = param($field_name);
my $current_value = deep_get($request_ref, "body_json", $field_name);
my $new_value = ($tag_ref->{tag_prefix} // '') . ($tag_ref->{canon_tagid} // $tag_ref->{tagid});
if ($current_value) {
$new_value = $current_value . ',' . $new_value;
}
param($field_name, $new_value);
deep_set($request_ref, "body_json", $field_name, $new_value);
}
}

Expand Down Expand Up @@ -4291,6 +4297,25 @@ HTML
return;
}

=head2 display_list_of_tags ( $request_ref, $query_ref )

Return an array of names of all request parameters.

=cut

sub list_all_request_params ($request_ref) {

# CGI params (query string and POST body)
my @params = multi_param();

# Add params from the JSON body if any
if (defined $request_ref->{body_json}) {
push @params, keys %{$request_ref->{body_json}};
}

return @params;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool !


=head2 display_search_results ( $request_ref )

This function builds the HTML returned by the /search endpoint.
Expand Down Expand Up @@ -4318,7 +4343,7 @@ sub display_search_results ($request_ref) {

my $current_link = '';

foreach my $field (multi_param()) {
foreach my $field (list_all_request_params($request_ref)) {
if (
($field eq "page")
or ($field eq "fields")
Expand Down Expand Up @@ -4584,7 +4609,7 @@ sub add_params_to_query ($request_ref, $query_ref) {

my $and = $query_ref->{"\$and"};

foreach my $field (multi_param()) {
foreach my $field (list_all_request_params($request_ref)) {

$log->debug("add_params_to_query - field", {field => $field}) if $log->is_debug();

Expand Down Expand Up @@ -4616,7 +4641,7 @@ sub add_params_to_query ($request_ref, $query_ref) {
# xyz_tags=-c products without the c tag
# xyz_tags=a,b,-c,-d

my $values = remove_tags_and_quote(decode utf8 => single_param($field));
my $values = remove_tags_and_quote(request_param($request_ref, $field));

$log->debug("add_params_to_query - tags param",
{field => $field, lc => $lc, tag_lc => $tag_lc, values => $values})
Expand Down Expand Up @@ -4748,7 +4773,7 @@ sub add_params_to_query ($request_ref, $query_ref) {
# We can have multiple conditions, separated with a comma
# e.g. sugars_100g=>10,<=20

my $conditions = single_param($field);
my $conditions = request_param($request_ref, $field);

$log->debug("add_params_to_query - nutrient conditions", {field => $field, conditions => $conditions})
if $log->is_debug();
Expand All @@ -4766,7 +4791,7 @@ sub add_params_to_query ($request_ref, $query_ref) {
}
else {
$operator = '=';
$value = single_param($field);
$value = request_param($request_ref, $field);
}

$log->debug("add_params_to_query - nutrient condition",
Expand Down Expand Up @@ -4796,7 +4821,7 @@ sub add_params_to_query ($request_ref, $query_ref) {
# Exact match on a specific field (e.g. "code")
elsif (defined $valid_params{$field}) {

my $values = remove_tags_and_quote(decode utf8 => single_param($field));
my $values = remove_tags_and_quote(request_param($request_ref, $field));

# Possible values:
# xyz=a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"count" : 8,
"count" : 9,
"page" : 1,
"page_count" : 8,
"page_count" : 9,
"page_size" : 24,
"products" : [
{
Expand All @@ -19,6 +19,9 @@
{
"product_name" : "Carrots - No label - Belgium - brand2"
},
{
"product_name" : "Chocolate - Organic, Café Label - Martinique"
},
{
"product_name" : "Chocolate - Organic, Fair trade - Martinique"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"count" : 5,
"count" : 6,
"page" : 1,
"page_count" : 5,
"page_count" : 6,
"page_size" : 24,
"products" : [
{
Expand All @@ -13,6 +13,9 @@
{
"product_name" : "Apples - Organic, Fair trade - France"
},
{
"product_name" : "Chocolate - Organic, Café Label - Martinique"
},
{
"product_name" : "Chocolate - Organic, Fair trade - Martinique"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"count" : 10,
"count" : 11,
"page" : 1,
"page_count" : 10,
"page_count" : 11,
"page_size" : 24,
"products" : [
{
Expand All @@ -22,6 +22,9 @@
{
"product_name" : "Carrots - Organic - France - brand1, brand2"
},
{
"product_name" : "Chocolate - Organic, Café Label - Martinique"
},
{
"product_name" : "Chocolate - Organic, Fair trade - Martinique"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"count" : 7,
"count" : 8,
"page" : 1,
"page_count" : 7,
"page_count" : 8,
"page_size" : 24,
"products" : [
{
Expand All @@ -16,6 +16,9 @@
{
"product_name" : "Carrots - Organic - France - brand1, brand2"
},
{
"product_name" : "Chocolate - Organic, Café Label - Martinique"
},
{
"product_name" : "Chocolate - Organic, Fair trade - Martinique"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"count" : 12,
"count" : 13,
"page" : 1,
"page_count" : 12,
"page_count" : 13,
"page_size" : 24,
"products" : [
{
Expand Down Expand Up @@ -31,6 +31,9 @@
{
"product_name" : "Carrots - Organic - France - brand1, brand2"
},
{
"product_name" : "Chocolate - Organic, Café Label - Martinique"
},
{
"product_name" : "Chocolate - Organic, Fair trade - Martinique"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"count" : 1,
"page" : 1,
"page_count" : 1,
"page_size" : 24,
"products" : [
{
"labels_tags" : [
"en:fair-trade",
"en:organic",
"de:café-label"
],
"product_name" : "Chocolate - Organic, Café Label - Martinique"
}
],
"skip" : 0
}
23 changes: 22 additions & 1 deletion tests/integration/facets.t
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ my @products = (
origins => "en:martinique",
),
},
# German product with accents in labels
{
%{dclone(\%empty_product_form)},
(
code => '200000000013',
product_name => "Chocolate - Organic, Café Label - Martinique",
categories => "en:chocolate",
labels => "en:organic,en:fair-trade,Café Label",
lang => "de",
lc => "de",
),
},
);

foreach my $product_ref (@products) {
Expand All @@ -175,7 +187,7 @@ edit_product(
{
%{dclone(\%empty_product_form)},
(
code => '200000000013',
code => '200000000101',
product_name => "Yogurt - Organic, Fair trade - Martinique",
categories => "en:yogurt",
labels => "en:organic,en:fair-trade",
Expand Down Expand Up @@ -310,6 +322,15 @@ my $tests_ref = [
expected_status_code => 200,
sort_products_by => 'product_name',
},
# accented facet value in German
{
test_case => 'de-accented-cafe-label',
method => 'GET',
subdomain => 'world-de',
path => '/label/café-label.json?fields=product_name,labels_tags',
expected_status_code => 200,
sort_products_by => 'product_name',
},
];

# note: we need to execute the tests with bob, because we need authentication
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/display.t
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,8 @@ display_tag($facets_ref);
is($facets_ref->{'current_link'}, '/category/breads/data-quality');
is($facets_ref->{'redirect'}, '/category/breads/data-quality');

$request_ref->{body_json}{labels_tags} = 'en:organic';
is(request_param($request_ref, 'unexisting_field'), undef);
is(request_param($request_ref, 'labels_tags'), 'en:organic') or diag explain request_param($request_ref, 'labels_tags');

done_testing();
Loading