3 # This file is part of Koha.
5 # Copyright (C) 2018 Andreas Jonsson <andreas.jonsson@kreablo.se>
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 3;
23 use t::lib::TestBuilder;
29 my $scriptDir = dirname(File::Spec->rel2abs( __FILE__ ));
31 my $dbh = C4::Context->dbh;
33 # Set only to avoid exception.
34 $ENV{"OVERRIDE_SYSPREF_dateformat"} = 'metric';
36 $dbh->{AutoCommit} = 0;
37 $dbh->{RaiseError} = 1;
39 my $builder = t::lib::TestBuilder->new;
41 my $library1 = $builder->build({
44 my $library2 = $builder->build({
47 my $library3 = $builder->build({
50 my $borrower = $builder->build({
53 branchcode => $library1->{branchcode},
56 $dbh->do(<<DELETESQL);
58 WHERE module='circulation'
59 AND code = 'PREDUEDGST'
60 AND message_transport_type='email'
63 $dbh->do(<<DELETESQL);
64 DELETE FROM message_attributes WHERE message_name = 'Advance_Notice'
67 my $message_attribute = $builder->build({
68 source => 'MessageAttribute',
70 message_name => 'Advance_Notice'
74 my $letter = $builder->build({
77 module => 'circulation',
80 message_transport_type => 'email',
83 content => '<<count>> <<branches.branchname>>'
86 my $borrower_message_preference = $builder->build({
87 source => 'BorrowerMessagePreference',
89 borrowernumber => $borrower->{borrowernumber},
92 message_attribute_id => $message_attribute->{message_attribute_id}
96 my $borrower_message_transport_preference = $builder->build({
97 source => 'BorrowerMessageTransportPreference',
99 borrower_message_preference_id => $borrower_message_preference->{borrower_message_preference_id},
100 message_transport_type => 'email'
104 my $biblio = $builder->build({
107 my $biblioitem = $builder->build({
108 source => 'Biblioitem',
110 biblionumber => $biblio->{biblionumber}
113 my $item1 = $builder->build({
116 my $item2 = $builder->build({
119 my $now = DateTime->now();
120 my $tomorrow = $now->add(days => 1)->strftime('%F');
122 my $issue1 = $builder->build({
125 date_due => $tomorrow,
126 itemnumber => $item1->{itemnumber},
127 branchcode => $library1->{branchcode},
128 borrowernumber => $borrower->{borrowernumber},
133 my $issue2 = $builder->build({
136 date_due => $tomorrow,
137 itemnumber => $item2->{itemnumber},
138 branchcode => $library2->{branchcode},
139 branchcode => $library3->{branchcode},
140 borrowernumber => $borrower->{borrowernumber},
145 C4::Context->set_preference('EnhancedMessagingPreferences', 1);
148 my $scriptFile = "$scriptDir/../../../misc/cronjobs/advance_notices.pl";
149 open my $scriptfh, "<", $scriptFile or die "Failed to open $scriptFile: $!";
151 while (<$scriptfh>) {
156 @ARGV = ('advanced_notices.pl', '-c');
160 # We simulate script execution by evaluating the script code in the context
163 eval $script; #Violates 'ProhibitStringyEval'
169 my $sthmq = $dbh->prepare('SELECT * FROM message_queue WHERE borrowernumber = ?');
170 $sthmq->execute($borrower->{borrowernumber});
172 my $messages = $sthmq->fetchall_hashref('message_id');
174 is(scalar(keys %$messages), 1, 'There is one message in the queue');
175 for my $message (keys %$messages) {
176 $messages->{$message}->{content} =~ /(\d+) (.*)/;
180 is ($count, '2', 'Issue count is 2');
181 is ($branchname, $library1->{branchname}, 'Branchname is that of borrowers home branch.');