Line 0
Link Here
|
|
|
1 |
package t::lib::TestObjects::HoldFactory; |
2 |
|
3 |
# Copyright KohaSuomi 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 |
use Carp; |
23 |
use DateTime; |
24 |
|
25 |
use C4::Context; |
26 |
use Koha::Database; |
27 |
use C4::Circulation; |
28 |
use C4::Members; |
29 |
use C4::Items; |
30 |
use C4::Reserves; |
31 |
use Koha::Borrowers; |
32 |
use Koha::Biblios; |
33 |
use Koha::BiblioItems; |
34 |
use Koha::Items; |
35 |
use Koha::Checkouts; |
36 |
|
37 |
use t::lib::TestObjects::BorrowerFactory; |
38 |
use t::lib::TestObjects::ItemFactory; |
39 |
use t::lib::TestObjects::BiblioFactory; |
40 |
|
41 |
use base qw(t::lib::TestObjects::ObjectFactory); |
42 |
|
43 |
sub new { |
44 |
my ($class) = @_; |
45 |
|
46 |
my $self = {}; |
47 |
bless($self, $class); |
48 |
return $self; |
49 |
} |
50 |
|
51 |
sub getDefaultHashKey { |
52 |
return ['reservenotes']; |
53 |
} |
54 |
sub getObjectType { |
55 |
return 'HASH'; |
56 |
} |
57 |
|
58 |
=head t::lib::TestObjects::HoldFactory::createTestGroup( $data [, $hashKey], @stashes ) |
59 |
|
60 |
my $holds = t::lib::TestObjects::HoldFactory->createTestGroup([ |
61 |
{#Hold params |
62 |
}, |
63 |
{#More hold params |
64 |
}, |
65 |
], undef, $testContext1, $testContext2, $testContext3); |
66 |
|
67 |
t::lib::TestObjects::ObjectFactory->tearDownTestContext($testContext1); |
68 |
|
69 |
@PARAM1, ARRAY of HASHes. |
70 |
[ { |
71 |
cardnumber => '167Azava0001', #Borrower's cardnumber |
72 |
isbn => '971040323123', #ISBN of the Biblio, even if the record normally doesn't have a ISBN, you must mock one on it. |
73 |
barcode => undef || '911N12032', #Item's barcode, if this is an Item-level hold. |
74 |
branchcode => 'CPL', |
75 |
waitingdate => undef || '2015-01-15', #Since when has this hold been waiting for pickup? |
76 |
}, |
77 |
{ |
78 |
... |
79 |
} |
80 |
] |
81 |
@PARAM2, String, the HASH-element to use as the returning HASHes key. |
82 |
@PARAM3-5 HASHRef of test contexts. You can save the given objects to multiple |
83 |
test contexts. Usually one is enough. These test contexts are |
84 |
used to help tear down DB changes. |
85 |
@RETURNS HASHRef of $hashKey => HASH-objects representing koha.reserves-table columns |
86 |
The HASH is keyed with <reservenotes>, or the given $hashKey. |
87 |
Example: { |
88 |
'11A001-971040323123-43' => {...}, |
89 |
'my-nice-hold' => {...}, |
90 |
... |
91 |
} |
92 |
} |
93 |
=cut |
94 |
|
95 |
sub handleTestObject { |
96 |
my ($class, $object, $stashes) = @_; |
97 |
|
98 |
C4::Reserves::AddReserve($object->{branchcode} || 'CPL', |
99 |
$object->{borrower}->borrowernumber, |
100 |
$object->{biblio}->{biblionumber}, |
101 |
undef, #constraint |
102 |
undef, #bibitems |
103 |
undef, #priority |
104 |
$object->{reservedate}, #resdate |
105 |
$object->{expirationdate}, #expdate |
106 |
$object->{reservenotes}, #notes |
107 |
undef, #title |
108 |
($object->{item} ? $object->{item}->itemnumber : undef), #checkitem |
109 |
undef, #found |
110 |
); |
111 |
my $reserve_id = C4::Reserves::GetReserveId({biblionumber => $object->{biblio}->{biblionumber}, |
112 |
itemnumber => ($object->{item} ? $object->{item}->itemnumber : undef), |
113 |
borrowernumber => $object->{borrower}->borrowernumber, |
114 |
}); |
115 |
unless ($reserve_id) { |
116 |
die "HoldFactory->handleTestObject():> Couldn't create a reserve. for isbn => ".$object->{isbn}.", barcode => ".$object->{barcode}. |
117 |
", item => ".($object->{barcode} ? $object->{barcode} : '')."\n"; |
118 |
} |
119 |
my $hold = C4::Reserves::GetReserve($reserve_id); |
120 |
foreach my $key (keys $object) { |
121 |
$hold->{$key} = $object->{$key}; |
122 |
} |
123 |
|
124 |
if ($object->{waitingdate}) { |
125 |
eval { |
126 |
C4::Reserves::ModReserveAffect($hold->{item}->itemnumber, $hold->{borrower}->borrowernumber); |
127 |
|
128 |
#Modify the waitingdate. An ugly hack for a ugly module. Bear with me my men! |
129 |
my $dbh = C4::Context->dbh; |
130 |
my $query = "UPDATE reserves SET waitingdate = ? WHERE reserve_id = ?"; |
131 |
my $sth = $dbh->prepare($query); |
132 |
$sth->execute( $hold->{waitingdate}, $hold->{reserve_id} ); |
133 |
}; |
134 |
if ($@) { |
135 |
warn "HoldFactory->handleTestObject():> Error when setting the waitingdate for ".$class->getHashKey($object)."$@"; |
136 |
} |
137 |
} |
138 |
|
139 |
return $hold; |
140 |
} |
141 |
|
142 |
=head validateAndPopulateDefaultValues |
143 |
@OVERLOAD |
144 |
|
145 |
Validates given Object parameters and makes sure that critical fields are given |
146 |
and populates defaults for missing values. |
147 |
=cut |
148 |
|
149 |
sub validateAndPopulateDefaultValues { |
150 |
my ($self, $object, $hashKey, $stashes) = @_; |
151 |
$self->SUPER::validateAndPopulateDefaultValues($object, $hashKey); |
152 |
|
153 |
unless ($object->{cardnumber}) { |
154 |
croak __PACKAGE__.":> Mandatory parameter 'cardnumber' missing."; |
155 |
} |
156 |
unless ($object->{isbn}) { |
157 |
croak __PACKAGE__.":> Mandatory parameter 'isbn' missing."; |
158 |
} |
159 |
|
160 |
my $borrowers = t::lib::TestObjects::BorrowerFactory->createTestGroup( |
161 |
{cardnumber => $object->{cardnumber}}, |
162 |
undef, @$stashes); |
163 |
my $borrower = $borrowers->{ $object->{cardnumber} }; |
164 |
$object->{borrower} = $borrower if $borrower; |
165 |
|
166 |
my $biblios = t::lib::TestObjects::BiblioFactory->createTestGroup({"biblio.title" => "Test holds' Biblio", |
167 |
"biblioitems.isbn" => $object->{isbn}}, |
168 |
undef, @$stashes); |
169 |
my $biblio = $biblios->{ $object->{isbn} || '971-972-call-me' }; #Get default isbn or the given one |
170 |
$object->{biblio} = $biblio if $biblio; |
171 |
|
172 |
#Get test Item |
173 |
if ($object->{barcode}) { |
174 |
my $items = t::lib::TestObjects::ItemFactory->createTestGroup({barcode => $object->{barcode}, isbn => $object->{isbn}}, undef, @$stashes); |
175 |
my $item = $items->{ $object->{barcode} }; |
176 |
$object->{item} = $item; |
177 |
} |
178 |
|
179 |
return $object; |
180 |
} |
181 |
|
182 |
=head |
183 |
|
184 |
my $objects = createTestGroup(); |
185 |
##Do funky stuff |
186 |
deleteTestGroup($records); |
187 |
|
188 |
Removes the given test group from the DB. |
189 |
|
190 |
=cut |
191 |
|
192 |
sub deleteTestGroup { |
193 |
my ($self, $objects) = @_; |
194 |
|
195 |
#For some reason DBIx cannot delete from OldReserves-table so using DBI and not losing sleep |
196 |
my $dbh = C4::Context->dbh(); |
197 |
my $del_old_sth = $dbh->prepare("DELETE FROM old_reserves WHERE reserve_id = ?"); |
198 |
my $del_sth = $dbh->prepare("DELETE FROM reserves WHERE reserve_id = ?"); |
199 |
|
200 |
while( my ($key, $object) = each %$objects) { |
201 |
$del_sth->execute($object->{reserve_id}); |
202 |
$del_old_sth->execute($object->{reserve_id}); |
203 |
} |
204 |
} |
205 |
|
206 |
1; |