use Modern::Perl;
-use Test::More tests => 51;
+use Test::More tests => 50;
use Test::MockModule;
use Test::Exception;
use Koha::Holds;
use Koha::List::Patron;
use Koha::Patrons;
+use Koha::Patron::Relationship;
use t::lib::Mocks;
use t::lib::TestBuilder;
my $builder = t::lib::TestBuilder->new;
my $dbh = C4::Context->dbh;
-# Remove invalid guarantorid's as long as we have no FK
-$dbh->do("UPDATE borrowers b1 LEFT JOIN borrowers b2 ON b2.borrowernumber=b1.guarantorid SET b1.guarantorid=NULL where b1.guarantorid IS NOT NULL AND b2.borrowernumber IS NULL");
-
my $library1 = $builder->build({
source => 'Branch',
});
# XXX should be randomised and checked against the database
my $IMPOSSIBLE_CARDNUMBER = "XYZZZ999";
-#my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $branchprinter)= @_;
-my @USERENV = (
- 1,
- 'test',
- 'MASTERTEST',
- 'Test',
- 'Test',
- 't',
- 'Test',
- 0,
-);
-my $BRANCH_IDX = 5;
-
-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 = (
userid => 'tomasito'
);
-my $addmem=AddMember(%data);
-ok($addmem, "AddMember()");
+my $addmem=Koha::Patron->new(\%data)->store->borrowernumber;
+ok($addmem, "Koha::Patron->store()");
-my $member = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )
+my $patron = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )
or BAIL_OUT("Cannot read member with card $CARDNUMBER");
-$member = $member->unblessed;
+my $member = $patron->unblessed;
ok ( $member->{firstname} eq $FIRSTNAME &&
$member->{surname} eq $SURNAME &&
$member->{email} = $EMAIL;
$member->{phone} = $PHONE;
$member->{emailpro} = $EMAILPRO;
-ModMember(%$member);
+$patron->set($member)->store;
my $changedmember = Koha::Patrons->find( { cardnumber => $CARDNUMBER } )->unblessed;
ok ( $changedmember->{firstname} eq $CHANGED_FIRSTNAME &&
$changedmember->{email} eq $EMAIL &&
dateexpiry => '',
dateenrolled => '',
);
-my $borrowernumber = AddMember( %data );
-my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
-is( $borrower->{dateofbirth}, undef, 'AddMember should undef dateofbirth if empty string is given');
-is( $borrower->{debarred}, undef, 'AddMember should undef debarred if empty string is given');
-isnt( $borrower->{dateexpiry}, '0000-00-00', 'AddMember should not set dateexpiry to 0000-00-00 if empty string is given');
-isnt( $borrower->{dateenrolled}, '0000-00-00', 'AddMember should not set dateenrolled to 0000-00-00 if empty string is given');
-
-ModMember( borrowernumber => $borrowernumber, 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, 'ModMember should undef dateofbirth if empty string is given');
-is( $borrower->{debarred}, undef, 'ModMember should undef debarred if empty string is given');
-isnt( $borrower->{dateexpiry}, '0000-00-00', 'ModMember should not set dateexpiry to 0000-00-00 if empty string is given');
-isnt( $borrower->{dateenrolled}, '0000-00-00', 'ModMember should not set dateenrolled to 0000-00-00 if empty string is given');
+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');
-ModMember( borrowernumber => $borrowernumber, dateofbirth => '1970-01-01', debarred => '2042-01-01', dateexpiry => '9999-12-31', dateenrolled => '2015-09-06' );
+$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', 'ModMember should correctly set dateofbirth if a valid date is given');
-is( $borrower->{debarred}, '2042-01-01', 'ModMember should correctly set debarred if a valid date is given');
-is( $borrower->{dateexpiry}, '9999-12-31', 'ModMember should correctly set dateexpiry if a valid date is given');
-is( $borrower->{dateenrolled}, '2015-09-06', 'ModMember should correctly set dateenrolled if a valid date is given');
+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 'ModMember should not update userid if not true' => sub {
+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 = AddMember( %data );
- $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
+ $borrowernumber = Koha::Patron->new( \%data )->store->borrowernumber;
+ my $patron = Koha::Patrons->find( $borrowernumber );
+ my $borrower = $patron->unblessed;
- ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => '' );
+ $patron->set( { firstname => 'Tomas', userid => '' } )->store;
$borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
- is ( $borrower->{userid}, $data{userid}, 'ModMember should not update the userid with an empty string' );
- ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => 0 );
+ 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}, 'ModMember should not update the userid with an 0');
- ModMember( borrowernumber => $borrowernumber, firstname => 'Tomas', userid => undef );
+ 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}, 'ModMember should not update the userid with an undefined value');
+ is ( $borrower->{userid}, $data{userid}, 'Koha::Patron->store should not update the userid with an undefined value');
};
#Regression tests for bug 10612
categorycode=>'STAFFER',
branchcode => $library3->{branchcode},
dateexpiry => '2015-01-01',
- guarantorid=> undef,
},
});
my $bor1inlist = $borrower1->{borrowernumber};
categorycode=>'STAFFER',
branchcode => $library3->{branchcode},
dateexpiry => '2015-01-01',
- guarantorid=> undef,
},
});
categorycode=>'KIDclamp',
branchcode => $library3->{branchcode},
dateexpiry => '2015-01-01',
- guarantorid=> undef, # will be filled later
},
});
# IndependentBranches is off.
t::lib::Mocks::mock_preference('IndependentBranches', 0);
-my $owner = AddMember (categorycode => 'STAFFER', branchcode => $library2->{branchcode} );
+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 });
-my $patstodel = GetBorrowersToExpunge( {patron_list_id => $list1->patron_list_id() } );
+$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' );
+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() } );
$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 );
+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');
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=>'' );
+$relationship->delete();
$builder->build({
source => 'Issue',
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 => ['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;
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() } );
# 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', guarantorid => undef } } );
-$builder->build({ source => 'Borrower', value => { lastseen => '2016-02-02 02:02:02', categorycode => 'CIVILIAN', guarantorid => undef } } );
-$builder->build({ source => 'Borrower', value => { lastseen => '2016-03-03 03:03:03', categorycode => 'CIVILIAN', guarantorid => undef } } );
+$builder->build({ source => 'Borrower', value => { lastseen => '2016-01-01 01:01:01', categorycode => 'CIVILIAN' } } );
+$builder->build({ source => 'Borrower', value => { lastseen => '2016-02-02 02:02:02', categorycode => 'CIVILIAN' } } );
+$builder->build({ source => 'Borrower', value => { lastseen => '2016-03-03 03:03:03', categorycode => 'CIVILIAN' } } );
$patstodel = GetBorrowersToExpunge( { last_seen => '1999-12-12' });
is( scalar @$patstodel, 0, 'TrackLastPatronActivity - 0 patrons must be deleted' );
$patstodel = GetBorrowersToExpunge( { last_seen => '2016-02-15' });
## 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 = AddMember( categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} );
+$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 = AddMember( categorycode => $patron_category->{categorycode}, branchcode => $library2->{branchcode} );
-ok( $borrowernumber > 0, 'AddMember should have inserted the patron even if no userid is given' );
+$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 => 2;
+ 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) VALUES ('2014-01-01 01:02:03',0,?)", undef, (scalar localtime)."_$_");
+ $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' );
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');
+ 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");
+
};
sub _find_member {
return $found;
}
-# Regression tests for BZ15343
-my $password="";
-( $borrowernumber, $password ) = AddMember_Opac(surname=>"Dick",firstname=>'Philip',branchcode => $library2->{branchcode});
-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 = Koha::Patrons->find( $borrowernumber )->unblessed;
-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' );
-
-subtest 'Trivial test for AddMember_Auto' => sub {
- plan tests => 3;
- my $members_mock = Test::MockModule->new( 'C4::Members' );
- $members_mock->mock( 'fixup_cardnumber', sub { 12345; } );
- my $library = $builder->build({ source => 'Branch' });
- my $category = $builder->build({ source => 'Category' });
- my %borr = AddMember_Auto( surname=> 'Dick3', firstname => 'Philip', branchcode => $library->{branchcode}, categorycode => $category->{categorycode}, password => '34567890' );
- ok( $borr{borrowernumber}, 'Borrower hash contains borrowernumber' );
- is( $borr{cardnumber}, 12345, 'Borrower hash contains cardnumber' );
- my $patron = Koha::Patrons->find( $borr{borrowernumber} );
- isnt( $patron, undef, 'Patron found' );
-};
-
$schema->storage->txn_rollback;
-subtest 'AddMember (invalid categorycode) tests' => sub {
+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;
};
throws_ok
- { AddMember( %{ $patron_data } ); }
+ { Koha::Patron->new( $patron_data )->store; }
'Koha::Exceptions::Object::FKConstraint',
'AddMember raises an exception on invalid categorycode';