Bug 11126: (RM followup) remove diags from tests
[koha.git] / t / db_dependent / Holds / LocalHoldsPriority.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use t::lib::Mocks;
6 use C4::Context;
7 use C4::Branch;
8
9 use Test::More tests => 6;
10 use MARC::Record;
11 use C4::Biblio;
12 use C4::Items;
13 use C4::Members;
14
15 BEGIN {
16     use FindBin;
17     use lib $FindBin::Bin;
18     use_ok('C4::Reserves');
19 }
20
21 my $dbh = C4::Context->dbh;
22
23 # Start transaction
24 $dbh->{AutoCommit} = 0;
25 $dbh->{RaiseError} = 1;
26
27 my $borrowers_count = 5;
28
29 # Create a helper biblio
30 my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio();
31 # Create a helper item for the biblio.
32 my ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
33   AddItem( { homebranch => 'MPL', holdingbranch => 'CPL' }, $bibnum );
34
35 my @branchcodes = qw{XXX RPL CPL MPL CPL MPL};
36
37 # Create some borrowers
38 my @borrowernumbers;
39 foreach ( 1 .. $borrowers_count ) {
40     my $borrowernumber = AddMember(
41         firstname    => 'my firstname',
42         surname      => 'my surname ' . $_,
43         categorycode => 'S',
44         branchcode   => $branchcodes[$_],
45     );
46     push @borrowernumbers, $borrowernumber;
47 }
48
49 my $biblionumber = $bibnum;
50
51 my @branches = GetBranchesLoop();
52 my $branch   = $branches[0][0]{value};
53
54 # Create five item level holds
55 my $i = 1;
56 foreach my $borrowernumber (@borrowernumbers) {
57     AddReserve(
58         $branchcodes[$i],
59         $borrowernumber,
60         $biblionumber,
61         my $constraint = 'a',
62         my $bibitems   = q{},
63         my $priority = $i,
64         my $resdate,
65         my $expdate,
66         my $notes = q{},
67         $title,
68         my $checkitem,
69         my $found,
70     );
71
72     $i++;
73 }
74
75 my ($status, $reserve, $all_reserves);
76
77 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 0 );
78 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
79 ok( $reserve->{borrowernumber} eq $borrowernumbers[0], "Received expected results with LocalHoldsPriority disabled" );
80
81 t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
82
83 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
84 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
85 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
86 ok( $reserve->{borrowernumber} eq $borrowernumbers[2], "Received expected results with PickupLibrary/homebranch" );
87
88 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
89 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'holdingbranch' );
90 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
91 ok( $reserve->{borrowernumber} eq $borrowernumbers[1], "Received expected results with PickupLibrary/holdingbranch" );
92
93 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
94 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'holdingbranch' );
95 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
96 ok( $reserve->{borrowernumber} eq $borrowernumbers[1], "Received expected results with HomeLibrary/holdingbranch" );
97
98 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'HomeLibrary' );
99 t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
100 ($status, $reserve, $all_reserves) = CheckReserves($itemnumber);
101 ok( $reserve->{borrowernumber} eq $borrowernumbers[2], "Received expected results with HomeLibrary/homebranch" );
102
103 # Helper method to set up a Biblio.
104 sub create_helper_biblio {
105     my $bib   = MARC::Record->new();
106     my $title = 'Silence in the library';
107     $bib->append_fields(
108         MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
109         MARC::Field->new( '245', ' ', ' ', a => $title ),
110     );
111     return ( $bibnum, $title, $bibitemnum ) = AddBiblio( $bib, '' );
112 }