Bug 15009: QA follow-up
[koha.git] / t / db_dependent / Members.t
index 146d563..e369128 100755 (executable)
 
 use Modern::Perl;
 
-use Test::More tests => 56;
+use Test::More tests => 76;
 use Test::MockModule;
 use Data::Dumper;
 use C4::Context;
 use Koha::Database;
+use Koha::List::Patron;
 
+
+use t::lib::Mocks;
 use t::lib::TestBuilder;
 
 BEGIN {
@@ -130,7 +133,7 @@ ok ( $changedmember->{firstname} eq $CHANGED_FIRSTNAME &&
      , "Member Changed")
   or diag("Mismatching member details: ".Dumper($member, $changedmember));
 
-C4::Context->set_preference( 'CardnumberLength', '' );
+t::lib::Mocks::mock_preference( 'CardnumberLength', '' );
 C4::Context->clear_syspref_cache();
 
 my $checkcardnum=C4::Members::checkcardnumber($CARDNUMBER, "");
@@ -139,7 +142,7 @@ is ($checkcardnum, "1", "Card No. in use");
 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
 is ($checkcardnum, "0", "Card No. not used");
 
-C4::Context->set_preference( 'CardnumberLength', '4' );
+t::lib::Mocks::mock_preference( 'CardnumberLength', '4' );
 C4::Context->clear_syspref_cache();
 
 $checkcardnum=C4::Members::checkcardnumber($IMPOSSIBLE_CARDNUMBER, "");
@@ -147,13 +150,13 @@ is ($checkcardnum, "2", "Card number is too long");
 
 
 
-C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'OFF' );
+t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'OFF' );
 C4::Context->clear_syspref_cache();
 
 my $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
 is ($notice_email, $EMAIL, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is off");
 
-C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'emailpro' );
+t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'emailpro' );
 C4::Context->clear_syspref_cache();
 
 $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
@@ -228,6 +231,132 @@ $borrowernumber = AddMember( %data );
 $borrower = GetMember( borrowernumber => $borrowernumber );
 is( $borrower->{userid}, $data{userid}, 'AddMember should insert the given userid' );
 
+#Regression tests for bug 10612
+my $library3 = $builder->build({
+    source => 'Branch',
+});
+$builder->build({
+        source => 'Category',
+        value => {
+            categorycode         => 'STAFFER',
+            description          => 'Staff dont batch del',
+            category_type        => 'S',
+        },
+});
+
+$builder->build({
+        source => 'Category',
+        value => {
+            categorycode         => 'CIVILIAN',
+            description          => 'Civilian batch del',
+            category_type        => 'A',
+        },
+});
+
+$builder->build({
+        source => 'Category',
+        value => {
+            categorycode         => 'KIDclamp',
+            description          => 'Kid to be guaranteed',
+            category_type        => 'C',
+        },
+});
+
+#$builder->clear( { source => 'Borrower' } );
+my $borrower1 = $builder->build({
+        source => 'Borrower',
+        value  => {
+            categorycode=>'STAFFER',
+            branchcode => $library3->{branchcode},
+            dateexpiry => '2015-01-01',
+        },
+});
+my $bor1inlist = $borrower1->{borrowernumber};
+my $borrower2 = $builder->build({
+        source => 'Borrower',
+        value  => {
+            categorycode=>'STAFFER',
+            branchcode => $library3->{branchcode},
+            dateexpiry => '2015-01-01',
+        },
+});
+
+my $guarantee = $builder->build({
+        source => 'Borrower',
+        value  => {
+            categorycode=>'KIDclamp',
+            branchcode => $library3->{branchcode},
+            dateexpiry => '2015-01-01',
+        },
+});
+
+my $bor2inlist = $borrower2->{borrowernumber};
+
+$builder->build({
+        source => 'OldIssue',
+        value  => {
+            borrowernumber => $bor2inlist,
+            timestamp => '2016-01-01',
+        },
+});
+
+my $owner = AddMember (categorycode => 'STAFFER', branchcode => $library2->{branchcode} );
+my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
+my @listpatrons = ($bor1inlist, $bor2inlist);
+AddPatronsToList(  { list => $list1, borrowernumbers => \@listpatrons });
+my $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),0,'No staff deleted from list of all staff');
+ModMember( borrowernumber => $bor2inlist, categorycode => 'CIVILIAN' );
+$patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
+ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted from list');
+$patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by branchcode and list');
+$patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by expirationdate and list');
+$patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Staff patron not deleted by last issue date');
+
+ModMember( borrowernumber => $bor1inlist, categorycode => 'CIVILIAN' );
+ModMember( borrowernumber => $guarantee->{borrowernumber} ,guarantorid=>$bor1inlist );
+
+$patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
+ok( scalar(@$patstodel)== 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted from list');
+$patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by branchcode and list');
+$patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02', patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by expirationdate and list');
+$patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
+ok( scalar(@$patstodel) == 1 && $patstodel->[0]->{'borrowernumber'} eq $bor2inlist,'Guarantor patron not deleted by last issue date');
+ModMember( borrowernumber => $guarantee->{borrowernumber}, guarantorid=>'' );
+
+$builder->build({
+        source => 'Issue',
+        value  => {
+            borrowernumber => $bor2inlist,
+        },
+});
+$patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
+is( scalar(@$patstodel),1,'Borrower with issue not deleted from list');
+$patstodel = GetBorrowersToExpunge( {branchcode => $library3->{branchcode},patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),1,'Borrower with issue not deleted by branchcode and list');
+$patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),1,'Borrower with issue not deleted by category_code and list');
+$patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),1,'Borrower with issue not deleted by expiration_date and list');
+$builder->clear( { source => 'Issue' } );
+$patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id()} );
+ok( scalar(@$patstodel)== 2,'Borrowers without issue deleted from list');
+$patstodel = GetBorrowersToExpunge( {category_code => 'CIVILIAN',patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),2,'Borrowers without issues deleted by category_code and list');
+$patstodel = GetBorrowersToExpunge( {expired_before => '2015-01-02',patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),2,'Borrowers without issues deleted by expiration_date and list');
+$patstodel = GetBorrowersToExpunge( {not_borrowed_since => '2016-01-02', patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),2,'Borrowers without issues deleted by last issue date');
+
+
+
+
+
 # Regression tests for BZ13502
 ## Remove all entries with userid='' (should be only 1 max)
 $dbh->do(q|DELETE FROM borrowers WHERE userid = ''|);
@@ -244,6 +373,35 @@ ok( $borrower->{userid},  'A userid should have been generated correctly' );
 is( Check_Userid( C4::Context->config('user'), '' ), 0,
     'Check_Userid should return 0 for the DB user (Bug 12226)');
 
+subtest 'GetMemberAccountRecords' => sub {
+
+    plan tests => 2;
+
+    my $borrowernumber = $builder->build({ source => 'Borrower' })->{ borrowernumber };
+    my $accountline_1  = $builder->build({
+        source => 'Accountline',
+        value  => {
+            borrowernumber    => $borrowernumber,
+            amountoutstanding => 64.60
+        }
+    });
+
+    my ($total,undef,undef) = GetMemberAccountRecords( $borrowernumber );
+    is( $total , 64.60, "Rounding works correctly in total calculation (single value)" );
+
+    my $accountline_2 = $builder->build({
+        source => 'Accountline',
+        value  => {
+            borrowernumber    => $borrowernumber,
+            amountoutstanding => 10.65
+        }
+    });
+
+    ($total,undef,undef) = GetMemberAccountRecords( $borrowernumber );
+    is( $total , 75.25, "Rounding works correctly in total calculation (multiple values)" );
+
+};
+
 subtest 'GetMemberAccountBalance' => sub {
 
     plan tests => 10;
@@ -278,15 +436,15 @@ subtest 'GetMemberAccountBalance' => sub {
         'Expected 15 outstanding for both borrowernumber and cardnumber.');
 
     # do not count holds charges
-    C4::Context->set_preference( 'HoldsInNoissuesCharge', '1' );
-    C4::Context->set_preference( 'ManInvInNoissuesCharge', '0' );
+    t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '1' );
+    t::lib::Mocks::mock_preference( 'ManInvInNoissuesCharge', '0' );
     my ($total, $total_minus_charges,
         $other_charges) = C4::Members::GetMemberAccountBalance(123);
     is( $total, 15 , "Total calculated correctly");
     is( $total_minus_charges, 15, "Holds charges are not count if HoldsInNoissuesCharge=1");
     is( $other_charges, 0, "Holds charges are not considered if HoldsInNoissuesCharge=1");
 
-    C4::Context->set_preference( 'HoldsInNoissuesCharge', '0' );
+    t::lib::Mocks::mock_preference( 'HoldsInNoissuesCharge', '0' );
     ($total, $total_minus_charges,
         $other_charges) = C4::Members::GetMemberAccountBalance(123);
     is( $total, 15 , "Total calculated correctly");
@@ -308,8 +466,8 @@ subtest 'purgeSelfRegistration' => sub {
     #purge members in temporary category
     my $c= 'XYZ';
     $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')");
-    C4::Context->set_preference('PatronSelfRegistrationDefaultCategory', $c );
-    C4::Context->set_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360);
+    t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
+    t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360);
     C4::Members::DeleteExpiredOpacRegistrations();
     $dbh->do("INSERT INTO borrowers (surname, address, city, branchcode, categorycode, dateenrolled) VALUES ('Testaabbcc', 'Street 1', 'CITY', ?, '$c', '2014-01-01 01:02:03')", undef, $library1->{branchcode});
     is( C4::Members::DeleteExpiredOpacRegistrations(), 1, 'Test for DeleteExpiredOpacRegistrations');
@@ -409,4 +567,12 @@ sub testAgeAccessors {
     $member->{dateofbirth} = $original_dateofbirth; #It is polite to revert made changes in the unit tests.
 } #sub testAgeAccessors
 
+# regression test for bug 16009
+my $patron;
+eval {
+    my $patron = GetMember(cardnumber => undef);
+};
+is($@, '', 'Bug 16009: GetMember(cardnumber => undef) works');
+is($patron, undef, 'Bug 16009: GetMember(cardnumber => undef) returns undef');
+
 1;