Bug 15878 - Updated unit tests for hbyymmincr barcodes
[koha.git] / t / db_dependent / Members.t
index 13e6b57..797e9bc 100755 (executable)
 
 use Modern::Perl;
 
-use Test::More tests => 76;
+use Test::More tests => 77;
 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'});
@@ -164,50 +167,6 @@ ModMember(borrowernumber => $member->{'borrowernumber'}, dateexpiry => '2001-01-
 $member = GetMemberDetails($member->{'borrowernumber'});
 ok($member->{is_expired}, "GetMemberDetails() indicates that patron is expired");
 
-
-my $message_type = 'B';
-my $message = 'my message';
-my $messages_count = GetMessagesCount($member->{borrowernumber}, $message_type, $BRANCHCODE);
-is( $messages_count, 0, 'GetMessagesCount returns the number of messages correclty' );
-
-is( AddMessage(), undef, 'AddMessage without argument returns undef' );
-is( AddMessage(undef, $message_type, $message, $BRANCHCODE), undef,  'AddMessage without the borrower number returns undef' );
-is( AddMessage($member->{borrowernumber}, undef, $message, $BRANCHCODE), undef,  'AddMessage without the message type returns undef' );
-is( AddMessage($member->{borrowernumber}, $message_type, undef, $BRANCHCODE), undef,  'AddMessage without the message returns undef' );
-is( AddMessage($member->{borrowernumber}, $message_type, $message, undef), undef,  'AddMessage without the branch code returns undef' );
-is( AddMessage($member->{borrowernumber}, $message_type, $message, $BRANCHCODE), 1,  'AddMessage functions correctly' );
-
-$messages_count = GetMessagesCount();
-is( $messages_count, 0, 'GetMessagesCount without argument returns 0' );
-$messages_count = GetMessagesCount(undef, $message_type, $BRANCHCODE);
-is( $messages_count, '0', 'GetMessagesCount without the borrower number returns the number of messages' );
-$messages_count = GetMessagesCount($member->{borrowernumber}, undef, $BRANCHCODE);
-is( $messages_count, '1', 'GetMessagesCount without the message type returns the total number of messages' );
-$messages_count = GetMessagesCount($member->{borrowernumber}, $message_type, undef);
-is( $messages_count, '1', 'GetMessagesCount without the branchcode returns the total number of messages' );
-$messages_count = GetMessagesCount($member->{borrowernumber}, $message_type, $BRANCHCODE);
-is( $messages_count, '1', 'GetMessagesCount returns the number of messages correctly' );
-
-my $messages = GetMessages();
-is( @$messages, 0, 'GetMessages without argument returns 0' );
-$messages = GetMessages($member->{borrowernumber}, $message_type, $BRANCHCODE);
-is( @$messages, 1, 'GetMessages returns the correct number of messages' );
-is( $messages->[0]->{borrowernumber}, $member->{borrowernumber}, 'GetMessages returns the borrower number correctly' );
-is( $messages->[0]->{message_type}, $message_type, 'GetMessages returns the message type correclty' );
-is( $messages->[0]->{message}, $message, 'GetMessages returns the message correctly' );
-is( $messages->[0]->{branchcode}, $BRANCHCODE, 'GetMessages returns the branch code correctly' );
-
-$messages_count = GetMessagesCount($member->{borrowernumber}, $message_type, $BRANCHCODE);
-is( $messages_count, 1, 'GetMessagesCount returns the number of messages correclty' );
-
-DeleteMessage();
-$messages = GetMessages($member->{borrowernumber}, $message_type, $BRANCHCODE);
-is( @$messages, 1, 'DeleteMessage without message id does not delete messages' );
-DeleteMessage($messages->[0]->{message_id});
-$messages = GetMessages($member->{borrowernumber}, $message_type, $BRANCHCODE);
-is( @$messages, 0, 'DeleteMessage deletes a message correctly' );
-
-
 # clean up 
 DelMember($member->{borrowernumber});
 my $borrower = GetMember( cardnumber => $CARDNUMBER );
@@ -272,6 +231,144 @@ $borrowernumber = AddMember( %data );
 $borrower = GetMember( borrowernumber => $borrowernumber );
 is( $borrower->{userid}, $data{userid}, 'AddMember should insert the given userid' );
 
+subtest 'ModMember should not update userid if not true' => sub {
+    plan tests => 3;
+    ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => '' );
+    $borrower = GetMember( borrowernumber => $borrowernumber );
+    is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an empty string' );
+    ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => 0 );
+    $borrower = GetMember( borrowernumber => $borrowernumber );
+    is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an 0');
+    ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => undef );
+    $borrower = GetMember( borrowernumber => $borrowernumber );
+    is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an undefined value');
+};
+
+#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',
+        },
+});
+
+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->schema->resultset( 'Issue' )->delete_all;
+$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 = ''|);
@@ -288,6 +385,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;
@@ -322,15 +448,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");
@@ -352,8 +478,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');
@@ -371,6 +497,9 @@ my $password="";
 is( $password =~ /^[a-zA-Z]{10}$/ , 1, 'Test for autogenerated password if none submitted');
 ( $borrowernumber, $password ) = AddMember_Opac(surname=>"Deckard",firstname=>"Rick",password=>"Nexus-6",branchcode => $library2->{branchcode});
 is( $password eq "Nexus-6", 1, 'Test password used if submitted');
+$borrower = GetMember(borrowernumber => $borrowernumber);
+my $hashed_up =  Koha::AuthUtils::hash_password("Nexus-6", $borrower->{password});
+is( $borrower->{password} eq $hashed_up, 1, 'Check password hash equals hash of submitted password' );
 
 
 
@@ -450,4 +579,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;