From c43b58cf597740e7835f1067ea7ea5e2fb394bf1 Mon Sep 17 00:00:00 2001 From: Jane Sandberg Date: Fri, 20 May 2022 13:26:57 -0600 Subject: [PATCH] LP1968754: Better handling for blank and wildcard course searches Signed-off-by: Jane Sandberg --- .../perlmods/lib/OpenILS/WWW/EGCatLoader/Course.pm | 4 +- Open-ILS/src/perlmods/t/25-course-opac-search.t | 54 ++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Open-ILS/src/perlmods/t/25-course-opac-search.t diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Course.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Course.pm index f0e8ab0423..671d0d35c2 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Course.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Course.pm @@ -298,7 +298,9 @@ sub _create_where_clause { for my $query_obj (@{$queries}) { my $type = $query_obj->{'qtype'}; my $query = $query_obj->{'value'}; - $query =~ s/\*//g; + + # Do we have a blank query, or just wildcards and/or spaces? + next if $query =~ /^[\s\*]*$/; my $bool = $query_obj->{'bool'}; my $contains = $query_obj->{'contains'}; my $operator = ($contains eq 'nocontains') ? '!~*' : '~*'; diff --git a/Open-ILS/src/perlmods/t/25-course-opac-search.t b/Open-ILS/src/perlmods/t/25-course-opac-search.t new file mode 100644 index 0000000000..05d89597a6 --- /dev/null +++ b/Open-ILS/src/perlmods/t/25-course-opac-search.t @@ -0,0 +1,54 @@ +#!perl +use strict; use warnings; +use Test::More tests => 7; +use OpenILS::WWW::EGCatLoader; + +use_ok('OpenILS::WWW::EGCatLoader'); +can_ok( 'OpenILS::WWW::EGCatLoader', '_create_where_clause' ); + +my $orgs = (1..9); + +my @course_name_query = {'qtype' => 'name','contains' => 'contains','bool' => 'and','value' => 'zebra'}; +my $course_name_expected = {"-and" => [ + {"owning_lib"=> $orgs}, + {"-not"=>{"+acmc"=>"is_archived"}}, + {"name"=>{"~*"=>"zebra"}}]}; +is_query_parsed_correctly ( \@course_name_query, $course_name_expected, 'Create a valid course name search json query' ); + +my @empty_number_query = {'qtype' => 'course_number','contains' => 'contains','bool' => 'and','value' => ''}; +my $blank_query_expected = {"-and" => [ + {"owning_lib"=> $orgs}, + {"-not"=>{"+acmc"=>"is_archived"}}]}; +is_query_parsed_correctly ( \@empty_number_query, $blank_query_expected, + 'Create a valid course number search json query for blank search' ); + +my @instructor_wildcard_query = {'qtype' => 'instructor','contains' => 'contains','bool' => 'and','value' => '*'}; +is_query_parsed_correctly( \@instructor_wildcard_query, $blank_query_expected, + 'Create a valid instructor search json query for wildcard search' ); + +my @instructor_blank_query = {'qtype' => 'instructor','contains' => 'contains','bool' => 'and','value' => ''}; +is_query_parsed_correctly( \@instructor_blank_query, $blank_query_expected, + 'Create a valid instructor search json query for blank search' ); + +my @instructor_query = {}; +my $instructor_query_expected = {"-and" => [ + {"owning_lib"=> $orgs}, + {"-not"=>{"+acmc"=>"is_archived"}}, + {"id"=>{"in"=>{"where"=> + {"usr"=>{"in"=> + {"select"=>{"au"=>["id"]}, + "where"=>{"name_kw_tsvector"=> + {"@@"=>{"value"=>["plainto_tsquery","leonard"]}}}, + "from"=>"au"}}}, + "select"=>{"acmcu"=>["course"]},"from"=>"acmcu"}}}]}; +is_query_parsed_correctly( \@instructor_blank_query, $blank_query_expected, + 'Create a valid instructor search json query'); + +sub is_query_parsed_correctly { + my ($query_hash, $expected, $description) = @_; + my $ctx = {'processed_search_query' => '*'}; + my $query = OpenILS::WWW::EGCatLoader::_create_where_clause($ctx, $query_hash, $orgs); + return is_deeply($query, $expected, $description); +} + +1; \ No newline at end of file -- 2.11.0