From 198d8c0d84021bc2e498835a10cf36f597de2e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Cohen=20Arazi?= Date: Tue, 30 Sep 2025 12:08:35 -0300 Subject: [PATCH] Bug 40911: Regression tests --- t/db_dependent/SIP/Patron.t | 106 +++++++++++++++++++++++++----------- 1 file changed, 73 insertions(+), 33 deletions(-) diff --git a/t/db_dependent/SIP/Patron.t b/t/db_dependent/SIP/Patron.t index fc6e03bd28..c62936b030 100755 --- a/t/db_dependent/SIP/Patron.t +++ b/t/db_dependent/SIP/Patron.t @@ -1,11 +1,24 @@ #!/usr/bin/perl -# Some tests for SIP::ILS::Patron -# This needs to be extended! Your help is appreciated.. +# 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. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . use Modern::Perl; use Test::NoWarnings; -use Test::More tests => 12; +use Test::More tests => 10; +use Test::Warn; use t::lib::Mocks; use t::lib::TestBuilder; @@ -17,48 +30,62 @@ use Koha::DateUtils qw( dt_from_string output_pref ); use Koha::Patron::Attributes; use Koha::Patrons; -my $schema = Koha::Database->new->schema; -$schema->storage->txn_begin; - +my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new(); -my $patron1 = $builder->build( { source => 'Borrower' } ); -my $card = $patron1->{cardnumber}; - -# Check existing card number -my $sip_patron = C4::SIP::ILS::Patron->new($card); -is( defined $sip_patron, 1, "Patron is valid" ); -# Check invalid cardnumber by deleting patron -$schema->resultset('Borrower')->search( { cardnumber => $card } )->delete; -my $sip_patron2 = C4::SIP::ILS::Patron->new($card); -is( $sip_patron2, undef, "Patron is not valid (anymore)" ); +subtest "patron creation tests" => sub { -subtest "new tests" => sub { + plan tests => 10; - plan tests => 5; - - my $patron = $builder->build( { source => 'Borrower' } ); + $schema->storage->txn_begin; + my $patron = $builder->build( { source => 'Borrower' } ); my $cardnumber = $patron->{cardnumber}; my $userid = $patron->{userid}; my $borrowernumber = $patron->{borrowernumber}; - my $ils_patron = C4::SIP::ILS::Patron->new($cardnumber); - is( ref($ils_patron), 'C4::SIP::ILS::Patron', 'Found patron via cardnumber scalar' ); - $ils_patron = C4::SIP::ILS::Patron->new($userid); - is( ref($ils_patron), 'C4::SIP::ILS::Patron', 'Found patron via userid scalar' ); - $ils_patron = C4::SIP::ILS::Patron->new( { borrowernumber => $borrowernumber } ); - is( ref($ils_patron), 'C4::SIP::ILS::Patron', 'Found patron via borrowernumber hashref' ); - $ils_patron = C4::SIP::ILS::Patron->new( { cardnumber => $cardnumber } ); - is( ref($ils_patron), 'C4::SIP::ILS::Patron', 'Found patron via cardnumber hashref' ); - $ils_patron = C4::SIP::ILS::Patron->new( { userid => $userid } ); - is( ref($ils_patron), 'C4::SIP::ILS::Patron', 'Found patron via userid hashref' ); + # Test valid patron creation + my $sip_patron = C4::SIP::ILS::Patron->new($cardnumber); + is( ref($sip_patron), 'C4::SIP::ILS::Patron', 'Found patron via cardnumber scalar' ); + + $sip_patron = C4::SIP::ILS::Patron->new($userid); + is( ref($sip_patron), 'C4::SIP::ILS::Patron', 'Found patron via userid scalar' ); + + $sip_patron = C4::SIP::ILS::Patron->new( { borrowernumber => $borrowernumber } ); + is( ref($sip_patron), 'C4::SIP::ILS::Patron', 'Found patron via borrowernumber hashref' ); + + $sip_patron = C4::SIP::ILS::Patron->new( { cardnumber => $cardnumber } ); + is( ref($sip_patron), 'C4::SIP::ILS::Patron', 'Found patron via cardnumber hashref' ); + + $sip_patron = C4::SIP::ILS::Patron->new( { userid => $userid } ); + is( ref($sip_patron), 'C4::SIP::ILS::Patron', 'Found patron via userid hashref' ); + + # Test invalid patron scenarios + my $sip_patron2 = C4::SIP::ILS::Patron->new('NONEXISTENT'); + is( $sip_patron2, undef, "Nonexistent patron returns undef" ); + + # Test undefined/empty input handling (no warnings expected) + warnings_are { + my $sip_patron3 = C4::SIP::ILS::Patron->new(undef); + is( $sip_patron3, undef, "Undefined patron_id returns undef" ); + } + [], 'No warnings for undefined patron_id'; + + warnings_are { + my $sip_patron4 = C4::SIP::ILS::Patron->new(''); + is( $sip_patron4, undef, "Empty patron_id returns undef" ); + } + [], 'No warnings for empty patron_id'; + + $schema->storage->txn_rollback; }; subtest "OverduesBlockCirc tests" => sub { plan tests => 6; + $schema->storage->txn_begin; + my $odue_patron = $builder->build( { source => 'Borrower', @@ -102,12 +129,15 @@ subtest "OverduesBlockCirc tests" => sub { $odue_sip_patron = C4::SIP::ILS::Patron->new( $good_patron->{cardnumber} ); is( $odue_sip_patron->{charge_ok}, 1, "Not blocked without overdues when set to 'Block'" ); + $schema->storage->txn_rollback; }; subtest "Test build_patron_attribute_string" => sub { plan tests => 2; + $schema->storage->txn_begin; + my $patron = $builder->build( { source => 'Borrower' } ); my $attribute_type = $builder->build( { source => 'BorrowerAttributeType' } ); @@ -146,12 +176,16 @@ subtest "Test build_patron_attribute_string" => sub { $attribute_string, "XYTest Attribute|YZAnother Test Attribute|", 'Attribute field generated correctly with multiple params' ); + + $schema->storage->txn_rollback; }; subtest "Test build_custom_field_string" => sub { plan tests => 5; + $schema->storage->txn_begin; + my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { surname => "Duck", firstname => "Darkwing" } } ); @@ -199,12 +233,15 @@ subtest "Test build_custom_field_string" => sub { 'Custom fields processed correctly, bad template generate no text' ); + $schema->storage->txn_rollback; }; subtest "fine_items tests" => sub { plan tests => 12; + $schema->storage->txn_begin; + my $patron = $builder->build( { source => 'Borrower', @@ -261,15 +298,16 @@ subtest "fine_items tests" => sub { # Check an invalid start boundary $fine_items = $sip_patron->fine_items( 98, 99 ); is( @$fine_items, 0, "Got zero fine items" ); -}; -$schema->storage->txn_rollback; + $schema->storage->txn_rollback; +}; subtest "Patron expiration tests" => sub { plan tests => 5; $schema->storage->txn_begin; + my $patron = $builder->build_object( { class => 'Koha::Patrons', @@ -326,7 +364,6 @@ subtest "Patron expiration tests" => sub { ); $schema->storage->txn_rollback; - }; subtest "NoIssuesChargeGuarantees tests" => sub { @@ -521,8 +558,11 @@ subtest "NoIssuesChargeGuarantorsWithGuarantees tests" => sub { }; subtest "Patron messages tests" => sub { + plan tests => 2; + $schema->storage->txn_begin; + my $today = output_pref( { dt => dt_from_string(), dateonly => 1 } ); my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { opacnote => q{} } } ); my $library = $builder->build_object( { class => 'Koha::Libraries' } ); -- 2.51.0