From c72fc4ff1ee0e689ea0404dbb3d9c20782c5ae78 Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Wed, 6 Apr 2022 10:38:38 +0000 Subject: [PATCH] Bug 23538: Unit tests Test plan: 1. Run unit tests: sudo koha-shell cd t/db_dependent/Koha prove -v Patron.t Sponsored-by: Catalyst IT Signed-off-by: David Nind Signed-off-by: Katrin Fischer --- t/db_dependent/Koha/Patron.t | 58 +++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 977ec261d6..fa035b8c04 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 18; +use Test::More tests => 19; use Test::Exception; use Test::Warn; @@ -1292,3 +1292,59 @@ subtest 'encode_secret and decoded_secret' => sub { $schema->storage->txn_rollback; }; + +subtest 'notify_library_of_registration()' => sub { + + plan tests => 6; + + $schema->storage->txn_begin; + my $dbh = C4::Context->dbh; + + my $library = $builder->build_object( + { + class => 'Koha::Libraries', + value => { + branchemail => 'from@mybranch.com', + branchreplyto => 'to@mybranch.com' + } + } + ); + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { + branchcode => $library->branchcode + } + } + ); + + t::lib::Mocks::mock_preference( 'KohaAdminEmailAddress', 'root@localhost' ); + t::lib::Mocks::mock_preference( 'EmailAddressForPatronRegistrations', 'library@localhost' ); + + # Test when EmailPatronRegistrations equals BranchEmailAddress + t::lib::Mocks::mock_preference( 'EmailPatronRegistrations', 'BranchEmailAddress' ); + is( $patron->notify_library_of_registration(C4::Context->preference('EmailPatronRegistrations')), 1, 'OPAC_REG email is queued if EmailPatronRegistration syspref equals BranchEmailAddress'); + my $sth = $dbh->prepare("SELECT to_address FROM message_queue where borrowernumber = ?"); + $sth->execute( $patron->borrowernumber ); + my $to_address = $sth->fetchrow_array; + is( $to_address, 'to@mybranch.com', 'OPAC_REG email queued to go to branchreplyto address when EmailPatronRegistration equals BranchEmailAddress' ); + $dbh->do(q|DELETE FROM message_queue|); + + # Test when EmailPatronRegistrations equals EmailAddressForPatronRegistrations + t::lib::Mocks::mock_preference( 'EmailPatronRegistrations', 'EmailAddressForPatronRegistrations' ); + is( $patron->notify_library_of_registration(C4::Context->preference('EmailPatronRegistrations')), 1, 'OPAC_REG email is queued if EmailPatronRegistration syspref equals EmailAddressForPatronRegistrations'); + $sth->execute( $patron->borrowernumber ); + $to_address = $sth->fetchrow_array; + is( $to_address, 'library@localhost', 'OPAC_REG email queued to go to EmailAddressForPatronRegistrations syspref when EmailPatronRegistration equals EmailAddressForPatronRegistrations' ); + $dbh->do(q|DELETE FROM message_queue|); + + # Test when EmailPatronRegistrations equals KohaAdminEmailAddress + t::lib::Mocks::mock_preference( 'EmailPatronRegistrations', 'KohaAdminEmailAddress' ); + is( $patron->notify_library_of_registration(C4::Context->preference('EmailPatronRegistrations')), 1, 'OPAC_REG email is queued if EmailPatronRegistration syspref equals KohaAdminEmailAddress'); + $sth->execute( $patron->borrowernumber ); + $to_address = $sth->fetchrow_array; + is( $to_address, 'root@localhost', 'OPAC_REG email queued to go to KohaAdminEmailAddress syspref when EmailPatronRegistration equals KohaAdminEmailAddress' ); + $dbh->do(q|DELETE FROM message_queue|); + + $schema->storage->txn_rollback; +}; -- 2.30.2