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