From 0965dfcc15567216315ce73716abc469c0f27f53 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 28 Aug 2014 10:51:03 +0200 Subject: [PATCH] Bug 12833: Patron search should search on extended attributes Before Bug 9811, the patron search searched on extended attributes. This patch restore this behavior. Test plan: 0/ Create a patron attribute PA 1/ Create a patron A (cardnumber CNA) with PA="foo" 2/ Create a patron B (cardnumber CNB) with PA="foo bar" 3/ Search for CNA should redirect on the patron detail page. 4/ Search for "foo" should display the search result with 2 results. Signed-off-by: Nick Clemens Signed-off-by: Katrin Fischer 'Searchable' patron attributes can now be searched for again. Works as described, passes stests and QA script. Signed-off-by: Tomas Cohen Arazi --- C4/Utils/DataTables/Members.pm | 9 ++++ svc/members/search | 14 +++++- t/db_dependent/Utils/Datatables_Members.t | 53 +++++++++++++++++++---- 3 files changed, 66 insertions(+), 10 deletions(-) diff --git a/C4/Utils/DataTables/Members.pm b/C4/Utils/DataTables/Members.pm index aa0f838bde..95f6a82c9a 100644 --- a/C4/Utils/DataTables/Members.pm +++ b/C4/Utils/DataTables/Members.pm @@ -79,6 +79,15 @@ sub search { push @where_args, $term; } + if ( C4::Context->preference('ExtendedPatronAttributes') and $searchmember ) { + my $matching_borrowernumbers = C4::Members::Attributes::SearchIdMatchingAttribute($searchmember); + + for my $borrowernumber ( @$matching_borrowernumbers ) { + push @where_strs_or, "borrowers.borrowernumber = ?"; + push @where_args, $borrowernumber; + } + } + push @where_strs, '('. join (' OR ', @where_strs_or) . ')' if @where_strs_or; } diff --git a/svc/members/search b/svc/members/search index 1b3a02aa9e..e0b64a50fa 100755 --- a/svc/members/search +++ b/svc/members/search @@ -57,8 +57,18 @@ foreach (grep {$_ =~ /^mDataProp/} keys %dt_params) { $dt_params{$_} =~ s/^dt_//; } +my $results; +if ( $searchfieldstype and $searchfieldstype eq 'standard' ) { + my $member = C4::Members::GetMember( cardnumber => $searchmember ); + $results = { + iTotalRecords => 1, + iTotalDisplayRecords => 1, + patrons => [ $member ], + } if $member; +} + # Perform the patrons search -my $results = C4::Utils::DataTables::Members::search( +$results = C4::Utils::DataTables::Members::search( { searchmember => $searchmember, firstletter => $firstletter, @@ -69,7 +79,7 @@ my $results = C4::Utils::DataTables::Members::search( dt_params => \%dt_params, } -); +) unless $results; $template->param( sEcho => $sEcho, diff --git a/t/db_dependent/Utils/Datatables_Members.t b/t/db_dependent/Utils/Datatables_Members.t index 1c85423799..e0deaaee74 100644 --- a/t/db_dependent/Utils/Datatables_Members.t +++ b/t/db_dependent/Utils/Datatables_Members.t @@ -17,16 +17,20 @@ use Modern::Perl; -use Test::More tests => 11; +use Test::More tests => 13; use C4::Context; use C4::Branch; use C4::Members; +use C4::Members::Attributes; +use C4::Members::AttributeTypes; + +use t::lib::Mocks; + use_ok( "C4::Utils::DataTables::Members" ); my $dbh = C4::Context->dbh; -my $res; # Start transaction $dbh->{AutoCommit} = 0; @@ -83,12 +87,12 @@ my %jane_doe = ( userid => 'jane.doe' ); -$res = AddMember( %john_doe ); -warn "Error adding John Doe, check your tests" unless $res; -$res = AddMember( %john_smith ); -warn "Error adding John Smith, check your tests" unless $res; -$res = AddMember( %jane_doe ); -warn "Error adding Jane Doe, check your tests" unless $res; +$john_doe{borrowernumber} = AddMember( %john_doe ); +warn "Error adding John Doe, check your tests" unless $john_doe{borrowernumber}; +$john_smith{borrowernumber} = AddMember( %john_smith ); +warn "Error adding John Smith, check your tests" unless $john_smith{borrowernumber}; +$jane_doe{borrowernumber} = AddMember( %jane_doe ); +warn "Error adding Jane Doe, check your tests" unless $jane_doe{borrowernumber}; # Set common datatables params my %dt_params = ( @@ -168,6 +172,39 @@ is( $search_results->{ patrons }[1]->{ cardnumber }, $jane_doe{ cardnumber }, "Jane Doe is the second result"); +my $attribute_type = C4::Members::AttributeTypes->new( 'ATM_1', 'my attribute type' ); +$attribute_type->{staff_searchable} = 1; +$attribute_type->store; + + +C4::Members::Attributes::SetBorrowerAttributes( + $john_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for a common user' } ] +); +C4::Members::Attributes::SetBorrowerAttributes( + $jane_doe{borrowernumber}, [ { code => $attribute_type->{code}, value => 'the default value for another common user' } ] +); + +t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 1); +$search_results = C4::Utils::DataTables::Members::search({ + searchmember => "common user", + searchfieldstype => 'standard', + searchtype => 'contains', + branchcode => $branchcode, + dt_params => \%dt_params +}); + +is( $search_results->{ iTotalDisplayRecords}, 2, "There are 2 common users" ); + +t::lib::Mocks::mock_preference('ExtendedPatronAttributes', 0); +$search_results = C4::Utils::DataTables::Members::search({ + searchmember => "common user", + searchfieldstype => 'standard', + searchtype => 'contains', + branchcode => $branchcode, + dt_params => \%dt_params +}); +is( $search_results->{ iTotalDisplayRecords}, 0, "There are still 2 common users, but the patron attribute is not searchable " ); + $dbh->rollback; 1; -- 2.39.5