Bug 14757 [QA Followup] - Add Unit Tests
[koha.git] / t / db_dependent / Letters / TemplateToolkit.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2016 ByWater Solutions
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 use Test::More tests => 13;
22
23 use MARC::Record;
24
25 use t::lib::TestBuilder;
26
27 use C4::Letters;
28 use C4::Members;
29 use C4::Biblio;
30 use Koha::Database;
31 use Koha::DateUtils;
32 use Koha::Biblio;
33 use Koha::Biblioitem;
34 use Koha::Item;
35 use Koha::Hold;
36 use Koha::NewsItem;
37 use Koha::Serial;
38 use Koha::Subscription;
39 use Koha::Suggestion;
40 use Koha::Checkout;
41 use Koha::Patron::Modification;
42
43 my $schema = Koha::Database->schema;
44 $schema->storage->txn_begin();
45
46 my $builder = t::lib::TestBuilder->new();
47
48 my $dbh = C4::Context->dbh;
49 $dbh->{RaiseError} = 1;
50
51 $dbh->do(q|DELETE FROM letter|);
52
53 my $date = dt_from_string;
54
55 my $library = $builder->build( { source => 'Branch' } );
56 my $patron  = $builder->build( { source => 'Borrower' } );
57 my $patron2 = $builder->build( { source => 'Borrower' } );
58
59 my $biblio = Koha::Biblio->new(
60     {
61         title => 'Test Biblio'
62     }
63 )->store();
64
65 my $biblioitem = Koha::Biblioitem->new(
66     {
67         biblionumber => $biblio->id()
68     }
69 )->store();
70
71 my $item = Koha::Item->new(
72     {
73         biblionumber     => $biblio->id(),
74         biblioitemnumber => $biblioitem->id()
75     }
76 )->store();
77
78 my $hold = Koha::Hold->new(
79     {
80         borrowernumber => $patron->{borrowernumber},
81         biblionumber   => $biblio->id()
82     }
83 )->store();
84
85 my $news         = Koha::NewsItem->new()->store();
86 my $serial       = Koha::Serial->new()->store();
87 my $subscription = Koha::Subscription->new()->store();
88 my $suggestion   = Koha::Suggestion->new()->store();
89 my $checkout     = Koha::Checkout->new( { itemnumber => $item->id() } )->store();
90 my $modification = Koha::Patron::Modification->new( { verification_token => "TEST" } )->store();
91
92 my $prepared_letter;
93
94 my $sth =
95   $dbh->prepare(q{INSERT INTO letter (module, code, name, title, content) VALUES ('test',?,'Test','Test',?)});
96
97 $sth->execute( "TEST_PATRON", "[% borrower.id %]" );
98 $prepared_letter = GetPreparedLetter(
99     (
100         module      => 'test',
101         letter_code => 'TEST_PATRON',
102         tables      => {
103             borrowers => $patron->{borrowernumber},
104         },
105     )
106 );
107 is( $prepared_letter->{content}, $patron->{borrowernumber}, 'Patron object used correctly with scalar' );
108
109 $prepared_letter = GetPreparedLetter(
110     (
111         module      => 'test',
112         letter_code => 'TEST_PATRON',
113         tables      => {
114             borrowers => $patron,
115         },
116     )
117 );
118 is( $prepared_letter->{content}, $patron->{borrowernumber}, 'Patron object used correctly with hashref' );
119
120 $prepared_letter = GetPreparedLetter(
121     (
122         module      => 'test',
123         letter_code => 'TEST_PATRON',
124         tables      => {
125             borrowers => [ $patron->{borrowernumber} ],
126         },
127     )
128 );
129 is( $prepared_letter->{content}, $patron->{borrowernumber}, 'Patron object used correctly with arrayref' );
130
131 $sth->execute( "TEST_BIBLIO", "[% biblio.id %]" );
132 $prepared_letter = GetPreparedLetter(
133     (
134         module      => 'test',
135         letter_code => 'TEST_BIBLIO',
136         tables      => {
137             biblio => $biblio->id(),
138         },
139     )
140 );
141 is( $prepared_letter->{content}, $biblio->id, 'Biblio object used correctly' );
142
143 $sth->execute( "TEST_LIBRARY", "[% branch.id %]" );
144 $prepared_letter = GetPreparedLetter(
145     (
146         module      => 'test',
147         letter_code => 'TEST_LIBRARY',
148         tables      => {
149             branches => $library->{branchcode}
150         },
151     )
152 );
153 is( $prepared_letter->{content}, $library->{branchcode}, 'Library object used correctly' );
154
155 $sth->execute( "TEST_ITEM", "[% item.id %]" );
156 $prepared_letter = GetPreparedLetter(
157     (
158         module      => 'test',
159         letter_code => 'TEST_ITEM',
160         tables      => {
161             items => $item->id()
162         },
163     )
164 );
165 is( $prepared_letter->{content}, $item->id(), 'Item object used correctly' );
166
167 $sth->execute( "TEST_NEWS", "[% news.id %]" );
168 $prepared_letter = GetPreparedLetter(
169     (
170         module      => 'test',
171         letter_code => 'TEST_NEWS',
172         tables      => {
173             opac_news => $news->id()
174         },
175     )
176 );
177 is( $prepared_letter->{content}, $news->id(), 'News object used correctly' );
178
179 $sth->execute( "TEST_HOLD", "[% hold.id %]" );
180 $prepared_letter = GetPreparedLetter(
181     (
182         module      => 'test',
183         letter_code => 'TEST_HOLD',
184         tables      => {
185             reserves => [ $patron->{borrowernumber}, $biblio->id() ]
186         },
187     )
188 );
189 is( $prepared_letter->{content}, $hold->id(), 'Hold object used correctly' );
190
191 $sth->execute( "TEST_SERIAL", "[% serial.id %]" );
192 $prepared_letter = GetPreparedLetter(
193     (
194         module      => 'test',
195         letter_code => 'TEST_SERIAL',
196         tables      => {
197             serial => $serial->id()
198         },
199     )
200 );
201 is( $prepared_letter->{content}, $serial->id(), 'Serial object used correctly' );
202
203 $sth->execute( "TEST_SUBSCRIPTION", "[% subscription.id %]" );
204 $prepared_letter = GetPreparedLetter(
205     (
206         module      => 'test',
207         letter_code => 'TEST_SUBSCRIPTION',
208         tables      => {
209             subscription => $subscription->id()
210         },
211     )
212 );
213 is( $prepared_letter->{content}, $subscription->id(), 'Subscription object used correctly' );
214
215 $sth->execute( "TEST_SUGGESTION", "[% suggestion.id %]" );
216 $prepared_letter = GetPreparedLetter(
217     (
218         module      => 'test',
219         letter_code => 'TEST_SUGGESTION',
220         tables      => {
221             suggestions => $suggestion->id()
222         },
223     )
224 );
225 is( $prepared_letter->{content}, $suggestion->id(), 'Suggestion object used correctly' );
226
227 $sth->execute( "TEST_ISSUE", "[% checkout.id %]" );
228 $prepared_letter = GetPreparedLetter(
229     (
230         module      => 'test',
231         letter_code => 'TEST_ISSUE',
232         tables      => {
233             issues => $item->id()
234         },
235     )
236 );
237 is( $prepared_letter->{content}, $checkout->id(), 'Checkout object used correctly' );
238
239 $sth->execute( "TEST_MODIFICATION", "[% patron_modification.id %]" );
240 $prepared_letter = GetPreparedLetter(
241     (
242         module      => 'test',
243         letter_code => 'TEST_MODIFICATION',
244         tables      => {
245             borrower_modifications => $modification->verification_token,
246         },
247     )
248 );
249 is( $prepared_letter->{content}, $modification->id(), 'Patron modification object used correctly' );