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