Bug 6906: Tests - Do not assume CPL exists
[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::Item;
27 use Koha::DateUtils;
28 use t::lib::TestBuilder;
29
30 use Test::More tests => 32;
31 use Test::Warn;
32
33 use_ok('Koha::Hold');
34
35 my $schema = Koha::Database->new()->schema();
36 $schema->storage->txn_begin();
37
38 # add two branches and a borrower
39 my $builder = t::lib::TestBuilder->new;
40 my @branches;
41 foreach( 1..2 ) {
42     push @branches, $builder->build({ source => 'Branch' });
43 }
44 my $borrower = $builder->build({ source => 'Borrower' });
45
46 my $biblio = MARC::Record->new();
47 my $title  = 'Silence in the library';
48 $biblio->append_fields(
49     MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
50     MARC::Field->new( '245', ' ', ' ', a => $title ),
51 );
52 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
53
54 my $item = Koha::Item->new(
55     {
56         biblionumber     => $biblionumber,
57         biblioitemnumber => $biblioitemnumber,
58         holdingbranch    => $branches[0]->{branchcode},
59         homebranch       => $branches[0]->{branchcode},
60     }
61 );
62 $item->store();
63
64 my $hold = Koha::Hold->new(
65     {
66         biblionumber   => $biblionumber,
67         itemnumber     => $item->id(),
68         waitingdate    => '2000-01-01',
69         borrowernumber => $borrower->{borrowernumber},
70         branchcode     => $branches[1]->{branchcode},
71         suspend        => 0,
72     }
73 );
74 $hold->store();
75
76 is( $hold->suspend, 0, "Hold is not suspended" );
77 $hold->suspend_hold();
78 is( $hold->suspend, 1, "Hold is suspended" );
79 $hold->resume();
80 is( $hold->suspend, 0, "Hold is not suspended" );
81 my $dt = dt_from_string();
82 $hold->suspend_hold( $dt );
83 $dt->truncate( to => 'day' );
84 is( $hold->suspend, 1, "Hold is suspended" );
85 is( $hold->suspend_until, "$dt", "Hold is suspended with a date, truncation takes place automatically" );
86 $hold->resume();
87 is( $hold->suspend, 0, "Hold is not suspended" );
88 is( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
89 $hold->found('W');
90 warning_like { $hold->suspend_hold }
91     qr/Unable to suspend waiting hold!/, 'Catch warn about failed suspend';
92 is( $hold->suspend, 0, "Waiting hold cannot be suspended" );
93
94 $item = $hold->item();
95
96 my $hold_borrower = $hold->borrower();
97 ok( $hold_borrower, 'Got hold borrower' );
98 is( $hold_borrower->borrowernumber(), $borrower->{borrowernumber}, 'Hold borrower matches correct borrower' );
99
100 t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', '' );
101 $dt = $hold->waiting_expires_on();
102 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if ReservesMaxPickUpDelay is not set" );
103
104 is( $hold->is_waiting, 1, 'The hold is waiting' );
105 is( $hold->is_found, 1, 'The hold is found');
106 ok( !$hold->is_in_transit, 'The hold is not in transit' );
107
108 t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', '5' );
109 $dt = $hold->waiting_expires_on();
110 is( $dt->ymd, "2000-01-06",
111     "Koha::Hold->waiting_expires_on returns DateTime of waitingdate + ReservesMaxPickUpDelay if set" );
112
113 $hold->found('T');
114 $dt = $hold->waiting_expires_on();
115 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to 'T' )" );
116 isnt( $hold->is_waiting, 1, 'The hold is not waiting (T)' );
117 is( $hold->is_found, 1, 'The hold is found');
118 is( $hold->is_in_transit, 1, 'The hold is in transit' );
119
120 $hold->found(q{});
121 $dt = $hold->waiting_expires_on();
122 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to empty string )" );
123 isnt( $hold->is_waiting, 1, 'The hold is not waiting (W)' );
124 is( $hold->is_found, 0, 'The hold is not found' );
125 ok( !$hold->is_in_transit, 'The hold is not in transit' );
126
127 # Test method is_cancelable
128 $hold->found(undef);
129 ok( $hold->is_cancelable(), "Unfound hold is cancelable" );
130 $hold->found('W');
131 ok( $hold->is_cancelable, "Waiting hold is cancelable" );
132 $hold->found('T');
133 ok( !$hold->is_cancelable, "In transit hold is not cancelable" );
134
135 # Test method is_at_destination
136 $hold->found(undef);
137 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
138 $hold->found('T');
139 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
140 $hold->found('W');
141 ok( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the same as the item's holdingbranch is not at destination" );
142 $item->holdingbranch( $branches[1]->{branchcode} );
143 ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
144
145 $schema->storage->txn_rollback();
146
147 1;