|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
# |
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Copyright (C) 2018 Andreas Jonsson <andreas.jonsson@kreablo.se> |
| 6 |
# |
| 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. |
| 11 |
# |
| 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. |
| 16 |
# |
| 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>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
|
| 22 |
use Test::More tests => 3; |
| 23 |
use t::lib::TestBuilder; |
| 24 |
use DateTime; |
| 25 |
use File::Spec; |
| 26 |
use File::Basename; |
| 27 |
use Data::Dumper; |
| 28 |
|
| 29 |
my $scriptDir = dirname(File::Spec->rel2abs( __FILE__ )); |
| 30 |
|
| 31 |
my $dbh = C4::Context->dbh; |
| 32 |
|
| 33 |
# Set only to avoid exception. |
| 34 |
$ENV{"OVERRIDE_SYSPREF_dateformat"} = 'metric'; |
| 35 |
|
| 36 |
$dbh->{AutoCommit} = 0; |
| 37 |
$dbh->{RaiseError} = 1; |
| 38 |
|
| 39 |
my $builder = t::lib::TestBuilder->new; |
| 40 |
|
| 41 |
my $library1 = $builder->build({ |
| 42 |
source => 'Branch', |
| 43 |
}); |
| 44 |
my $library2 = $builder->build({ |
| 45 |
source => 'Branch', |
| 46 |
}); |
| 47 |
my $library3 = $builder->build({ |
| 48 |
source => 'Branch', |
| 49 |
}); |
| 50 |
my $borrower = $builder->build({ |
| 51 |
source => 'Borrower', |
| 52 |
value => { |
| 53 |
branchcode => $library1->{branchcode}, |
| 54 |
} |
| 55 |
}); |
| 56 |
$dbh->do(<<DELETESQL); |
| 57 |
DELETE FROM letter |
| 58 |
WHERE module='circulation' |
| 59 |
AND code = 'PREDUEDGST' |
| 60 |
AND message_transport_type='email' |
| 61 |
AND branchcode='' |
| 62 |
DELETESQL |
| 63 |
$dbh->do(<<DELETESQL); |
| 64 |
DELETE FROM message_attributes WHERE message_name = 'Advance_Notice' |
| 65 |
DELETESQL |
| 66 |
|
| 67 |
my $message_attribute = $builder->build({ |
| 68 |
source => 'MessageAttribute', |
| 69 |
value => { |
| 70 |
message_name => 'Advance_Notice' |
| 71 |
} |
| 72 |
}); |
| 73 |
|
| 74 |
my $letter = $builder->build({ |
| 75 |
source => 'Letter', |
| 76 |
value => { |
| 77 |
module => 'circulation', |
| 78 |
code => 'PREDUEDGST', |
| 79 |
branchcode => '', |
| 80 |
message_transport_type => 'email', |
| 81 |
lang => 'default', |
| 82 |
is_html => 0, |
| 83 |
content => '<<count>> <<branches.branchname>>' |
| 84 |
} |
| 85 |
}); |
| 86 |
my $borrower_message_preference = $builder->build({ |
| 87 |
source => 'BorrowerMessagePreference', |
| 88 |
value => { |
| 89 |
borrowernumber => $borrower->{borrowernumber}, |
| 90 |
wants_digest => 1, |
| 91 |
days_in_advance => 1, |
| 92 |
message_attribute_id => $message_attribute->{message_attribute_id} |
| 93 |
} |
| 94 |
}); |
| 95 |
|
| 96 |
my $borrower_message_transport_preference = $builder->build({ |
| 97 |
source => 'BorrowerMessageTransportPreference', |
| 98 |
value => { |
| 99 |
borrower_message_preference_id => $borrower_message_preference->{borrower_message_preference_id}, |
| 100 |
message_transport_type => 'email' |
| 101 |
} |
| 102 |
}); |
| 103 |
|
| 104 |
my $biblio = $builder->build({ |
| 105 |
source => 'Biblio', |
| 106 |
}); |
| 107 |
my $biblioitem = $builder->build({ |
| 108 |
source => 'Biblioitem', |
| 109 |
value => { |
| 110 |
biblionumber => $biblio->{biblionumber} |
| 111 |
} |
| 112 |
}); |
| 113 |
my $item1 = $builder->build({ |
| 114 |
source => 'Item' |
| 115 |
}); |
| 116 |
my $item2 = $builder->build({ |
| 117 |
source => 'Item' |
| 118 |
}); |
| 119 |
my $now = DateTime->now(); |
| 120 |
my $tomorrow = $now->add(days => 1)->strftime('%F'); |
| 121 |
|
| 122 |
my $issue1 = $builder->build({ |
| 123 |
source => 'Issue', |
| 124 |
value => { |
| 125 |
date_due => $tomorrow, |
| 126 |
itemnumber => $item1->{itemnumber}, |
| 127 |
branchcode => $library1->{branchcode}, |
| 128 |
borrowernumber => $borrower->{borrowernumber}, |
| 129 |
returndate => undef |
| 130 |
} |
| 131 |
}); |
| 132 |
|
| 133 |
my $issue2 = $builder->build({ |
| 134 |
source => 'Issue', |
| 135 |
value => { |
| 136 |
date_due => $tomorrow, |
| 137 |
itemnumber => $item2->{itemnumber}, |
| 138 |
branchcode => $library2->{branchcode}, |
| 139 |
branchcode => $library3->{branchcode}, |
| 140 |
borrowernumber => $borrower->{borrowernumber}, |
| 141 |
returndate => undef |
| 142 |
} |
| 143 |
}); |
| 144 |
|
| 145 |
C4::Context->set_preference('EnhancedMessagingPreferences', 1); |
| 146 |
|
| 147 |
my $script = ''; |
| 148 |
my $scriptFile = "$scriptDir/../../../misc/cronjobs/advance_notices.pl"; |
| 149 |
open my $scriptfh, "<", $scriptFile or die "Failed to open $scriptFile: $!"; |
| 150 |
|
| 151 |
while (<$scriptfh>) { |
| 152 |
$script .= $_; |
| 153 |
} |
| 154 |
close $scriptfh; |
| 155 |
|
| 156 |
@ARGV = ('advanced_notices.pl', '-c'); |
| 157 |
|
| 158 |
## no critic |
| 159 |
|
| 160 |
# We simulate script execution by evaluating the script code in the context |
| 161 |
# of this unit test. |
| 162 |
|
| 163 |
eval $script; #Violates 'ProhibitStringyEval' |
| 164 |
|
| 165 |
## use critic |
| 166 |
|
| 167 |
die $@ if $@; |
| 168 |
|
| 169 |
my $sthmq = $dbh->prepare('SELECT * FROM message_queue WHERE borrowernumber = ?'); |
| 170 |
$sthmq->execute($borrower->{borrowernumber}); |
| 171 |
|
| 172 |
my $messages = $sthmq->fetchall_hashref('message_id'); |
| 173 |
|
| 174 |
is(scalar(keys %$messages), 1, 'There is one message in the queue'); |
| 175 |
for my $message (keys %$messages) { |
| 176 |
$messages->{$message}->{content} =~ /(\d+) (.*)/; |
| 177 |
my $count = $1; |
| 178 |
my $branchname = $2; |
| 179 |
|
| 180 |
is ($count, '2', 'Issue count is 2'); |
| 181 |
is ($branchname, $library1->{branchname}, 'Branchname is that of borrowers home branch.'); |
| 182 |
} |
| 183 |
|
| 184 |
$dbh->rollback; |