3 # This file is part of Koha
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 4;
24 use Koha::DateUtils qw( dt_from_string output_pref );
25 use Koha::Checkouts::Renewal;
26 use Koha::Checkouts::Renewals;
28 use t::lib::TestBuilder;
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
33 subtest "store() tests" => sub {
37 $schema->storage->txn_begin;
39 my $librarian = $builder->build_object( { class => 'Koha::Patrons' } );
40 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
41 my $item = $builder->build_sample_item;
43 my $checkout = $builder->build_object(
45 class => 'Koha::Checkouts',
47 borrowernumber => $patron->borrowernumber,
48 itemnumber => $item->itemnumber,
49 branchcode => $patron->branchcode
54 my $old_checkout = $builder->build_object(
56 class => 'Koha::Old::Checkouts',
58 borrowernumber => $patron->borrowernumber,
59 itemnumber => $item->itemnumber,
60 branchcode => $patron->branchcode
66 Koha::Checkouts::Renewal->new( { interface => 'intranet' } )->store()
68 'Koha::Exceptions::Object::FKConstraint',
69 'Exception thrown if no checkout_id is passed on creation';
71 my $renewal = Koha::Checkouts::Renewal->new(
73 checkout_id => $checkout->id,
74 renewer_id => $librarian->borrowernumber,
75 interface => 'intranet'
79 is( ref($renewal), 'Koha::Checkouts::Renewal', 'Object type is correct' );
81 Koha::Checkouts::Renewals->search( { checkout_id => $checkout->id } )
84 'Renewal stored on the DB'
87 { # hide useless warnings
89 open STDERR, '>', '/dev/null';
91 my $another_checkout =
92 $builder->build_object( { class => 'Koha::Checkouts' } );
93 my $checkout_id = $another_checkout->id;
94 $another_checkout->delete;
99 $THE_renewal = Koha::Checkouts::Renewal->new(
101 checkout_id => $checkout_id,
102 interface => 'intranet'
106 'Koha::Exceptions::Object::FKConstraint',
107 'An exception is thrown on invalid checkout_id';
110 is( $@->broken_fk, 'checkout_id', 'Exception field is correct' );
113 $schema->storage->txn_rollback;
116 subtest 'renewer() tests' => sub {
120 $schema->storage->txn_begin;
122 my $librarian = $builder->build_object( { class => 'Koha::Patrons' } );
123 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
124 my $item = $builder->build_sample_item;
125 my $checkout = $builder->build_object(
127 class => 'Koha::Checkouts',
129 borrowernumber => $patron->borrowernumber,
130 itemnumber => $item->itemnumber,
131 branchcode => $patron->branchcode
136 my $renewal = Koha::Checkouts::Renewal->new(
138 checkout_id => $checkout->id,
139 renewer_id => $librarian->id,
140 interface => 'intranet'
144 my $renewal_renewer_patron = $renewal->renewer;
145 is( ref($renewal_renewer_patron),
147 'Koha::Checkouts::Renewal->renewer should return a Koha::Patron' );
148 is( $renewal->renewer_id, $renewal_renewer_patron->borrowernumber,
149 'Koha::Checkouts::Renewal->renewer should return the correct borrower'
152 my $checkout_id = $checkout->id;
155 Koha::Checkouts::Renewals->search( { checkout_id => $checkout_id } );
156 is( $renewals->count, 1,
157 'Koha::Checkouts::Renewal is not deleted on librarian deletion' );
159 $schema->storage->txn_rollback;
162 subtest 'checkout() tests' => sub {
166 $schema->storage->txn_begin;
168 my $librarian = $builder->build_object( { class => 'Koha::Patrons' } );
169 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
170 my $item = $builder->build_sample_item;
171 my $checkout = $builder->build_object(
173 class => 'Koha::Checkouts',
175 borrowernumber => $patron->borrowernumber,
176 itemnumber => $item->itemnumber,
177 branchcode => $patron->branchcode
182 my $renewal = Koha::Checkouts::Renewal->new(
184 checkout_id => $checkout->id,
185 renewer_id => $librarian->id,
186 interface => 'intranet'
190 my $renewal_checkout = $renewal->checkout;
191 is( ref($renewal_checkout), 'Koha::Checkout',
192 'Koha::Checkouts::Renewal->checkout should return a Koha::Checkout' );
193 is( $renewal->checkout_id, $renewal_checkout->id,
194 'Koha::Checkouts::Renewal->checkout should return the correct checkout'
197 my $issue_id = $checkout->issue_id;
201 Koha::Checkouts::Renewals->search( { checkout_id => $issue_id } );
202 is( $renewals->count, 1,
203 'Koha::Checkouts::Renewal remains on checkout deletion' );
205 $renewal->discard_changes;
206 is( $renewal->checkout, undef,
207 'Koha::Checkouts::Renewal->checkout should return undef if checkout has been deleted'
210 $schema->storage->txn_rollback;
213 subtest 'old_checkout() tests' => sub {
217 $schema->storage->txn_begin;
219 my $librarian = $builder->build_object( { class => 'Koha::Patrons' } );
220 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
221 my $item = $builder->build_sample_item;
222 my $old_checkout = $builder->build_object(
224 class => 'Koha::Old::Checkouts',
226 borrowernumber => $patron->borrowernumber,
227 itemnumber => $item->itemnumber,
228 branchcode => $patron->branchcode
233 my $renewal = Koha::Checkouts::Renewal->new(
235 checkout_id => $old_checkout->id,
236 renewer_id => $librarian->id,
237 interface => 'intranet'
241 my $renewal_old_checkout = $renewal->old_checkout;
242 is( ref($renewal_old_checkout), 'Koha::Old::Checkout',
243 'Koha::Checkouts::Renewal->old_checkout should return a Koha::Old::Checkout'
245 is( $renewal->checkout_id, $renewal_old_checkout->id,
246 'Koha::Checkouts::Renewal->old_checkout should return the correct old checkout'
249 my $issue_id = $old_checkout->issue_id;
250 $old_checkout->delete;
253 Koha::Checkouts::Renewals->search( { checkout_id => $issue_id } );
254 is( $renewals->count, 1,
255 'Koha::Checkouts::Renewal remains on old_checkout deletion' );
257 # FIXME: Should we actually set null on OldCheckout deletion?
259 $renewal->discard_changes;
260 is( $renewal->old_checkout, undef,
261 'Koha::Checkouts::Renewal->old_checkout should return undef if old_checkout has been deleted'
264 $schema->storage->txn_rollback;