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