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