Bug 19059: Move C4::Reserves::CancelReserve to Koha::Hold->cancel
[koha.git] / t / db_dependent / Holds / HoldItemtypeLimit.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use C4::Context;
6
7 use Test::More tests => 4;
8
9 use t::lib::TestBuilder;
10
11 use Koha::Holds;
12
13 BEGIN {
14     use FindBin;
15     use lib $FindBin::Bin;
16     use_ok('C4::Reserves');
17 }
18
19 my $schema = Koha::Database->schema;
20 $schema->storage->txn_begin;
21 my $dbh = C4::Context->dbh;
22
23 my $builder = t::lib::TestBuilder->new;
24
25 my $library = $builder->build({
26     source => 'Branch',
27 });
28
29 my $bib_title = "Test Title";
30
31
32 my $borrower = $builder->build({
33     source => 'Borrower',
34     value => {
35         branchcode => $library->{branchcode},
36     }
37 });
38
39 my $itemtype1 = $builder->build({
40     source => 'Itemtype',
41     value => {
42         notforloan => 0
43     }
44 });
45
46 my $itemtype2 = $builder->build({
47     source => 'Itemtype',
48     value => {
49         notforloan => 0
50     }
51 });
52
53
54 # Test hold_fulfillment_policy
55 my $right_itemtype = $itemtype1->{itemtype};
56 my $wrong_itemtype = $itemtype2->{itemtype};
57 my $borrowernumber = $borrower->{borrowernumber};
58 my $branchcode = $library->{branchcode};
59 $dbh->do("DELETE FROM reserves");
60 $dbh->do("DELETE FROM issues");
61 $dbh->do("DELETE FROM items");
62 $dbh->do("DELETE FROM biblio");
63 $dbh->do("DELETE FROM biblioitems");
64 $dbh->do("DELETE FROM transport_cost");
65 $dbh->do("DELETE FROM tmp_holdsqueue");
66 $dbh->do("DELETE FROM hold_fill_targets");
67 $dbh->do("DELETE FROM default_branch_circ_rules");
68 $dbh->do("DELETE FROM default_branch_item_rules");
69 $dbh->do("DELETE FROM default_circ_rules");
70 $dbh->do("DELETE FROM branch_item_rules");
71
72 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$bib_title', '2011-02-01')");
73
74 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$bib_title'")
75   or BAIL_OUT("Cannot find newly created biblio record");
76
77 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$right_itemtype')");
78
79 my $biblioitemnumber =
80   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
81   or BAIL_OUT("Cannot find newly created biblioitems record");
82
83 $dbh->do("
84     INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
85     VALUES ($biblionumber, $biblioitemnumber, '$branchcode', '$branchcode', 0, 0, 0, 0, NULL, '$right_itemtype')
86 ");
87
88 my $itemnumber =
89   $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber")
90   or BAIL_OUT("Cannot find newly created item");
91
92 $dbh->do("DELETE FROM default_circ_rules");
93 $dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
94
95 # Itemtypes match
96 my $reserve_id = AddReserve( $branchcode, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
97 my ( $status ) = CheckReserves($itemnumber);
98 is( $status, 'Reserved', "Hold where itemtype matches item's itemtype targed" );
99 Koha::Holds->find( $reserve_id )->cancel;
100
101 # Itemtypes don't match
102 $reserve_id = AddReserve( $branchcode, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
103 ( $status ) = CheckReserves($itemnumber);
104 is($status, q{}, "Hold where itemtype does not match item's itemtype not targeted" );
105 Koha::Holds->find( $reserve_id )->cancel;
106
107 # No itemtype set
108 $reserve_id = AddReserve( $branchcode, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
109 ( $status ) = CheckReserves($itemnumber);
110 is( $status, 'Reserved', "Item targeted with no hold itemtype set" );
111 Koha::Holds->find( $reserve_id )->cancel;
112
113 # Cleanup
114 $schema->storage->txn_rollback;