#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
#
-# This is to test C4/Members
-# It requires a working Koha database with the sample data
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
-use strict;
-use warnings;
+use Modern::Perl;
-use Test::More tests => 24;
-use Data::Dumper;
+use Test::More tests => 54;
+use Test::MockModule;
+use Test::Exception;
+
+use Data::Dumper qw/Dumper/;
use C4::Context;
+use Koha::Database;
+use Koha::Holds;
+use Koha::List::Patron;
+use Koha::Patrons;
+use Koha::Patron::Relationship;
+
+use t::lib::Mocks;
+use t::lib::TestBuilder;
BEGIN {
- use_ok('C4::Members');
+ use_ok('C4::Members', qw( checkcardnumber GetBorrowersToExpunge DeleteUnverifiedOpacRegistrations DeleteExpiredOpacRegistrations ));
}
+my $schema = Koha::Database->schema;
+$schema->storage->txn_begin;
+my $builder = t::lib::TestBuilder->new;
my $dbh = C4::Context->dbh;
-# Start transaction
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
-
+my $library1 = $builder->build({
+ source => 'Branch',
+});
+my $library2 = $builder->build({
+ source => 'Branch',
+});
+my $patron_category = $builder->build({ source => 'Category' });
my $CARDNUMBER = 'TESTCARD01';
my $FIRSTNAME = 'Marie';
my $SURNAME = 'Mcknight';
-my $CATEGORYCODE = 'S';
-my $BRANCHCODE = 'CPL';
+my $BRANCHCODE = $library1->{branchcode};
my $CHANGED_FIRSTNAME = "Marry Ann";
my $EMAIL = "Marie\@email.com";
my $EMAILPRO = "Marie\@work.com";
-my $ETHNICITY = "German";
my $PHONE = "555-12123";
# XXX should be randomised and checked against the database
my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999";
-my $INDEPENDENT_BRANCHES_PREF = 'IndependentBranches';
-
-# XXX make a non-commit transaction and rollback rather than insert/delete
-
-#my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_;
-my @USERENV = (
- 1,
- 'test',
- 'MASTERTEST',
- 'Test',
- 'Test',
- 't',
- 'Test',
- 0,
-);
-my $BRANCH_IDX = 4;
-
-C4::Context->_new_userenv ('DUMMY_SESSION_ID');
-C4::Context->set_userenv ( @USERENV );
-
-my $userenv = C4::Context->userenv
- or BAIL_OUT("No userenv");
+t::lib::Mocks::mock_userenv();
# Make a borrower for testing
my %data = (
cardnumber => $CARDNUMBER,
- firstname => $FIRSTNAME,
+ firstname => $FIRSTNAME . q{ },
surname => $SURNAME,
- categorycode => $CATEGORYCODE,
+ categorycode => $patron_category->{categorycode},
branchcode => $BRANCHCODE,
- dateofbirth => ''
+ dateofbirth => '',
+ dateexpiry => '9999-12-31',
+ userid => 'tomasito'
);
-my $addmem=AddMember(%data);
-ok($addmem, "AddMember()");
+my $addmem=Koha::Patron->new(\%data)->store->borrowernumber;
+ok($addmem, "Koha::Patron->store()");
-my $member=GetMemberDetails("",$CARDNUMBER)
+my $patron = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )
or BAIL_OUT("Cannot read member with card $CARDNUMBER");
+my $member = $patron->unblessed;
ok ( $member->{firstname} eq $FIRSTNAME &&
$member->{surname} eq $SURNAME &&
- $member->{categorycode} eq $CATEGORYCODE &&
+ $member->{categorycode} eq $patron_category->{categorycode} &&
$member->{branchcode} eq $BRANCHCODE
, "Got member")
or diag("Mismatching member details: ".Dumper(\%data, $member));
is($member->{dateofbirth}, undef, "Empty dates handled correctly");
-$member->{firstname} = $CHANGED_FIRSTNAME;
+$member->{firstname} = $CHANGED_FIRSTNAME . q{ };
$member->{email} = $EMAIL;
-$member->{ethnicity} = $ETHNICITY;
$member->{phone} = $PHONE;
$member->{emailpro} = $EMAILPRO;
-ModMember(%$member);
-my $changedmember=GetMemberDetails("",$CARDNUMBER);
+$patron->set($member)->store;
+my $changedmember = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )->unblessed;
ok ( $changedmember->{firstname} eq $CHANGED_FIRSTNAME &&
$changedmember->{email} eq $EMAIL &&
- $changedmember->{ethnicity} eq $ETHNICITY &&
$changedmember->{phone} eq $PHONE &&
$changedmember->{emailpro} eq $EMAILPRO
, "Member Changed")
or diag("Mismatching member details: ".Dumper($member, $changedmember));
-C4::Context->set_preference( $INDEPENDENT_BRANCHES_PREF, '0' );
-C4::Context->clear_syspref_cache();
-
-my $results = Search($CARDNUMBER);
-ok (@$results == 1, "Search cardnumber returned only one result")
- or diag("Multiple members with Card $CARDNUMBER: ".Dumper($results));
-ok (_find_member($results), "Search cardnumber")
- or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
-
-my @searchstring=($SURNAME);
-$results = Search(\@searchstring);
-ok (_find_member($results), "Search (arrayref)")
- or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
-
-$results = Search(\@searchstring,undef,undef,undef,["surname"]);
-ok (_find_member($results), "Surname Search (arrayref)")
- or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
-
-$results = Search("$CHANGED_FIRSTNAME $SURNAME", "surname");
-ok (_find_member($results), "Full name Search (string)")
- or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
-
-@searchstring=($PHONE);
-$results = Search(\@searchstring,undef,undef,undef,["phone"]);
-ok (_find_member($results), "Phone Search (arrayref)")
- or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
-
-$results = Search($PHONE,undef,undef,undef,["phone"]);
-ok (_find_member($results), "Phone Search (string)")
- or diag("Card $CARDNUMBER not found in the resultset: ".Dumper($results));
-
-C4::Context->set_preference( $INDEPENDENT_BRANCHES_PREF, '1' );
-C4::Context->clear_syspref_cache();
-
-$results = Search("$CHANGED_FIRSTNAME $SURNAME", "surname");
-ok (!_find_member($results), "Full name Search (string) for independent branches, different branch")
- or diag("Card $CARDNUMBER found in the resultset for independent branches: ".Dumper(C4::Context->preference($INDEPENDENT_BRANCHES_PREF), $results));
-
-@searchstring=($SURNAME);
-$results = Search(\@searchstring);
-ok (!_find_member($results), "Search (arrayref) for independent branches, different branch")
- or diag("Card $CARDNUMBER found in the resultset for independent branches: ".Dumper(C4::Context->preference($INDEPENDENT_BRANCHES_PREF), $results));
-
-$USERENV[$BRANCH_IDX] = $BRANCHCODE;
-C4::Context->set_userenv ( @USERENV );
-
-$results = Search("$CHANGED_FIRSTNAME $SURNAME", "surname");
-ok (_find_member($results), "Full name Search (string) for independent branches, same branch")
- or diag("Card $CARDNUMBER not found in the resultset for independent branches: ".Dumper(C4::Context->preference($INDEPENDENT_BRANCHES_PREF), $results));
-
-@searchstring=($SURNAME);
-$results = Search(\@searchstring);
-ok (_find_member($results), "Search (arrayref) for independent branches, same branch")
- or diag("Card $CARDNUMBER not found in the resultset for independent branches: ".Dumper(C4::Context->preference($INDEPENDENT_BRANCHES_PREF), $results));
-
-C4::Context->set_preference( 'CardnumberLength', '' );
+t::lib::Mocks::mock_preference( 'CardnumberLength', '' );
C4::Context->clear_syspref_cache();
my $checkcardnum=C4::Members::checkcardnumber($CARDNUMBER, "");
$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, "");
is ($checkcardnum, "2", "Card number is too long");
-my $age=GetAge("1992-08-14", "2011-01-19");
-is ($age, "18", "Age correct");
-
-$age=GetAge("2011-01-19", "1992-01-19");
-is ($age, "-19", "Birthday In the Future");
-C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'OFF' );
-C4::Context->clear_syspref_cache();
+# Add a new borrower
+%data = (
+ cardnumber => "123456789",
+ firstname => "Tomasito",
+ surname => "None",
+ categorycode => $patron_category->{categorycode},
+ branchcode => $library2->{branchcode},
+ dateofbirth => '',
+ debarred => '',
+ dateexpiry => '',
+ dateenrolled => '',
+);
+my $borrowernumber = Koha::Patron->new( \%data )->store->borrowernumber;
+$patron = Koha::Patrons->find( $borrowernumber );
+my $borrower = $patron->unblessed;
+is( $borrower->{dateofbirth}, undef, 'Koha::Patron->store should undef dateofbirth if empty string is given');
+is( $borrower->{debarred}, undef, 'Koha::Patron->store should undef debarred if empty string is given');
+isnt( $borrower->{dateexpiry}, '0000-00-00', 'Koha::Patron->store should not set dateexpiry to 0000-00-00 if empty string is given');
+isnt( $borrower->{dateenrolled}, '0000-00-00', 'Koha::Patron->store should not set dateenrolled to 0000-00-00 if empty string is given');
+
+$patron->set({ dateofbirth => '', debarred => '', dateexpiry => '', dateenrolled => '' })->store;
+$borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+is( $borrower->{dateofbirth}, undef, 'Koha::Patron->store should undef dateofbirth if empty string is given');
+is( $borrower->{debarred}, undef, 'Koha::Patron->store should undef debarred if empty string is given');
+isnt( $borrower->{dateexpiry}, '0000-00-00', 'Koha::Patron->store should not set dateexpiry to 0000-00-00 if empty string is given');
+isnt( $borrower->{dateenrolled}, '0000-00-00', 'Koha::Patron->store should not set dateenrolled to 0000-00-00 if empty string is given');
+
+$patron->set({ dateofbirth => '1970-01-01', debarred => '2042-01-01', dateexpiry => '9999-12-31', dateenrolled => '2015-09-06' })->store;
+$borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+is( $borrower->{dateofbirth}, '1970-01-01', 'Koha::Patron->store should correctly set dateofbirth if a valid date is given');
+is( $borrower->{debarred}, '2042-01-01', 'Koha::Patron->store should correctly set debarred if a valid date is given');
+is( $borrower->{dateexpiry}, '9999-12-31', 'Koha::Patron->store should correctly set dateexpiry if a valid date is given');
+is( $borrower->{dateenrolled}, '2015-09-06', 'Koha::Patron->store should correctly set dateenrolled if a valid date is given');
+
+subtest 'Koha::Patron->store should not update userid if not true' => sub {
+ plan tests => 3;
+
+ # TODO Move this to t/db_dependent/Koha/Patrons.t subtest ->store
+
+ $data{ cardnumber } = "234567890";
+ $data{userid} = 'a_user_id';
+ $borrowernumber = Koha::Patron->new( \%data )->store->borrowernumber;
+ my $patron = Koha::Patrons->find( $borrowernumber );
+ my $borrower = $patron->unblessed;
+
+ $patron->set( { firstname => 'Tomas', userid => '' } )->store;
+ $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+ is ( $borrower->{userid}, $data{userid}, 'Koha::Patron->store should not update the userid with an empty string' );
+ $patron->set( { firstname => 'Tomas', userid => 0 } )->store;
+ $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+ is ( $borrower->{userid}, $data{userid}, 'Koha::Patron->store should not update the userid with an 0');
+ $patron->set( { firstname => 'Tomas', userid => undef } )->store;
+ $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+ is ( $borrower->{userid}, $data{userid}, 'Koha::Patron->store 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',
+ flags => undef,
+ },
+});
+my $bor1inlist = $borrower1->{borrowernumber};
+my $borrower2 = $builder->build({
+ source => 'Borrower',
+ value => {
+ categorycode=>'STAFFER',
+ branchcode => $library3->{branchcode},
+ dateexpiry => '2015-01-01',
+ flags => undef,
+ },
+});
+
+my $guarantee = $builder->build({
+ source => 'Borrower',
+ value => {
+ categorycode=>'KIDclamp',
+ branchcode => $library3->{branchcode},
+ dateexpiry => '2015-01-01',
+ flags => undef,
+ },
+});
+
+my $bor2inlist = $borrower2->{borrowernumber};
+
+$builder->build({
+ source => 'OldIssue',
+ value => {
+ borrowernumber => $bor2inlist,
+ timestamp => '2016-01-01',
+ },
+});
+
+# The following calls to GetBorrowersToExpunge are assuming that the pref
+# IndependentBranches is off.
+t::lib::Mocks::mock_preference('IndependentBranches', 0);
+
+my $anonymous_patron = Koha::Patron->new({ categorycode => 'CIVILIAN', branchcode => $library2->{branchcode} })->store->borrowernumber;
+t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous_patron);
+
+my $owner = Koha::Patron->new({ categorycode => 'STAFFER', branchcode => $library2->{branchcode} })->store->borrowernumber;
+my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } );
+
+AddPatronsToList( { list => $list1, borrowernumbers => [$anonymous_patron] } );
+my $patstodel = GetBorrowersToExpunge( { patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel), 0, 'Anonymous Patron not deleted from list' );
+
+my @listpatrons = ($bor1inlist, $bor2inlist);
+AddPatronsToList( { list => $list1, borrowernumbers => \@listpatrons });
+$patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),0,'No staff deleted from list of all staff');
+Koha::Patrons->find($bor2inlist)->set({ categorycode => 'CIVILIAN' })->store;
+$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');
+
+Koha::Patrons->find($bor1inlist)->set({ categorycode => 'CIVILIAN' })->store;
+
+t::lib::Mocks::mock_preference( 'borrowerRelationship', 'test' );
+
+my $relationship = Koha::Patron::Relationship->new( { guarantor_id => $bor1inlist, guarantee_id => $guarantee->{borrowernumber}, relationship => 'test' } )->store();
+
+$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');
+$relationship->delete();
+
+$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( {category_code => [], patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),1,'category_code can contain an empty arrayref');
+$patstodel = GetBorrowersToExpunge( {category_code => ['CIVILIAN','STAFFER'],patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),1,'Borrower with issue not deleted by multiple 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( {category_code => ['CIVILIAN','STAFFER'],patron_list_id => $list1->patron_list_id() } );
+is( scalar(@$patstodel),2,'Borrowers without issues deleted by multiple 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');
+
+# Test GetBorrowersToExpunge and TrackLastPatronActivity
+$dbh->do(q|UPDATE borrowers SET lastseen=NULL|);
+$builder->build({
+ source => 'Borrower',
+ value => {
+ lastseen => '2016-01-01 01:01:01',
+ categorycode => 'CIVILIAN',
+ flags => undef,
+ }
+});
+$builder->build({
+ source => 'Borrower',
+ value => {
+ lastseen => '2016-02-02 02:02:02',
+ categorycode => 'CIVILIAN',
+ flags => undef,
+ }
+});
+$builder->build({
+ source => 'Borrower',
+ value => {
+ lastseen => '2016-03-03 03:03:03',
+ categorycode => 'CIVILIAN',
+ flags => undef,
+ }
+});
+$patstodel = GetBorrowersToExpunge( { last_seen => '1999-12-12' });
+is( scalar @$patstodel, 0, 'TrackLastPatronActivity - 0 patrons must be deleted' );
+$patstodel = GetBorrowersToExpunge( { last_seen => '2016-02-15' });
+is( scalar @$patstodel, 2, 'TrackLastPatronActivity - 2 patrons must be deleted' );
+$patstodel = GetBorrowersToExpunge( { last_seen => '2016-04-04' });
+is( scalar @$patstodel, 3, 'TrackLastPatronActivity - 3 patrons must be deleted' );
+my $patron2 = $builder->build({
+ source => 'Borrower',
+ value => {
+ lastseen => undef,
+ flags => undef,
+ }
+});
+t::lib::Mocks::mock_preference( 'TrackLastPatronActivity', '0' );
+Koha::Patrons->find( $patron2->{borrowernumber} )->track_login;
+is( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should not be changed' );
+Koha::Patrons->find( $patron2->{borrowernumber} )->track_login({ force => 1 });
+isnt( Koha::Patrons->find( $patron2->{borrowernumber} )->lastseen, undef, 'Lastseen should be changed now' );
+
+# Test GetBorrowersToExpunge and regular patron with permission
+$builder->build({
+ source => 'Category',
+ value => {
+ categorycode => 'SMALLSTAFF',
+ description => 'Small staff',
+ category_type => 'A',
+ },
+});
+$borrowernumber = Koha::Patron->new({
+ categorycode => 'SMALLSTAFF',
+ branchcode => $library2->{branchcode},
+ flags => undef,
+})->store->borrowernumber;
+$patron = Koha::Patrons->find( $borrowernumber );
+$patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
+is( scalar @$patstodel, 1, 'Regular patron with flags=undef can be deleted' );
+$patron->set({ flags => 0 })->store;
+$patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
+is( scalar @$patstodel, 1, 'Regular patron with flags=0 can be deleted' );
+$patron->set({ flags => 4 })->store;
+$patstodel = GetBorrowersToExpunge( {category_code => 'SMALLSTAFF' } );
+is( scalar @$patstodel, 0, 'Regular patron with flags>0 can not be deleted' );
+
+# Regression tests for BZ13502
+## Remove all entries with userid='' (should be only 1 max)
+$dbh->do(q|DELETE FROM borrowers WHERE userid = ''|);
+## And create a patron with a userid=''
+$borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
+$dbh->do(q|UPDATE borrowers SET userid = '' WHERE borrowernumber = ?|, undef, $borrowernumber);
+# Create another patron and verify the userid has been generated
+$borrowernumber = Koha::Patron->new({ categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} })->store->borrowernumber;
+ok( $borrowernumber > 0, 'Koha::Patron->store should have inserted the patron even if no userid is given' );
+$borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+ok( $borrower->{userid}, 'A userid should have been generated correctly' );
+
+subtest 'purgeSelfRegistration' => sub {
+ plan tests => 5;
+
+ #purge unverified
+ my $d=360;
+ C4::Members::DeleteUnverifiedOpacRegistrations($d);
+ foreach(1..3) {
+ $dbh->do("INSERT INTO borrower_modifications (timestamp, borrowernumber, verification_token, changed_fields) VALUES ('2014-01-01 01:02:03',0,?,'firstname,surname')", undef, (scalar localtime)."_$_");
+ }
+ is( C4::Members::DeleteUnverifiedOpacRegistrations($d), 3, 'Test for DeleteUnverifiedOpacRegistrations' );
+
+ #purge members in temporary category
+ my $c= 'XYZ';
+ $dbh->do("INSERT IGNORE INTO categories (categorycode) VALUES ('$c')");
+ t::lib::Mocks::mock_preference('PatronSelfRegistrationDefaultCategory', $c );
+ t::lib::Mocks::mock_preference('PatronSelfRegistrationExpireTemporaryAccountsDelay', 360);
+ C4::Members::DeleteExpiredOpacRegistrations();
+ my $self_reg = $builder->build_object({
+ class => 'Koha::Patrons',
+ value => {
+ dateenrolled => '2014-01-01 01:02:03',
+ categorycode => $c
+ }
+ });
+
+ my $checkout = $builder->build_object({
+ class=>'Koha::Checkouts',
+ value=>{
+ borrowernumber=>$self_reg->borrowernumber
+ }
+ });
+ is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with checkout");
+
+ my $account_line = $builder->build_object(
+ {
+ class => 'Koha::Account::Lines',
+ value => {
+ borrowernumber => $self_reg->borrowernumber,
+ amountoutstanding => 5,
+ acocunttype => undef
+ }
+ }
+ );
+ is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with checkout and fine");
+
+ $checkout->delete;
+ is( C4::Members::DeleteExpiredOpacRegistrations(), 0, "DeleteExpiredOpacRegistrations doesn't delete borrower with fine and no checkout");
+
+ $account_line->delete;
+ is( C4::Members::DeleteExpiredOpacRegistrations(), 1, "DeleteExpiredOpacRegistrations does delete borrower with no fines and no checkouts");
+
+};
-my $notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
-is ($notice_email, $EMAIL, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is off");
+sub _find_member {
+ my ($resultset) = @_;
+ my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
+ return $found;
+}
-C4::Context->set_preference( 'AutoEmailPrimaryAddress', 'emailpro' );
-C4::Context->clear_syspref_cache();
+$schema->storage->txn_rollback;
-$notice_email = GetNoticeEmailAddress($member->{'borrowernumber'});
-is ($notice_email, $EMAILPRO, "GetNoticeEmailAddress returns correct value when AutoEmailPrimaryAddress is emailpro");
+subtest 'Koha::Patron->store (invalid categorycode) tests' => sub {
+ plan tests => 1;
+ # TODO Move this to t/db_dependent/Koha/Patrons.t subtest ->store
+ $schema->storage->txn_begin;
-# clean up
-DelMember($member->{borrowernumber});
-$results = Search($CARDNUMBER,undef,undef,undef,["cardnumber"]);
-ok (!_find_member($results), "Delete member")
- or diag("Card $CARDNUMBER found for the deleted member in the resultset: ".Dumper($results));
+ my $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
+ my $category_id = $category->id;
+ # Remove category to make sure the id is not on the DB
+ $category->delete;
+ my $patron_data = {
+ categorycode => $category_id
+ };
-exit;
+ throws_ok
+ { Koha::Patron->new( $patron_data )->store; }
+ 'Koha::Exceptions::Object::FKConstraint',
+ 'AddMember raises an exception on invalid categorycode';
-sub _find_member {
- my ($resultset) = @_;
- my $found = $resultset && grep( { $_->{cardnumber} && $_->{cardnumber} eq $CARDNUMBER } @$resultset );
- return $found;
-}
+ $schema->storage->txn_rollback;
+};