View | Details | Raw Unified | Return to bug 20478
Collapse All | Expand All

(-)a/t/db_dependent/cronjobs/advance_notices_digest.t (-1 / +174 lines)
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
64
my $message_attribute = $builder->build({
65
    source => 'MessageAttribute',
66
    value => {
67
	message_name => 'advance_notice'
68
    }
69
});
70
    
71
my $letter = $builder->build({
72
    source => 'Letter',
73
    value => {
74
        module => 'circulation',
75
        code => 'PREDUEDGST',
76
        branchcode => '',
77
        message_transport_type => 'email',
78
        lang => 'default',
79
        is_html => 0,
80
        content => '<<count>> <<branches.branchname>>'
81
    }
82
});
83
my $borrower_message_preference = $builder->build({
84
    source => 'BorrowerMessagePreference',
85
    value => {
86
        borrowernumber => $borrower->{borrowernumber},
87
        wants_digest => 1,
88
        days_in_advance => 1,
89
        message_attribute_id => $message_attribute->{message_attribute_id}
90
    }
91
});
92
93
my $borrower_message_transport_preference = $builder->build({
94
    source => 'BorrowerMessageTransportPreference',
95
    value => {
96
        borrower_message_preference_id => $borrower_message_preference->{borrower_message_preference_id},
97
        message_transport_type => 'email'
98
    }
99
});
100
101
my $biblio = $builder->build({
102
    source => 'Biblio',
103
});
104
my $biblioitem = $builder->build({
105
    source => 'Biblioitem',
106
    value => {
107
        biblionumber => $biblio->{biblionumber}
108
    }
109
});
110
my $item1 = $builder->build({
111
    source => 'Item'
112
});
113
my $item2 = $builder->build({
114
    source => 'Item'
115
});
116
my $now = DateTime->now();
117
my $tomorrow = $now->add(days => 1)->strftime('%F');
118
119
my $issue1 = $builder->build({
120
    source => 'Issue',
121
    value => {
122
        date_due => $tomorrow,
123
        itemnumber => $item1->{itemnumber},
124
        branchcode => $library1->{branchcode},
125
        borrowernumber => $borrower->{borrowernumber},
126
        returndate => undef
127
    }
128
});
129
130
my $issue2 = $builder->build({
131
    source => 'Issue',
132
    value => {
133
        date_due => $tomorrow,
134
        itemnumber => $item2->{itemnumber},
135
        branchcode => $library2->{branchcode},
136
        branchcode => $library3->{branchcode},
137
        borrowernumber => $borrower->{borrowernumber},
138
        returndate => undef
139
    }
140
});
141
142
C4::Context->set_preference('EnhancedMessagingPreferences', 1);
143
144
my $script = '';
145
my $scriptFile = "$scriptDir/../../../misc/cronjobs/advance_notices.pl";
146
open SCRIPT, "<", $scriptFile or die "Failed to open $scriptFile: $!";
147
148
while (<SCRIPT>) {
149
    $script .= $_;
150
}
151
close SCRIPT;
152
153
@ARGV = ('advanced_notices.pl', '-c');
154
155
eval $script;
156
die $@ if $@;
157
158
my $sthmq = $dbh->prepare('SELECT * FROM message_queue WHERE borrowernumber = ?');
159
$sthmq->execute($borrower->{borrowernumber});
160
161
my $messages = $sthmq->fetchall_hashref('message_id');
162
163
is(scalar(keys %$messages), 1, 'There is one message in the queue');
164
for my $message (keys %$messages) {
165
    $messages->{$message}->{content} =~ /(\d+) (.*)/;
166
    my $count = $1;
167
    my $branchname = $2;
168
169
    is ($count, '2', 'Issue count is 2');
170
    is ($branchname, $library1->{branchname}, 'Branchname is that of borrowers home branch.');
171
}
172
173
$dbh->rollback;
174

Return to bug 20478