|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha |
| 4 |
# |
| 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. |
| 9 |
# |
| 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. |
| 14 |
# |
| 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>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 4; |
| 21 |
use Test::Exception; |
| 22 |
|
| 23 |
use Koha::Database; |
| 24 |
use Koha::DateUtils qw( dt_from_string output_pref ); |
| 25 |
use Koha::Checkouts::Renewal; |
| 26 |
use Koha::Checkouts::Renewals; |
| 27 |
|
| 28 |
use t::lib::TestBuilder; |
| 29 |
|
| 30 |
my $schema = Koha::Database->new->schema; |
| 31 |
my $builder = t::lib::TestBuilder->new; |
| 32 |
|
| 33 |
subtest "store() tests" => sub { |
| 34 |
|
| 35 |
plan tests => 5; |
| 36 |
|
| 37 |
$schema->storage->txn_begin; |
| 38 |
|
| 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; |
| 42 |
|
| 43 |
my $checkout = $builder->build_object( |
| 44 |
{ |
| 45 |
class => 'Koha::Checkouts', |
| 46 |
value => { |
| 47 |
borrowernumber => $patron->borrowernumber, |
| 48 |
itemnumber => $item->itemnumber, |
| 49 |
branchcode => $patron->branchcode |
| 50 |
} |
| 51 |
} |
| 52 |
); |
| 53 |
|
| 54 |
my $old_checkout = $builder->build_object( |
| 55 |
{ |
| 56 |
class => 'Koha::Old::Checkouts', |
| 57 |
value => { |
| 58 |
borrowernumber => $patron->borrowernumber, |
| 59 |
itemnumber => $item->itemnumber, |
| 60 |
branchcode => $patron->branchcode |
| 61 |
} |
| 62 |
} |
| 63 |
); |
| 64 |
|
| 65 |
throws_ok { |
| 66 |
Koha::Checkouts::Renewal->new( { interface => 'intranet' } )->store() |
| 67 |
} |
| 68 |
'Koha::Exceptions::Object::FKConstraint', |
| 69 |
'Exception thrown if no checkout_id is passed on creation'; |
| 70 |
|
| 71 |
my $renewal = Koha::Checkouts::Renewal->new( |
| 72 |
{ |
| 73 |
checkout_id => $checkout->id, |
| 74 |
renewer_id => $librarian->borrowernumber, |
| 75 |
interface => 'intranet' |
| 76 |
} |
| 77 |
)->store; |
| 78 |
|
| 79 |
is( ref($renewal), 'Koha::Checkouts::Renewal', 'Object type is correct' ); |
| 80 |
is( |
| 81 |
Koha::Checkouts::Renewals->search( { checkout_id => $checkout->id } ) |
| 82 |
->count, |
| 83 |
1, |
| 84 |
'Renewal stored on the DB' |
| 85 |
); |
| 86 |
|
| 87 |
{ # hide useless warnings |
| 88 |
local *STDERR; |
| 89 |
open STDERR, '>', '/dev/null'; |
| 90 |
|
| 91 |
my $another_checkout = |
| 92 |
$builder->build_object( { class => 'Koha::Checkouts' } ); |
| 93 |
my $checkout_id = $another_checkout->id; |
| 94 |
$another_checkout->delete; |
| 95 |
|
| 96 |
my $THE_renewal; |
| 97 |
|
| 98 |
throws_ok { |
| 99 |
$THE_renewal = Koha::Checkouts::Renewal->new( |
| 100 |
{ |
| 101 |
checkout_id => $checkout_id, |
| 102 |
interface => 'intranet' |
| 103 |
} |
| 104 |
)->store; |
| 105 |
} |
| 106 |
'Koha::Exceptions::Object::FKConstraint', |
| 107 |
'An exception is thrown on invalid checkout_id'; |
| 108 |
close STDERR; |
| 109 |
|
| 110 |
is( $@->broken_fk, 'checkout_id', 'Exception field is correct' ); |
| 111 |
} |
| 112 |
|
| 113 |
$schema->storage->txn_rollback; |
| 114 |
}; |
| 115 |
|
| 116 |
subtest 'renewer() tests' => sub { |
| 117 |
|
| 118 |
plan tests => 3; |
| 119 |
|
| 120 |
$schema->storage->txn_begin; |
| 121 |
|
| 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( |
| 126 |
{ |
| 127 |
class => 'Koha::Checkouts', |
| 128 |
value => { |
| 129 |
borrowernumber => $patron->borrowernumber, |
| 130 |
itemnumber => $item->itemnumber, |
| 131 |
branchcode => $patron->branchcode |
| 132 |
} |
| 133 |
} |
| 134 |
); |
| 135 |
|
| 136 |
my $renewal = Koha::Checkouts::Renewal->new( |
| 137 |
{ |
| 138 |
checkout_id => $checkout->id, |
| 139 |
renewer_id => $librarian->id, |
| 140 |
interface => 'intranet' |
| 141 |
} |
| 142 |
)->store; |
| 143 |
|
| 144 |
my $renewal_renewer_patron = $renewal->renewer; |
| 145 |
is( ref($renewal_renewer_patron), |
| 146 |
'Koha::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' |
| 150 |
); |
| 151 |
|
| 152 |
my $checkout_id = $checkout->id; |
| 153 |
$librarian->delete; |
| 154 |
my $renewals = |
| 155 |
Koha::Checkouts::Renewals->search( { checkout_id => $checkout_id } ); |
| 156 |
is( $renewals->count, 1, |
| 157 |
'Koha::Checkouts::Renewal is not deleted on librarian deletion' ); |
| 158 |
|
| 159 |
$schema->storage->txn_rollback; |
| 160 |
}; |
| 161 |
|
| 162 |
subtest 'checkout() tests' => sub { |
| 163 |
|
| 164 |
plan tests => 4; |
| 165 |
|
| 166 |
$schema->storage->txn_begin; |
| 167 |
|
| 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( |
| 172 |
{ |
| 173 |
class => 'Koha::Checkouts', |
| 174 |
value => { |
| 175 |
borrowernumber => $patron->borrowernumber, |
| 176 |
itemnumber => $item->itemnumber, |
| 177 |
branchcode => $patron->branchcode |
| 178 |
} |
| 179 |
} |
| 180 |
); |
| 181 |
|
| 182 |
my $renewal = Koha::Checkouts::Renewal->new( |
| 183 |
{ |
| 184 |
checkout_id => $checkout->id, |
| 185 |
renewer_id => $librarian->id, |
| 186 |
interface => 'intranet' |
| 187 |
} |
| 188 |
)->store; |
| 189 |
|
| 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' |
| 195 |
); |
| 196 |
|
| 197 |
my $issue_id = $checkout->issue_id; |
| 198 |
$checkout->delete; |
| 199 |
|
| 200 |
my $renewals = |
| 201 |
Koha::Checkouts::Renewals->search( { checkout_id => $issue_id } ); |
| 202 |
is( $renewals->count, 1, |
| 203 |
'Koha::Checkouts::Renewal remains on checkout deletion' ); |
| 204 |
|
| 205 |
$renewal->discard_changes; |
| 206 |
is( $renewal->checkout, undef, |
| 207 |
'Koha::Checkouts::Renewal->checkout should return undef if checkout has been deleted' |
| 208 |
); |
| 209 |
|
| 210 |
$schema->storage->txn_rollback; |
| 211 |
}; |
| 212 |
|
| 213 |
subtest 'old_checkout() tests' => sub { |
| 214 |
|
| 215 |
plan tests => 4; |
| 216 |
|
| 217 |
$schema->storage->txn_begin; |
| 218 |
|
| 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( |
| 223 |
{ |
| 224 |
class => 'Koha::Old::Checkouts', |
| 225 |
value => { |
| 226 |
borrowernumber => $patron->borrowernumber, |
| 227 |
itemnumber => $item->itemnumber, |
| 228 |
branchcode => $patron->branchcode |
| 229 |
} |
| 230 |
} |
| 231 |
); |
| 232 |
|
| 233 |
my $renewal = Koha::Checkouts::Renewal->new( |
| 234 |
{ |
| 235 |
checkout_id => $old_checkout->id, |
| 236 |
renewer_id => $librarian->id, |
| 237 |
interface => 'intranet' |
| 238 |
} |
| 239 |
)->store; |
| 240 |
|
| 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' |
| 244 |
); |
| 245 |
is( $renewal->checkout_id, $renewal_old_checkout->id, |
| 246 |
'Koha::Checkouts::Renewal->old_checkout should return the correct old checkout' |
| 247 |
); |
| 248 |
|
| 249 |
my $issue_id = $old_checkout->issue_id; |
| 250 |
$old_checkout->delete; |
| 251 |
|
| 252 |
my $renewals = |
| 253 |
Koha::Checkouts::Renewals->search( { checkout_id => $issue_id } ); |
| 254 |
is( $renewals->count, 1, |
| 255 |
'Koha::Checkouts::Renewal remains on old_checkout deletion' ); |
| 256 |
|
| 257 |
# FIXME: Should we actually set null on OldCheckout deletion? |
| 258 |
|
| 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' |
| 262 |
); |
| 263 |
|
| 264 |
$schema->storage->txn_rollback; |
| 265 |
}; |