Bug 12833: Patron search should search on extended attributes
authorJonathan Druart <jonathan.druart@biblibre.com>
Thu, 28 Aug 2014 08:51:03 +0000 (10:51 +0200)
committerTomas Cohen Arazi <tomascohen@gmail.com>
Tue, 9 Sep 2014 13:08:59 +0000 (10:08 -0300)
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 <nick@quecheelibrary.org>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
'Searchable' patron attributes can now be searched for again.
Works as described, passes stests and QA script.

Signed-off-by: Tomas Cohen Arazi <tomascohen@gmail.com>
C4/Utils/DataTables/Members.pm
svc/members/search
t/db_dependent/Utils/Datatables_Members.t

index aa0f838..95f6a82 100644 (file)
@@ -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;
     }
index 1b3a02a..e0b64a5 100755 (executable)
@@ -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,
index 1c85423..e0deaae 100644 (file)
 
 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;