use Modern::Perl;
-use Test::More tests => 40;
+use Test::More tests => 44;
use Test::Warn;
use Test::Exception;
use Test::MockModule;
use Time::Fake;
use DateTime;
use JSON;
-use Data::Dumper;
use utf8;
-use C4::Circulation;
+use C4::Circulation qw( AddIssue AddReturn );
use C4::Biblio;
-use C4::Auth qw(checkpw_hash);
+use C4::Auth qw( checkpw checkpw_hash );
use Koha::ActionLogs;
use Koha::Holds;
use Koha::Old::Holds;
use Koha::Patrons;
use Koha::Old::Patrons;
+use Koha::Patron::Attribute::Types;
use Koha::Patron::Categories;
use Koha::Patron::Relationship;
use Koha::Database;
-use Koha::DateUtils;
+use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::Virtualshelf;
use Koha::Virtualshelves;
+use Koha::Notice::Messages;
use t::lib::TestBuilder;
use t::lib::Mocks;
is( ref($retrieved_patron_1->library), 'Koha::Library', 'Koha::Patron->library should return a Koha::Library object' );
};
+subtest 'sms_provider' => sub {
+ plan tests => 3;
+ my $sms_provider = $builder->build({source => 'SmsProvider' });
+ is( $retrieved_patron_1->sms_provider, undef, '->sms_provider should return undef if none defined' );
+ $retrieved_patron_1->sms_provider_id( $sms_provider->{id} )->store;
+ is_deeply( $retrieved_patron_1->sms_provider->unblessed, $sms_provider, 'Koha::Patron->sms_provider returns the correct SMS provider' );
+ is( ref($retrieved_patron_1->sms_provider), 'Koha::SMS::Provider', 'Koha::Patron->sms_provider should return a Koha::SMS::Provider object' );
+};
+
subtest 'guarantees' => sub {
- plan tests => 13;
+
+ plan tests => 9;
t::lib::Mocks::mock_preference( 'borrowerRelationship', 'test|test2' );
my $guarantees = $new_patron_1->guarantee_relationships;
is( ref($guarantees), 'Koha::Patron::Relationships', 'Koha::Patron->guarantees should return a Koha::Patrons result set in a scalar context' );
is( $guarantees->count, 0, 'new_patron_1 should have 0 guarantee relationships' );
- my @guarantees = $new_patron_1->guarantee_relationships;
- is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantee_relationships should return an array in a list context' );
- is( scalar(@guarantees), 0, 'new_patron_1 should have 0 guarantee' );
my $guarantee_1 = $builder->build({ source => 'Borrower' });
my $relationship_1 = Koha::Patron::Relationship->new( { guarantor_id => $new_patron_1->id, guarantee_id => $guarantee_1->{borrowernumber}, relationship => 'test' } )->store();
$guarantees = $new_patron_1->guarantee_relationships;
is( ref($guarantees), 'Koha::Patron::Relationships', 'Koha::Patron->guarantee_relationships should return a Koha::Patrons result set in a scalar context' );
is( $guarantees->count, 2, 'new_patron_1 should have 2 guarantees' );
- @guarantees = $new_patron_1->guarantee_relationships;
- is( ref(\@guarantees), 'ARRAY', 'Koha::Patron->guarantee_relationships should return an array in a list context' );
- is( scalar(@guarantees), 2, 'new_patron_1 should have 2 guarantees' );
- $_->delete for @guarantees;
+
+ $guarantees->delete;
#Test return order of guarantees BZ 18635
my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
};
subtest 'siblings' => sub {
- plan tests => 7;
+
+ plan tests => 6;
+
my $siblings = $new_patron_1->siblings;
is( $siblings, undef, 'Koha::Patron->siblings should not crashed if the patron has no guarantor' );
my $guarantee_1 = $builder->build( { source => 'Borrower' } );
my $retrieved_guarantee_1 = Koha::Patrons->find($guarantee_1);
$siblings = $retrieved_guarantee_1->siblings;
is( ref($siblings), 'Koha::Patrons', 'Koha::Patron->siblings should return a Koha::Patrons result set in a scalar context' );
- my @siblings = $retrieved_guarantee_1->siblings;
- is( ref( \@siblings ), 'ARRAY', 'Koha::Patron->siblings should return an array in a list context' );
is( $siblings->count, 0, 'guarantee_1 should not have siblings yet' );
my $guarantee_2 = $builder->build( { source => 'Borrower' } );
my $relationship_2 = Koha::Patron::Relationship->new( { guarantor_id => $new_patron_1->borrowernumber, guarantee_id => $guarantee_2->{borrowernumber}, relationship => 'test' } )->store();
subtest 'has_overdues' => sub {
plan tests => 3;
- my $biblioitem_1 = $builder->build( { source => 'Biblioitem' } );
- my $item_1 = $builder->build(
- { source => 'Item',
- value => {
- homebranch => $library->{branchcode},
- holdingbranch => $library->{branchcode},
- notforloan => 0,
- itemlost => 0,
- withdrawn => 0,
- biblionumber => $biblioitem_1->{biblionumber}
- }
- }
- );
+ my $item_1 = $builder->build_sample_item;
my $retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
is( $retrieved_patron->has_overdues, 0, );
my $tomorrow = DateTime->today( time_zone => C4::Context->tz() )->add( days => 1 );
- my $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $tomorrow, branchcode => $library->{branchcode} })->store();
+ my $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->itemnumber, date_due => $tomorrow, branchcode => $library->{branchcode} })->store();
is( $retrieved_patron->has_overdues, 0, );
$issue->delete();
my $yesterday = DateTime->today(time_zone => C4::Context->tz())->add( days => -1 );
- $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->{itemnumber}, date_due => $yesterday, branchcode => $library->{branchcode} })->store();
+ $issue = Koha::Checkout->new({ borrowernumber => $new_patron_1->id, itemnumber => $item_1->itemnumber, date_due => $yesterday, branchcode => $library->{branchcode} })->store();
$retrieved_patron = Koha::Patrons->find( $new_patron_1->borrowernumber );
is( $retrieved_patron->has_overdues, 1, );
$issue->delete();
{ # Testing invalid is going to expiry date
t::lib::Mocks::mock_preference('NotifyBorrowerDeparture', 30);
# mock_config does not work here, because of tz vs timezone subroutines
- my $context = new Test::MockModule('C4::Context');
+ my $context = Test::MockModule->new('C4::Context');
$context->mock( 'tz', sub {
'America/Sao_Paulo';
});
};
subtest "delete" => sub {
- plan tests => 6;
+ plan tests => 13;
t::lib::Mocks::mock_preference( 'BorrowersLog', 1 );
- my $patron = $builder->build( { source => 'Borrower' } );
- my $retrieved_patron = Koha::Patrons->find( $patron->{borrowernumber} );
- my $hold = $builder->build(
- { source => 'Reserve',
- value => { borrowernumber => $patron->{borrowernumber} }
- }
- );
- my $list = $builder->build(
- { source => 'Virtualshelve',
- value => { owner => $patron->{borrowernumber} }
- }
- );
+ t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'transfer' );
+ Koha::Virtualshelves->delete;
+
+ my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+ my $patron_for_sharing = $builder->build_object({ class => 'Koha::Patrons' });
+ my $staff_patron = $builder->build_object({ class => 'Koha::Patrons' });
+ t::lib::Mocks::mock_userenv({ patron => $staff_patron });
+
+ my $hold = $builder->build_object({ class => 'Koha::Holds', value => { borrowernumber => $patron->borrowernumber } });
+ my $modification = $builder->build_object({ class => 'Koha::Patron::Modifications', value => { borrowernumber => $patron->borrowernumber } });
+ my $private_list = Koha::Virtualshelf->new({
+ shelfname => "private",
+ owner => $patron->borrowernumber,
+ public => 0,
+ })->store;
+ my $public_list = Koha::Virtualshelf->new({
+ shelfname => "public",
+ owner => $patron->borrowernumber,
+ public => 1,
+ })->store;
+ my $list_to_share = Koha::Virtualshelf->new({
+ shelfname => "shared",
+ owner => $patron->borrowernumber,
+ public => 0,
+ })->store;
- my $deleted = $retrieved_patron->delete;
+ $list_to_share->share("valid key")->accept( "valid key", $patron_for_sharing->borrowernumber );
+ $list_to_share->share("valid key")->accept( "valid key", $staff_patron->borrowernumber ); # this share should be removed at deletion too
+ my $deleted = $patron->delete;
is( ref($deleted), 'Koha::Patron', 'Koha::Patron->delete should return the deleted patron object if the patron has been correctly deleted' );
+ ok( $patron->borrowernumber, 'Still have the deleted borrowernumber' );
- is( Koha::Patrons->find( $patron->{borrowernumber} ), undef, 'Koha::Patron->delete should have deleted the patron' );
+ is( Koha::Patrons->find( $patron->borrowernumber ), undef, 'Koha::Patron->delete should have deleted the patron' );
- is (Koha::Old::Holds->search( { reserve_id => $hold->{ reserve_id } } )->count, 1, q|Koha::Patron->delete should have cancelled patron's holds| );
+ is (Koha::Old::Holds->search({ reserve_id => $hold->reserve_id })->count, 1, q|Koha::Patron->delete should have cancelled patron's holds| );
- is( Koha::Holds->search( { borrowernumber => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have cancelled patron's holds 2| );
+ is( Koha::Holds->search( { borrowernumber => $patron->borrowernumber } )->count, 0, q|Koha::Patron->delete should have cancelled patron's holds 2| );
- is( Koha::Virtualshelves->search( { owner => $patron->{borrowernumber} } )->count, 0, q|Koha::Patron->delete should have deleted patron's lists| );
+ my $transferred_lists = Koha::Virtualshelves->search({ owner => $staff_patron->borrowernumber })->count;
+ is( $transferred_lists, 2, 'Public and shared lists should stay in database under a different owner with a unique name, while private lists delete, with ListOwnershipPatronDeletion set to Transfer');
+ is( Koha::Virtualshelfshares->search({ borrowernumber => $staff_patron->borrowernumber })->count, 0, "New owner of list should have shares removed" );
+ is( Koha::Virtualshelfshares->search({ borrowernumber => $patron_for_sharing->borrowernumber })->count, 1, "But the other share is still there" );
+ is( Koha::Virtualshelves->search({ owner => $patron->borrowernumber })->count, 0, q|Koha::Patron->delete should have deleted patron's lists/removed their ownership| );
- my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $retrieved_patron->borrowernumber } )->count;
+ is( Koha::Patron::Modifications->search( { borrowernumber => $patron->borrowernumber } )->count, 0, q|Koha::Patron->delete should have deleted patron's modifications| );
+
+ my $number_of_logs = $schema->resultset('ActionLog')->search( { module => 'MEMBERS', action => 'DELETE', object => $patron->borrowernumber } )->count;
is( $number_of_logs, 1, 'With BorrowerLogs, Koha::Patron->delete should have logged' );
+
+ t::lib::Mocks::mock_preference( 'ListOwnershipUponPatronDeletion', 'delete' );
+ Koha::Virtualshelves->delete;
+ my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
+ my $private_list2 = Koha::Virtualshelf->new({
+ shelfname => "private",
+ owner => $patron2->borrowernumber,
+ public => 0,
+ })->store;
+ my $public_list2 = Koha::Virtualshelf->new({
+ shelfname => "public",
+ owner => $patron2->borrowernumber,
+ public => 1,
+ })->store;
+ my $list_to_share2 = Koha::Virtualshelf->new({
+ shelfname => "shared",
+ owner => $patron2->borrowernumber,
+ public => 0,
+ })->store;
+ $list_to_share2->share("valid key")->accept( "valid key", $patron_for_sharing->borrowernumber );
+
+ # Delete patron2, check if shelves and shares are now empty
+ $patron2->delete;
+ is( Koha::Virtualshelves->count, 0, 'All lists should be gone now' );
+ is( Koha::Virtualshelfshares->count, 0, 'All shares should be gone too' );
};
subtest 'Koha::Patrons->delete' => sub {
$patron->delete;
};
-subtest 'checkouts + pending_checkouts + get_overdues + old_checkouts' => sub {
+subtest 'checkouts + pending_checkouts + overdues + old_checkouts' => sub {
plan tests => 17;
my $library = $builder->build( { source => 'Branch' } );
- my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
- my $item_1 = $builder->build(
+ my $biblionumber_1 = $builder->build_sample_biblio->biblionumber;
+ my $item_1 = $builder->build_sample_item(
{
- source => 'Item',
- value => {
- homebranch => $library->{branchcode},
- holdingbranch => $library->{branchcode},
- biblionumber => $biblionumber_1,
- itemlost => 0,
- withdrawn => 0,
- }
+ library => $library->{branchcode},
+ biblionumber => $biblionumber_1,
}
);
- my $item_2 = $builder->build(
+ my $item_2 = $builder->build_sample_item(
{
- source => 'Item',
- value => {
- homebranch => $library->{branchcode},
- holdingbranch => $library->{branchcode},
- biblionumber => $biblionumber_1,
- itemlost => 0,
- withdrawn => 0,
- }
+ library => $library->{branchcode},
+ biblionumber => $biblionumber_1,
}
);
- my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
- my $item_3 = $builder->build(
+ my $biblionumber_2 = $builder->build_sample_biblio->biblionumber;
+ my $item_3 = $builder->build_sample_item(
{
- source => 'Item',
- value => {
- homebranch => $library->{branchcode},
- holdingbranch => $library->{branchcode},
- biblionumber => $biblionumber_2,
- itemlost => 0,
- withdrawn => 0,
- }
+ library => $library->{branchcode},
+ biblionumber => $biblionumber_2,
}
);
my $patron = $builder->build(
t::lib::Mocks::mock_userenv({ branchcode => $library->{branchcode} });
- AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
- AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
- AddIssue( $patron, $item_3->{barcode} );
+ AddIssue( $patron, $item_1->barcode, DateTime->now->subtract( days => 1 ) );
+ AddIssue( $patron, $item_2->barcode, DateTime->now->subtract( days => 5 ) );
+ AddIssue( $patron, $item_3->barcode );
$patron = Koha::Patrons->find( $patron->{borrowernumber} );
$checkouts = $patron->checkouts;
is( ref($pending_checkouts), 'Koha::Checkouts', 'pending_checkouts should return a Koha::Checkouts object' );
my $first_checkout = $pending_checkouts->next;
- is( $first_checkout->unblessed_all_relateds->{biblionumber}, $item_3->{biblionumber}, 'pending_checkouts should prefetch values from other tables (here biblio)' );
+ is( $first_checkout->unblessed_all_relateds->{biblionumber}, $item_3->biblionumber, 'pending_checkouts should prefetch values from other tables (here biblio)' );
- my $overdues = $patron->get_overdues;
+ my $overdues = $patron->overdues;
is( $overdues->count, 2, 'Patron should have 2 overdues');
- is( ref($overdues), 'Koha::Checkouts', 'Koha::Patron->get_overdues should return Koha::Checkouts' );
- is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' );
- is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' );
+ is( ref($overdues), 'Koha::Checkouts', 'Koha::Patron->overdues should return Koha::Checkouts' );
+ is( $overdues->next->itemnumber, $item_1->itemnumber, 'The issue should be returned in the same order as they have been done, first is correct' );
+ is( $overdues->next->itemnumber, $item_2->itemnumber, 'The issue should be returned in the same order as they have been done, second is correct' );
- C4::Circulation::AddReturn( $item_1->{barcode} );
- C4::Circulation::AddReturn( $item_2->{barcode} );
+ C4::Circulation::AddReturn( $item_1->barcode );
+ C4::Circulation::AddReturn( $item_2->barcode );
$old_checkouts = $patron->old_checkouts;
is( $old_checkouts->count, 2, 'old_checkouts should return 2 old checkouts that patron' );
is( ref($old_checkouts), 'Koha::Old::Checkouts', 'old_checkouts should return a Koha::Old::Checkouts object' );
};
subtest 'get_age' => sub {
- plan tests => 7;
+ plan tests => 31;
my $patron = $builder->build( { source => 'Borrower' } );
$patron = Koha::Patrons->find( $patron->{borrowernumber} );
- my $today = dt_from_string;
+ my @dates = (
+ {
+ today => '2020-02-28',
+ has_12 => { date => '2007-08-27', expected_age => 12 },
+ almost_18 => { date => '2002-03-01', expected_age => 17 },
+ has_18_today => { date => '2002-02-28', expected_age => 18 },
+ had_18_yesterday => { date => '2002-02-27', expected_age => 18 },
+ almost_16 => { date => '2004-02-29', expected_age => 15 },
+ has_16_today => { date => '2004-02-28', expected_age => 16 },
+ had_16_yesterday => { date => '2004-02-27', expected_age => 16 },
+ new_born => { date => '2020-01-27', expected_age => 0 },
+ },
+ {
+ today => '2020-02-29',
+ has_12 => { date => '2007-08-27', expected_age => 12 },
+ almost_18 => { date => '2002-03-01', expected_age => 17 },
+ has_18_today => { date => '2002-02-28', expected_age => 18 },
+ had_18_yesterday => { date => '2002-02-27', expected_age => 18 },
+ almost_16 => { date => '2004-03-01', expected_age => 15 },
+ has_16_today => { date => '2004-02-29', expected_age => 16 },
+ had_16_yesterday => { date => '2004-02-28', expected_age => 16 },
+ new_born => { date => '2020-01-27', expected_age => 0 },
+ },
+ {
+ today => '2020-03-01',
+ has_12 => { date => '2007-08-27', expected_age => 12 },
+ almost_18 => { date => '2002-03-02', expected_age => 17 },
+ has_18_today => { date => '2002-03-01', expected_age => 18 },
+ had_18_yesterday => { date => '2002-02-28', expected_age => 18 },
+ almost_16 => { date => '2004-03-02', expected_age => 15 },
+ has_16_today => { date => '2004-03-01', expected_age => 16 },
+ had_16_yesterday => { date => '2004-02-29', expected_age => 16 },
+ },
+ {
+ today => '2019-01-31',
+ has_12 => { date => '2006-08-27', expected_age => 12 },
+ almost_18 => { date => '2001-02-01', expected_age => 17 },
+ has_18_today => { date => '2001-01-31', expected_age => 18 },
+ had_18_yesterday => { date => '2001-01-30', expected_age => 18 },
+ almost_16 => { date => '2003-02-01', expected_age => 15 },
+ has_16_today => { date => '2003-01-31', expected_age => 16 },
+ had_16_yesterday => { date => '2003-01-30', expected_age => 16 },
+ },
+ );
$patron->dateofbirth( undef );
is( $patron->get_age, undef, 'get_age should return undef if no dateofbirth is defined' );
- $patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1, end_of_month => 'limit' ) );
- is( $patron->get_age, 12, 'Patron should be 12' );
- $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 1, end_of_month => 'limit' ) );
- is( $patron->get_age, 17, 'Patron should be 17, happy birthday tomorrow!' );
- $patron->dateofbirth( $today->clone->add( years => -18, months => 0, days => 0, end_of_month => 'limit' ) );
- is( $patron->get_age, 18, 'Patron should be 18' );
- $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -31, end_of_month => 'limit' ) );
- is( $patron->get_age, 19, 'Patron should be 19' );
- $patron->dateofbirth( $today->clone->add( years => -18, months => -12, days => -30, end_of_month => 'limit' ) );
- is( $patron->get_age, 19, 'Patron should be 19 again' );
- $patron->dateofbirth( $today->clone->add( years => 0, months => -1, days => -1, end_of_month => 'limit' ) );
- is( $patron->get_age, 0, 'Patron is a newborn child' );
+
+ for my $date ( @dates ) {
+
+ my $dt = dt_from_string($date->{today});
+
+ Time::Fake->offset( $dt->epoch );
+
+ for my $k ( keys %$date ) {
+ next if $k eq 'today';
+
+ my $dob = $date->{$k};
+ $patron->dateofbirth( dt_from_string( $dob->{date}, 'iso' ) );
+ is(
+ $patron->get_age,
+ $dob->{expected_age},
+ sprintf(
+ "Today=%s, dob=%s, should be %d",
+ $date->{today}, $dob->{date}, $dob->{expected_age}
+ )
+ );
+ }
+
+ Time::Fake->reset;
+
+ }
$patron->delete;
};
subtest 'is_valid_age' => sub {
plan tests => 10;
- my $today = dt_from_string;
+ my $dt = dt_from_string('2020-02-28');
+
+ Time::Fake->offset( $dt->epoch );
my $category = $builder->build({
source => 'Category',
my $patron = $builder->build({
source => 'Borrower',
value => {
- categorycode => 'AGE_5_10'
+ categorycode => $category->categorycode
}
});
$patron = Koha::Patrons->find( $patron->{borrowernumber} );
$patron->dateofbirth( undef );
is( $patron->is_valid_age, 1, 'Patron with no dateofbirth is always valid for any category');
- $patron->dateofbirth( $today->clone->add( years => -12, months => -6, days => -1 ) );
- is( $patron->is_valid_age, 0, 'Patron is 12, so the age is above allowed range 5-10 years');
-
- $patron->dateofbirth( $today->clone->add( years => -3, months => -6, days => -1 ) );
- is( $patron->is_valid_age, 0, 'Patron is 3, so the age is below allowed range 5-10 years');
-
- $patron->dateofbirth( $today->clone->add( years => -7, months => -6, days => -1 ) );
- is( $patron->is_valid_age, 1, 'Patron is 7, so the age perfectly suits allowed range 5-10 years');
+ my @dates = (
+ {
+ today => '2020-02-28',
+ add_m12_m6_m1 =>
+ { date => '2007-08-27', expected_age => 12, valid => 0 },
+ add_m3_m6_m1 =>
+ { date => '2016-08-27', expected_age => 3, valid => 0 },
+ add_m7_m6_m1 =>
+ { date => '2013-02-28', expected_age => 7, valid => 1 },
+ add_m5_0_0 =>
+ { date => '2015-02-28', expected_age => 5, valid => 1 },
+ add_m5_0_p1 =>
+ { date => '2015-03-01', expected_age => 4, valid => 0 },
+ add_m5_0_m1 =>
+ { date => '2015-02-27', expected_age => 5, valid => 1 },
+ add_m11_0_0 =>
+ { date => '2009-02-28', expected_age => 11, valid => 0 },
+ add_m11_0_p1 =>
+ { date => '2009-03-01', expected_age => 10, valid => 1 },
+ add_m11_0_m1 =>
+ { date => '2009-02-27', expected_age => 11, valid => 0 },
+ },
+ );
- $patron->dateofbirth( $today->clone->add( years => -5, months => 0, days => 0 ) );
- is( $patron->is_valid_age, 1, 'Patron celebrates the 5th birthday today, so the age is allowed for this category');
+ for my $date ( @dates ) {
- $patron->dateofbirth( $today->clone->add( years => -5, months => 0, days => 1 ) );
- is( $patron->is_valid_age, 0, 'Patron will celebrate the 5th birthday tomorrow, so the age is NOT allowed for this category');
+ my $dt = dt_from_string($date->{today});
- $patron->dateofbirth( $today->clone->add( years => -5, months => 0, days => -1 ) );
- is( $patron->is_valid_age, 1, 'Patron celebrated the 5th birthday yesterday, so the age is allowed for this category');
+ Time::Fake->offset( $dt->epoch );
- $patron->dateofbirth( $today->clone->add( years => -11, months => 0, days => 0 ) );
- is( $patron->is_valid_age, 0, 'Patron celebrate the 11th birthday today, so the age is NOT allowed for this category');
+ for my $k ( sort keys %$date ) {
+ next if $k eq 'today';
+
+ my $dob = $date->{$k};
+ $patron->dateofbirth( dt_from_string( $dob->{date}, 'iso' ) );
+ is(
+ $patron->is_valid_age,
+ $dob->{valid},
+ sprintf(
+ "Today=%s, dob=%s, is %s, should be valid=%s in category %s",
+ $date->{today}, $dob->{date}, $dob->{expected_age}, $dob->{valid}, $category->categorycode
+ )
+ );
+ }
- $patron->dateofbirth( $today->clone->add( years => -11, months => 0, days => 1 ) );
- is( $patron->is_valid_age, 1, 'Patron will celebrate the 11th birthday tomorrow, so the age is allowed for this category');
+ Time::Fake->reset;
- $patron->dateofbirth( $today->clone->add( years => -11, months => 0, days => -1 ) );
- is( $patron->is_valid_age, 0, 'Patron celebrated the 11th birthday yesterday, so the age is NOT allowed for this category');
+ }
$patron->delete;
$category->delete;
plan tests => 6;
my $library = $builder->build( { source => 'Branch' } );
- my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
- my $item_1 = $builder->build(
+ my $biblionumber_1 = $builder->build_sample_biblio->biblionumber;
+ my $item_1 = $builder->build_sample_item(
{
- source => 'Item',
- value => {
- homebranch => $library->{branchcode},
- holdingbranch => $library->{branchcode},
- biblionumber => $biblionumber_1
- }
+ library => $library->{branchcode},
+ biblionumber => $biblionumber_1,
}
);
- my $item_2 = $builder->build(
+ my $item_2 = $builder->build_sample_item(
{
- source => 'Item',
- value => {
- homebranch => $library->{branchcode},
- holdingbranch => $library->{branchcode},
- biblionumber => $biblionumber_1
- }
+ library => $library->{branchcode},
+ biblionumber => $biblionumber_1,
}
);
- my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
- my $item_3 = $builder->build(
+ my $biblionumber_2 = $builder->build_sample_biblio->biblionumber;
+ my $item_3 = $builder->build_sample_item(
{
- source => 'Item',
- value => {
- homebranch => $library->{branchcode},
- holdingbranch => $library->{branchcode},
- biblionumber => $biblionumber_2
- }
+ library => $library->{branchcode},
+ biblionumber => $biblionumber_2,
}
);
+
my $patron = $builder->build(
{
source => 'Borrower',
'Koha::Patron->holds should return a Koha::Holds objects' );
is( $holds->count, 0, 'There should not be holds placed by this patron yet' );
- C4::Reserves::AddReserve( $library->{branchcode},
- $patron->borrowernumber, $biblionumber_1 );
+ C4::Reserves::AddReserve(
+ {
+ branchcode => $library->{branchcode},
+ borrowernumber => $patron->borrowernumber,
+ biblionumber => $biblionumber_1
+ }
+ );
# In the future
- C4::Reserves::AddReserve( $library->{branchcode},
- $patron->borrowernumber, $biblionumber_2, undef, undef, dt_from_string->add( days => 2 ) );
+ C4::Reserves::AddReserve(
+ {
+ branchcode => $library->{branchcode},
+ borrowernumber => $patron->borrowernumber,
+ biblionumber => $biblionumber_2,
+ expiration_date => dt_from_string->add( days => 2 )
+ }
+ );
$holds = $patron->holds;
is( $holds->count, 2, 'There should be 2 holds placed by this patron' );
$patron->delete;
};
-subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub {
- plan tests => 4;
+subtest 'search_patrons_to_anonymise' => sub {
+
+ plan tests => 5;
# TODO create a subroutine in t::lib::Mocks
my $branch = $builder->build({ source => 'Branch' });
t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
+ subtest 'Anonymous Patron should be undeleteable' => sub {
+ plan tests => 2;
+
+ my $anonymous_patron = Koha::Patrons->find( $anonymous->{borrowernumber} );
+ throws_ok { $anonymous_patron->delete(); }
+ 'Koha::Exceptions::Patron::FailedDeleteAnonymousPatron',
+ 'Attempt to delete anonymous patron throws exception.';
+ $anonymous_patron = Koha::Patrons->find( $anonymous->{borrowernumber} );
+ is( $anonymous_patron->id, $anonymous->{borrowernumber}, "Anonymous Patron was not deleted" );
+ };
+
subtest 'patron privacy is 1 (default)' => sub {
plan tests => 9;
value => { privacy => 1, }
}
);
- my $item_1 = $builder->build(
- { source => 'Item',
- value => {
- itemlost => 0,
- withdrawn => 0,
- },
- }
- );
+ my $item_1 = $builder->build_sample_item;
my $issue_1 = $builder->build(
{ source => 'Issue',
value => {
borrowernumber => $patron->{borrowernumber},
- itemnumber => $item_1->{itemnumber},
- },
- }
- );
- my $item_2 = $builder->build(
- { source => 'Item',
- value => {
- itemlost => 0,
- withdrawn => 0,
+ itemnumber => $item_1->itemnumber,
},
}
);
+ my $item_2 = $builder->build_sample_item;
my $issue_2 = $builder->build(
{ source => 'Issue',
value => {
borrowernumber => $patron->{borrowernumber},
- itemnumber => $item_2->{itemnumber},
+ itemnumber => $item_2->itemnumber,
},
}
);
- my ( $returned_1, undef, undef ) = C4::Circulation::AddReturn( $item_1->{barcode}, undef, undef, dt_from_string('2010-10-10') );
- my ( $returned_2, undef, undef ) = C4::Circulation::AddReturn( $item_2->{barcode}, undef, undef, dt_from_string('2011-11-11') );
+ my ( $returned_1, undef, undef ) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string('2010-10-10') );
+ my ( $returned_2, undef, undef ) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string('2011-11-11') );
is( $returned_1 && $returned_2, 1, 'The items should have been returned' );
my $patrons_to_anonymise = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->search( { 'me.borrowernumber' => $patron->{borrowernumber} } );
is( ref($patrons_to_anonymise), 'Koha::Patrons', 'search_patrons_to_anonymise should return Koha::Patrons' );
- my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2011-11-12' } )->anonymise_issue_history( { before => '2010-10-11' } );
+ my $rows_affected = Koha::Old::Checkouts->search(
+ {
+ borrowernumber => [
+ Koha::Patrons->search_patrons_to_anonymise(
+ { before => '2011-11-12' }
+ )->get_column('borrowernumber')
+ ],
+ returndate => { '<' => '2011-10-11', }
+ }
+ )->anonymize;
ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
$patrons_to_anonymise = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } );
my $dbh = C4::Context->dbh;
my $sth = $dbh->prepare(q|SELECT borrowernumber FROM old_issues where itemnumber = ?|);
- $sth->execute($item_1->{itemnumber});
+ $sth->execute($item_1->itemnumber);
my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'With privacy=1, the issue should have been anonymised' );
- $sth->execute($item_2->{itemnumber});
+ $sth->execute($item_2->itemnumber);
($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'The issue should not have been anonymised, the returned date is later' );
- $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2011-11-12' } )->anonymise_issue_history;
- $sth->execute($item_2->{itemnumber});
+ $rows_affected = Koha::Old::Checkouts->search(
+ {
+ borrowernumber => [
+ Koha::Patrons->search_patrons_to_anonymise(
+ { before => '2011-11-12' }
+ )->get_column('borrowernumber')
+ ],
+ returndate => { '<' => '2011-11-12', }
+ }
+ )->anonymize;
+ $sth->execute($item_2->itemnumber);
($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue should have been anonymised, the returned date is before' );
my $sth_reset = $dbh->prepare(q|UPDATE old_issues SET borrowernumber = ? WHERE itemnumber = ?|);
- $sth_reset->execute( $patron->{borrowernumber}, $item_1->{itemnumber} );
- $sth_reset->execute( $patron->{borrowernumber}, $item_2->{itemnumber} );
- $rows_affected = Koha::Patrons->search_patrons_to_anonymise->anonymise_issue_history;
- $sth->execute($item_1->{itemnumber});
+ $sth_reset->execute( $patron->{borrowernumber}, $item_1->itemnumber );
+ $sth_reset->execute( $patron->{borrowernumber}, $item_2->itemnumber );
+ $rows_affected = Koha::Old::Checkouts->search(
+ {
+ borrowernumber => [
+ Koha::Patrons->search_patrons_to_anonymise->get_column(
+ 'borrowernumber')
+ ]
+ }
+ )->anonymize;
+ $sth->execute($item_1->itemnumber);
($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue 1 should have been anonymised, before parameter was not passed' );
- $sth->execute($item_2->{itemnumber});
+ $sth->execute($item_2->itemnumber);
($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue 2 should have been anonymised, before parameter was not passed' );
value => { privacy => 0, }
}
);
- my $item = $builder->build(
- { source => 'Item',
- value => {
- itemlost => 0,
- withdrawn => 0,
- },
- }
- );
+ my $item = $builder->build_sample_item;
my $issue = $builder->build(
{ source => 'Issue',
value => {
borrowernumber => $patron->{borrowernumber},
- itemnumber => $item->{itemnumber},
+ itemnumber => $item->itemnumber,
},
}
);
- my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, dt_from_string('2010-10-10') );
+ my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->barcode, undef, undef, dt_from_string('2010-10-10') );
is( $returned, 1, 'The item should have been returned' );
my $dbh = C4::Context->dbh;
my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
SELECT borrowernumber FROM old_issues where itemnumber = ?
- |, undef, $item->{itemnumber});
+ |, undef, $item->itemnumber);
is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'With privacy=0, the issue should not be anonymised' );
Koha::Patrons->find( $patron->{borrowernumber})->delete;
};
t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
subtest 'AnonymousPatron is not defined' => sub {
- plan tests => 3;
+
+ plan tests => 2;
t::lib::Mocks::mock_preference('IndependentBranches', 0);
my $patron = $builder->build(
value => { privacy => 1, }
}
);
- my $item = $builder->build(
- { source => 'Item',
- value => {
- itemlost => 0,
- withdrawn => 0,
- },
- }
- );
+ my $item = $builder->build_sample_item;
my $issue = $builder->build(
{ source => 'Issue',
value => {
borrowernumber => $patron->{borrowernumber},
- itemnumber => $item->{itemnumber},
+ itemnumber => $item->itemnumber,
},
}
);
- my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, dt_from_string('2010-10-10') );
+ my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->barcode, undef, undef, dt_from_string('2010-10-10') );
is( $returned, 1, 'The item should have been returned' );
- my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->anonymise_issue_history( { before => '2010-10-11' } );
- ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
+ my $patrons_to_anonymize = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } );
+ ok( $patrons_to_anonymize->count > 0, 'search_patrons_to_anonymize' );
- my $dbh = C4::Context->dbh;
- my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
- SELECT borrowernumber FROM old_issues where itemnumber = ?
- |, undef, $item->{itemnumber});
- is( $borrowernumber_used_to_anonymised, undef, 'With AnonymousPatron is not defined, the issue should have been anonymised anyway' );
Koha::Patrons->find( $patron->{borrowernumber})->delete;
};
value => { privacy => 1 } # Another branchcode than the logged in librarian
}
);
- my $item = $builder->build(
- { source => 'Item',
- value => {
- itemlost => 0,
- withdrawn => 0,
- },
- }
- );
+ my $item = $builder->build_sample_item;
my $issue = $builder->build(
{ source => 'Issue',
value => {
borrowernumber => $patron->{borrowernumber},
- itemnumber => $item->{itemnumber},
+ itemnumber => $item->itemnumber,
},
}
);
- my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, dt_from_string('2010-10-10') );
+ my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->barcode, undef, undef, dt_from_string('2010-10-10') );
is( Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->count, 0 );
Koha::Patrons->find( $patron->{borrowernumber})->delete;
};
$patron_other->delete;
};
-subtest 'get_overdues' => sub {
+subtest 'overdues' => sub {
plan tests => 7;
my $library = $builder->build( { source => 'Branch' } );
- my ($biblionumber_1) = AddBiblio( MARC::Record->new, '' );
- my $item_1 = $builder->build(
+ my $biblionumber_1 = $builder->build_sample_biblio->biblionumber;
+ my $item_1 = $builder->build_sample_item(
{
- source => 'Item',
- value => {
- homebranch => $library->{branchcode},
- holdingbranch => $library->{branchcode},
- biblionumber => $biblionumber_1
- }
+ library => $library->{branchcode},
+ biblionumber => $biblionumber_1,
}
);
- my $item_2 = $builder->build(
+ my $item_2 = $builder->build_sample_item(
{
- source => 'Item',
- value => {
- homebranch => $library->{branchcode},
- holdingbranch => $library->{branchcode},
- biblionumber => $biblionumber_1
- }
+ library => $library->{branchcode},
+ biblionumber => $biblionumber_1,
}
);
- my ($biblionumber_2) = AddBiblio( MARC::Record->new, '' );
- my $item_3 = $builder->build(
+ my $item_3 = $builder->build_sample_item(
{
- source => 'Item',
- value => {
- homebranch => $library->{branchcode},
- holdingbranch => $library->{branchcode},
- biblionumber => $biblionumber_2
- }
+ library => $library->{branchcode},
}
);
+
my $patron = $builder->build(
{
source => 'Borrower',
t::lib::Mocks::mock_preference({ branchcode => $library->{branchcode} });
- AddIssue( $patron, $item_1->{barcode}, DateTime->now->subtract( days => 1 ) );
- AddIssue( $patron, $item_2->{barcode}, DateTime->now->subtract( days => 5 ) );
- AddIssue( $patron, $item_3->{barcode} );
+ AddIssue( $patron, $item_1->barcode, DateTime->now->subtract( days => 1 ) );
+ AddIssue( $patron, $item_2->barcode, DateTime->now->subtract( days => 5 ) );
+ AddIssue( $patron, $item_3->barcode );
$patron = Koha::Patrons->find( $patron->{borrowernumber} );
- my $overdues = $patron->get_overdues;
+ my $overdues = $patron->overdues;
is( $overdues->count, 2, 'Patron should have 2 overdues');
- is( $overdues->next->itemnumber, $item_1->{itemnumber}, 'The issue should be returned in the same order as they have been done, first is correct' );
- is( $overdues->next->itemnumber, $item_2->{itemnumber}, 'The issue should be returned in the same order as they have been done, second is correct' );
+ is( $overdues->next->itemnumber, $item_1->itemnumber, 'The issue should be returned in the same order as they have been done, first is correct' );
+ is( $overdues->next->itemnumber, $item_2->itemnumber, 'The issue should be returned in the same order as they have been done, second is correct' );
my $o = $overdues->reset->next;
my $unblessed_overdue = $o->unblessed_all_relateds;
$patron_2->delete;
};
-subtest 'attributes' => sub {
- plan tests => 2;
-
- my $library1 = Koha::Library->new({
- branchcode => 'LIBPATRON',
- branchname => 'Library of testing patron',
- })->store;
-
- my $library2 = Koha::Library->new({
- branchcode => 'LIBATTR',
- branchname => 'Library for testing attribute',
- })->store;
-
- my $category = Koha::Patron::Category->new({
- categorycode => 'CAT1',
- description => 'Category 1',
- })->store;
-
- my $patron = Koha::Patron->new({
- firstname => 'Patron',
- surname => 'with attributes',
- branchcode => 'LIBPATRON',
- categorycode => 'CAT1',
- })->store;
-
- my $attribute_type1 = Koha::Patron::Attribute::Type->new({
- code => 'CODE_A',
- description => 'Code A desciption',
- })->store;
-
- my $attribute_type2 = Koha::Patron::Attribute::Type->new({
- code => 'CODE_B',
- description => 'Code A desciption',
- })->store;
-
- $attribute_type2->library_limits ( [ $library2->branchcode ] );
-
- Koha::Patron::Attribute->new({ borrowernumber => $patron->borrowernumber, code => $attribute_type1->code, attribute => 'value 1' } )->store();
- Koha::Patron::Attribute->new({ borrowernumber => $patron->borrowernumber, code => $attribute_type2->code, attribute => 'value 2' } )->store();
-
- is( $patron->attributes->count, 1, 'There should be one attribute');
-
- $attribute_type2->library_limits ( [ $library1->branchcode ] );
-
- is( $patron->attributes->count, 2, 'There should be 2 attributes');
-
- $patron->delete;
-};
-
$nb_of_patrons = Koha::Patrons->search->count;
$retrieved_patron_1->delete;
is( Koha::Patrons->search->count, $nb_of_patrons - 1, 'Delete should have deleted the patron' );
my $loser_1 = $builder->build({ source => 'Borrower' })->{borrowernumber};
my $loser_2 = $builder->build({ source => 'Borrower' })->{borrowernumber};
+ my $anonymous_patron_orig = C4::Context->preference('AnonymousPatron');
+ my $anonymous_patron = $builder->build({ source => 'Borrower' })->{borrowernumber};
+ t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron );
+
while (my ($r, $field) = each(%$resultsets)) {
$builder->build({ source => $r, value => { $field => $keeper->id } });
$builder->build({ source => $r, value => { $field => $loser_1 } });
is( Koha::Patrons->find($loser_1), undef, 'Loser 1 has been deleted' );
is( Koha::Patrons->find($loser_2), undef, 'Loser 2 has been deleted' );
+ is( ref Koha::Patrons->find($anonymous_patron), 'Koha::Patron', 'Anonymous Patron was not deleted' );
+
+ $anonymous_patron = Koha::Patrons->find($anonymous_patron);
+ $results = $anonymous_patron->merge_with( [ $keeper->id ] );
+ is( $results, undef, "Anonymous patron cannot have other patrons merged into it" );
+ is( Koha::Patrons->search( { borrowernumber => $keeper->id } )->count, 1, "Patron from attempted merge with AnonymousPatron still exists" );
+
+ subtest 'extended attributes' => sub {
+ plan tests => 8;
+ my $keep_patron =
+ $builder->build_object( { class => 'Koha::Patrons' } );
+ my $merge_patron =
+ $builder->build_object( { class => 'Koha::Patrons' } );
+
+ my $attribute_type_normal_1 = $builder->build_object(
+ {
+ class => 'Koha::Patron::Attribute::Types',
+ value => { repeatable => 0, unique_id => 0 }
+ }
+ );
+ my $attribute_type_normal_2 = $builder->build_object(
+ {
+ class => 'Koha::Patron::Attribute::Types',
+ value => { repeatable => 0, unique_id => 0 }
+ }
+ );
+
+ my $attribute_type_repeatable = $builder->build_object(
+ {
+ class => 'Koha::Patron::Attribute::Types',
+ value => { repeatable => 1, unique_id => 0 }
+ }
+ );
+
+ my $attr_keep = [
+ {
+ code => $attribute_type_normal_1->code,
+ attribute => 'from attr 1'
+ },
+ {
+ code => $attribute_type_repeatable->code,
+ attribute => 'from attr repeatable'
+ }
+ ];
+
+ my $attr_merge = [
+ {
+ code => $attribute_type_normal_2->code,
+ attribute => 'to attr 2'
+ },
+ {
+ code => $attribute_type_repeatable->code,
+ attribute => 'to attr repeatable'
+ },
+ ];
+
+ $keep_patron->extended_attributes($attr_keep);
+ $merge_patron->extended_attributes($attr_merge);
+
+ $keep_patron->merge_with( [ $merge_patron->borrowernumber ] );
+ my $merged_attributes = $keep_patron->extended_attributes;
+ is( $merged_attributes->count, 4 );
+
+ sub compare_attributes {
+ my ( $got, $expected, $code ) = @_;
+
+ is_deeply(
+ [
+ sort $got->search( { code => $code } )
+ ->get_column('attribute')
+ ],
+ $expected
+ );
+ }
+ compare_attributes(
+ $merged_attributes,
+ ['from attr 1'],
+ $attribute_type_normal_1->code
+ );
+ compare_attributes(
+ $merged_attributes,
+ ['to attr 2'],
+ $attribute_type_normal_2->code
+ );
+ compare_attributes(
+ $merged_attributes,
+ [ 'from attr repeatable', 'to attr repeatable' ],
+ $attribute_type_repeatable->code
+ );
+
+ # Cleanup
+ $keep_patron->delete;
+ $merge_patron->delete;
+
+ # Recreate but don't expect an exception if 2 non-repeatable attributes exist, pick the one from the patron we keep
+ $keep_patron =
+ $builder->build_object( { class => 'Koha::Patrons' } );
+ $merge_patron =
+ $builder->build_object( { class => 'Koha::Patrons' } );
+
+ $keep_patron->extended_attributes($attr_keep);
+ $merge_patron->extended_attributes(
+ [
+ @$attr_merge,
+ {
+ code => $attribute_type_normal_1->code,
+ attribute => 'yet another attribute for non-repeatable'
+ }
+ ]
+ );
+
+ $keep_patron->merge_with( [ $merge_patron->borrowernumber ] );
+ $merged_attributes = $keep_patron->extended_attributes;
+ is( $merged_attributes->count, 4 );
+ compare_attributes(
+ $merged_attributes,
+ ['from attr 1'],
+ $attribute_type_normal_1->code
+ );
+ compare_attributes(
+ $merged_attributes,
+ ['to attr 2'],
+ $attribute_type_normal_2->code
+ );
+ compare_attributes(
+ $merged_attributes,
+ [ 'from attr repeatable', 'to attr repeatable' ],
+ $attribute_type_repeatable->code
+ );
+
+ };
+
+ t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
$schema->storage->txn_rollback;
};
my $patron_1 = $builder->build_object({class=> 'Koha::Patrons'});
my $patron_2 = $builder->build_object({class=> 'Koha::Patrons'});
- throws_ok
- { $patron_2->userid($patron_1->userid)->store; }
+ {
+ local *STDERR;
+ open STDERR, '>', '/dev/null';
+ throws_ok { $patron_2->userid( $patron_1->userid )->store; }
'Koha::Exceptions::Object::DuplicateID',
- 'Koha::Patron->store raises an exception on duplicate ID';
+ 'Koha::Patron->store raises an exception on duplicate ID';
+ close STDERR;
+ }
# Test password
t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
};
$schema->storage->txn_begin;
+subtest 'filter_by_expiration_date' => sub {
+ plan tests => 3;
+ my $count1 = Koha::Patrons->filter_by_expiration_date({ days => 28 })->count;
+ my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
+ $patron1->dateexpiry( dt_from_string->subtract(days => 27) )->store;
+ is( Koha::Patrons->filter_by_expiration_date({ days => 28 })->count, $count1, 'No more expired' );
+ $patron1->dateexpiry( dt_from_string->subtract(days => 28) )->store;
+ is( Koha::Patrons->filter_by_expiration_date({ days => 28 })->count, $count1 + 1, 'One more expired' );
+ $patron1->dateexpiry( dt_from_string->subtract(days => 29) )->store;
+ is( Koha::Patrons->filter_by_expiration_date({ days => 28 })->count, $count1 + 1, 'Same number again' );
+};
+
subtest 'search_unsubscribed' => sub {
plan tests => 4;
};
subtest 'search_anonymize_candidates' => sub {
- plan tests => 5;
+ plan tests => 7;
my $patron1 = $builder->build_object({ class => 'Koha::Patrons' });
my $patron2 = $builder->build_object({ class => 'Koha::Patrons' });
$patron1->anonymized(0);
$patron1->login_attempts(3)->store;
is( Koha::Patrons->search_anonymize_candidates({locked => 1})->count,
$cnt+1, 'Locked flag' );
+
+ t::lib::Mocks::mock_preference( 'FailedLoginAttempts', q{} );
+ # Patron 1 still on 3 == locked
+ is( Koha::Patrons->search_anonymize_candidates({locked => 1})->count,
+ $cnt+1, 'Still expect same number for FailedLoginAttempts empty' );
+ $patron1->login_attempts(0)->store;
+ # Patron 1 unlocked
+ is( Koha::Patrons->search_anonymize_candidates({locked => 1})->count,
+ $cnt, 'Patron 1 unlocked' );
};
subtest 'search_anonymized' => sub {
$patron2->discard_changes; # refresh
is( $patron2->firstname, undef, 'First name patron2 cleared' );
};
+
+subtest 'queue_notice' => sub {
+ plan tests => 11;
+
+ my $dbh = C4::Context->dbh;
+ t::lib::Mocks::mock_preference( 'AutoEmailPrimaryAddress', 'email' );
+ my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
+ my $branch = $builder->build_object( { class => 'Koha::Libraries' } );
+ my $letter_e = $builder->build_object( {
+ class => 'Koha::Notice::Templates',
+ value => {
+ branchcode => $branch->branchcode,
+ message_transport_type => 'email',
+ lang => 'default'
+ }
+ });
+ my $letter_p = $builder->build_object( {
+ class => 'Koha::Notice::Templates',
+ value => {
+ code => $letter_e->code,
+ module => $letter_e->module,
+ branchcode => $branch->branchcode,
+ message_transport_type => 'print',
+ lang => 'default'
+ }
+ });
+ my $letter_s = $builder->build_object( {
+ class => 'Koha::Notice::Templates',
+ value => {
+ code => $letter_e->code,
+ module => $letter_e->module,
+ branchcode => $branch->branchcode,
+ message_transport_type => 'sms',
+ lang => 'default'
+ }
+ });
+
+ my $letter_params = {
+ letter_code => $letter_e->code,
+ branchcode => $letter_e->branchcode,
+ module => $letter_e->module,
+ borrowernumber => $patron->borrowernumber,
+ tables => {
+ borrowers => $patron->borrowernumber,
+ }
+ };
+ my @mtts = ('email');
+
+ is( $patron->queue_notice(), undef, "Nothing is done if no params passed");
+ is( $patron->queue_notice({ letter_params => $letter_params }),undef, "Nothing done if only letter");
+ is_deeply(
+ $patron->queue_notice({ letter_params => $letter_params, message_transports => \@mtts }),
+ {sent => ['email'] }, "Email sent"
+ );
+ $patron->email("")->store;
+ is_deeply(
+ $patron->queue_notice({ letter_params => $letter_params, message_transports => \@mtts }),
+ {sent => ['print'],fallback => ['email']}, "Email fallsback to print if no email"
+ );
+ push @mtts, 'sms';
+ is_deeply(
+ $patron->queue_notice({ letter_params => $letter_params, message_transports => \@mtts }),
+ {sent => ['print','sms'],fallback => ['email']}, "Email fallsback to print if no email, sms sent"
+ );
+ $patron->smsalertnumber("")->store;
+ my $counter = Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count;
+ is_deeply(
+ $patron->queue_notice({ letter_params => $letter_params, message_transports => \@mtts }),
+ {sent => ['print'],fallback => ['email','sms']}, "Email fallsback to print if no emai, sms fallsback to print if no sms, only one print sent"
+ );
+ is( Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count, $counter+1,"Count of queued notices went up by one");
+
+ # Enable notification for Hold_Filled - Things are hardcoded here but should work with default data
+ $dbh->do(q|INSERT INTO borrower_message_preferences( borrowernumber, message_attribute_id ) VALUES ( ?, ?)|, undef, $patron->borrowernumber, 4 );
+ my $borrower_message_preference_id = $dbh->last_insert_id(undef, undef, "borrower_message_preferences", undef);
+ $dbh->do(q|INSERT INTO borrower_message_transport_preferences( borrower_message_preference_id, message_transport_type) VALUES ( ?, ? )|, undef, $borrower_message_preference_id, 'email' );
+
+ is( $patron->queue_notice({ letter_params => $letter_params, message_transports => \@mtts, message_name => 'Hold_Filled' }),undef, "Nothing done if transports and name sent");
+
+ $patron->email(q|awesome@ismymiddle.name|)->store;
+ is_deeply(
+ $patron->queue_notice({ letter_params => $letter_params, message_name => 'Hold_Filled' }),
+ {sent => ['email'] }, "Email sent when using borrower preferences"
+ );
+ $counter = Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count;
+ is_deeply(
+ $patron->queue_notice({ letter_params => $letter_params, message_name => 'Hold_Filled', test_mode => 1 }),
+ {sent => ['email'] }, "Report that email sent when using borrower preferences in test_mode"
+ );
+ is( Koha::Notice::Messages->search({borrowernumber => $patron->borrowernumber })->count, $counter,"Count of queued notices not increased in test mode");
+};
+
+subtest 'filter_by_amount_owed' => sub {
+ plan tests => 6;
+
+ my $library = $builder->build({source => 'Branch' });
+ my $category = $builder->build({source => 'Category' });
+
+ my $new_patron_cf_1 = Koha::Patron->new(
+ {
+ cardnumber => 'test_cn_cf_1',
+ branchcode => $library->{branchcode},
+ categorycode => $category->{categorycode},
+ surname => 'surname for patron1',
+ firstname => 'firstname for patron1',
+ userid => 'a_nonexistent_userid_cf_1',
+ }
+ )->store;
+ my $new_patron_cf_2 = Koha::Patron->new(
+ {
+ cardnumber => 'test_cn_cf_2',
+ branchcode => $library->{branchcode},
+ categorycode => $category->{categorycode},
+ surname => 'surname for patron2',
+ firstname => 'firstname for patron2',
+ userid => 'a_nonexistent_userid_cf_2',
+ }
+ )->store;
+ my $new_patron_cf_3 = Koha::Patron->new(
+ {
+ cardnumber => 'test_cn_cf_3',
+ branchcode => $library->{branchcode},
+ categorycode => $category->{categorycode},
+ surname => 'surname for patron3',
+ firstname => 'firstname for patron3',
+ userid => 'a_nonexistent_userid_cf_3',
+ }
+ )->store;
+
+ my $results = Koha::Patrons->search(
+ {
+ 'me.borrowernumber' => [
+ $new_patron_cf_1->borrowernumber,
+ $new_patron_cf_2->borrowernumber,
+ $new_patron_cf_3->borrowernumber
+ ]
+ }
+ );
+
+ my $fine1 = $builder->build(
+ {
+ source => 'Accountline',
+ value => {
+ borrowernumber => $new_patron_cf_1->borrowernumber,
+ amountoutstanding => 12.00,
+ amount => 12.00,
+ debit_type_code => 'OVERDUE',
+ branchcode => $library->{branchcode}
+ },
+ }
+ );
+ my $fine2 = $builder->build(
+ {
+ source => 'Accountline',
+ value => {
+ borrowernumber => $new_patron_cf_2->borrowernumber,
+ amountoutstanding => 8.00,
+ amount => 8.00,
+ debit_type_code => 'OVERDUE',
+ branchcode => $library->{branchcode}
+
+ },
+ }
+ );
+ my $fine3 = $builder->build(
+ {
+ source => 'Accountline',
+ value => {
+ borrowernumber => $new_patron_cf_2->borrowernumber,
+ amountoutstanding => 10.00,
+ amount => 10.00,
+ debit_type_code => 'OVERDUE',
+ branchcode => $library->{branchcode}
+ },
+ }
+ );
+
+ my $filtered = $results->filter_by_amount_owed();
+ is( ref($filtered), 'Koha::Patrons',
+'Koha::Patrons->filter_by_amount_owed should return a Koha::Patrons result set in a scalar context'
+ );
+
+ my $lower_limit = 12.00;
+ my $upper_limit = 16.00;
+
+ # Catch user with 1 x 12.00 fine and user with no fines.
+ $filtered =
+ $results->filter_by_amount_owed( { less_than => $upper_limit } );
+ is( $filtered->_resultset->as_subselect_rs->count, 2,
+"filter_by_amount_owed({ less_than => $upper_limit }) found two patrons"
+ );
+
+ # Catch user with 1 x 8.00 and 1 x 10.00 fine
+ $filtered =
+ $results->filter_by_amount_owed( { more_than => $lower_limit } );
+ is( $filtered->_resultset->as_subselect_rs->count, 1,
+"filter_by_amount_owed({ more_than => $lower_limit }) found two patrons"
+ );
+
+ # User with 2 fines falls above upper limit - Excluded,
+ # user with 1 fine falls below lower limit - Excluded
+ # and user with no fines falls below lower limit - Excluded.
+ $filtered = $results->filter_by_amount_owed(
+ { more_than => $lower_limit, less_than => $upper_limit } );
+ is( $filtered->_resultset->as_subselect_rs->count, 0,
+"filter_by_amount_owed({ more_than => $lower_limit, less_than => $upper_limit }) found zero patrons"
+ );
+
+ my $library2 = $builder->build({source => 'Branch' });
+ my $fine4 = $builder->build(
+ {
+ source => 'Accountline',
+ value => {
+ borrowernumber => $new_patron_cf_2->borrowernumber,
+ amountoutstanding => 10.00,
+ amount => 10.00,
+ debit_type_code => 'HOLD',
+ branchcode => $library2->{branchcode}
+ },
+ }
+ );
+
+ # Catch only the user with a HOLD fee over 6.00
+ $filtered = $results->filter_by_amount_owed( { more_than => 6.00, debit_type => 'HOLD' } );
+ is( $filtered->_resultset->as_subselect_rs->count, 1,
+"filter_by_amount_owed({ more_than => 6.00, debit_type => 'HOLD' }) found one patron"
+ );
+
+ # Catch only the user with a fee over 6.00 at the specified library
+ $filtered = $results->filter_by_amount_owed( { more_than => 6.00, library => $library2->{branchcode} } );
+ is( $filtered->_resultset->as_subselect_rs->count, 1,
+"filter_by_amount_owed({ more_than => 6.00, library => $library2->{branchcode} }) found one patron"
+ );
+
+};
+
+subtest 'filter_by_have_subpermission' => sub {
+ plan tests => 4;
+
+ $schema->storage->txn_begin;
+
+ my $library = $builder->build_object( { class => 'Koha::Libraries' } );
+ my $patron_1 = $builder->build_object(
+ {
+ class => 'Koha::Patrons',
+ value => { flags => 1, branchcode => $library->branchcode }
+ }
+ );
+
+ my $patron_2 = $builder->build_object( # 4096 = 1 << 12 for suggestions
+ {
+ class => 'Koha::Patrons',
+ value => { flags => 4096, branchcode => $library->branchcode }
+ }
+ );
+
+ my $patron_3 = $builder->build_object(
+ {
+ class => 'Koha::Patrons',
+ value => { flags => 0, branchcode => $library->branchcode }
+ }
+ );
+ $builder->build(
+ {
+ source => 'UserPermission',
+ value => {
+ borrowernumber => $patron_3->borrowernumber,
+ module_bit => 11,
+ code => 'order_manage',
+ },
+ }
+ );
+
+ is_deeply(
+ [
+ Koha::Patrons->search( { branchcode => $library->branchcode } )
+ ->filter_by_have_subpermission('suggestions.suggestions_manage')
+ ->get_column('borrowernumber')
+ ],
+ [ $patron_1->borrowernumber, $patron_2->borrowernumber ],
+ 'Superlibrarian and patron with suggestions.suggestions_manage'
+ );
+
+ is_deeply(
+ [
+ Koha::Patrons->search( { branchcode => $library->branchcode } )
+ ->filter_by_have_subpermission('acquisition.order_manage')
+ ->get_column('borrowernumber')
+ ],
+ [ $patron_1->borrowernumber, $patron_3->borrowernumber ],
+ 'Superlibrarian and patron with acquisition.order_manage'
+ );
+
+ is_deeply(
+ [
+ Koha::Patrons->search( { branchcode => $library->branchcode } )
+ ->filter_by_have_subpermission('parameters.manage_cities')
+ ->get_column('borrowernumber')
+ ],
+ [ $patron_1->borrowernumber ],
+ 'Only Superlibrarian is returned'
+ );
+
+ throws_ok {
+ Koha::Patrons->search( { branchcode => $library->branchcode } )
+ ->filter_by_have_subpermission('dont_exist.subperm');
+ } 'Koha::Exceptions::ObjectNotFound';
+
+
+ $schema->storage->txn_rollback;
+};
+
$schema->storage->txn_rollback;