Bug 14310 [QA Followup] - Adapt existing code to use new methods
[koha.git] / t / db_dependent / Hold.t
blob6307e5d30ddf82e0d1c451bef978bc7aa04686ac
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use C4::Context;
21 use C4::Biblio qw( AddBiblio );
22 use Koha::Database;
23 use Koha::Borrowers;
24 use Koha::Libraries;
25 use Koha::Item;
26 use Koha::DateUtils;
28 use Test::More tests => 31;
30 use_ok('Koha::Hold');
32 my $schema = Koha::Database->new()->schema();
33 $schema->storage->txn_begin();
35 my $dbh = C4::Context->dbh;
36 $dbh->{RaiseError} = 1;
38 my @branches = Koha::Libraries->search();
39 my $borrower = Koha::Borrowers->search()->next();
41 my $biblio = MARC::Record->new();
42 my $title = 'Silence in the library';
43 $biblio->append_fields(
44 MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
45 MARC::Field->new( '245', ' ', ' ', a => $title ),
47 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
49 my $item = Koha::Item->new(
51 biblionumber => $biblionumber,
52 biblioitemnumber => $biblioitemnumber,
53 holdingbranch => $branches[0]->branchcode(),
54 homebranch => $branches[0]->branchcode(),
57 $item->store();
59 my $hold = Koha::Hold->new(
61 biblionumber => $biblionumber,
62 itemnumber => $item->id(),
63 waitingdate => '2000-01-01',
64 borrowernumber => $borrower->borrowernumber(),
65 branchcode => $branches[1]->branchcode(),
66 suspend => 0,
69 $hold->store();
71 is( $hold->suspend, 0, "Hold is not suspended" );
72 $hold->suspend_hold();
73 is( $hold->suspend, 1, "Hold is suspended" );
74 $hold->resume();
75 is( $hold->suspend, 0, "Hold is not suspended" );
76 my $dt = dt_from_string();
77 $hold->suspend_hold( $dt );
78 $dt->truncate( to => 'day' );
79 is( $hold->suspend, 1, "Hold is suspended" );
80 is( $hold->suspend_until, "$dt", "Hold is suspended with a date, truncation takes place automatically" );
81 $hold->resume();
82 is( $hold->suspend, 0, "Hold is not suspended" );
83 is( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
84 $hold->found('W');
85 $hold->suspend_hold();
86 is( $hold->suspend, 0, "Waiting hold cannot be suspended" );
88 $item = $hold->item();
90 my $hold_borrower = $hold->borrower();
91 ok( $hold_borrower, 'Got hold borrower' );
92 is( $hold_borrower->borrowernumber(), $borrower->borrowernumber(), 'Hold borrower matches correct borrower' );
94 C4::Context->set_preference( 'ReservesMaxPickUpDelay', '' );
95 $dt = $hold->waiting_expires_on();
96 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if ReservesMaxPickUpDelay is not set" );
98 is( $hold->is_waiting, 1, 'The hold is waiting' );
99 is( $hold->is_found, 1, 'The hold is found');
100 ok( !$hold->is_in_transit, 'The hold is not in transit' );
102 C4::Context->set_preference( 'ReservesMaxPickUpDelay', '5' );
103 $dt = $hold->waiting_expires_on();
104 is( $dt->ymd, "2000-01-06",
105 "Koha::Hold->waiting_expires_on returns DateTime of waitingdate + ReservesMaxPickUpDelay if set" );
107 $hold->found('T');
108 $dt = $hold->waiting_expires_on();
109 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to 'T' )" );
110 isnt( $hold->is_waiting, 1, 'The hold is not waiting (T)' );
111 is( $hold->is_found, 1, 'The hold is found');
112 is( $hold->is_in_transit, 1, 'The hold is in transit' );
114 $hold->found(q{});
115 $dt = $hold->waiting_expires_on();
116 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to empty string )" );
117 isnt( $hold->is_waiting, 1, 'The hold is not waiting (W)' );
118 is( $hold->is_found, 0, 'The hold is not found' );
119 ok( !$hold->is_in_transit, 'The hold is not in transit' );
121 # Test method is_cancelable
122 $hold->found(undef);
123 ok( $hold->is_cancelable(), "Unfound hold is cancelable" );
124 $hold->found('W');
125 ok( $hold->is_cancelable, "Waiting hold is cancelable" );
126 $hold->found('T');
127 ok( !$hold->is_cancelable, "In transit hold is not cancelable" );
129 # Test method is_at_destination
130 $hold->found(undef);
131 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
132 $hold->found('T');
133 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
134 $hold->found('W');
135 ok( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the same as the item's holdingbranch is not at destination" );
136 $item->holdingbranch( $branches[1]->branchcode() );
137 ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
139 $schema->storage->txn_rollback();