From be8632dc2ab90d500c4f3649a63195f78f93802e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nazl=C4=B1=20=C3=87etin?=
Date: Wed, 6 Feb 2019 12:01:04 +0000
Subject: [PATCH] Bug 5770: Unit test and skeleton perl
---
C4/Letters.pm | 5 +++
C4/Suggestions.pm | 2 +-
installer/data/mysql/atomicupdate/bug_5770.perl | 39 +++++++++++++++++
installer/data/mysql/atomicupdate/bug_5770.sql | 30 -------------
t/db_dependent/Suggestions.t | 58 ++++++++++++++++++++++++-
5 files changed, 101 insertions(+), 33 deletions(-)
create mode 100644 installer/data/mysql/atomicupdate/bug_5770.perl
delete mode 100644 installer/data/mysql/atomicupdate/bug_5770.sql
diff --git a/C4/Letters.pm b/C4/Letters.pm
index 0f2378d6603..810d06cde30 100644
--- a/C4/Letters.pm
+++ b/C4/Letters.pm
@@ -1077,6 +1077,11 @@ ENDSQL
push @query_params, $params->{'borrowernumber'};
}
+ if ( exists $params->{'letter_code'} ) {
+ push @whereclauses, ' letter_code = ? ';
+ push @query_params, $params->{'letter_code'};
+ }
+
if ( @whereclauses ) {
$statement .= ' WHERE ' . join( 'AND', @whereclauses );
}
diff --git a/C4/Suggestions.pm b/C4/Suggestions.pm
index ece7c413cd1..08a02f844d8 100644
--- a/C4/Suggestions.pm
+++ b/C4/Suggestions.pm
@@ -471,7 +471,7 @@ sub NewSuggestion {
my $emailpurchasesuggestions = C4::Context->preference("EmailPurchaseSuggestions");
my $toaddress = ( $emailpurchasesuggestions eq "BranchEmailAddress" )
- ? Koha::Libraries->find($full_suggestion->{branchcode})->branchemail
+ ? ( Koha::Libraries->find($full_suggestion->{branchcode})->branchemail || C4::Context->preference('KohaAdminEmailAddress') )
: C4::Context->preference( $emailpurchasesuggestions ) ;
C4::Letters::EnqueueLetter(
diff --git a/installer/data/mysql/atomicupdate/bug_5770.perl b/installer/data/mysql/atomicupdate/bug_5770.perl
new file mode 100644
index 00000000000..7e4e7de6d1d
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_5770.perl
@@ -0,0 +1,39 @@
+$DBversion = '18.12.00.XXX'; # will be replaced by the RM
+if( CheckVersion( $DBversion ) ) {
+ $dbh->do( q{
+ INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES
+ ('EmailPurchaseSuggestions','KohaAdminEmailAddress','EmailAddressForSuggestions|BranchEmailAddress|KohaAdminEmailAddress','Choose email address that will be sent new purchase suggestions','Choice'),
+ ('EmailAddressForSuggestions','','','If you choose EmailAddressForSuggestions you should enter a valid email address','free')
+ });
+ $dbh->do( q{
+ INSERT IGNORE INTO `letter` (module, code, name, title, content, is_html, message_transport_type) VALUES
+ ('suggestions','NEW_SUGGESTION','New suggestion','New suggestion','Suggestion pending approval
+ Suggested by
+
+
+ Title suggested
+
+ - Library: <>
+ - Title: <>
+ - Author: <>
+ - Copyright date: <>
+ - Standard number (ISBN, ISSN or other): <>
+ - Publisher: <>
+ - Collection title: <>
+ - Publication place: <>
+ - Quantity: <>
+ - Item type: <>
+ - Reason for suggestion: <>
+ - Notes: <>
+
+ ',1, 'email')
+ });
+
+ SetVersion( $DBversion );
+ print "Upgrade to $DBversion done (Bug 5770 - Email librarian when purchase suggestion made)\n";
+}
diff --git a/installer/data/mysql/atomicupdate/bug_5770.sql b/installer/data/mysql/atomicupdate/bug_5770.sql
deleted file mode 100644
index 7537972bcba..00000000000
--- a/installer/data/mysql/atomicupdate/bug_5770.sql
+++ /dev/null
@@ -1,30 +0,0 @@
-INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES
-('EmailPurchaseSuggestions','KohaAdminEmailAddress','EmailAddressForSuggestions|BranchEmailAddress|KohaAdminEmailAddress','Choose email address that will be sent new purchase suggestions','Choice'),
-('EmailAddressForSuggestions','','','If you choose EmailAddressForSuggestions you should enter a valid email address','free');
-
-INSERT IGNORE INTO `letter` (module, code, name, title, content, is_html, message_transport_type)
-VALUES ('suggestions','NEW_SUGGESTION','New suggestion','New suggestion','Suggestion pending approval
-Suggested by
-
-
-Title suggested
-
- - Library: <>
- - Title: <>
- - Author: <>
- - Copyright date: <>
- - Standard number (ISBN, ISSN or other): <>
- - Publisher: <>
- - Collection title: <>
- - Publication place: <>
- - Quantity: <>
- - Item type: <>
- - Reason for suggestion: <>
- - Notes: <>
-
-',1, 'email');
diff --git a/t/db_dependent/Suggestions.t b/t/db_dependent/Suggestions.t
index 16e0dd2a29c..4f2a350af83 100644
--- a/t/db_dependent/Suggestions.t
+++ b/t/db_dependent/Suggestions.t
@@ -18,7 +18,7 @@
use Modern::Perl;
use DateTime::Duration;
-use Test::More tests => 106;
+use Test::More tests => 107;
use Test::Warn;
use t::lib::Mocks;
@@ -32,7 +32,6 @@ use Koha::DateUtils qw( dt_from_string output_pref );
use Koha::Libraries;
use Koha::Patrons;
use Koha::Suggestions;
-
BEGIN {
use_ok('C4::Suggestions');
}
@@ -61,6 +60,7 @@ $dbh->do(q|DELETE FROM borrowers|);
$dbh->do(q|DELETE FROM letter|);
$dbh->do(q|DELETE FROM message_queue|);
$dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'CHECKED', 'my content')|);
+$dbh->do(q|INSERT INTO letter(module, code, content) VALUES ('suggestions', 'NEW_SUGGESTION', 'Content for new suggestion')|);
# Add CPL if missing.
if (not defined Koha::Libraries->find('CPL')) {
@@ -95,6 +95,7 @@ my $my_suggestion = {
publishercode => 'my publishercode',
suggestedby => $borrowernumber,
biblionumber => $biblionumber1,
+ branchcode => 'CPL',
managedby => '',
manageddate => '',
accepteddate => dt_from_string,
@@ -190,6 +191,7 @@ is( $suggestion->{note}, 'my note', 'ModSuggestion should not erase data if not
my $messages = C4::Letters::GetQueuedMessages({
borrowernumber => $borrowernumber,
+ letter_code => 'CHECKED',
});
is( @$messages, 0, 'ModSuggestions does not send an email if the status is not updated' );
@@ -217,6 +219,7 @@ $suggestion = GetSuggestion($my_suggestionid);
is( $suggestion->{STATUS}, $mod_suggestion3->{STATUS}, 'ModSuggestion modifies the status correctly' );
$messages = C4::Letters::GetQueuedMessages({
borrowernumber => $borrowernumber,
+ letter_code => 'CHECKED',
});
is( @$messages, 1, 'ModSuggestion sends an email if the status is updated' );
is ($messages->[0]->{message_transport_type}, 'email', 'When FallbackToSMSIfNoEmail syspref is disabled the suggestion message_transport_type is always email');
@@ -227,6 +230,7 @@ t::lib::Mocks::mock_preference( 'FallbackToSMSIfNoEmail', 1 );
ModSuggestion($mod_suggestion3);
$messages = C4::Letters::GetQueuedMessages({
borrowernumber => $borrowernumber,
+ letter_code => 'CHECKED',
});
is ($messages->[1]->{message_transport_type}, 'sms', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is sms if the borrower has no email');
@@ -242,6 +246,7 @@ my $mod_suggestion4 = {
ModSuggestion($mod_suggestion4);
$messages = C4::Letters::GetQueuedMessages({
borrowernumber => $borrowernumber2,
+ letter_code => 'CHECKED',
});
is ($messages->[0]->{message_transport_type}, 'email', 'When FallbackToSMSIfNoEmail syspref is enabled the suggestion message_transport_type is email if the borrower has an email');
@@ -462,4 +467,53 @@ subtest 'DelSuggestionsOlderThan' => sub {
is( Koha::Suggestions->count, 2, '1 suggestions>1d deleted' );
};
+subtest 'EmailPurchaseSuggestions' => sub {
+ plan tests => 5;
+
+ $dbh->do(q|DELETE FROM message_queue|);
+
+ Koha::Libraries->find('CPL')->update({ branchemail => 'branchemail@b.c' });
+ t::lib::Mocks::mock_preference( "KohaAdminEmailAddress", 'root@localhost');
+ t::lib::Mocks::mock_preference( "EmailAddressForSuggestions", 'a@b.c');
+ t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "KohaAdminEmailAddress"); # EmailAddressForSuggestions or BranchEmailAddress or KohaAdminEmailAddress
+
+ NewSuggestion($my_suggestion);
+ my $newsuggestions_messages = C4::Letters::GetQueuedMessages({
+ borrowernumber => $borrowernumber,
+ letter_code => 'NEW_SUGGESTION',
+ });
+
+ is( @$newsuggestions_messages, 1, 'NewSuggestions sends an email' );
+ my $message1 = C4::Letters::GetMessage( $newsuggestions_messages->[0]->{message_id});
+ is ($message1->{to_address}, 'root@localhost', 'to_address is KohaAdminEmailAddress');
+
+ t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "EmailAddressForSuggestions");
+ NewSuggestion($my_suggestion);
+ $newsuggestions_messages = C4::Letters::GetQueuedMessages({
+ borrowernumber => $borrowernumber,
+ letter_code => 'NEW_SUGGESTION',
+ });
+ my $message2 = C4::Letters::GetMessage( $newsuggestions_messages->[1]->{message_id});
+ is ($message2->{to_address}, 'a@b.c', 'to_address is EmailAddressForSuggestions');
+
+ t::lib::Mocks::mock_preference( "EmailPurchaseSuggestions", "BranchEmailAddress");
+ NewSuggestion($my_suggestion);
+ $newsuggestions_messages = C4::Letters::GetQueuedMessages({
+ borrowernumber => $borrowernumber,
+ letter_code => 'NEW_SUGGESTION',
+ });
+ my $message3 = C4::Letters::GetMessage( $newsuggestions_messages->[2]->{message_id});
+ is ($message3->{to_address}, 'branchemail@b.c', 'to_address is BranchEmailAddress');
+
+ Koha::Libraries->find('CPL')->update({ branchemail => undef });
+ NewSuggestion($my_suggestion);
+ $newsuggestions_messages = C4::Letters::GetQueuedMessages({
+ borrowernumber => $borrowernumber,
+ letter_code => 'NEW_SUGGESTION',
+ });
+ my $message4 = C4::Letters::GetMessage( $newsuggestions_messages->[3]->{message_id});
+ isnt ($message4->{to_address}, 'branchemail@b.c', 'to_address is KohaAdminEmailAddress. Because branchemail is undef');
+
+};
+
$schema->storage->txn_rollback;
--
2.11.0