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>.
22 use C4
::Biblio
qw( AddBiblio );
29 use t
::lib
::TestBuilder
;
31 use Test
::More tests
=> 29;
36 my $schema = Koha
::Database
->new()->schema();
37 $schema->storage->txn_begin();
39 # add two branches and a borrower
40 my $builder = t
::lib
::TestBuilder
->new;
43 push @branches, $builder->build({ source
=> 'Branch' });
45 my $borrower = $builder->build({ source
=> 'Borrower' });
47 my $biblio = MARC
::Record
->new();
48 my $title = 'Silence in the library';
49 $biblio->append_fields(
50 MARC
::Field
->new( '100', ' ', ' ', a
=> 'Moffat, Steven' ),
51 MARC
::Field
->new( '245', ' ', ' ', a
=> $title ),
53 my ( $biblionumber, $biblioitemnumber ) = AddBiblio
( $biblio, '' );
55 my $item = Koha
::Item
->new(
57 biblionumber
=> $biblionumber,
58 biblioitemnumber
=> $biblioitemnumber,
59 holdingbranch
=> $branches[0]->{branchcode
},
60 homebranch
=> $branches[0]->{branchcode
},
65 my $hold = Koha
::Hold
->new(
67 biblionumber
=> $biblionumber,
68 itemnumber
=> $item->id(),
69 waitingdate
=> '2000-01-01',
70 borrowernumber
=> $borrower->{borrowernumber
},
71 branchcode
=> $branches[1]->{branchcode
},
77 is
( $hold->suspend, 0, "Hold is not suspended" );
78 $hold->suspend_hold();
79 is
( $hold->suspend, 1, "Hold is suspended" );
81 is
( $hold->suspend, 0, "Hold is not suspended" );
82 my $dt = dt_from_string
();
83 $hold->suspend_hold( $dt );
84 $dt->truncate( to
=> 'day' );
85 is
( $hold->suspend, 1, "Hold is suspended" );
86 is
( $hold->suspend_until, "$dt", "Hold is suspended with a date, truncation takes place automatically" );
88 is
( $hold->suspend, 0, "Hold is not suspended" );
89 is
( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
91 warning_like
{ $hold->suspend_hold }
92 qr/Unable to suspend waiting hold!/, 'Catch warn about failed suspend';
93 is
( $hold->suspend, 0, "Waiting hold cannot be suspended" );
95 $item = $hold->item();
97 my $hold_borrower = $hold->borrower();
98 ok
( $hold_borrower, 'Got hold borrower' );
99 is
( $hold_borrower->borrowernumber(), $borrower->{borrowernumber
}, 'Hold borrower matches correct borrower' );
101 is
( $hold->is_waiting, 1, 'The hold is waiting' );
102 is
( $hold->is_found, 1, 'The hold is found');
103 ok
( !$hold->is_in_transit, 'The hold is not in transit' );
105 t
::lib
::Mocks
::mock_preference
( 'ReservesMaxPickUpDelay', '5' );
107 isnt
( $hold->is_waiting, 1, 'The hold is not waiting (T)' );
108 is
( $hold->is_found, 1, 'The hold is found');
109 is
( $hold->is_in_transit, 1, 'The hold is in transit' );
112 isnt
( $hold->is_waiting, 1, 'The hold is not waiting (W)' );
113 is
( $hold->is_found, 0, 'The hold is not found' );
114 ok
( !$hold->is_in_transit, 'The hold is not in transit' );
116 # Test method is_cancelable
118 ok
( $hold->is_cancelable(), "Unfound hold is cancelable" );
120 ok
( $hold->is_cancelable, "Waiting hold is cancelable" );
122 ok
( !$hold->is_cancelable, "In transit hold is not cancelable" );
124 # Test method is_at_destination
126 ok
( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
128 ok
( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
130 ok
( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the same as the item's holdingbranch is not at destination" );
131 $item->holdingbranch( $branches[1]->{branchcode
} );
132 ok
( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
134 $schema->storage->txn_rollback();
136 subtest
"delete() tests" => sub {
140 $schema->storage->txn_begin();
143 t
::lib
::Mocks
::mock_preference
( 'HoldsLog', 0 );
145 my $hold = $builder->build({ source
=> 'Reserve' });
147 my $hold_object = Koha
::Holds
->find( $hold->{ reserve_id
} );
148 my $deleted = $hold_object->delete;
149 is
( $deleted, 1, 'Koha::Hold->delete should return 1 if the hold has been correctly deleted' );
150 is
( Koha
::Holds
->search({ reserve_id
=> $hold->{ reserve_id
} })->count, 0,
151 "Koha::Hold->delete should have deleted the hold" );
153 my $number_of_logs = $schema->resultset('ActionLog')->search(
154 { module
=> 'HOLDS', action
=> 'DELETE', object
=> $hold->{ reserve_id
} } )->count;
155 is
( $number_of_logs, 0, 'With HoldsLogs, Koha::Hold->delete shouldn\'t have been logged' );
158 t
::lib
::Mocks
::mock_preference
( 'HoldsLog', 1 );
160 $hold = $builder->build({ source
=> 'Reserve' });
162 $hold_object = Koha
::Holds
->find( $hold->{ reserve_id
} );
163 $deleted = $hold_object->delete;
164 is
( $deleted, 1, 'Koha::Hold->delete should return 1 if the hold has been correctly deleted' );
165 is
( Koha
::Holds
->search({ reserve_id
=> $hold->{ reserve_id
} })->count, 0,
166 "Koha::Hold->delete should have deleted the hold" );
168 $number_of_logs = $schema->resultset('ActionLog')->search(
169 { module
=> 'HOLDS', action
=> 'DELETE', object
=> $hold->{ reserve_id
} } )->count;
170 is
( $number_of_logs, 1, 'With HoldsLogs, Koha::Hold->delete should have been logged' );
172 $schema->storage->txn_rollback();