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