Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Copyright Vaara-kirjastot 2015 |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it under the |
8 |
# terms of the GNU General Public License as published by the Free Software |
9 |
# Foundation; either version 3 of the License, or (at your option) any later |
10 |
# version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
14 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
15 |
# |
16 |
# You should have received a copy of the GNU General Public License along |
17 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
# |
20 |
|
21 |
use Modern::Perl; |
22 |
|
23 |
use Test::More; |
24 |
use DateTime; |
25 |
|
26 |
use Koha::DateUtils; |
27 |
|
28 |
use t::lib::TestObjects::ObjectFactory; |
29 |
use t::lib::TestObjects::BorrowerFactory; |
30 |
use Koha::Borrowers; |
31 |
use t::lib::TestObjects::ItemFactory; |
32 |
use Koha::Items; |
33 |
use t::lib::TestObjects::BiblioFactory; |
34 |
use Koha::Biblios; |
35 |
use t::lib::TestObjects::CheckoutFactory; |
36 |
use Koha::Checkouts; |
37 |
use t::lib::TestObjects::LetterTemplateFactory; |
38 |
use Koha::LetterTemplates; |
39 |
|
40 |
|
41 |
my $testContext = {}; #Gather all created Objects here so we can finally remove them all. |
42 |
|
43 |
|
44 |
|
45 |
########## BorrowerFactory subtests ########## |
46 |
subtest 't::lib::TestObjects::BorrowerFactory' => sub { |
47 |
my $subtestContext = {}; |
48 |
##Create and Delete. Add one |
49 |
my $f = t::lib::TestObjects::BorrowerFactory->new(); |
50 |
my $objects = $f->createTestGroup([ |
51 |
{firstname => 'Olli-Antti', |
52 |
surname => 'Kivi', |
53 |
cardnumber => '11A001', |
54 |
branchcode => 'CPL', |
55 |
}, |
56 |
], undef, $subtestContext, undef, $testContext); |
57 |
is($objects->{'11A001'}->cardnumber, '11A001', "Borrower '11A001'."); |
58 |
##Add one more to test incrementing the subtestContext. |
59 |
$objects = $f->createTestGroup([ |
60 |
{firstname => 'Olli-Antti2', |
61 |
surname => 'Kivi2', |
62 |
cardnumber => '11A002', |
63 |
branchcode => 'FFL', |
64 |
}, |
65 |
], undef, $subtestContext, undef, $testContext); |
66 |
is($subtestContext->{borrowers}->{'11A001'}->cardnumber, '11A001', "Borrower '11A001' from \$subtestContext."); #From subtestContext |
67 |
is($objects->{'11A002'}->branchcode, 'FFL', "Borrower '11A002'."); #from just created hash. |
68 |
|
69 |
##Delete objects |
70 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($subtestContext); |
71 |
my $object11A001 = Koha::Borrowers->find({cardnumber => '11A001'}); |
72 |
ok (not($object11A001), "Borrower '11A001' deleted"); |
73 |
my $object11A002 = Koha::Borrowers->find({cardnumber => '11A002'}); |
74 |
ok (not($object11A002), "Borrower '11A002' deleted"); |
75 |
|
76 |
#Prepare for autoremoval. |
77 |
$objects = $f->createTestGroup([ |
78 |
{firstname => 'Olli-Antti', |
79 |
surname => 'Kivi', |
80 |
cardnumber => '11A001', |
81 |
branchcode => 'CPL', |
82 |
}, |
83 |
{firstname => 'Olli-Antti2', |
84 |
surname => 'Kivi2', |
85 |
cardnumber => '11A002', |
86 |
branchcode => 'FFL', |
87 |
}, |
88 |
], undef, undef, undef, $testContext); |
89 |
}; |
90 |
|
91 |
|
92 |
|
93 |
########## BiblioFactory and ItemFactory subtests ########## |
94 |
subtest 't::lib::TestObjects::BiblioFactory and ::ItemFactory' => sub { |
95 |
my $subtestContext = {}; |
96 |
##Create and Delete. Add one |
97 |
my $fb = t::lib::TestObjects::BiblioFactory->new(); |
98 |
my $biblios = $fb->createTestGroup([ |
99 |
{'biblio.title' => 'I wish I met your mother', |
100 |
'biblio.author' => 'Pertti Kurikka', |
101 |
'biblio.copyrightdate' => '1960', |
102 |
'biblioitems.isbn' => '9519671580', |
103 |
'biblioitems.itemtype' => 'BK', |
104 |
}, |
105 |
], 'biblioitems.isbn', $subtestContext, undef, $testContext); |
106 |
my $f = t::lib::TestObjects::ItemFactory->new(); |
107 |
my $objects = $f->createTestGroup([ |
108 |
{biblionumber => $biblios->{9519671580}->{biblionumber}, |
109 |
barcode => '167Nabe0001', |
110 |
homebranch => 'CPL', |
111 |
holdingbranch => 'CPL', |
112 |
price => '0.50', |
113 |
replacementprice => '0.50', |
114 |
itype => 'BK', |
115 |
biblioisbn => '9519671580', |
116 |
itemcallnumber => 'PK 84.2', |
117 |
}, |
118 |
], 'barcode', $subtestContext, undef, $testContext); |
119 |
|
120 |
is($objects->{'167Nabe0001'}->barcode, '167Nabe0001', "Item '167Nabe0001'."); |
121 |
##Add one more to test incrementing the subtestContext. |
122 |
$objects = $f->createTestGroup([ |
123 |
{biblionumber => $biblios->{9519671580}->{biblionumber}, |
124 |
barcode => '167Nabe0002', |
125 |
homebranch => 'CPL', |
126 |
holdingbranch => 'FFL', |
127 |
price => '3.50', |
128 |
replacementprice => '3.50', |
129 |
itype => 'BK', |
130 |
biblioisbn => '9519671580', |
131 |
itemcallnumber => 'JK 84.2', |
132 |
}, |
133 |
], 'barcode', $subtestContext, undef, $testContext); |
134 |
|
135 |
is($subtestContext->{items}->{'167Nabe0001'}->barcode, '167Nabe0001', "Item '167Nabe0001' from \$subtestContext."); |
136 |
is($objects->{'167Nabe0002'}->holdingbranch, 'FFL', "Item '167Nabe0002'."); |
137 |
is(ref($biblios->{9519671580}), 'MARC::Record', "Biblio 'I wish I met your mother'."); |
138 |
|
139 |
##Delete objects |
140 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($subtestContext); |
141 |
my $object1 = Koha::Items->find({barcode => '167Nabe0001'}); |
142 |
ok (not($object1), "Item '167Nabe0001' deleted"); |
143 |
my $object2 = Koha::Items->find({barcode => '167Nabe0002'}); |
144 |
ok (not($object2), "Item '167Nabe0002' deleted"); |
145 |
my $object3 = Koha::Biblios->find({title => 'I wish I met your mother', author => "Pertti Kurikka"}); |
146 |
ok (not($object2), "Biblio 'I wish I met your mother' deleted"); |
147 |
|
148 |
#Prepare for autoremoval. |
149 |
$biblios = $fb->createTestGroup([ |
150 |
{'biblio.title' => 'I wish I met your mother', |
151 |
'biblio.author' => 'Pertti Kurikka', |
152 |
'biblio.copyrightdate' => '1960', |
153 |
'biblioitems.isbn' => '9519671580', |
154 |
'biblioitems.itemtype' => 'BK', |
155 |
}, |
156 |
], 'biblioitems.isbn', undef, undef, $testContext); |
157 |
$objects = $f->createTestGroup([ |
158 |
{biblionumber => $biblios->{9519671580}->{biblionumber}, |
159 |
barcode => '167Nabe0001', |
160 |
homebranch => 'CPL', |
161 |
holdingbranch => 'CPL', |
162 |
price => '0.50', |
163 |
replacementprice => '0.50', |
164 |
itype => 'BK', |
165 |
biblioisbn => '9519671580', |
166 |
itemcallnumber => 'PK 84.2', |
167 |
}, |
168 |
{biblionumber => $biblios->{9519671580}->{biblionumber}, |
169 |
barcode => '167Nabe0002', |
170 |
homebranch => 'CPL', |
171 |
holdingbranch => 'FFL', |
172 |
price => '3.50', |
173 |
replacementprice => '3.50', |
174 |
itype => 'BK', |
175 |
biblioisbn => '9519671580', |
176 |
itemcallnumber => 'JK 84.2', |
177 |
}, |
178 |
], 'barcode', undef, undef, $testContext); |
179 |
}; |
180 |
|
181 |
|
182 |
|
183 |
########## CheckoutFactory subtests ########## |
184 |
subtest 't::lib::TestObjects::CheckoutFactory' => sub { |
185 |
my $subtestContext = {}; |
186 |
##Create and Delete using dependencies in the $testContext instantiated in previous subtests. |
187 |
my $f = t::lib::TestObjects::CheckoutFactory->new(); |
188 |
my $objects = $f->createTestGroup([ |
189 |
{ |
190 |
cardnumber => '11A001', |
191 |
barcode => '167Nabe0001', |
192 |
daysOverdue => 7, |
193 |
daysAgoCheckedout => 28, |
194 |
}, |
195 |
{ |
196 |
cardnumber => '11A002', |
197 |
barcode => '167Nabe0002', |
198 |
daysOverdue => -7, |
199 |
daysAgoCheckedout => 14, |
200 |
}, |
201 |
], undef, undef, undef, undef); |
202 |
|
203 |
is(Koha::DateUtils::dt_from_string($objects->{'11A001-167Nabe0001'}->issuedate)->day(), |
204 |
DateTime->now(time_zone => C4::Context->tz())->subtract(days => '28')->day() |
205 |
, "Checkout '11A001-167Nabe0001', adjusted issuedates match."); |
206 |
is(Koha::DateUtils::dt_from_string($objects->{'11A002-167Nabe0002'}->date_due)->day(), |
207 |
DateTime->now(time_zone => C4::Context->tz())->subtract(days => '-7')->day() |
208 |
, "Checkout '11A002-167Nabe0002', adjusted date_dues match."); |
209 |
|
210 |
$f->deleteTestGroup($objects); |
211 |
my $object1 = Koha::Checkouts->find({borrowernumber => $objects->{'11A001-167Nabe0001'}->borrowernumber, |
212 |
itemnumber => $objects->{'11A001-167Nabe0001'}->itemnumber}); |
213 |
ok (not($object1), "Checkout '11A001-167Nabe0001' deleted"); |
214 |
my $object2 = Koha::Checkouts->find({borrowernumber => $objects->{'11A002-167Nabe0002'}->borrowernumber, |
215 |
itemnumber => $objects->{'11A002-167Nabe0002'}->itemnumber}); |
216 |
ok (not($object2), "Checkout '11A002-167Nabe0002' deleted"); |
217 |
}; |
218 |
|
219 |
|
220 |
|
221 |
########## LetterTemplateFactory subtests ########## |
222 |
subtest 't::lib::TestObjects::LetterTemplateFactory' => sub { |
223 |
my $subtestContext = {}; |
224 |
##Create and Delete using dependencies in the $testContext instantiated in previous subtests. |
225 |
my $f = t::lib::TestObjects::LetterTemplateFactory->new(); |
226 |
my $hashLT = {letter_id => 'circulation-ODUE1-CPL-print', |
227 |
module => 'circulation', |
228 |
code => 'ODUE1', |
229 |
branchcode => 'CPL', |
230 |
name => 'Notice1', |
231 |
is_html => undef, |
232 |
title => 'Notice1', |
233 |
message_transport_type => 'print', |
234 |
content => '<item>Barcode: <<items.barcode>>, bring it back!</item>', |
235 |
}; |
236 |
my $objects = $f->createTestGroup([ |
237 |
$hashLT, |
238 |
], undef, undef, undef, undef); |
239 |
|
240 |
my $letterTemplate = Koha::LetterTemplates->find($hashLT); |
241 |
is($objects->{'circulation-ODUE1-CPL-print'}->name, $letterTemplate->name, "LetterTemplate 'circulation-ODUE1-CPL-print'"); |
242 |
|
243 |
#Delete them |
244 |
$f->deleteTestGroup($objects); |
245 |
$letterTemplate = Koha::LetterTemplates->find($hashLT); |
246 |
ok(not(defined($letterTemplate)), "LetterTemplate 'circulation-ODUE1-CPL-print' deleted"); |
247 |
}; |
248 |
|
249 |
|
250 |
|
251 |
########## Global test context subtests ########## |
252 |
subtest 't::lib::TestObjects::ObjectFactory clearing global test context' => sub { |
253 |
my $object11A001 = Koha::Borrowers->find({cardnumber => '11A001'}); |
254 |
ok ($object11A001, "Global Borrower '11A001' exists"); |
255 |
my $object11A002 = Koha::Borrowers->find({cardnumber => '11A002'}); |
256 |
ok ($object11A002, "Global Borrower '11A002' exists"); |
257 |
|
258 |
my $object1 = Koha::Items->find({barcode => '167Nabe0001'}); |
259 |
ok ($object1, "Global Item '167Nabe0001' exists"); |
260 |
my $object2 = Koha::Items->find({barcode => '167Nabe0002'}); |
261 |
ok ($object2, "Global Item '167Nabe0002' exists"); |
262 |
my $object3 = Koha::Biblios->find({title => 'I wish I met your mother', author => "Pertti Kurikka"}); |
263 |
ok ($object2, "Global Biblio 'I wish I met your mother' exists"); |
264 |
|
265 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext); |
266 |
|
267 |
$object11A001 = Koha::Borrowers->find({cardnumber => '11A001'}); |
268 |
ok (not($object11A001), "Global Borrower '11A001' deleted"); |
269 |
$object11A002 = Koha::Borrowers->find({cardnumber => '11A002'}); |
270 |
ok (not($object11A002), "Global Borrower '11A002' deleted"); |
271 |
|
272 |
$object1 = Koha::Items->find({barcode => '167Nabe0001'}); |
273 |
ok (not($object1), "Global Item '167Nabe0001' deleted"); |
274 |
$object2 = Koha::Items->find({barcode => '167Nabe0002'}); |
275 |
ok (not($object2), "Global Item '167Nabe0002' deleted"); |
276 |
$object3 = Koha::Biblios->find({title => 'I wish I met your mother', author => "Pertti Kurikka"}); |
277 |
ok (not($object2), "Global Biblio 'I wish I met your mother' deleted"); |
278 |
}; |
279 |
|
280 |
|
281 |
|
282 |
done_testing(); |
283 |
|
284 |
1; |