6 use Koha::CirculationRules;
8 use Test::More tests => 4;
10 use t::lib::TestBuilder;
16 use lib $FindBin::Bin;
17 use_ok('C4::Reserves');
20 my $schema = Koha::Database->schema;
21 $schema->storage->txn_begin;
22 my $dbh = C4::Context->dbh;
24 my $builder = t::lib::TestBuilder->new;
26 my $library = $builder->build({
30 my $bib_title = "Test Title";
33 my $borrower = $builder->build({
36 branchcode => $library->{branchcode},
40 my $itemtype1 = $builder->build({
47 my $itemtype2 = $builder->build({
55 # Test hold_fulfillment_policy
56 my $right_itemtype = $itemtype1->{itemtype};
57 my $wrong_itemtype = $itemtype2->{itemtype};
58 my $borrowernumber = $borrower->{borrowernumber};
59 my $branchcode = $library->{branchcode};
60 $dbh->do("DELETE FROM reserves");
61 $dbh->do("DELETE FROM issues");
62 $dbh->do("DELETE FROM items");
63 $dbh->do("DELETE FROM biblio");
64 $dbh->do("DELETE FROM biblioitems");
65 $dbh->do("DELETE FROM transport_cost");
66 $dbh->do("DELETE FROM tmp_holdsqueue");
67 $dbh->do("DELETE FROM hold_fill_targets");
69 $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$bib_title', '2011-02-01')");
71 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$bib_title'")
72 or BAIL_OUT("Cannot find newly created biblio record");
74 $dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$right_itemtype')");
76 my $biblioitemnumber =
77 $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
78 or BAIL_OUT("Cannot find newly created biblioitems record");
81 INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
82 VALUES ($biblionumber, $biblioitemnumber, '$branchcode', '$branchcode', 0, 0, 0, 0, NULL, '$right_itemtype')
86 $dbh->selectrow_array("SELECT itemnumber FROM items WHERE biblionumber = $biblionumber")
87 or BAIL_OUT("Cannot find newly created item");
89 $dbh->do("DELETE FROM circulation_rules");
90 Koha::CirculationRules->set_rules(
96 hold_fulfillment_policy => 'any',
102 my $reserve_id = AddReserve(
104 branchcode => $branchcode,
105 borrowernumber => $borrowernumber,
106 biblionumber => $biblionumber,
108 itemtype => $right_itemtype,
111 my ( $status ) = CheckReserves($itemnumber);
112 is( $status, 'Reserved', "Hold where itemtype matches item's itemtype targed" );
113 Koha::Holds->find( $reserve_id )->cancel;
115 # Itemtypes don't match
116 $reserve_id = AddReserve(
118 branchcode => $branchcode,
119 borrowernumber => $borrowernumber,
120 biblionumber => $biblionumber,
122 itemtype => $wrong_itemtype,
126 ( $status ) = CheckReserves($itemnumber);
127 is($status, q{}, "Hold where itemtype does not match item's itemtype not targeted" );
128 Koha::Holds->find( $reserve_id )->cancel;
131 $reserve_id = AddReserve(
133 branchcode => $branchcode,
134 borrowernumber => $borrowernumber,
135 biblionumber => $biblionumber,
139 ( $status ) = CheckReserves($itemnumber);
140 is( $status, 'Reserved', "Item targeted with no hold itemtype set" );
141 Koha::Holds->find( $reserve_id )->cancel;
144 $schema->storage->txn_rollback;