Bug 15009: QA follow-up
[koha.git] / t / db_dependent / Members.t
index f0b80fe..e369128 100755 (executable)
 
 use Modern::Perl;
 
-use Test::More tests => 58;
+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;
@@ -229,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 = ''|);
@@ -245,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;