Bug 19057: Move C4::Reserve::GetReserve to Koha::Holds
[koha.git] / t / db_dependent / Hold.t
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 t::lib::Mocks;
21 use C4::Context;
22 use C4::Biblio qw( AddBiblio );
23 use Koha::Database;
24 use Koha::Libraries;
25 use Koha::Patrons;
26 use Koha::Holds;
27 use Koha::Item;
28 use Koha::DateUtils;
29 use t::lib::TestBuilder;
30
31 use Test::More tests => 29;
32 use Test::Warn;
33
34 use_ok('Koha::Hold');
35
36 my $schema = Koha::Database->new()->schema();
37 $schema->storage->txn_begin();
38
39 # add two branches and a borrower
40 my $builder = t::lib::TestBuilder->new;
41 my @branches;
42 foreach( 1..2 ) {
43     push @branches, $builder->build({ source => 'Branch' });
44 }
45 my $borrower = $builder->build({ source => 'Borrower' });
46
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 ),
52 );
53 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
54
55 my $item = Koha::Item->new(
56     {
57         biblionumber     => $biblionumber,
58         biblioitemnumber => $biblioitemnumber,
59         holdingbranch    => $branches[0]->{branchcode},
60         homebranch       => $branches[0]->{branchcode},
61     }
62 );
63 $item->store();
64
65 my $hold = Koha::Hold->new(
66     {
67         biblionumber   => $biblionumber,
68         itemnumber     => $item->id(),
69         waitingdate    => '2000-01-01',
70         borrowernumber => $borrower->{borrowernumber},
71         branchcode     => $branches[1]->{branchcode},
72         suspend        => 0,
73     }
74 );
75 $hold->store();
76
77 is( $hold->suspend, 0, "Hold is not suspended" );
78 $hold->suspend_hold();
79 is( $hold->suspend, 1, "Hold is suspended" );
80 $hold->resume();
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" );
87 $hold->resume();
88 is( $hold->suspend, 0, "Hold is not suspended" );
89 is( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
90 $hold->found('W');
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" );
94
95 $item = $hold->item();
96
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' );
100
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' );
104
105 t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', '5' );
106 $hold->found('T');
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' );
110
111 $hold->found(q{});
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' );
115
116 # Test method is_cancelable
117 $hold->found(undef);
118 ok( $hold->is_cancelable(), "Unfound hold is cancelable" );
119 $hold->found('W');
120 ok( $hold->is_cancelable, "Waiting hold is cancelable" );
121 $hold->found('T');
122 ok( !$hold->is_cancelable, "In transit hold is not cancelable" );
123
124 # Test method is_at_destination
125 $hold->found(undef);
126 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
127 $hold->found('T');
128 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
129 $hold->found('W');
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" );
133
134 $schema->storage->txn_rollback();
135
136 subtest "delete() tests" => sub {
137
138     plan tests => 6;
139
140     $schema->storage->txn_begin();
141
142     # Disable logging
143     t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
144
145     my $hold = $builder->build({ source => 'Reserve' });
146
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" );
152
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' );
156
157     # Enable logging
158     t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
159
160     $hold = $builder->build({ source => 'Reserve' });
161
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" );
167
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' );
171
172     $schema->storage->txn_rollback();
173  };
174